From: alex Date: Sun, 2 Jan 2022 08:13:56 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=f7d68c6d5eb83ed58ac0fb1584b360974a4e4e55;p=seeder ... --- diff --git a/inc/feed.h b/inc/feed.h index f62e8b5..261ed72 100644 --- a/inc/feed.h +++ b/inc/feed.h @@ -6,10 +6,11 @@ #include #define FEED_DIRECTORY PREFIX "/feeds" +#define FEED_FILE_EXTENSION ".xml" int feeds(); int feed_entries(FILE*,struct torrent*); -int feed_info(struct torrent*,struct rss_channel_info*); +int feed_info(FILE*,struct torrent*); char *feed_path(const char*); #endif diff --git a/inc/rss.h b/inc/rss.h index f2e27d1..6781dfb 100644 --- a/inc/rss.h +++ b/inc/rss.h @@ -59,11 +59,11 @@ struct rss_entry { void rss_channel_info_free(struct rss_channel_info*); int rss_channel_info_init(struct rss_channel_info**); -int rss_entry(FILE*,struct rss_entry*); +int rss_entry(FILE*,const struct rss_entry*); void rss_entry_free(struct rss_entry*); int rss_entry_init(struct rss_entry**); int rss_footer(FILE*); int rss_header(FILE*); -int rss_info(FILE*,struct rss_channel_info*); +int rss_info(FILE*,const struct rss_channel_info*); #endif diff --git a/src/feed/entries.c b/src/feed/entries.c index 5f26f16..71b91bd 100644 --- a/src/feed/entries.c +++ b/src/feed/entries.c @@ -7,6 +7,9 @@ int feed_entries(FILE *fp, struct torrent *torrent) { struct rss_entry **entries, *entry; size_t count, index; + if(NULL==fp) { return -1; } + if(NULL==torrent) { return -1; } + count = 0; for(size_t i=0;ifiles.roots->size;i++) { if(torrent->files.roots->map[i]!=NULL) { count++; } @@ -35,6 +38,16 @@ int feed_entries(FILE *fp, struct torrent *torrent) { qsort(entries,count,sizeof(struct rss_entry*),&entries_sort); + for(size_t i=0;i -int feed_info(struct torrent *torrent_p, struct rss_channel_info *info) { +int feed_info(FILE *fp, struct torrent *torrent_p) { + struct rss_channel_info *info; + + if(NULL==fp) { return -1; } + if(NULL==torrent_p) { return -1; } + + info = NULL; + if(rss_channel_info_init(&info)<0) { return -1; } + + if(meta_info(torrent_p->root,info)<0) { goto clean; } + + if(rss_info(fp,info)<0) { goto clean; } + + rss_channel_info_free(info); + + return 1; +clean: + if(info!=NULL) { rss_channel_info_free(info); } return -1; } diff --git a/src/feed/path.c b/src/feed/path.c index 09fcec9..f9b6ac2 100644 --- a/src/feed/path.c +++ b/src/feed/path.c @@ -1,5 +1,24 @@ #include char *feed_path(const char *name) { - return NULL; + char *p, *path; + + p = concat(FEED_DIRECTORY,name); + if(NULL==p) { return NULL; } + + path = malloc(strlen(p)+strlen(FEED_FILE_EXTENSION)+1); + if(NULL==path) { + free(p); + return NULL; + } + + if(sprintf(path,"%s" FEED_FILE_EXTENSION,p)<0) { + free(p); + free(path); + return NULL; + } + + free(p); + + return path; } diff --git a/src/rss/entry.c b/src/rss/entry.c index 2d3ecf2..2214d45 100644 --- a/src/rss/entry.c +++ b/src/rss/entry.c @@ -8,7 +8,7 @@ static int rss_entry_pubdate(FILE*,const struct tm*); static int rss_entry_start(FILE*); static int rss_entry_title(FILE*,const char*); -int rss_entry(FILE *fp, struct rss_entry *entry) { +int rss_entry(FILE *fp, const struct rss_entry *entry) { if(NULL==fp) { return -1; } if(NULL==entry) { return -1; } diff --git a/src/rss/info.c b/src/rss/info.c index 84f62c9..3dc8474 100644 --- a/src/rss/info.c +++ b/src/rss/info.c @@ -6,7 +6,7 @@ static int rss_info_last_build_date(FILE*,const struct tm*); static int rss_info_link(FILE*,const char*); static int rss_info_title(FILE*,const char*); -int rss_info(FILE *fp, struct rss_channel_info *info) { +int rss_info(FILE *fp, const struct rss_channel_info *info) { if(NULL==fp) { return -1; } if(NULL==info) { return -1; } diff --git a/src/torrent/path.c b/src/torrent/path.c index 628f98d..2a73b3c 100644 --- a/src/torrent/path.c +++ b/src/torrent/path.c @@ -12,7 +12,7 @@ char *torrent_file_path(unsigned char *infohash, size_t len) { p = concat(TORRENT_FILE_DIRECTORY,hex); if(NULL==p) { return NULL; } - path = malloc(strlen(p)+strlen(".torrent")+1); + path = malloc(strlen(p)+strlen(TORRENT_FILE_EXTENSION)+1); if(NULL==path) { free(p); return NULL; diff --git a/test/unit/Makefile.am b/test/unit/Makefile.am index 0f27ab3..88e0b6a 100644 --- a/test/unit/Makefile.am +++ b/test/unit/Makefile.am @@ -4,7 +4,8 @@ AM_CPPFLAGS = \ -Werror EXTRA_DIST = \ - test_utils.h + test_utils.h \ + test_macros.h if ENABLE_DEBUG else @@ -12,7 +13,7 @@ AM_CPPFLAGS += \ -DNDEBUG endif -check_PROGRAMS = bencode.tests block.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 rss.tests torrent.tests tree.tests TESTS = $(check_PROGRAMS) if ENABLE_MEMCHECK @@ -21,8 +22,17 @@ AM_LOG_FLAGS = --leak-check=full -v --track-origins=yes --error-exitcode=1 endif common_SOURCES = \ - test_utils.c \ - $(top_srcdir)/src/hash.c + test_utils.c + +torrent_SOURCES = \ + $(top_srcdir)/src/bencode/encode.c \ + $(top_srcdir)/src/block.c \ + $(top_srcdir)/src/file.c \ + $(top_srcdir)/src/hashmap.c \ + $(top_srcdir)/src/torrent/add.c \ + $(top_srcdir)/src/torrent/free.c \ + $(top_srcdir)/src/torrent/init.c \ + $(top_srcdir)/src/tree.c bencode_tests_SOURCES = \ $(common_SOURCES) \ @@ -33,14 +43,31 @@ bencode_tests_SOURCES = \ block_tests_SOURCES = \ $(common_SOURCES) \ block.tests.c \ - $(top_srcdir)/src/block.c + $(top_srcdir)/src/block.c \ + $(top_srcdir)/src/hash.c + +feed_tests_SOURCES = \ + $(common_SOURCES) \ + $(torrent_SOURCES) \ + feed.tests.c \ + $(top_srcdir)/src/feed/entries.c \ + $(top_srcdir)/src/feed/info.c \ + $(top_srcdir)/src/fs/concat.c \ + $(top_srcdir)/src/fs/dir.c \ + $(top_srcdir)/src/hash.c \ + $(top_srcdir)/src/meta.c \ + $(top_srcdir)/src/rss/entry.c \ + $(top_srcdir)/src/rss/free.c \ + $(top_srcdir)/src/rss/info.c \ + $(top_srcdir)/src/rss/init.c file_tests_SOURCES = \ $(common_SOURCES) \ file.tests.c \ $(top_srcdir)/src/bencode/encode.c \ $(top_srcdir)/src/block.c \ - $(top_srcdir)/src/file.c + $(top_srcdir)/src/file.c \ + $(top_srcdir)/src/hash.c fs_concat_tests_SOURCES = \ $(common_SOURCES) \ @@ -54,7 +81,8 @@ fs_filter_tests_SOURCES = \ hash_tests_SOURCES = \ $(common_SOURCES) \ - hash.tests.c + hash.tests.c \ + $(top_srcdir)/src/hash.c hashmap_tests_SOURCES = \ $(common_SOURCES) \ @@ -75,6 +103,7 @@ meta_tests_CPPFLAGS = $(AM_CPPFLAGS) \ rss_tests_SOURCES = \ $(common_SOURCES) \ rss.tests.c \ + $(top_srcdir)/src/hash.c \ $(top_srcdir)/src/rss/entry.c \ $(top_srcdir)/src/rss/footer.c \ $(top_srcdir)/src/rss/free.c \ @@ -84,21 +113,15 @@ rss_tests_SOURCES = \ torrent_tests_SOURCES = \ $(common_SOURCES) \ + $(torrent_SOURCES) \ torrent.tests.c \ - $(top_srcdir)/src/bencode/encode.c \ - $(top_srcdir)/src/block.c \ - $(top_srcdir)/src/file.c \ $(top_srcdir)/src/fs/concat.c \ $(top_srcdir)/src/fs/dir.c \ - $(top_srcdir)/src/hashmap.c \ - $(top_srcdir)/src/torrent/add.c \ + $(top_srcdir)/src/hash.c \ $(top_srcdir)/src/torrent/file.c \ - $(top_srcdir)/src/torrent/free.c \ - $(top_srcdir)/src/torrent/init.c \ $(top_srcdir)/src/torrent/magnet.c \ $(top_srcdir)/src/torrent/path.c \ - $(top_srcdir)/src/torrent/piece.c \ - $(top_srcdir)/src/tree.c + $(top_srcdir)/src/torrent/piece.c torrent_tests_CPPFLAGS = $(AM_CPPFLAGS) \ -DTORRENT_INFO_SRC_FILE="$(top_srcdir)/src/torrent/info.c" @@ -109,4 +132,5 @@ tree_tests_SOURCES = \ $(top_srcdir)/src/bencode/encode.c \ $(top_srcdir)/src/block.c \ $(top_srcdir)/src/file.c \ + $(top_srcdir)/src/hash.c \ $(top_srcdir)/src/tree.c diff --git a/test/unit/feed.tests.c b/test/unit/feed.tests.c new file mode 100644 index 0000000..31f5bef --- /dev/null +++ b/test/unit/feed.tests.c @@ -0,0 +1,78 @@ +#include + +#include + +int main(); +static void feed_info_basic_test(); +static void feed_entries_basic_test(); +static FILE *setup_file_pointer(); + +int main() { + setup_env(); + + feed_info_basic_test(); + feed_entries_basic_test(); + + clean_env(); + + return EXIT_SUCCESS; +} + +static void feed_entries_basic_test() { + FILE *fp; + struct torrent *p; + unsigned char hash[crypto_hash_sha256_BYTES]; + unsigned char expected[crypto_hash_sha256_BYTES] = {185,160,108,238,240,237,217,219,97,190,118,39,179,198,241,61,191,24,149,75,124,56,189,148,163,13,185,110,30,115,222,186}; + + fp = setup_file_pointer(); + + TORRENT_SETUP_EXAMPLE(&p); + + assert(-1==feed_entries(NULL,NULL)); + assert(-1==feed_entries(fp,NULL)); + + assert(1==feed_entries(fp,p)); + + torrent_free(p); + + fclose(fp); + + HASH_FILE(TEST_FILE_7,hash,crypto_hash_sha256_BYTES); + assert(memcmp(hash,expected,crypto_hash_sha256_BYTES)==0); + + reset_env(); +} + +static void feed_info_basic_test() { + FILE *fp; + struct torrent *p; + unsigned char hash[crypto_hash_sha256_BYTES]; + unsigned char expected[crypto_hash_sha256_BYTES] = {185,160,108,238,240,237,217,219,97,190,118,39,179,198,241,61,191,24,149,75,124,56,189,148,163,13,185,110,30,115,222,186}; + + fp = setup_file_pointer(); + + assert(feed_info(NULL,NULL)==-1); + assert(feed_info(fp,NULL)==-1); + + assert(1==torrent_init(&p,TEST_DIRECTORY,TEST_DIRECTORY,16384)); + + assert(1==feed_info(fp,p)); + + torrent_free(p); + + fclose(fp); + + HASH_FILE(TEST_FILE_7,hash,crypto_hash_sha256_BYTES); + assert(memcmp(hash,expected,crypto_hash_sha256_BYTES)==0); + + reset_env(); +} + +static FILE *setup_file_pointer() { + FILE *fp; + + fp = fopen(TEST_FILE_7,"w"); + assert(fp!=NULL); + + return fp; +} diff --git a/test/unit/meta.tests.c b/test/unit/meta.tests.c index b64a622..495d06c 100644 --- a/test/unit/meta.tests.c +++ b/test/unit/meta.tests.c @@ -1,5 +1,7 @@ #include +#include + char *strptime(const char *s, const char *format, struct tm *tm) { return (char*)s; } diff --git a/test/unit/rss.tests.c b/test/unit/rss.tests.c index 1c0822a..059fe45 100644 --- a/test/unit/rss.tests.c +++ b/test/unit/rss.tests.c @@ -128,7 +128,7 @@ static void rss_entry_correctness_test() { fclose(fp); - hash_file(TEST_FILE_7,hash,crypto_hash_sha256_BYTES); + HASH_FILE(TEST_FILE_7,hash,crypto_hash_sha256_BYTES); assert(memcmp(hash,expected,crypto_hash_sha256_BYTES)==0); rss_entry_free(p); @@ -168,7 +168,7 @@ static void rss_footer_correctness_test() { fclose(fp); - hash_file(TEST_FILE_7,hash,crypto_hash_sha256_BYTES); + HASH_FILE(TEST_FILE_7,hash,crypto_hash_sha256_BYTES); assert(memcmp(hash,expected,crypto_hash_sha256_BYTES)==0); reset_env(); @@ -197,7 +197,7 @@ static void rss_header_correctness_test() { fclose(fp); - hash_file(TEST_FILE_7,hash,crypto_hash_sha256_BYTES); + HASH_FILE(TEST_FILE_7,hash,crypto_hash_sha256_BYTES); assert(memcmp(hash,expected,crypto_hash_sha256_BYTES)==0); reset_env(); @@ -289,7 +289,7 @@ static void rss_info_correctness_test() { fclose(fp); - hash_file(TEST_FILE_7,hash,crypto_hash_sha256_BYTES); + HASH_FILE(TEST_FILE_7,hash,crypto_hash_sha256_BYTES); assert(memcmp(hash,expected,crypto_hash_sha256_BYTES)==0); rss_channel_info_free(p); diff --git a/test/unit/test_macros.h b/test/unit/test_macros.h new file mode 100644 index 0000000..9c9233f --- /dev/null +++ b/test/unit/test_macros.h @@ -0,0 +1,72 @@ +#ifndef __TEST_MACROS_H_ +#define __TEST_MACROS_H_ + +/* Parameters: + * const char *path + * unsigned char *hash + * size_t hash_len + */ +#define HASH_FILE(path,hash,hash_len) { \ + uint8_t buf[16384]; \ + crypto_hash_sha256_state st; \ + FILE *fp; \ + size_t i; \ +\ + assert(hash_len==crypto_hash_sha256_BYTES); \ +\ + fp = fopen(path,"r"); \ + assert(fp!=NULL); \ +\ + assert(1==hash_init(&st)); \ +\ + while(feof(fp)==0) { \ + i = fread(buf,sizeof(uint8_t),16384,fp); \ + if(i>0) { \ + assert(1==hash_update(&st,buf,i)); \ + } \ + } \ +\ + assert(0==fclose(fp)); \ +\ + assert(hash_final(&st,hash,crypto_hash_sha256_BYTES)==1);\ +} + +/* Parameters: + * struct file *file_p + * const char *filename + * int root_val + */ +#define TORRENT_SETUP_EXAMPLE_FILE(file_p,filename,root_val) { \ + struct block *block, *p; \ +\ + assert(file_init(file_p,filename)==1); \ + memset((*file_p)->root,root_val,crypto_hash_sha256_BYTES); \ + for(size_t i=0;i<10;i++) { \ + assert(block_init(&block)==1); \ + memset(block->hash,i,crypto_hash_sha256_BYTES); \ +\ + if((*file_p)->piece_layers==NULL) { \ + (*file_p)->piece_layers = block; \ + } else { \ + p->next = block; \ + } \ + p = block; \ + } \ +} + +/* Parameters: + * struct torrent **p + */ +#define TORRENT_SETUP_EXAMPLE(p) { \ + struct file *file1, *file2; \ +\ + TORRENT_SETUP_EXAMPLE_FILE(&file1,TEST_FILE_1,1); \ + TORRENT_SETUP_EXAMPLE_FILE(&file2,TEST_FILE_2,2); \ +\ + assert(torrent_init(p,TEST_DIRECTORY,TEST_DIRECTORY,16384)==1); \ +\ + assert(torrent_add(*p,file1)==1); \ + assert(torrent_add(*p,file2)==1); \ +} + +#endif diff --git a/test/unit/test_utils.c b/test/unit/test_utils.c index d91a323..05482ba 100644 --- a/test/unit/test_utils.c +++ b/test/unit/test_utils.c @@ -40,28 +40,3 @@ static void create_test_file(const char *filename, const char *contents) { assert(strlen(contents)==fwrite(contents,sizeof(char),strlen(contents),fp)); assert(fclose(fp)==0); } - -void hash_file(const char *filepath, unsigned char *hash, size_t hash_len) { - uint8_t buf[16384]; - crypto_hash_sha256_state st; - FILE *fp; - size_t i; - - assert(hash_len==crypto_hash_sha256_BYTES); - - fp = fopen(filepath,"r"); - assert(fp!=NULL); - - assert(1==hash_init(&st)); - - while(feof(fp)==0) { - i = fread(buf,sizeof(uint8_t),16384,fp); - if(i>0) { - assert(1==hash_update(&st,buf,i)); - } - } - - assert(0==fclose(fp)); - - assert(hash_final(&st,hash,crypto_hash_sha256_BYTES)==1); -} diff --git a/test/unit/test_utils.h b/test/unit/test_utils.h index e09bc0f..2c62102 100644 --- a/test/unit/test_utils.h +++ b/test/unit/test_utils.h @@ -1,6 +1,8 @@ #ifndef __TEST_UTILS_H_ #define __TEST_UTILS_H_ +#include + #include #include #include @@ -35,6 +37,5 @@ void clean_env(); void reset_env(); void setup_env(); -void hash_file(const char*,unsigned char*,size_t); #endif diff --git a/test/unit/torrent.tests.c b/test/unit/torrent.tests.c index da79425..4ae8528 100644 --- a/test/unit/torrent.tests.c +++ b/test/unit/torrent.tests.c @@ -11,8 +11,6 @@ static void torrent_file_path_basic_test(); static void torrent_file_piece_layers_basic_test(); static void torrent_init_basic_test(); static void torrent_magnet_basic_test(); -static void torrent_setup_example(struct torrent**); -static void torrent_setup_example_file(struct file**,const char*,uint8_t); int main() { setup_env(); @@ -84,7 +82,7 @@ static void torrent_add_basic_test() { static void torrent_file_basic_test() { struct torrent *torrent; - torrent_setup_example(&torrent); + TORRENT_SETUP_EXAMPLE(&torrent); assert(1==torrent_file(torrent)); @@ -95,7 +93,7 @@ static void torrent_file_info_basic_test() { struct torrent *torrent; uint8_t *buf; - torrent_setup_example(&torrent); + TORRENT_SETUP_EXAMPLE(&torrent); assert(-1==torrent_file_info(NULL,NULL)); assert(-1==torrent_file_info(torrent,NULL)); @@ -156,7 +154,7 @@ static void torrent_file_info_conformance_test() { assert(i==32); sprintf(path,"%s/torrents/%s.torrent",TEST_DIRECTORY,infohash_expected); - hash_file(path,out,crypto_hash_sha256_BYTES); + HASH_FILE(path,out,crypto_hash_sha256_BYTES); assert(memcmp(out,expected,crypto_hash_sha256_BYTES)==0); @@ -193,12 +191,12 @@ static void torrent_file_piece_layers_basic_test() { fp = fopen(TEST_FILE_6,"w"); assert(fp!=NULL); - torrent_setup_example(&torrent); + TORRENT_SETUP_EXAMPLE(&torrent); assert(torrent_file_piece_layers(fp,torrent)==1); assert(0==fclose(fp)); - hash_file(TEST_FILE_6,hash,crypto_hash_sha256_BYTES); + HASH_FILE(TEST_FILE_6,hash,crypto_hash_sha256_BYTES); assert(memcmp(hash,expected,crypto_hash_sha256_BYTES)==0); @@ -224,7 +222,7 @@ static void torrent_magnet_basic_test() { char *p; char expected[] = "magnet:?xt=urn:btmh:59b2020fe4275cac5c3b8ac32bdc79010c95cf0bac2fd91ced8dd071f8ef91ca&dn=" TEST_DIRECTORY; - torrent_setup_example(&torrent); + TORRENT_SETUP_EXAMPLE(&torrent); p = torrent_magnet(torrent); assert(p!=NULL); @@ -236,34 +234,3 @@ static void torrent_magnet_basic_test() { reset_env(); } - -static void torrent_setup_example_file(struct file **file_p, const char *filename, uint8_t root_val) { - struct block *block, *p; - - assert(file_init(file_p,filename)==1); - memset((*file_p)->root,root_val,crypto_hash_sha256_BYTES); - for(size_t i=0;i<10;i++) { - assert(block_init(&block)==1); - memset(block->hash,i,crypto_hash_sha256_BYTES); - - if((*file_p)->piece_layers==NULL) { - (*file_p)->piece_layers = block; - } else { - p->next = block; - } - - p = block; - } -} - -static void torrent_setup_example(struct torrent **p) { - struct file *file1, *file2; - - torrent_setup_example_file(&file1,TEST_FILE_1,1); - torrent_setup_example_file(&file2,TEST_FILE_2,2); - - assert(torrent_init(p,TEST_DIRECTORY,TEST_DIRECTORY,16384)==1); - - assert(torrent_add(*p,file1)==1); - assert(torrent_add(*p,file2)==1); -}