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