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