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