1*cdfa2a7eSchristos /* $NetBSD: kodFile.c,v 1.2 2020/05/25 20:47:35 christos Exp $ */
2067f5680Schristos
3f17b710fSchristos #include "config.h"
4a6f3f22fSchristos
5f17b710fSchristos #include "ntp_types.h"
6f17b710fSchristos #include "ntp_stdlib.h" // For estrdup()
7f17b710fSchristos #include "fileHandlingTest.h"
8f17b710fSchristos #include "kod_management.h"
9f17b710fSchristos
10f17b710fSchristos #include "unity.h"
11f17b710fSchristos
12f17b710fSchristos /*
13f17b710fSchristos * We access some parts of the kod database directly, without
14f17b710fSchristos * going through the public interface
15f17b710fSchristos */
16f17b710fSchristos extern int kod_db_cnt;
17f17b710fSchristos extern struct kod_entry** kod_db;
18f17b710fSchristos extern char* kod_db_file;
19f17b710fSchristos
20a6f3f22fSchristos void setUp(void);
21a6f3f22fSchristos void test_ReadEmptyFile(void);
22a6f3f22fSchristos void test_ReadCorrectFile(void);
23a6f3f22fSchristos void test_ReadFileWithBlankLines(void);
24a6f3f22fSchristos void test_WriteEmptyFile(void);
25a6f3f22fSchristos void test_WriteFileWithSingleEntry(void);
26a6f3f22fSchristos void test_WriteFileWithMultipleEntries(void);
27a6f3f22fSchristos
28a6f3f22fSchristos
29a6f3f22fSchristos void
setUp(void)30a6f3f22fSchristos setUp(void) {
31f17b710fSchristos kod_db_cnt = 0;
32f17b710fSchristos kod_db = NULL;
334c290c01Schristos init_lib();
34f17b710fSchristos }
35f17b710fSchristos
36f17b710fSchristos
37a6f3f22fSchristos void
test_ReadEmptyFile(void)38a6f3f22fSchristos test_ReadEmptyFile(void) {
39f17b710fSchristos kod_init_kod_db(CreatePath("kod-test-empty", INPUT_DIR), TRUE);
40f17b710fSchristos
41f17b710fSchristos TEST_ASSERT_EQUAL(0, kod_db_cnt);
42f17b710fSchristos }
43f17b710fSchristos
44a6f3f22fSchristos
45a6f3f22fSchristos void
test_ReadCorrectFile(void)46a6f3f22fSchristos test_ReadCorrectFile(void) {
47f17b710fSchristos kod_init_kod_db(CreatePath("kod-test-correct", INPUT_DIR), TRUE);
48f17b710fSchristos
49f17b710fSchristos TEST_ASSERT_EQUAL(2, kod_db_cnt);
50f17b710fSchristos
51f17b710fSchristos struct kod_entry* res;
52f17b710fSchristos
53f17b710fSchristos TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
54f17b710fSchristos TEST_ASSERT_EQUAL_STRING("DENY", res->type);
55f17b710fSchristos TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
56f17b710fSchristos TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
57f17b710fSchristos
58f17b710fSchristos TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
59f17b710fSchristos TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
60f17b710fSchristos TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
61f17b710fSchristos TEST_ASSERT_EQUAL(0xfff, res->timestamp);
62f17b710fSchristos }
63f17b710fSchristos
64a6f3f22fSchristos
65a6f3f22fSchristos void
test_ReadFileWithBlankLines(void)66a6f3f22fSchristos test_ReadFileWithBlankLines(void) {
67f17b710fSchristos kod_init_kod_db(CreatePath("kod-test-blanks", INPUT_DIR), TRUE);
68f17b710fSchristos
69f17b710fSchristos TEST_ASSERT_EQUAL(3, kod_db_cnt);
70f17b710fSchristos
71f17b710fSchristos struct kod_entry* res;
72f17b710fSchristos
73f17b710fSchristos TEST_ASSERT_EQUAL(1, search_entry("192.0.2.5", &res));
74f17b710fSchristos TEST_ASSERT_EQUAL_STRING("DENY", res->type);
75f17b710fSchristos TEST_ASSERT_EQUAL_STRING("192.0.2.5", res->hostname);
76f17b710fSchristos TEST_ASSERT_EQUAL(0x12345678, res->timestamp);
77f17b710fSchristos
78f17b710fSchristos TEST_ASSERT_EQUAL(1, search_entry("192.0.2.100", &res));
79f17b710fSchristos TEST_ASSERT_EQUAL_STRING("RSTR", res->type);
80f17b710fSchristos TEST_ASSERT_EQUAL_STRING("192.0.2.100", res->hostname);
81f17b710fSchristos TEST_ASSERT_EQUAL(0xfff, res->timestamp);
82f17b710fSchristos
83f17b710fSchristos TEST_ASSERT_EQUAL(1, search_entry("example.com", &res));
84f17b710fSchristos TEST_ASSERT_EQUAL_STRING("DENY", res->type);
85f17b710fSchristos TEST_ASSERT_EQUAL_STRING("example.com", res->hostname);
86f17b710fSchristos TEST_ASSERT_EQUAL(0xabcd, res->timestamp);
87f17b710fSchristos }
88f17b710fSchristos
89a6f3f22fSchristos
90a6f3f22fSchristos void
test_WriteEmptyFile(void)91a6f3f22fSchristos test_WriteEmptyFile(void) {
92f17b710fSchristos kod_db_file = estrdup("kod-output-blank");
93f17b710fSchristos write_kod_db();
94f17b710fSchristos
95f17b710fSchristos // Open file and ensure that the filesize is 0 bytes.
96a6f3f22fSchristos FILE * is = fopen(kod_db_file, "rb");
97a6f3f22fSchristos TEST_ASSERT_NOT_NULL(is);
98f17b710fSchristos
99f17b710fSchristos TEST_ASSERT_EQUAL(0, GetFileSize(is));
100f17b710fSchristos
101f17b710fSchristos fclose(is);
102f17b710fSchristos }
103f17b710fSchristos
104a6f3f22fSchristos
105a6f3f22fSchristos void
test_WriteFileWithSingleEntry(void)106a6f3f22fSchristos test_WriteFileWithSingleEntry(void) {
107f17b710fSchristos kod_db_file = estrdup("kod-output-single");
108f17b710fSchristos add_entry("host1", "DENY");
109f17b710fSchristos
110f17b710fSchristos // Here we must manipulate the timestamps, so they match the one in
111f17b710fSchristos // the expected file.
112a6f3f22fSchristos
113f17b710fSchristos kod_db[0]->timestamp = 1;
114f17b710fSchristos
115f17b710fSchristos write_kod_db();
116f17b710fSchristos
117f17b710fSchristos // Open file and compare sizes.
118f17b710fSchristos FILE * actual = fopen(kod_db_file, "rb");
119f17b710fSchristos FILE * expected = fopen(CreatePath("kod-expected-single", INPUT_DIR),"rb");
120a6f3f22fSchristos
121a6f3f22fSchristos TEST_ASSERT_NOT_NULL(actual);
122a6f3f22fSchristos TEST_ASSERT_NOT_NULL(expected);
123f17b710fSchristos
124f17b710fSchristos TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
125f17b710fSchristos
126f17b710fSchristos TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
127f17b710fSchristos }
128f17b710fSchristos
129a6f3f22fSchristos
130a6f3f22fSchristos void
test_WriteFileWithMultipleEntries(void)131a6f3f22fSchristos test_WriteFileWithMultipleEntries(void) {
132f17b710fSchristos kod_db_file = estrdup("kod-output-multiple");
133f17b710fSchristos add_entry("example.com", "RATE");
134f17b710fSchristos add_entry("192.0.2.1", "DENY");
135f17b710fSchristos add_entry("192.0.2.5", "RSTR");
136f17b710fSchristos
137f17b710fSchristos //
138f17b710fSchristos // Manipulate timestamps. This is a bit of a hack, ideally these
139f17b710fSchristos // tests should not care about the internal representation.
140f17b710fSchristos //
141f17b710fSchristos kod_db[0]->timestamp = 0xabcd;
142f17b710fSchristos kod_db[1]->timestamp = 0xabcd;
143f17b710fSchristos kod_db[2]->timestamp = 0xabcd;
144f17b710fSchristos
145f17b710fSchristos write_kod_db();
146f17b710fSchristos
147f17b710fSchristos // Open file and compare sizes and content.
148f17b710fSchristos FILE * actual = fopen(kod_db_file, "rb");
149f17b710fSchristos FILE * expected = fopen(CreatePath("kod-expected-multiple", INPUT_DIR),"rb");
150a6f3f22fSchristos
151a6f3f22fSchristos TEST_ASSERT_NOT_NULL(actual);
152a6f3f22fSchristos TEST_ASSERT_NOT_NULL(expected);
153f17b710fSchristos
154f17b710fSchristos
155f17b710fSchristos TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
156f17b710fSchristos
157f17b710fSchristos TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
158f17b710fSchristos }
159