xref: /freebsd/contrib/ntp/sntp/tests/kodFile.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 #include "config.h"
2 
3 #include "ntp_types.h"
4 #include "ntp_stdlib.h" // For estrdup()
5 #include "fileHandlingTest.h"
6 #include "kod_management.h"
7 
8 #include "unity.h"
9 
10 /*
11  * We access some parts of the kod database directly, without
12  * going through the public interface
13  */
14 extern int kod_db_cnt;
15 extern struct kod_entry** kod_db;
16 extern char* kod_db_file;
17 
18 void setUp(void);
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);
25 
26 
27 void
28 setUp(void) {
29 	kod_db_cnt = 0;
30 	kod_db = NULL;
31 	init_lib();
32 }
33 
34 
35 void
36 test_ReadEmptyFile(void) {
37 	kod_init_kod_db(CreatePath("kod-test-empty", INPUT_DIR), TRUE);
38 
39 	TEST_ASSERT_EQUAL(0, kod_db_cnt);
40 }
41 
42 
43 void
44 test_ReadCorrectFile(void) {
45 	kod_init_kod_db(CreatePath("kod-test-correct", INPUT_DIR), TRUE);
46 
47 	TEST_ASSERT_EQUAL(2, kod_db_cnt);
48 
49 	struct kod_entry* res;
50 
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);
55 
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);
60 }
61 
62 
63 void
64 test_ReadFileWithBlankLines(void) {
65 	kod_init_kod_db(CreatePath("kod-test-blanks", INPUT_DIR), TRUE);
66 
67 	TEST_ASSERT_EQUAL(3, kod_db_cnt);
68 
69 	struct kod_entry* res;
70 
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);
75 
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);
80 
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);
85 }
86 
87 
88 void
89 test_WriteEmptyFile(void) {
90 	kod_db_file = estrdup("kod-output-blank");
91 	write_kod_db();
92 
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);
96 
97 	TEST_ASSERT_EQUAL(0, GetFileSize(is));
98 
99 	fclose(is);
100 }
101 
102 
103 void
104 test_WriteFileWithSingleEntry(void) {
105 	kod_db_file = estrdup("kod-output-single");
106 	add_entry("host1", "DENY");
107 
108 	// Here we must manipulate the timestamps, so they match the one in
109 	// the expected file.
110 
111 	kod_db[0]->timestamp = 1;
112 
113 	write_kod_db();
114 
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");
118 
119 	TEST_ASSERT_NOT_NULL(actual);
120 	TEST_ASSERT_NOT_NULL(expected);
121 
122 	TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
123 
124 	TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
125 }
126 
127 
128 void
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");
134 
135 	//
136 	// Manipulate timestamps. This is a bit of a hack, ideally these
137 	// tests should not care about the internal representation.
138 	//
139 	kod_db[0]->timestamp = 0xabcd;
140 	kod_db[1]->timestamp = 0xabcd;
141 	kod_db[2]->timestamp = 0xabcd;
142 
143 	write_kod_db();
144 
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");
148 
149 	TEST_ASSERT_NOT_NULL(actual);
150 	TEST_ASSERT_NOT_NULL(expected);
151 
152 
153 	TEST_ASSERT_EQUAL(GetFileSize(expected), GetFileSize(actual));
154 
155 	TEST_ASSERT_TRUE(CompareFileContent(expected, actual));
156 }
157