mirror of
https://github.com/falk-werner/webfused
synced 2024-10-27 20:44:08 +00:00
added skeleton of config
This commit is contained in:
parent
d304c843bc
commit
b5943bd7b6
@ -49,7 +49,10 @@ target_include_directories(userdb PUBLIC
|
||||
target_compile_options(userdb PUBLIC ${OPENSSL_CFLAGS_OTHER})
|
||||
|
||||
add_library(webfused-static STATIC
|
||||
src/webfused/daemon.c)
|
||||
src/webfused/daemon.c
|
||||
src/webfused/config/config.c
|
||||
src/webfused/config/factory.c
|
||||
)
|
||||
|
||||
add_executable(webfused
|
||||
src/webfused/main.c
|
||||
@ -113,6 +116,7 @@ target_include_directories(alltests PRIVATE
|
||||
target_compile_options(alltests PRIVATE ${GMOCK_CFLAGS} ${GTEST_CFLAGS} "-pthread")
|
||||
|
||||
target_link_libraries(alltests PRIVATE
|
||||
webfused-static
|
||||
${LIBCONFIG_LIBRARIES}
|
||||
${GMOCK_LIBRARIES}
|
||||
${GTEST_LIBRARIES}
|
||||
|
96
src/webfused/config/config.c
Normal file
96
src/webfused/config/config.c
Normal file
@ -0,0 +1,96 @@
|
||||
#include "webfused/config/config.h"
|
||||
#include "webfused/config/config_intern.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define WFD_CONFIG_DEFAULT_PORT (8080)
|
||||
#define WFD_CONFIG_DEFAULT_VHOSTNAME ("localhost")
|
||||
|
||||
extern struct wfd_config *
|
||||
wfd_config_create(void)
|
||||
{
|
||||
struct wfd_config * config = malloc(sizeof(struct wfd_config));
|
||||
config->port = WFD_CONFIG_DEFAULT_PORT;
|
||||
config->vhost_name = strdup(WFD_CONFIG_DEFAULT_VHOSTNAME);
|
||||
config->server_key = NULL;
|
||||
config->server_cert = NULL;
|
||||
config->server_doc_root = NULL;
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
void wfd_config_dispose(
|
||||
struct wfd_config * config)
|
||||
{
|
||||
free(config->vhost_name);
|
||||
free(config->server_cert);
|
||||
free(config->server_key);
|
||||
free(config->server_doc_root);
|
||||
free(config);
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
wfd_config_get_server_port(
|
||||
struct wfd_config * config)
|
||||
{
|
||||
return config->port;
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_config_get_server_vhostname(
|
||||
struct wfd_config * config)
|
||||
{
|
||||
return config->vhost_name;
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_config_get_server_cert(
|
||||
struct wfd_config * config)
|
||||
{
|
||||
return config->server_cert;
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_config_get_server_key(
|
||||
struct wfd_config * config)
|
||||
{
|
||||
return config->server_key;
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_config_get_server_document_root(
|
||||
struct wfd_config * config)
|
||||
{
|
||||
return config->server_doc_root;
|
||||
}
|
||||
|
||||
char const *
|
||||
wfd_config_get_auth_provider(
|
||||
struct wfd_config * config)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
struct wfd_auth_settings *
|
||||
wfd_config_get_auth_settings(
|
||||
struct wfd_config * config)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t
|
||||
wfd_config_get_filesystem_count(
|
||||
struct wfd_config * config)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct wfd_filesystem_info *
|
||||
wfd_confi_get_filesystem(
|
||||
struct wfd_config * config,
|
||||
size_t fs_index)
|
||||
{
|
||||
return NULL;
|
||||
}
|
64
src/webfused/config/config.h
Normal file
64
src/webfused/config/config.h
Normal file
@ -0,0 +1,64 @@
|
||||
#ifndef WFD_CONFIG_H
|
||||
#define WFD_CONFIG_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfd_config;
|
||||
struct wfd_auth_settins;
|
||||
struct wfd_filesystem_info;
|
||||
|
||||
extern void wfd_config_dispose(
|
||||
struct wfd_config * config);
|
||||
|
||||
extern int
|
||||
wfd_config_get_server_port(
|
||||
struct wfd_config * config);
|
||||
|
||||
extern char const *
|
||||
wfd_config_get_server_vhostname(
|
||||
struct wfd_config * config);
|
||||
|
||||
extern char const *
|
||||
wfd_config_get_server_cert(
|
||||
struct wfd_config * config);
|
||||
|
||||
extern char const *
|
||||
wfd_config_get_server_key(
|
||||
struct wfd_config * config);
|
||||
|
||||
extern char const *
|
||||
wfd_config_get_server_document_root(
|
||||
struct wfd_config * config);
|
||||
|
||||
extern char const *
|
||||
wfd_config_get_auth_provider(
|
||||
struct wfd_config * config);
|
||||
|
||||
extern struct wfd_auth_settings *
|
||||
wfd_config_get_auth_settings(
|
||||
struct wfd_config * config);
|
||||
|
||||
extern size_t
|
||||
wfd_config_get_filesystem_count(
|
||||
struct wfd_config * config);
|
||||
|
||||
extern struct wfd_filesystem_info *
|
||||
wfd_confi_get_filesystem(
|
||||
struct wfd_config * config,
|
||||
size_t fs_index);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
25
src/webfused/config/config_intern.h
Normal file
25
src/webfused/config/config_intern.h
Normal file
@ -0,0 +1,25 @@
|
||||
#ifndef WFD_CONFIG_INTERN_H
|
||||
#define WFD_CONFIG_INTERN_H
|
||||
|
||||
#ifdef _cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfd_config
|
||||
{
|
||||
char * vhost_name;
|
||||
char * server_cert;
|
||||
char * server_key;
|
||||
char * server_doc_root;
|
||||
int port;
|
||||
};
|
||||
|
||||
extern struct wfd_config *
|
||||
wfd_config_create(void);
|
||||
|
||||
#ifdef _cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
106
src/webfused/config/factory.c
Normal file
106
src/webfused/config/factory.c
Normal file
@ -0,0 +1,106 @@
|
||||
#include "webfused/config/factory.h"
|
||||
#include "webfused/config/config_intern.h"
|
||||
#include "webfused/config/config.h"
|
||||
|
||||
#include <libconfig.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
|
||||
#if ((LIBCONFIG_VER_MAJOR != 1) || (LIBCONFIG_VER_MINOR < 5))
|
||||
#error "linconfig 1.5 or higher needed"
|
||||
#endif
|
||||
|
||||
|
||||
#define WFD_CONFIG_VERSION_MAJOR 1
|
||||
#define WFD_CONFIG_VERSION_MINOR 0
|
||||
|
||||
static bool
|
||||
wfd_config_check_version(
|
||||
config_t * config)
|
||||
{
|
||||
int version_major;
|
||||
int rc = config_lookup_int(config, "version.major", &version_major);
|
||||
if (CONFIG_TRUE != rc)
|
||||
{
|
||||
// error: missing major version
|
||||
return false;
|
||||
}
|
||||
|
||||
if (WFD_CONFIG_VERSION_MAJOR != version_major)
|
||||
{
|
||||
// error: incompatible version, expected WFD_CONFIG_VERSION_MAJOR
|
||||
return false;
|
||||
}
|
||||
|
||||
int version_minor;
|
||||
rc = config_lookup_int(config, "version.minor", &version_minor);
|
||||
if (CONFIG_TRUE != rc)
|
||||
{
|
||||
// error: missing minor version
|
||||
return false;
|
||||
}
|
||||
|
||||
if (WFD_CONFIG_VERSION_MINOR < version_minor)
|
||||
{
|
||||
// warn: some features might be disabled
|
||||
}
|
||||
else if (WFD_CONFIG_VERSION_MINOR > version_minor)
|
||||
{
|
||||
// info: use default values
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static struct wfd_config *
|
||||
wfd_config_load(config_t * config)
|
||||
{
|
||||
struct wfd_config * result = wfd_config_create();
|
||||
|
||||
bool success = wfd_config_check_version(config);
|
||||
|
||||
if (!success)
|
||||
{
|
||||
wfd_config_dispose(result);
|
||||
result = NULL;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
struct wfd_config *
|
||||
wfd_config_load_file(
|
||||
char const * filename)
|
||||
{
|
||||
struct wfd_config * result = NULL;
|
||||
|
||||
config_t config;
|
||||
config_init(&config);
|
||||
int rc = config_read_file(&config, filename);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
result = wfd_config_load(&config);
|
||||
config_destroy(&config);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
struct wfd_config *
|
||||
wfd_config_load_string(
|
||||
char const * contents)
|
||||
{
|
||||
struct wfd_config * result = NULL;
|
||||
|
||||
config_t config;
|
||||
config_init(&config);
|
||||
int rc = config_read_string(&config, contents);
|
||||
if (CONFIG_TRUE == rc)
|
||||
{
|
||||
result = wfd_config_load(&config);
|
||||
config_destroy(&config);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
21
src/webfused/config/factory.h
Normal file
21
src/webfused/config/factory.h
Normal file
@ -0,0 +1,21 @@
|
||||
#ifndef WFD_CONFIG_FACTORY_H
|
||||
#define WFD_CONFIG_FACTORY_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
extern struct wfd_config *
|
||||
wfd_config_load_file(
|
||||
char const * filename);
|
||||
|
||||
extern struct wfd_config *
|
||||
wfd_config_load_string(
|
||||
char const * contents);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,13 +1,57 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <libconfig.h>
|
||||
|
||||
#include "webfused/config/factory.h"
|
||||
#include "webfused/config/config.h"
|
||||
|
||||
TEST(config, is_loadable)
|
||||
{
|
||||
config_t config;
|
||||
config_init(&config);
|
||||
struct wfd_config * config = wfd_config_load_file("webfused.conf");
|
||||
ASSERT_NE(nullptr, config);
|
||||
|
||||
int result = config_read_file(&config, "webfused.conf");
|
||||
ASSERT_EQ(CONFIG_TRUE, result);
|
||||
wfd_config_dispose(config);
|
||||
}
|
||||
|
||||
config_destroy(&config);
|
||||
TEST(config, minimal_config)
|
||||
{
|
||||
char const minimal[] = "version = { major = 1, minor = 0 }\n";
|
||||
struct wfd_config * config = wfd_config_load_string(minimal);
|
||||
ASSERT_NE(nullptr, config);
|
||||
|
||||
wfd_config_dispose(config);
|
||||
}
|
||||
|
||||
TEST(config, invalid_major_version_too_low)
|
||||
{
|
||||
char const too_low[] = "version = { major = 0, minor = 0 }\n";
|
||||
|
||||
struct wfd_config * config = wfd_config_load_string(too_low);
|
||||
ASSERT_EQ(nullptr, config);
|
||||
}
|
||||
|
||||
TEST(config, invalid_major_version_too_high)
|
||||
{
|
||||
char const too_high[] = "version = { major = 2, minor = 0 }\n";
|
||||
|
||||
struct wfd_config * config = wfd_config_load_string(too_high);
|
||||
ASSERT_EQ(nullptr, config);
|
||||
}
|
||||
|
||||
TEST(config, valid_older_minor)
|
||||
{
|
||||
char const valid[] = "version = { major = 1, minor = -1 }\n";
|
||||
|
||||
struct wfd_config * config = wfd_config_load_string(valid);
|
||||
ASSERT_NE(nullptr, config);
|
||||
|
||||
wfd_config_dispose(config);
|
||||
}
|
||||
|
||||
TEST(config, valid_newer_minor)
|
||||
{
|
||||
char const valid[] = "version = { major = 1, minor = 1 }\n";
|
||||
|
||||
struct wfd_config * config = wfd_config_load_string(valid);
|
||||
ASSERT_NE(nullptr, config);
|
||||
|
||||
wfd_config_dispose(config);
|
||||
}
|
Loading…
Reference in New Issue
Block a user