...
authoralex <[email protected]>
Tue, 28 Dec 2021 01:55:40 +0000 (17:55 -0800)
committeralex <[email protected]>
Tue, 28 Dec 2021 01:55:40 +0000 (17:55 -0800)
src/rss/entry.c
test/unit/Makefile.am
test/unit/rss.tests.c [new file with mode: 0644]
test/unit/test_utils.c
test/unit/test_utils.h
test/unit/torrent.tests.c

index 9874bd2efbd7a858cb52b59cc82aac289e06b5d7..2d3ecf2aa547b9f967b9adc70ea68be643ccdead 100644 (file)
@@ -22,7 +22,7 @@ int rss_entry(FILE *fp, struct rss_entry *entry) {
 
        if(rss_entry_end(fp)<0) { return -1; }
 
-       return -1;
+       return 1;
 }
 
 static int rss_entry_description(FILE *fp, const char *description) {
@@ -47,7 +47,7 @@ static int rss_entry_description(FILE *fp, const char *description) {
        const char end[] = RSS_TAG_DESCRIPTION_END;
        if(fwrite(end,sizeof(char),sizeof(end)-1,fp)!=sizeof(end)-1) { return -1; }
 
-       return -1;
+       return 1;
 }
 
 static int rss_entry_end(FILE *fp) {
@@ -96,12 +96,15 @@ static int rss_entry_link(FILE *fp, const char *link) {
 
 static int rss_entry_pubdate(FILE *fp,const struct tm *date) {
        char buf[40];
-       size_t buf_size = 40;
+       size_t len, buf_size = 40;
+
 
        const char start[] = RSS_TAG_PUBDATE_START;
        if(fwrite(start,sizeof(char),sizeof(start)-1,fp)!=sizeof(start)-1) { return -1; }
        
        if(!(strftime(buf,buf_size, "%a, %d %b %Y %H:%M:%S %z", date))) { return -1; }
+       len = strlen(buf);
+       if(fwrite(buf,sizeof(char),len,fp)!=len) { return -1; }
 
        const char end[] = RSS_TAG_PUBDATE_END;
        if(fwrite(end,sizeof(char),sizeof(end)-1,fp)!=sizeof(end)-1) { return -1; }
index be1b150d6c971e22f686d8794a545840d40963c1..342073701206db6e5a38d269e84f9a735405d437 100644 (file)
@@ -12,7 +12,7 @@ AM_CPPFLAGS += \
        -DNDEBUG
 endif
 
-check_PROGRAMS = bencode.tests block.tests file.tests fs.concat.tests fs.filter.tests hash.tests hashmap.tests torrent.tests tree.tests
+check_PROGRAMS = bencode.tests block.tests file.tests fs.concat.tests fs.filter.tests hash.tests hashmap.tests rss.tests torrent.tests tree.tests
 TESTS = $(check_PROGRAMS)
 
 if ENABLE_MEMCHECK
@@ -20,7 +20,9 @@ LOG_COMPILER = $(VALGRIND)
 AM_LOG_FLAGS = --leak-check=full -v --track-origins=yes --error-exitcode=1
 endif
 
-common_SOURCES = test_utils.c
+common_SOURCES = \
+       test_utils.c \
+       $(top_srcdir)/src/hash.c
 
 bencode_tests_SOURCES = \
        $(common_SOURCES) \
@@ -31,16 +33,14 @@ bencode_tests_SOURCES = \
 block_tests_SOURCES = \
        $(common_SOURCES) \
        block.tests.c \
-       $(top_srcdir)/src/block.c \
-       $(top_srcdir)/src/hash.c
+       $(top_srcdir)/src/block.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/hash.c
+       $(top_srcdir)/src/file.c
 
 fs_concat_tests_SOURCES = \
        $(common_SOURCES) \
@@ -54,14 +54,21 @@ fs_filter_tests_SOURCES = \
 
 hash_tests_SOURCES = \
        $(common_SOURCES) \
-       hash.tests.c \
-       $(top_srcdir)/src/hash.c
+       hash.tests.c
 
 hashmap_tests_SOURCES = \
        $(common_SOURCES) \
        hashmap.tests.c \
        $(top_srcdir)/src/hashmap.c
 
+rss_tests_SOURCES = \
+       $(common_SOURCES) \
+       rss.tests.c \
+       $(top_srcdir)/src/rss/entry.c \
+       $(top_srcdir)/src/rss/footer.c \
+       $(top_srcdir)/src/rss/header.c \
+       $(top_srcdir)/src/rss/info.c
+
 torrent_tests_SOURCES = \
        $(common_SOURCES) \
        torrent.tests.c \
@@ -70,7 +77,6 @@ torrent_tests_SOURCES = \
        $(top_srcdir)/src/file.c \
        $(top_srcdir)/src/fs/concat.c \
        $(top_srcdir)/src/fs/dir.c \
-       $(top_srcdir)/src/hash.c \
        $(top_srcdir)/src/hashmap.c \
        $(top_srcdir)/src/torrent/add.c \
        $(top_srcdir)/src/torrent/file.c \
@@ -88,5 +94,4 @@ 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/rss.tests.c b/test/unit/rss.tests.c
new file mode 100644 (file)
index 0000000..80bb43d
--- /dev/null
@@ -0,0 +1,268 @@
+#include<test_utils.h>
+
+#include<rss.h>
+
+int main();
+static FILE *setup_file_pointer();
+static void rss_entry_basic_test();
+static void rss_entry_correctness_test();
+static void rss_footer_basic_test();
+static void rss_footer_correctness_test();
+static void rss_header_basic_test();
+static void rss_header_correctness_test();
+static void rss_info_basic_test();
+static void rss_info_correctness_test();
+
+int main() {
+       setup_env();
+
+       rss_entry_basic_test();
+       rss_entry_correctness_test();
+       rss_footer_basic_test();
+       rss_footer_correctness_test();
+       rss_header_basic_test();
+       rss_header_correctness_test();
+       rss_info_basic_test();
+       rss_info_correctness_test();
+
+       clean_env();
+
+       return EXIT_SUCCESS;
+}
+
+static void rss_entry_basic_test() {
+       FILE *fp;
+       struct rss_entry p;
+
+       memset(&p,0,sizeof(struct rss_entry));
+
+       fp = setup_file_pointer();
+       
+       assert(rss_entry(NULL,NULL)==-1);
+       assert(rss_entry(fp,NULL)==-1);
+
+       assert(rss_entry(fp,&p)==-1);
+
+       char bad_title[] = "";
+       char sample_title[] = "sample title";
+       char bad_link[] = "";
+       char sample_link[] = "anystring will work?";
+       char bad_description[] = "";
+       char sample_description[] = "sample description";
+       char sample_guid[] = "laskdjflkasdjf";
+
+       p.title = sample_title;
+       p.link = sample_link;
+       p.description = sample_description;
+       p.guid = sample_guid;
+       p.pub_date = *localtime(&(time_t){time(NULL)});
+
+       assert(rss_entry(fp,&p)==1);
+       
+       p.title = bad_title;
+       assert(rss_entry(fp,&p)==-1);
+       p.title = sample_title;
+
+       p.link = bad_link;
+       assert(rss_entry(fp,&p)==-1);
+       p.link = sample_link;
+
+       p.description = bad_description;
+       assert(rss_entry(fp,&p)==1);
+       p.description = sample_description;
+       assert(rss_entry(fp,&p)==1);
+       
+       p.guid = NULL;
+       assert(rss_entry(fp,&p)==1);
+       p.guid = sample_guid;
+       assert(rss_entry(fp,&p)==1);
+
+       fclose(fp);
+       reset_env();
+}
+
+static void rss_entry_correctness_test() {
+       FILE *fp;
+       struct rss_entry p;
+       unsigned char hash[crypto_hash_sha256_BYTES];
+       unsigned char expected[crypto_hash_sha256_BYTES] = {252,1,77,6,36,221,191,114,180,137,47,54,77,39,151,196,0,85,115,184,127,210,230,152,114,130,36,140,33,239,194,30};
+
+       memset(&p,0,sizeof(struct rss_entry));
+
+       fp = setup_file_pointer();
+
+       char sample_title[] = "sample title";
+       char sample_link[] = "anystring will work?";
+       char sample_description[] = "sample description";
+       char sample_guid[] = "laskdjflkasdjf";
+
+       p.title = sample_title;
+       p.link = sample_link;
+       p.description = sample_description;
+       p.guid = sample_guid;
+       time_t seconds = 1231006505;
+       memcpy(&(p.pub_date), gmtime(&seconds), sizeof(struct tm));
+
+       assert(rss_entry(fp,&p)==1);
+
+       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 rss_footer_basic_test() {
+       FILE *fp;
+
+       fp = setup_file_pointer();
+
+       assert(rss_footer(NULL)==-1);
+       assert(rss_footer(fp)==1);
+
+       fclose(fp);
+       reset_env();
+}
+
+static void rss_footer_correctness_test() {
+       FILE *fp;
+       unsigned char hash[crypto_hash_sha256_BYTES];
+       unsigned char expected[crypto_hash_sha256_BYTES] = {229,145,51,89,170,75,21,138,198,27,210,136,221,86,97,100,48,231,144,218,253,17,34,247,151,199,181,192,129,106,66,195};
+
+       fp = setup_file_pointer();
+
+       assert(rss_footer(fp)==1);
+
+       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 rss_header_basic_test() {
+       FILE *fp;
+
+       fp = setup_file_pointer();
+
+       assert(rss_header(NULL)==-1);
+       assert(rss_header(fp)==1);
+
+       fclose(fp);
+       reset_env();
+}
+
+static void rss_header_correctness_test() {
+       FILE *fp;
+       unsigned char hash[crypto_hash_sha256_BYTES];
+       unsigned char expected[crypto_hash_sha256_BYTES] = {251 199     202     195     224     186     198     234
+0x7fffffffe158:        41      214     110     130     12      238     175     97
+0x7fffffffe160:        31      7       105     99      201     7       250     79
+0x7fffffffe168:        182     57      225     209     120     28      212     179};
+
+       fp = setup_file_pointer();
+
+       assert(rss_header(fp)==1);
+
+       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 rss_info_basic_test() {
+       FILE *fp;
+       struct rss_channel_info p;
+       
+       memset(&p,0,sizeof(struct rss_channel_info));
+
+       fp = setup_file_pointer();
+
+       assert(rss_info(NULL,NULL)==-1);
+       assert(rss_info(fp,NULL)==-1);
+
+       char bad_title[] = "";
+       char sample_title[] = "sample title";
+       char bad_link[] = "";
+       char sample_link[] = "alskdjflaiksdjflkasdf";
+       char bad_description[] = "";
+       char sample_description[] = "alsdjflaksdjfoikasjdfliasjdfoijawdf";
+       char bad_language_1[] = "";
+       char bad_language_2[] = "alskdjflkasdjfaslkdf";
+       char sample_language[] = "en-us";
+
+       p.title = sample_title;
+       p.link = sample_link;
+       p.description = sample_description;
+       p.language = sample_language;
+       p.last_build_date = *localtime(&(time_t){time(NULL)});
+
+       assert(rss_info(fp,&p)==1);
+
+       p.title = bad_title;
+       assert(rss_info(fp,&p)==-1);
+       p.title = sample_title;
+
+       p.link = bad_link;
+       assert(rss_info(fp,&p)==-1);
+       p.link = sample_link;
+
+       p.description = bad_description;
+       assert(rss_info(fp,&p)==1);
+       p.description = sample_description;
+       assert(rss_info(fp,&p)==1);
+
+       p.language = bad_language_1;
+       assert(rss_info(fp,&p)==1); // should succeed (fills with default)
+       p.language = bad_language_2;
+       assert(rss_info(fp,&p)==1); // no locale checking done (out of scope of rss_info)
+       p.language = sample_language;
+
+       fclose(fp);
+       reset_env();
+}
+
+static void rss_info_correctness_test() {
+       FILE *fp;
+       struct rss_channel_info p;
+       unsigned char hash[crypto_hash_sha256_BYTES];
+       unsigned char expected[crypto_hash_sha256_BYTES] = {252,1,77,6,36,221,191,114,180,137,47,54,77,39,151,196,0,85,115,184,127,210,230,152,114,130,36,140,33,239,194,30};
+
+       memset(&p,0,sizeof(struct rss_channel_info));
+
+       fp = setup_file_pointer();
+
+       char sample_title[] = "sample title";
+       char sample_link[] = "alskdjflaiksdjflkasdf";
+       char sample_description[] = "alsdjflaksdjfoikasjdfliasjdfoijawdf";
+       char sample_language[] = "en-us";
+
+       p.title = sample_title;
+       p.link = sample_link;
+       p.description = sample_description;
+       p.language = sample_language;
+       time_t seconds = 1231006505;
+       memcpy(&(p.last_build_date), gmtime(&seconds), sizeof(struct tm));
+
+       assert(rss_info(fp,&p)==1);
+
+       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;
+}
index 1ce8027316fab9fd4662e5066e6fbd3a7eca4ca7..924e55c86f0403536f0145b1c468590d2c48b403 100644 (file)
@@ -41,3 +41,28 @@ 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);
+}
index 44b407ee61867dea101378969ef420ba06429063..063aacd3965e88c64b7362cdcb5471a40f7b6fbf 100644 (file)
@@ -7,6 +7,7 @@
 #include<time.h>
 
 #include<default.h>
+#include<hash.h>
 #include<setup.h>
 #include<shutdown.h>
 
 #define TEST_FILE_4_CONTENTS ";alsikdjf;lkasdjflk\n;asjdflk\n;ajsdklfjl;aksdfjla;kj"
 #define TEST_FILE_5 PREFIX "/random.test"
 #define TEST_FILE_6 PREFIX "/file.torrent"
+#define TEST_FILE_7 PREFIX "/file.feed"
 
 void clean_env();
 void reset_env();
 void setup_env();
+void hash_file(const char*,unsigned char*,size_t);
 
 #endif
index 9e0739e1f7421b2c9b7e3d47366e7ea4ae1bd7ea..bf26afe02d5cd0918a7b971eda46e84e381ce931 100644 (file)
@@ -3,7 +3,6 @@
 #include<torrent.h>
 
 int main();
-static void hash_file(const char*,unsigned char*,size_t);
 static void torrent_add_basic_test();
 static void torrent_file_basic_test();
 static void torrent_file_info_basic_test();
@@ -185,31 +184,6 @@ static void torrent_file_path_basic_test() {
        free(p);
 }
 
-static 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);
-}
-
 static void torrent_file_piece_layers_basic_test() {
        struct torrent *torrent;
        FILE *fp;