--- /dev/null
+#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;
+}