From b5943bd7b676de335dd96237352b1e102d9130f4 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sun, 8 Mar 2020 16:14:55 +0100 Subject: [PATCH] added skeleton of config --- CMakeLists.txt | 6 +- src/webfused/config/config.c | 96 +++++++++++++++++++++++++ src/webfused/config/config.h | 64 +++++++++++++++++ src/webfused/config/config_intern.h | 25 +++++++ src/webfused/config/factory.c | 106 ++++++++++++++++++++++++++++ src/webfused/config/factory.h | 21 ++++++ test/test_config.cc | 56 +++++++++++++-- 7 files changed, 367 insertions(+), 7 deletions(-) create mode 100644 src/webfused/config/config.c create mode 100644 src/webfused/config/config.h create mode 100644 src/webfused/config/config_intern.h create mode 100644 src/webfused/config/factory.c create mode 100644 src/webfused/config/factory.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a6d5cb7..6e1d9a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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} diff --git a/src/webfused/config/config.c b/src/webfused/config/config.c new file mode 100644 index 0000000..c2d13ef --- /dev/null +++ b/src/webfused/config/config.c @@ -0,0 +1,96 @@ +#include "webfused/config/config.h" +#include "webfused/config/config_intern.h" + +#include +#include + +#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; +} diff --git a/src/webfused/config/config.h b/src/webfused/config/config.h new file mode 100644 index 0000000..8480f4e --- /dev/null +++ b/src/webfused/config/config.h @@ -0,0 +1,64 @@ +#ifndef WFD_CONFIG_H +#define WFD_CONFIG_H + +#ifndef __cplusplus +#include +#else +#include +#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 diff --git a/src/webfused/config/config_intern.h b/src/webfused/config/config_intern.h new file mode 100644 index 0000000..792e957 --- /dev/null +++ b/src/webfused/config/config_intern.h @@ -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 diff --git a/src/webfused/config/factory.c b/src/webfused/config/factory.c new file mode 100644 index 0000000..aeb5b78 --- /dev/null +++ b/src/webfused/config/factory.c @@ -0,0 +1,106 @@ +#include "webfused/config/factory.h" +#include "webfused/config/config_intern.h" +#include "webfused/config/config.h" + +#include +#include +#include + + +#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; +} diff --git a/src/webfused/config/factory.h b/src/webfused/config/factory.h new file mode 100644 index 0000000..f058b4c --- /dev/null +++ b/src/webfused/config/factory.h @@ -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 diff --git a/test/test_config.cc b/test/test_config.cc index 55d8e56..63bae21 100644 --- a/test/test_config.cc +++ b/test/test_config.cc @@ -1,13 +1,57 @@ #include -#include + +#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); + + wfd_config_dispose(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"; - int result = config_read_file(&config, "webfused.conf"); - ASSERT_EQ(CONFIG_TRUE, result); + struct wfd_config * config = wfd_config_load_string(valid); + ASSERT_NE(nullptr, config); - config_destroy(&config); + wfd_config_dispose(config); } \ No newline at end of file