4 #include "ntp_stdlib.h" // For estrdup()
5 #include "fileHandlingTest.h"
6 #include "kod_management.h"
11 * We access some parts of the kod database directly, without
12 * going through the public interface
14 extern int kod_db_cnt;
15 extern struct kod_entry** kod_db;
16 extern char* kod_db_file;
19 void test_ReadEmptyFile(void);
20 void test_ReadCorrectFile(void);
21 void test_ReadFileWithBlankLines(void);
22 void test_WriteEmptyFile(void);
23 void test_WriteFileWithSingleEntry(void);
24 void test_WriteFileWithMultipleEntries(void);
36 test_ReadEmptyFile(void) {
37 kod_init_kod_db(CreatePath("kod-test-empty", INPUT_DIR), TRUE);
39 TEST_ASSERT_EQUAL(0, kod_db_cnt);
44 test_ReadCorrectFile(void) {
45 kod_init_kod_db(CreatePath("kod-test-correct", INPUT_DIR), TRUE);
47 TEST_ASSERT_EQUAL(2, kod_db_cnt);
49 struct kod_entry* res;
51 TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
52 TEST_ASSERT_EQUAL_STRING("DENY", res->type);
53 TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
54 TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
56 TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
57 TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
58 TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
59 TEST_ASSERT_EQUAL(0xfff, res->timestamp);
64 test_ReadFileWithBlankLines(void) {
65 kod_init_kod_db(CreatePath("kod-test-blanks", INPUT_DIR), TRUE);
67 TEST_ASSERT_EQUAL(3, kod_db_cnt);
69 struct kod_entry* res;
71 TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
72 TEST_ASSERT_EQUAL_STRING("DENY", res->type);
73 TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
74 TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
76 TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
77 TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
78 TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
79 TEST_ASSERT_EQUAL(0xfff, res->timestamp);
81 TEST_ASSERT_EQUAL(1, search_entry("example.com", &res));
82 TEST_ASSERT_EQUAL_STRING("DENY", res->type);
83 TEST_ASSERT_EQUAL_STRING("example.com", res->hostname);
84 TEST_ASSERT_EQUAL(0xabcd, res->timestamp);
89 test_WriteEmptyFile(void) {
90 kod_db_file = estrdup("kod-output-blank");
93 // Open file and ensure that the filesize is 0 bytes.
94 FILE * is = fopen(kod_db_file, "rb");
95 TEST_ASSERT_NOT_NULL(is);
97 TEST_ASSERT_EQUAL(0, GetFileSize(is));
104 test_WriteFileWithSingleEntry(void) {
105 kod_db_file = estrdup("kod-output-single");
106 add_entry("host1", "DENY");
108 // Here we must manipulate the timestamps, so they match the one in
109 // the expected file.
111 kod_db[0]->timestamp = 1;
115 // Open file and compare sizes.
116 FILE * actual = fopen(kod_db_file, "rb");
117 FILE * expected = fopen(CreatePath("kod-expected-single", INPUT_DIR),"rb");
119 TEST_ASSERT_NOT_NULL(actual);
120 TEST_ASSERT_NOT_NULL(expected);
122 TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
124 TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
129 test_WriteFileWithMultipleEntries(void) {
130 kod_db_file = estrdup("kod-output-multiple");
131 add_entry("example.com", "RATE");
132 add_entry("192.0.2.1", "DENY");
133 add_entry("192.0.2.5", "RSTR");
136 // Manipulate timestamps. This is a bit of a hack, ideally these
137 // tests should not care about the internal representation.
139 kod_db[0]->timestamp = 0xabcd;
140 kod_db[1]->timestamp = 0xabcd;
141 kod_db[2]->timestamp = 0xabcd;
145 // Open file and compare sizes and content.
146 FILE * actual = fopen(kod_db_file, "rb");
147 FILE * expected = fopen(CreatePath("kod-expected-multiple", INPUT_DIR),"rb");
149 TEST_ASSERT_NOT_NULL(actual);
150 TEST_ASSERT_NOT_NULL(expected);
153 TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
155 TEST_ASSERT_TRUE(CompareFileContent(expected, actual));