3 #include "ntp_stdlib.h" // For estrdup()
6 #include "fileHandlingTest.h"
8 #include "kod_management.h"
13 * We access some parts of the kod database directly, without
14 * going through the public interface
16 extern int kod_db_cnt;
17 extern struct kod_entry** kod_db;
18 extern char* kod_db_file;
29 void test_ReadEmptyFile() {
30 kod_init_kod_db(CreatePath("kod-test-empty", INPUT_DIR), TRUE);
32 TEST_ASSERT_EQUAL(0, kod_db_cnt);
35 void test_ReadCorrectFile() {
36 kod_init_kod_db(CreatePath("kod-test-correct", INPUT_DIR), TRUE);
38 TEST_ASSERT_EQUAL(2, kod_db_cnt);
40 struct kod_entry* res;
42 TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
43 TEST_ASSERT_EQUAL_STRING("DENY", res->type);
44 TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
45 TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
47 TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
48 TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
49 TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
50 TEST_ASSERT_EQUAL(0xfff, res->timestamp);
53 void test_ReadFileWithBlankLines() {
54 kod_init_kod_db(CreatePath("kod-test-blanks", INPUT_DIR), TRUE);
56 TEST_ASSERT_EQUAL(3, kod_db_cnt);
58 struct kod_entry* res;
60 TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
61 TEST_ASSERT_EQUAL_STRING("DENY", res->type);
62 TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
63 TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
65 TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
66 TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
67 TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
68 TEST_ASSERT_EQUAL(0xfff, res->timestamp);
70 TEST_ASSERT_EQUAL(1, search_entry("example.com", &res));
71 TEST_ASSERT_EQUAL_STRING("DENY", res->type);
72 TEST_ASSERT_EQUAL_STRING("example.com", res->hostname);
73 TEST_ASSERT_EQUAL(0xabcd, res->timestamp);
76 void test_WriteEmptyFile() {
77 //kod_db_file = estrdup(CreatePath("kod-output-blank", OUTPUT_DIR)); //causing issues on psp-at1, replaced
78 kod_db_file = estrdup("kod-output-blank");
79 //printf("kod PATH: %s\n",kod_db_file);
82 // Open file and ensure that the filesize is 0 bytes.
84 is = fopen(kod_db_file, "rb");//std::ios::binary);
85 TEST_ASSERT_FALSE(is == NULL );//is.fail());
87 TEST_ASSERT_EQUAL(0, GetFileSize(is));
92 void test_WriteFileWithSingleEntry() {
93 //kod_db_file = estrdup(CreatePath("kod-output-single", OUTPUT_DIR)); //causing issues on psp-at1, replaced
94 kod_db_file = estrdup("kod-output-single");
95 //printf("kod PATH: %s\n",kod_db_file);
96 add_entry("host1", "DENY");
98 // Here we must manipulate the timestamps, so they match the one in
101 kod_db[0]->timestamp = 1;
105 // Open file and compare sizes.
106 FILE * actual = fopen(kod_db_file, "rb");
107 FILE * expected = fopen(CreatePath("kod-expected-single", INPUT_DIR),"rb");
108 TEST_ASSERT_TRUE(actual !=NULL);//TEST_ASSERT_TRUE(actual.good());
109 TEST_ASSERT_TRUE(expected !=NULL);//TEST_ASSERT_TRUE(expected.good());
111 TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
113 TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
116 void test_WriteFileWithMultipleEntries() {
117 //kod_db_file = estrdup(CreatePath("kod-output-multiple", OUTPUT_DIR)); //causing issues on psp-at1, replaced
118 kod_db_file = estrdup("kod-output-multiple");
119 //printf("kod PATH: %s\n",kod_db_file);
120 add_entry("example.com", "RATE");
121 add_entry("192.0.2.1", "DENY");
122 add_entry("192.0.2.5", "RSTR");
125 // Manipulate timestamps. This is a bit of a hack, ideally these
126 // tests should not care about the internal representation.
128 kod_db[0]->timestamp = 0xabcd;
129 kod_db[1]->timestamp = 0xabcd;
130 kod_db[2]->timestamp = 0xabcd;
134 // Open file and compare sizes and content.
135 FILE * actual = fopen(kod_db_file, "rb");
136 FILE * expected = fopen(CreatePath("kod-expected-multiple", INPUT_DIR),"rb");
137 TEST_ASSERT_TRUE(actual !=NULL);//TEST_ASSERT_TRUE(actual.good());
138 TEST_ASSERT_TRUE(expected !=NULL);//TEST_ASSERT_TRUE(expected.good());
141 TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
143 TEST_ASSERT_TRUE(CompareFileContent(expected, actual));