From f6955194f2113effd1abfbf71935b3196a8fd35b Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 15 Jan 2022 12:20:01 -0800 Subject: [PATCH] ... --- src/init.c | 3 +- src/opt/env.c | 3 + src/opt/filter.c | 2 + src/opt/set.c | 1 + test/unit/Makefile.am | 25 ++++++- test/unit/opt.tests.c | 141 ++++++++++++++++++++++++++++++++++++++ test/unit/torrent.tests.c | 2 + 7 files changed, 175 insertions(+), 2 deletions(-) create mode 100644 test/unit/opt.tests.c diff --git a/src/init.c b/src/init.c index 4da9b66..908bdb5 100644 --- a/src/init.c +++ b/src/init.c @@ -1,7 +1,8 @@ #include // NOTE: see src/opt/set.c for list of fields/options -// allowed to be set in config files +// allowed to be set in config files. see src/opt/env.c +// for a list of environmental variables checked. struct option long_options[] = { {"config-file", required_argument, 0, 'c'}, diff --git a/src/opt/env.c b/src/opt/env.c index 31f2005..5e33b89 100644 --- a/src/opt/env.c +++ b/src/opt/env.c @@ -15,6 +15,9 @@ int opt_load_from_env() { log_info(OPT_MESSAGE_ENV_LOADING); CHECK_ENV("CONFIG",opt_load_config_file); + CHECK_ENV("FEED_URL",opt_set_feed_url); + CHECK_ENV("FILE_FILTER",opt_set_file_filter); + CHECK_ENV("LOG_FILE",opt_set_out_stream); CHECK_ENV("PIECE_LENGTH",opt_set_piece_length); CHECK_ENV("WORKER_THREADS",opt_set_worker_threads); diff --git a/src/opt/filter.c b/src/opt/filter.c index c85e963..1b8cadf 100644 --- a/src/opt/filter.c +++ b/src/opt/filter.c @@ -1,6 +1,8 @@ #include int opt_set_file_filter(const char *filter) { + if(NULL==filter) { return -1; } + if(strcmp(filter,"default")==0) { global_options.file_filter = FILE_FILTER_DEFAULT; } else if(strcmp(filter,"-dotfiles")==0) { diff --git a/src/opt/set.c b/src/opt/set.c index 25d7f02..3a5108b 100644 --- a/src/opt/set.c +++ b/src/opt/set.c @@ -6,6 +6,7 @@ struct option_lookup_table_entry { }; // NOTE: see src/init.c for list of CLI options +// see src/opt/env.c for environmental variables struct option_lookup_table_entry option_lookup_table[] = { {"feed_url",&opt_set_feed_url}, diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index d45dacb..49cd59e 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -13,7 +13,7 @@ AM_CPPFLAGS += \ -DNDEBUG endif -check_PROGRAMS = bencode.tests block.tests feed.tests file.tests fs.concat.tests fs.filter.tests hash.tests hashmap.tests meta.tests rss.tests torrent.tests tree.tests +check_PROGRAMS = bencode.tests block.tests feed.tests file.tests fs.concat.tests fs.filter.tests hash.tests hashmap.tests meta.tests opt.tests rss.tests torrent.tests tree.tests TESTS = $(check_PROGRAMS) if ENABLE_MEMCHECK @@ -112,6 +112,29 @@ meta_tests_SOURCES = \ meta_tests_CPPFLAGS = $(AM_CPPFLAGS) \ -DMETA_SRC_FILE="$(top_srcdir)/src/meta.c" +opt_tests_SOURCES = \ + $(common_SOURCES) \ + $(torrent_SOURCES) \ + opt.tests.c \ + $(top_srcdir)/src/default.c \ + $(top_srcdir)/src/fs/dir.c \ + $(top_srcdir)/src/fs/concat.c \ + $(top_srcdir)/src/hash.c \ + $(top_srcdir)/src/log.c \ + $(top_srcdir)/src/opt/config.c \ + $(top_srcdir)/src/opt/env.c \ + $(top_srcdir)/src/opt/feed.c \ + $(top_srcdir)/src/opt/filter.c \ + $(top_srcdir)/src/opt/loglevel.c \ + $(top_srcdir)/src/opt/out.c \ + $(top_srcdir)/src/opt/piecel.c \ + $(top_srcdir)/src/opt/set.c \ + $(top_srcdir)/src/opt/watch.c \ + $(top_srcdir)/src/opt/worker.c + +opt_tests_CPPFLAGS = $(AM_CPPFLAGS) \ + -DTORRENT_INFO_SRC_FILE="$(top_srcdir)/src/torrent/info.c" + rss_tests_SOURCES = \ $(common_SOURCES) \ rss.tests.c \ diff --git a/test/unit/opt.tests.c b/test/unit/opt.tests.c new file mode 100644 index 0000000..821dd0f --- /dev/null +++ b/test/unit/opt.tests.c @@ -0,0 +1,141 @@ +#include + +#include + +int session_torrent_add(struct torrent *p) { return 1; } + +#include INCLUDE(TORRENT_INFO_SRC_FILE) + +pthread_t logging_thread; +pthread_mutex_t logging_mutex = PTHREAD_MUTEX_INITIALIZER; + +int main(); +static void opt_load_config_file_basic_test(); +static void opt_load_from_env_basic_test(); +static void opt_add_watch_basic_test(); +static void opt_set_feed_url_basic_test(); +static void opt_set_file_filter_basic_test(); +static void opt_set_piece_length_basic_test(); +static void opt_set_worker_threads_basic_test(); + +int main() { + setup_env(); + + // so logging works properly and default opt values set + assert(1==defaults()); + + opt_load_config_file_basic_test(); + opt_load_from_env_basic_test(); + opt_add_watch_basic_test(); + opt_set_feed_url_basic_test(); + opt_set_file_filter_basic_test(); + opt_set_piece_length_basic_test(); + opt_set_worker_threads_basic_test(); + + clean_env(); + + return EXIT_SUCCESS; +} + +static void opt_load_config_file_basic_test() { + assert(opt_load_config_file(NULL)==-1); + assert(opt_load_config_file(TEST_DIRECTORY "/notarealfileslkdjfalksdjf")==-1); + + assert(1==defaults()); +} + +static void opt_load_from_env_basic_test() { + assert(0==setenv("SEEDER_FEED_URL","test",1)); + assert(0==setenv("SEEDER_FILE_FILTER","all",1)); + assert(0==setenv("SEEDER_PIECE_LENGTH","32768",1)); + assert(0==setenv("SEEDER_WORKER_THREADS","1",1)); + + assert(opt_load_from_env()==1); + + assert(strcmp(global_options.feed_url,"test")==0); + assert(FILE_FILTER_ALL==global_options.file_filter); + assert(32768==global_options.piece_length); + assert(1==global_options.worker_threads); + + assert(1==defaults()); + + reset_env(); +} + +static void opt_set_feed_url_basic_test() { + assert(-1==opt_set_feed_url(NULL)); + + assert(1==opt_set_feed_url("https://test.com")); + assert(1==opt_set_feed_url("https://test2.com")); + + assert(strcmp(global_options.feed_url,"https://test2.com")==0); + + assert(1==defaults()); +} + +static void opt_add_watch_basic_test() { + assert(-1==opt_add_watch(NULL)); + assert(-1==opt_add_watch("notarealexistingdirectorylksdjflkajsdfklajsklf")); + + assert(1==opt_add_watch(TEST_DIRECTORY)); + + assert(1==defaults()); + + reset_env(); +} + +static void opt_set_file_filter_basic_test() { + assert(-1==opt_set_file_filter(NULL)); + + assert(-1==opt_set_file_filter("invalidfiltervalue")); + + assert(1==opt_set_file_filter("default")); + assert(1==opt_set_file_filter("-dotfiles")); + assert(1==opt_set_file_filter("all")); + + assert(FILE_FILTER_ALL==global_options.file_filter); +} + +static void opt_set_piece_length_basic_test() { + assert(-1==opt_set_piece_length(NULL)); + + assert(-1==opt_set_piece_length("-1")); + assert(-1==opt_set_piece_length("57")); + assert(-1==opt_set_piece_length("0")); + + assert(-1==opt_set_piece_length("2")); + assert(-1==opt_set_piece_length("4")); + assert(-1==opt_set_piece_length("8")); + assert(-1==opt_set_piece_length("16")); + assert(-1==opt_set_piece_length("32")); + assert(-1==opt_set_piece_length("64")); + assert(-1==opt_set_piece_length("128")); + assert(-1==opt_set_piece_length("256")); + assert(-1==opt_set_piece_length("512")); + assert(-1==opt_set_piece_length("1024")); + assert(-1==opt_set_piece_length("2048")); + assert(-1==opt_set_piece_length("4096")); + assert(-1==opt_set_piece_length("8192")); + assert(1==opt_set_piece_length("16384")); + assert(1==opt_set_piece_length("32768")); + assert(1==opt_set_piece_length("65536")); + assert(1==opt_set_piece_length("131072")); + assert(1==opt_set_piece_length("262144")); + assert(1==opt_set_piece_length("524288")); + assert(1==opt_set_piece_length("1048576")); + + assert(1048576==global_options.piece_length); + + assert(1==defaults()); +} + +static void opt_set_worker_threads_basic_test() { + assert(-1==opt_set_worker_threads(NULL)); + assert(-1==opt_set_worker_threads("notanumber")); + + assert(1==opt_set_worker_threads("10")); + + assert(10==global_options.worker_threads); + + assert(1==defaults()); +} diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c index 70792e4..7be05b4 100644 --- a/test/unit/torrent.tests.c +++ b/test/unit/torrent.tests.c @@ -1,5 +1,7 @@ #include +#include + #include INCLUDE(TORRENT_INFO_SRC_FILE) int main(); -- 2.39.5