1276da39aSCy Schubert #include "config.h"
29034852cSGleb Smirnoff
3276da39aSCy Schubert #include "ntp_types.h"
4276da39aSCy Schubert #include "ntp_stdlib.h" // For estrdup()
5276da39aSCy Schubert #include "fileHandlingTest.h"
6276da39aSCy Schubert #include "kod_management.h"
7276da39aSCy Schubert
8276da39aSCy Schubert #include "unity.h"
9276da39aSCy Schubert
10276da39aSCy Schubert /*
11276da39aSCy Schubert * We access some parts of the kod database directly, without
12276da39aSCy Schubert * going through the public interface
13276da39aSCy Schubert */
14276da39aSCy Schubert extern int kod_db_cnt;
15276da39aSCy Schubert extern struct kod_entry** kod_db;
16276da39aSCy Schubert extern char* kod_db_file;
17276da39aSCy Schubert
189034852cSGleb Smirnoff void setUp(void);
199034852cSGleb Smirnoff void test_ReadEmptyFile(void);
209034852cSGleb Smirnoff void test_ReadCorrectFile(void);
219034852cSGleb Smirnoff void test_ReadFileWithBlankLines(void);
229034852cSGleb Smirnoff void test_WriteEmptyFile(void);
239034852cSGleb Smirnoff void test_WriteFileWithSingleEntry(void);
249034852cSGleb Smirnoff void test_WriteFileWithMultipleEntries(void);
259034852cSGleb Smirnoff
269034852cSGleb Smirnoff
279034852cSGleb Smirnoff void
setUp(void)289034852cSGleb Smirnoff setUp(void) {
29276da39aSCy Schubert kod_db_cnt = 0;
30276da39aSCy Schubert kod_db = NULL;
31*3311ff84SXin LI init_lib();
32276da39aSCy Schubert }
33276da39aSCy Schubert
34276da39aSCy Schubert
359034852cSGleb Smirnoff void
test_ReadEmptyFile(void)369034852cSGleb Smirnoff test_ReadEmptyFile(void) {
37276da39aSCy Schubert kod_init_kod_db(CreatePath("kod-test-empty", INPUT_DIR), TRUE);
38276da39aSCy Schubert
39276da39aSCy Schubert TEST_ASSERT_EQUAL(0, kod_db_cnt);
40276da39aSCy Schubert }
41276da39aSCy Schubert
429034852cSGleb Smirnoff
439034852cSGleb Smirnoff void
test_ReadCorrectFile(void)449034852cSGleb Smirnoff test_ReadCorrectFile(void) {
45276da39aSCy Schubert kod_init_kod_db(CreatePath("kod-test-correct", INPUT_DIR), TRUE);
46276da39aSCy Schubert
47276da39aSCy Schubert TEST_ASSERT_EQUAL(2, kod_db_cnt);
48276da39aSCy Schubert
49276da39aSCy Schubert struct kod_entry* res;
50276da39aSCy Schubert
51276da39aSCy Schubert TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
52276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING("DENY", res->type);
53276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
54276da39aSCy Schubert TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
55276da39aSCy Schubert
56276da39aSCy Schubert TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
57276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
58276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
59276da39aSCy Schubert TEST_ASSERT_EQUAL(0xfff, res->timestamp);
60276da39aSCy Schubert }
61276da39aSCy Schubert
629034852cSGleb Smirnoff
639034852cSGleb Smirnoff void
test_ReadFileWithBlankLines(void)649034852cSGleb Smirnoff test_ReadFileWithBlankLines(void) {
65276da39aSCy Schubert kod_init_kod_db(CreatePath("kod-test-blanks", INPUT_DIR), TRUE);
66276da39aSCy Schubert
67276da39aSCy Schubert TEST_ASSERT_EQUAL(3, kod_db_cnt);
68276da39aSCy Schubert
69276da39aSCy Schubert struct kod_entry* res;
70276da39aSCy Schubert
71276da39aSCy Schubert TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
72276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING("DENY", res->type);
73276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
74276da39aSCy Schubert TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
75276da39aSCy Schubert
76276da39aSCy Schubert TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
77276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
78276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
79276da39aSCy Schubert TEST_ASSERT_EQUAL(0xfff, res->timestamp);
80276da39aSCy Schubert
81276da39aSCy Schubert TEST_ASSERT_EQUAL(1, search_entry("example.com", &res));
82276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING("DENY", res->type);
83276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING("example.com", res->hostname);
84276da39aSCy Schubert TEST_ASSERT_EQUAL(0xabcd, res->timestamp);
85276da39aSCy Schubert }
86276da39aSCy Schubert
879034852cSGleb Smirnoff
889034852cSGleb Smirnoff void
test_WriteEmptyFile(void)899034852cSGleb Smirnoff test_WriteEmptyFile(void) {
90276da39aSCy Schubert kod_db_file = estrdup("kod-output-blank");
91276da39aSCy Schubert write_kod_db();
92276da39aSCy Schubert
93276da39aSCy Schubert // Open file and ensure that the filesize is 0 bytes.
949034852cSGleb Smirnoff FILE * is = fopen(kod_db_file, "rb");
959034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(is);
96276da39aSCy Schubert
97276da39aSCy Schubert TEST_ASSERT_EQUAL(0, GetFileSize(is));
98276da39aSCy Schubert
99276da39aSCy Schubert fclose(is);
100276da39aSCy Schubert }
101276da39aSCy Schubert
1029034852cSGleb Smirnoff
1039034852cSGleb Smirnoff void
test_WriteFileWithSingleEntry(void)1049034852cSGleb Smirnoff test_WriteFileWithSingleEntry(void) {
105276da39aSCy Schubert kod_db_file = estrdup("kod-output-single");
106276da39aSCy Schubert add_entry("host1", "DENY");
107276da39aSCy Schubert
108276da39aSCy Schubert // Here we must manipulate the timestamps, so they match the one in
109276da39aSCy Schubert // the expected file.
1109034852cSGleb Smirnoff
111276da39aSCy Schubert kod_db[0]->timestamp = 1;
112276da39aSCy Schubert
113276da39aSCy Schubert write_kod_db();
114276da39aSCy Schubert
115276da39aSCy Schubert // Open file and compare sizes.
116276da39aSCy Schubert FILE * actual = fopen(kod_db_file, "rb");
117276da39aSCy Schubert FILE * expected = fopen(CreatePath("kod-expected-single", INPUT_DIR),"rb");
1189034852cSGleb Smirnoff
1199034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(actual);
1209034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(expected);
121276da39aSCy Schubert
122276da39aSCy Schubert TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
123276da39aSCy Schubert
124276da39aSCy Schubert TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
125276da39aSCy Schubert }
126276da39aSCy Schubert
1279034852cSGleb Smirnoff
1289034852cSGleb Smirnoff void
test_WriteFileWithMultipleEntries(void)1299034852cSGleb Smirnoff test_WriteFileWithMultipleEntries(void) {
130276da39aSCy Schubert kod_db_file = estrdup("kod-output-multiple");
131276da39aSCy Schubert add_entry("example.com", "RATE");
132276da39aSCy Schubert add_entry("192.0.2.1", "DENY");
133276da39aSCy Schubert add_entry("192.0.2.5", "RSTR");
134276da39aSCy Schubert
135276da39aSCy Schubert //
136276da39aSCy Schubert // Manipulate timestamps. This is a bit of a hack, ideally these
137276da39aSCy Schubert // tests should not care about the internal representation.
138276da39aSCy Schubert //
139276da39aSCy Schubert kod_db[0]->timestamp = 0xabcd;
140276da39aSCy Schubert kod_db[1]->timestamp = 0xabcd;
141276da39aSCy Schubert kod_db[2]->timestamp = 0xabcd;
142276da39aSCy Schubert
143276da39aSCy Schubert write_kod_db();
144276da39aSCy Schubert
145276da39aSCy Schubert // Open file and compare sizes and content.
146276da39aSCy Schubert FILE * actual = fopen(kod_db_file, "rb");
147276da39aSCy Schubert FILE * expected = fopen(CreatePath("kod-expected-multiple", INPUT_DIR),"rb");
1489034852cSGleb Smirnoff
1499034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(actual);
1509034852cSGleb Smirnoff TEST_ASSERT_NOT_NULL(expected);
151276da39aSCy Schubert
152276da39aSCy Schubert
153276da39aSCy Schubert TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
154276da39aSCy Schubert
155276da39aSCy Schubert TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
156276da39aSCy Schubert }
157