xref: /freebsd/contrib/ntp/sntp/tests/kodDatabase.c (revision 11d38a5764295585a2472d5e861fa8abe1a11eb2)
1 #include "config.h"
2 
3 #include "ntp_types.h"
4 #include "sntptest.h"
5 #include "ntp_stdlib.h"
6 #include "sntp-opts.h"
7 #include "kod_management.h"
8 #include "ntp_io.h"
9 
10 #include "unity.h"
11 
12 void setUp(void);
13 void test_SingleEntryHandling(void);
14 void test_MultipleEntryHandling(void);
15 void test_NoMatchInSearch(void);
16 void test_AddDuplicate(void);
17 void test_DeleteEntry(void);
18 
19 
20 void
21 setUp(void) {
22 	kod_init_kod_db("/dev/null", TRUE);
23 }
24 
25 
26 void
27 test_SingleEntryHandling(void) {
28 	const char HOST[] = "192.0.2.5";
29 	const char REASON[] = "DENY";
30 
31 	add_entry(HOST, REASON);
32 
33 	struct kod_entry* result;
34 
35 	TEST_ASSERT_EQUAL(1, search_entry(HOST, &result));
36 	TEST_ASSERT_EQUAL_STRING(HOST, result->hostname);
37 	TEST_ASSERT_EQUAL_STRING(REASON, result->type);
38 }
39 
40 
41 void
42 test_MultipleEntryHandling(void) {
43 	const char HOST1[] = "192.0.2.3";
44 	const char REASON1[] = "DENY";
45 
46 	const char HOST2[] = "192.0.5.5";
47 	const char REASON2[] = "RATE";
48 
49 	const char HOST3[] = "192.0.10.1";
50 	const char REASON3[] = "DENY";
51 
52 	add_entry(HOST1, REASON1);
53 	add_entry(HOST2, REASON2);
54 	add_entry(HOST3, REASON3);
55 
56 	struct kod_entry* result;
57 
58 	TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result));
59 	TEST_ASSERT_EQUAL_STRING(HOST1, result->hostname);
60 	TEST_ASSERT_EQUAL_STRING(REASON1, result->type);
61 
62 	TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result));
63 	TEST_ASSERT_EQUAL_STRING(HOST2, result->hostname);
64 	TEST_ASSERT_EQUAL_STRING(REASON2, result->type);
65 
66 	TEST_ASSERT_EQUAL(1, search_entry(HOST3, &result));
67 	TEST_ASSERT_EQUAL_STRING(HOST3, result->hostname);
68 	TEST_ASSERT_EQUAL_STRING(REASON3, result->type);
69 
70 	free(result);
71 }
72 
73 
74 void
75 test_NoMatchInSearch(void) {
76 	const char HOST_ADD[] = "192.0.2.6";
77 	const char HOST_NOTADD[] = "192.0.6.1";
78 	const char REASON[] = "DENY";
79 
80 	add_entry(HOST_ADD, REASON);
81 
82 	struct kod_entry* result;
83 
84 	TEST_ASSERT_EQUAL(0, search_entry(HOST_NOTADD, &result));
85 	TEST_ASSERT_TRUE(result == NULL);
86 }
87 
88 
89 void
90 test_AddDuplicate(void) {
91 	const char HOST[] = "192.0.2.3";
92 	const char REASON1[] = "RATE";
93 	const char REASON2[] = "DENY";
94 
95 	add_entry(HOST, REASON1);
96 	struct kod_entry* result1;
97 	TEST_ASSERT_EQUAL(1, search_entry(HOST, &result1));
98 
99 	/*
100 	 * Sleeps for two seconds since we want to ensure that
101 	 * the timestamp is updated to a new value.
102 	 */
103 	sleep(2);
104 
105 	add_entry(HOST, REASON2);
106 	struct kod_entry* result2;
107 	TEST_ASSERT_EQUAL(1, search_entry(HOST, &result2));
108 
109 	TEST_ASSERT_FALSE(result1->timestamp == result2->timestamp);
110 
111 	free(result1);
112 	free(result2);
113 }
114 
115 
116 void
117 test_DeleteEntry(void) {
118 	const char HOST1[] = "192.0.2.1";
119 	const char HOST2[] = "192.0.2.2";
120 	const char HOST3[] = "192.0.2.3";
121 	const char REASON[] = "DENY";
122 
123 	add_entry(HOST1, REASON);
124 	add_entry(HOST2, REASON);
125 	add_entry(HOST3, REASON);
126 
127 	struct kod_entry* result;
128 
129 	TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result));
130 	free(result);
131 
132 	delete_entry(HOST2, REASON);
133 
134 	TEST_ASSERT_EQUAL(0, search_entry(HOST2, &result));
135 
136 	// Ensure that the other entry is still there.
137 	TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result));
138 	free(result);
139 }
140