]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Sat, 15 Jan 2022 20:20:01 +0000 (12:20 -0800)
committeralex <[email protected]>
Sat, 15 Jan 2022 20:20:01 +0000 (12:20 -0800)
src/init.c
src/opt/env.c
src/opt/filter.c
src/opt/set.c
test/unit/Makefile.am
test/unit/opt.tests.c [new file with mode: 0644]
test/unit/torrent.tests.c

index 4da9b66b431851cd1c11865793da0bb668cb4216..908bdb5be4f22beafaad4bbe12148149502ca19e 100644 (file)
@@ -1,7 +1,8 @@
 #include<init.h>
 
 // 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'},
index 31f2005468048835edcbba0655bd9bc07f591fa0..5e33b89cb5c7f15e67d47cfde5b02dbf488f7385 100644 (file)
@@ -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);
 
index c85e963062c92aeaff58d8d77fc2da8781455cda..1b8cadf208c2f2266d26afc66ec5eeb1c6e602eb 100644 (file)
@@ -1,6 +1,8 @@
 #include<opt.h>
 
 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) {
index 25d7f02f8caf4e50b625670a612acce8182b3869..3a5108b509eaa083b0e540a99826b4c605fdbfd6 100644 (file)
@@ -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},
index d45dacbdba0323564b070b4017d803685f7d4cc1..49cd59e873c8bd49b844127030f7288af24b0b18 100644 (file)
@@ -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 (file)
index 0000000..821dd0f
--- /dev/null
@@ -0,0 +1,141 @@
+#include<test_utils.h>
+
+#include<opt.h>
+
+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());
+}
index 70792e487c5cb6b1c4e157e4d2ba6f7c715e975f..7be05b49a95731429df3018c475210aadd2b2fda 100644 (file)
@@ -1,5 +1,7 @@
 #include<test_utils.h>
 
+#include<torrent.h>
+
 #include INCLUDE(TORRENT_INFO_SRC_FILE)
 
 int main();