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