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);
35 test_ReadEmptyFile(void) {
36 kod_init_kod_db(CreatePath("kod-test-empty", INPUT_DIR), TRUE);
38 TEST_ASSERT_EQUAL(0, kod_db_cnt);
43 test_ReadCorrectFile(void) {
44 kod_init_kod_db(CreatePath("kod-test-correct", INPUT_DIR), TRUE);
46 TEST_ASSERT_EQUAL(2, kod_db_cnt);
48 struct kod_entry* res;
50 TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
51 TEST_ASSERT_EQUAL_STRING("DENY", res->type);
52 TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
53 TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
55 TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
56 TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
57 TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
58 TEST_ASSERT_EQUAL(0xfff, res->timestamp);
63 test_ReadFileWithBlankLines(void) {
64 kod_init_kod_db(CreatePath("kod-test-blanks", INPUT_DIR), TRUE);
66 TEST_ASSERT_EQUAL(3, kod_db_cnt);
68 struct kod_entry* res;
70 TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
71 TEST_ASSERT_EQUAL_STRING("DENY", res->type);
72 TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
73 TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
75 TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
76 TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
77 TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
78 TEST_ASSERT_EQUAL(0xfff, res->timestamp);
80 TEST_ASSERT_EQUAL(1, search_entry("example.com", &res));
81 TEST_ASSERT_EQUAL_STRING("DENY", res->type);
82 TEST_ASSERT_EQUAL_STRING("example.com", res->hostname);
83 TEST_ASSERT_EQUAL(0xabcd, res->timestamp);
88 test_WriteEmptyFile(void) {
89 kod_db_file = estrdup("kod-output-blank");
92 // Open file and ensure that the filesize is 0 bytes.
93 FILE * is = fopen(kod_db_file, "rb");
94 TEST_ASSERT_NOT_NULL(is);
96 TEST_ASSERT_EQUAL(0, GetFileSize(is));
103 test_WriteFileWithSingleEntry(void) {
104 kod_db_file = estrdup("kod-output-single");
105 add_entry("host1", "DENY");
107 // Here we must manipulate the timestamps, so they match the one in
108 // the expected file.
110 kod_db[0]->timestamp = 1;
114 // Open file and compare sizes.
115 FILE * actual = fopen(kod_db_file, "rb");
116 FILE * expected = fopen(CreatePath("kod-expected-single", INPUT_DIR),"rb");
118 TEST_ASSERT_NOT_NULL(actual);
119 TEST_ASSERT_NOT_NULL(expected);
121 TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
123 TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
128 test_WriteFileWithMultipleEntries(void) {
129 kod_db_file = estrdup("kod-output-multiple");
130 add_entry("example.com", "RATE");
131 add_entry("192.0.2.1", "DENY");
132 add_entry("192.0.2.5", "RSTR");
135 // Manipulate timestamps. This is a bit of a hack, ideally these
136 // tests should not care about the internal representation.
138 kod_db[0]->timestamp = 0xabcd;
139 kod_db[1]->timestamp = 0xabcd;
140 kod_db[2]->timestamp = 0xabcd;
144 // Open file and compare sizes and content.
145 FILE * actual = fopen(kod_db_file, "rb");
146 FILE * expected = fopen(CreatePath("kod-expected-multiple", INPUT_DIR),"rb");
148 TEST_ASSERT_NOT_NULL(actual);
149 TEST_ASSERT_NOT_NULL(expected);
152 TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
154 TEST_ASSERT_TRUE(CompareFileContent(expected, actual));