From 0c3ad93d63a40c645a1df53bf3f7c115705212de Mon Sep 17 00:00:00 2001 From: alex Date: Sat, 15 Jan 2022 17:50:39 -0800 Subject: [PATCH] ... --- src/default.c | 22 ++++++++++++---------- src/opt/config.c | 2 ++ src/opt/piecel.c | 2 ++ src/opt/watch.c | 5 +++-- src/opt/worker.c | 2 ++ test/unit/Makefile.am | 3 ++- test/unit/meta.tests.c | 32 ++++++++++++++++++++++++++++++-- test/unit/opt.tests.c | 3 +-- 8 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/default.c b/src/default.c index cdcf59d..9ac4b16 100644 --- a/src/default.c +++ b/src/default.c @@ -52,19 +52,21 @@ static int default_add_all_directories() { if(strcmp(ent->d_name,".")==0) { continue; } if(strcmp(ent->d_name,"..")==0) { continue; } - p = malloc(sizeof(char)*(strlen(PREFIX)+strlen(ent->d_name)+1)); - if(NULL==p) { - perror("malloc"); - return -1; - } + if(ent->d_type==DT_DIR) { + p = malloc(sizeof(char)*(strlen(PREFIX)+strlen(ent->d_name)+1)); + if(NULL==p) { + perror("malloc"); + return -1; + } + + sprintf(p,PREFIX "/%s",ent->d_name); + if(opt_add_watch(p)<0) { + free(p); + return -1; + } - sprintf(p,PREFIX "/%s",ent->d_name); - if(opt_add_watch(p)<0) { free(p); - return -1; } - - free(p); } closedir(dir); diff --git a/src/opt/config.c b/src/opt/config.c index 863ac56..fdab188 100644 --- a/src/opt/config.c +++ b/src/opt/config.c @@ -6,6 +6,8 @@ int opt_load_config_file(char *path) { char buf[1024]; int line, i; + if(NULL==path) { return -1; } + log_info(OPT_MESSAGE_LOADING_CONFIG_FILE,path); fp = fopen(path,"r"); diff --git a/src/opt/piecel.c b/src/opt/piecel.c index 3c6dac2..1aa8f8a 100644 --- a/src/opt/piecel.c +++ b/src/opt/piecel.c @@ -4,6 +4,8 @@ int opt_set_piece_length(const char *length) { char *end; unsigned long long i; + if(NULL==length) { return -1; } + i = strtoull(length,&end,10); if((i<16384)||(!(i&&!(i&(i-1))))) { diff --git a/src/opt/watch.c b/src/opt/watch.c index 2eb8644..fe2c056 100644 --- a/src/opt/watch.c +++ b/src/opt/watch.c @@ -4,12 +4,13 @@ int opt_add_watch(const char *directory) { struct torrent *p; char *name; - if(!is_directory(directory)) { + if(NULL==directory) { return -1; } + + if(is_directory(directory)<0) { log_err(OPT_MESSAGE_WATCH_INVALID_DIRECTORY,directory); return -1; } - name = strdup(directory); if(NULL==name) { return -1; } name = basename(name); diff --git a/src/opt/worker.c b/src/opt/worker.c index d830d76..9f70aab 100644 --- a/src/opt/worker.c +++ b/src/opt/worker.c @@ -3,6 +3,8 @@ int opt_set_worker_threads(const char *str) { char *end; + if(NULL==str) { return -1; } + global_options.worker_threads = strtoul(str,&end,10); if(0==global_options.worker_threads) { log_err(OPT_MESSAGE_WORKER_THREADS_INVALID,str); diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 49cd59e..dd21f44 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -130,7 +130,8 @@ opt_tests_SOURCES = \ $(top_srcdir)/src/opt/piecel.c \ $(top_srcdir)/src/opt/set.c \ $(top_srcdir)/src/opt/watch.c \ - $(top_srcdir)/src/opt/worker.c + $(top_srcdir)/src/opt/worker.c \ + $(top_srcdir)/src/session.c opt_tests_CPPFLAGS = $(AM_CPPFLAGS) \ -DTORRENT_INFO_SRC_FILE="$(top_srcdir)/src/torrent/info.c" diff --git a/test/unit/meta.tests.c b/test/unit/meta.tests.c index 495d06c..fe2ce60 100644 --- a/test/unit/meta.tests.c +++ b/test/unit/meta.tests.c @@ -10,9 +10,11 @@ char *strptime(const char *s, const char *format, struct tm *tm) { int main(); static void meta_entry_basic_test(); +static void meta_entry_overwrite_test(); static void meta_escape_basic_test(); static void meta_escape_length_test(); static void meta_info_basic_test(); +static void meta_info_overwrite_test(); static void meta_search_basic_test(); int main() { @@ -24,7 +26,9 @@ int main() { meta_search_basic_test(); meta_entry_basic_test(); + meta_entry_overwrite_test(); meta_info_basic_test(); + meta_info_overwrite_test(); clean_env(); @@ -52,15 +56,27 @@ static void meta_entry_basic_test() { assert(meta_entry(TEST_FILE_1,entry)==1); + // pub_date is skipped because of strptime issue + // (had to be mocked because of compilation issue during testing) assert(strcmp(entry->title,"test title")==0); assert(strcmp(entry->link,"https://whatisarealink.com")==0); -// assert(strcmp(entry->pub_date,"")==0); assert(strcmp(entry->description,"I wonder if \n this'll properly be formatted/escaped \\><?")==0); assert(strcmp(entry->guid,"magnet=asldkfjsldkfjslkdjfldsdjlfkjsdf")==0); rss_entry_free(entry); } +static void meta_entry_overwrite_test() { + struct rss_entry *entry; + + assert(1==rss_entry_init(&entry)); + + assert(meta_entry(TEST_FILE_1,entry)==1); + assert(meta_entry(TEST_FILE_1,entry)==1); + + rss_entry_free(entry); +} + static void meta_escape_length_test() { char buf[10]; @@ -138,15 +154,27 @@ static void meta_info_basic_test() { assert(meta_info(PREFIX,info)==1); + // last_build_date is skipped because of strptime issue + // (had to be mocked because of compilation issue during testing) assert(strcmp(info->title,"TITLE")==0); assert(strcmp(info->link,"http://test.com")==0); assert(strcmp(info->description,"what is a description")==0); assert(strcmp(info->language,"en-us")==0); -// assert(strcmp(info->last_build_date,"")==0); rss_channel_info_free(info); } +static void meta_info_overwrite_test() { + struct rss_channel_info *info; + + assert(rss_channel_info_init(&info)==1); + + assert(meta_info(PREFIX,info)==1); + assert(meta_info(PREFIX,info)==1); + + rss_channel_info_free(info); +} + static void meta_search_basic_test() { FILE *fp; fp = meta_search(NULL); diff --git a/test/unit/opt.tests.c b/test/unit/opt.tests.c index 821dd0f..70a95b3 100644 --- a/test/unit/opt.tests.c +++ b/test/unit/opt.tests.c @@ -2,8 +2,6 @@ #include -int session_torrent_add(struct torrent *p) { return 1; } - #include INCLUDE(TORRENT_INFO_SRC_FILE) pthread_t logging_thread; @@ -77,6 +75,7 @@ static void opt_add_watch_basic_test() { assert(-1==opt_add_watch(NULL)); assert(-1==opt_add_watch("notarealexistingdirectorylksdjflkajsdfklajsklf")); + opt_set_log_level(LOG_LEVEL_SILENT); assert(1==opt_add_watch(TEST_DIRECTORY)); assert(1==defaults()); -- 2.39.5