diff --git a/tests/offmsg/CMakeLists.txt b/tests/offmsg/CMakeLists.txt index a7901f60..d7ed4487 100644 --- a/tests/offmsg/CMakeLists.txt +++ b/tests/offmsg/CMakeLists.txt @@ -27,7 +27,8 @@ endif() set(SRC main.c - config.c) + config.c + ../../config/carrier_config.c) add_definitions(-DLIBCONFIG_STATIC) @@ -68,6 +69,7 @@ endif() include_directories( . ../../src/carrier + ../../config ${CARRIER_INT_DIST_DIR}/include) link_directories( diff --git a/tests/offmsg/config.c b/tests/offmsg/config.c index 3d196b63..01481f35 100644 --- a/tests/offmsg/config.c +++ b/tests/offmsg/config.c @@ -32,310 +32,34 @@ #include #include +#include "carrier_config.h" #include "config.h" -static int get_str(config_t *cfg, const char *name, const char *default_value, - char *value, size_t len) +static int extra_config_parser(void *p, ElaOptions *options) { - const char *stropt; - int rc; + config_t *cfg = (config_t *)p; + TestConfig *config = (TestConfig *)options; - rc = config_lookup_string(cfg, name, &stropt); - if (!rc) { - if (!default_value) { - fprintf(stderr, "Missing '%s' option.\n", name); - config_destroy(cfg); - exit(-1); - } else { - stropt = default_value; - } - } + config_lookup_int(cfg, "sender.loglevel", &config->sender_log_level); + config_lookup_int(cfg, "receiver.loglevel", &config->receiver_log_level); - if (strlen(stropt) >= len) { - fprintf(stderr, "Option '%s' too long.\n", name); - config_destroy(cfg); - exit(-1); - } - - strcpy(value, stropt); return 0; } -static int get_int(config_t *cfg, const char *name, int default_value) -{ - int intopt; - int rc; - - rc = config_lookup_int(cfg, name, &intopt); - return rc ? intopt : default_value; -} - -#if defined(_WIN32) || defined(_WIN64) -#define PATH_SEP "\\" -#define HOME_ENV "USERPROFILE" -#else -#define PATH_SEP "/" -#define HOME_ENV "HOME" -#endif - -static void qualified_path(const char *path, const char *ref, char *qualified) +TestConfig *load_config(const char *config_file, TestConfig *config) { - if (*path == PATH_SEP[0] || path[1] == ':') { - strcpy(qualified, path); - } else if (*path == '~') { - sprintf(qualified, "%s%s", getenv(HOME_ENV), path+1); - } else { - if (ref) { - const char *p = strrchr(ref, PATH_SEP[0]); - if (!p) p = ref; - - if (p - ref > 0) - strncpy(qualified, ref, p - ref); - else - *qualified = 0; - } else { - getcwd(qualified, PATH_MAX); - } + memset(config, 0, sizeof(TestConfig)); - if (*qualified) - strcat(qualified, PATH_SEP); - - strcat(qualified, path); - } + return (TestConfig *)carrier_config_load(config_file, extra_config_parser, + (ElaOptions *)config); } -static void config_destructor(void *p) +void free_config(TestConfig *config) { - TestConfig *config = (TestConfig *)p; - if (!config) return; - if (config->bootstraps) { - int i; - - for (i = 0; i < config->bootstraps_size; i++) - deref(config->bootstraps[i]); - - free(config->bootstraps); - } - - if (config->hive_bootstraps) { - int i; - - for (i = 0; i < config->hive_bootstraps_size; i++) - deref(config->hive_bootstraps[i]); - - free(config->hive_bootstraps); - } -} - -static void dht_bootstrap_destructor(void *p) -{ - BootstrapNode *node = (BootstrapNode *)p; - - if (!node) - return; - - if (node->ipv4) - free((void *)node->ipv4); - - if (node->ipv6) - free((void *)node->ipv6); - - if (node->port) - free((void *)node->port); - - if (node->public_key) - free((void *)node->public_key); -} - -static void hive_bootstrap_destructor(void *p) -{ - HiveBootstrapNode *node = (HiveBootstrapNode *)p; - - if (!node) - return; - - if (node->ipv4) - free((void *)node->ipv4); - - if (node->ipv6) - free((void *)node->ipv6); - - if (node->port) - free((void *)node->port); -} - -TestConfig *load_config(const char *config_file) -{ - TestConfig *config; - config_t cfg; - config_setting_t *bootstraps; - const char *stropt; - char number[64]; - char path[PATH_MAX]; - int intopt; - int entries; - int i; - int rc; - - config_init(&cfg); - rc = config_read_file(&cfg, config_file); - if (!rc) { - fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), - config_error_line(&cfg), config_error_text(&cfg)); - config_destroy(&cfg); - return NULL; - } - - config = (TestConfig *)rc_zalloc(sizeof(TestConfig), config_destructor); - if (!config) { - fprintf(stderr, "Load configuration failed, out of memory.\n"); - config_destroy(&cfg); - return NULL; - } - - rc = config_lookup_bool(&cfg, "udp_enabled", &intopt); - if (rc && !intopt) - config->udp_enabled = false; - else - config->udp_enabled = true; - - get_str(&cfg, "datadir", "~/.offmsg_tests", path, sizeof(path)); - qualified_path(path, config_file, config->data_location); - - config->sender_log_level = (int)get_int(&cfg, "sender.loglevel", 4); - config->receiver_log_level = (int)get_int(&cfg, "receiver.loglevel", 4); - - bootstraps = config_lookup(&cfg, "bootstraps"); - if (!bootstraps) { - fprintf(stderr, "Missing dht_bootstraps section.\n"); - config_destroy(&cfg); - deref(config); - return NULL; - } - - entries = config_setting_length(bootstraps); - if (entries <= 0) { - fprintf(stderr, "Empty dht_bootstraps option.\n"); - config_destroy(&cfg); - deref(config); - return NULL; - } - - config->bootstraps_size = entries; - config->bootstraps = (BootstrapNode **)calloc(1, config->bootstraps_size * - sizeof(BootstrapNode *)); - if (!config->bootstraps) { - fprintf(stderr, "Out of memory.\n"); - config_destroy(&cfg); - deref(config); - return NULL; - } - - for (i = 0; i < entries; i++) { - BootstrapNode *node; - - node = rc_zalloc(sizeof(BootstrapNode), dht_bootstrap_destructor); - if (!node) { - fprintf(stderr, "Out of memory.\n"); - config_destroy(&cfg); - deref(config); - return NULL; - } - - config_setting_t *bs = config_setting_get_elem(bootstraps, i); - - rc = config_setting_lookup_string(bs, "ipv4", (const char **)&stropt); - if (rc && *stropt) - node->ipv4 = (const char *)strdup(stropt); - else - node->ipv4 = NULL; - - rc = config_setting_lookup_string(bs, "ipv6", (const char **)&stropt); - if (rc && *stropt) - node->ipv6 = (const char *)strdup(stropt); - else - node->ipv6 = NULL; - - rc = config_setting_lookup_int(bs, "port", &intopt); - if (rc && intopt) { - sprintf(number, "%d", intopt); - node->port = (const char *)strdup(number); - } else - node->port = NULL; - - rc = config_setting_lookup_string(bs, "public_key", (const char **)&stropt); - if (rc && *stropt) - node->public_key = (const char *)strdup(stropt); - else - node->public_key = NULL; - - config->bootstraps[i] = node; - } - - bootstraps = config_lookup(&cfg, "hive_bootstraps"); - if (!bootstraps) { - fprintf(stderr, "Missing hive_bootstraps section.\n"); - config_destroy(&cfg); - deref(config); - return NULL; - } - - entries = config_setting_length(bootstraps); - if (entries <= 0) { - fprintf(stderr, "Empty hive_bootstraps option.\n"); - config_destroy(&cfg); - deref(config); - return NULL; - } - - config->hive_bootstraps_size = entries; - config->hive_bootstraps = (HiveBootstrapNode **)calloc(1, config->hive_bootstraps_size * - sizeof(HiveBootstrapNode *)); - if (!config->hive_bootstraps) { - fprintf(stderr, "Out of memory.\n"); - config_destroy(&cfg); - deref(config); - return NULL; - } - - for (i = 0; i < entries; i++) { - HiveBootstrapNode *node; - - node = rc_zalloc(sizeof(HiveBootstrapNode), hive_bootstrap_destructor); - if (!node) { - fprintf(stderr, "Out of memory.\n"); - config_destroy(&cfg); - deref(config); - return NULL; - } - - config_setting_t *bs = config_setting_get_elem(bootstraps, i); - - rc = config_setting_lookup_string(bs, "ipv4", &stropt); - if (rc && *stropt) - node->ipv4 = (const char *)strdup(stropt); - else - node->ipv4 = NULL; - - rc = config_setting_lookup_string(bs, "ipv6", &stropt); - if (rc && *stropt) - node->ipv6 = (const char *)strdup(stropt); - else - node->ipv6 = NULL; - - rc = config_setting_lookup_int(bs, "port", &intopt); - if (rc && intopt) { - sprintf(number, "%d", intopt); - node->port = (const char *)strdup(number); - } else - node->port = NULL; - - config->hive_bootstraps[i] = node; - } + carrier_config_free(&(config->shared_options)); - config_destroy(&cfg); - return config; + memset(config, 0, sizeof(TestConfig)); } diff --git a/tests/offmsg/config.h b/tests/offmsg/config.h index 71576db5..c21e22f5 100644 --- a/tests/offmsg/config.h +++ b/tests/offmsg/config.h @@ -28,19 +28,12 @@ #include typedef struct TestConfig { + ElaOptions shared_options; int sender_log_level; int receiver_log_level; - - char data_location[PATH_MAX]; - bool udp_enabled; - - int bootstraps_size; - BootstrapNode **bootstraps; - - int hive_bootstraps_size; - HiveBootstrapNode **hive_bootstraps; } TestConfig; -TestConfig *load_config(const char *config_file); +TestConfig *load_config(const char *config_file, TestConfig *config); +void free_config(TestConfig *config); #endif /* __TEST_CONFIG_H__ */ diff --git a/tests/offmsg/main.c b/tests/offmsg/main.c index 3ea126be..6208057e 100644 --- a/tests/offmsg/main.c +++ b/tests/offmsg/main.c @@ -31,6 +31,7 @@ #include #include +#include "carrier_config.h" #include "config.h" #define MSG_INACTIVE_TIMEOUT 60 @@ -314,10 +315,9 @@ const char *get_config_path(const char *cfg_file, const char *cfg_files[]) int main(int argc, char *argv[]) { - ElaOptions opts = {0}; ElaCallbacks callbacks = {0}; ElaCarrier *w = NULL; - TestConfig *config = NULL; + TestConfig config = {0}; const char *config_file = NULL; char logfile[PATH_MAX] = {0}; char datadir[PATH_MAX] = {0}; @@ -451,61 +451,26 @@ int main(int argc, char *argv[]) goto error_exit; } - config = load_config(config_file); - if (!config) { + if (!load_config(config_file, &config)) { vlogE("Error: Loading config file."); output_error(); goto error_exit; } - memset(&opts, 0, sizeof(opts)); - sprintf(logfile, "%s/%s.log", config->data_location, mode_str[mode]); + sprintf(logfile, "%s/%s.log", config.shared_options.persistent_location, mode_str[mode]); if (mode == RunMode_sender) - level = config->sender_log_level; + level = config.sender_log_level; else - level = config->receiver_log_level; + level = config.receiver_log_level; vlog_init(level, logfile, output_null); - opts.udp_enabled = config->udp_enabled; - sprintf(datadir, "%s/%s", config->data_location, mode_str[mode]); - opts.persistent_location = datadir; - opts.bootstraps_size = config->bootstraps_size; - opts.bootstraps = (BootstrapNode *)calloc(1, - sizeof(BootstrapNode) * opts.bootstraps_size); - if (!opts.bootstraps) { - output_error(); - goto error_exit; - } - - for (i = 0 ; i < opts.bootstraps_size; i++) { - BootstrapNode *b = &opts.bootstraps[i]; - BootstrapNode *node = config->bootstraps[i]; - - b->ipv4 = node->ipv4; - b->ipv6 = node->ipv6; - b->port = node->port; - b->public_key = node->public_key; - } - - opts.hive_bootstraps_size = config->hive_bootstraps_size; - opts.hive_bootstraps = (HiveBootstrapNode *)calloc(1, - sizeof(HiveBootstrapNode) * opts.hive_bootstraps_size); - if (!opts.hive_bootstraps) { - output_error(); - free(opts.bootstraps); - goto error_exit; - } - - for (i = 0 ; i < config->hive_bootstraps_size; i++) { - HiveBootstrapNode *b = &opts.hive_bootstraps[i]; - HiveBootstrapNode *node = config->hive_bootstraps[i]; - - b->ipv4 = node->ipv4; - b->ipv6 = node->ipv6; - b->port = node->port; - } + free(config.shared_options.log_file); + config.shared_options.log_file = strdup(logfile); + sprintf(datadir, "%s/%s", config.shared_options.persistent_location, mode_str[mode]); + free((void*)config.shared_options.persistent_location); + config.shared_options.persistent_location = strdup(datadir); memset(&callbacks, 0, sizeof(callbacks)); callbacks.idle = idle_callback; @@ -514,9 +479,8 @@ int main(int argc, char *argv[]) callbacks.friend_message = message_callback; callbacks.ready = ready_callback; - w = ela_new(&opts, &callbacks, ctx); - free(opts.bootstraps); - free(opts.hive_bootstraps); + w = ela_new(&config.shared_options, &callbacks, ctx); + carrier_config_free(&config.shared_options); if (!w) { vlogE("Create carrier instance error: 0x%x", ela_get_error()); @@ -567,8 +531,5 @@ int main(int argc, char *argv[]) if (w && !ctx->did_kill) ela_kill(w); - if (config) - deref(config); - return ctx->error; } diff --git a/tests/offmsg/offmsg_tests.conf b/tests/offmsg/offmsg_tests.conf index d8f65f1c..4512bd3a 100644 --- a/tests/offmsg/offmsg_tests.conf +++ b/tests/offmsg/offmsg_tests.conf @@ -1,6 +1,4 @@ -udp_enabled = true - -datadir = "~/.offmsg_tests" +@include "carrier.conf" sender = { loglevel = 5 @@ -9,42 +7,3 @@ sender = { receiver = { loglevel = 5 } - -bootstraps = ( - { - ipv4 = "13.58.208.50" - port = 33445 - public_key = "89vny8MrKdDKs7Uta9RdVmspPjnRMdwMmaiEW27pZ7gh" - }, - { - ipv4 = "18.216.102.47" - port = 33445 - public_key = "G5z8MqiNDFTadFUPfMdYsYtkUDbX5mNCMVHMZtsCnFeb" - }, - { - ipv4 = "18.216.6.197" - port = 33445 - public_key = "H8sqhRrQuJZ6iLtP2wanxt4LzdNrN2NNFnpPdq1uJ9n2" - }, - { - ipv4 = "52.83.171.135" - port = 33445 - public_key = "5tuHgK1Q4CYf4K5PutsEPK5E3Z7cbtEBdx7LwmdzqXHL" - }, - { - ipv4 = "52.83.191.228" - port = 33445 - public_key = "3khtxZo89SBScAMaHhTvD68pPHiKxgZT6hTCSZZVgNEm" - } -) - -hive_bootstraps = ( - { - ipv4 = "18.217.147.205" - port = 9094 - }, - { - ipv4 = "18.219.53.133" - port = 9094 - } -)