1276da39aSCy Schubert #include "config.h"
2276da39aSCy Schubert
3*3311ff84SXin LI #include "ntp_workimpl.h"
4276da39aSCy Schubert #include "ntp_types.h"
5276da39aSCy Schubert #include "sntptest.h"
6276da39aSCy Schubert #include "ntp_stdlib.h"
7276da39aSCy Schubert #include "sntp-opts.h"
8276da39aSCy Schubert #include "kod_management.h"
99034852cSGleb Smirnoff #include "ntp_io.h"
10276da39aSCy Schubert
11276da39aSCy Schubert #include "unity.h"
12276da39aSCy Schubert
139034852cSGleb Smirnoff void setUp(void);
149034852cSGleb Smirnoff void test_SingleEntryHandling(void);
159034852cSGleb Smirnoff void test_MultipleEntryHandling(void);
169034852cSGleb Smirnoff void test_NoMatchInSearch(void);
179034852cSGleb Smirnoff void test_AddDuplicate(void);
189034852cSGleb Smirnoff void test_DeleteEntry(void);
199034852cSGleb Smirnoff
209034852cSGleb Smirnoff
219034852cSGleb Smirnoff void
setUp(void)229034852cSGleb Smirnoff setUp(void) {
23276da39aSCy Schubert kod_init_kod_db("/dev/null", TRUE);
24*3311ff84SXin LI init_lib();
25276da39aSCy Schubert }
26276da39aSCy Schubert
27276da39aSCy Schubert
289034852cSGleb Smirnoff void
test_SingleEntryHandling(void)299034852cSGleb Smirnoff test_SingleEntryHandling(void) {
309034852cSGleb Smirnoff const char HOST[] = "192.0.2.5";
319034852cSGleb Smirnoff const char REASON[] = "DENY";
32276da39aSCy Schubert
33276da39aSCy Schubert add_entry(HOST, REASON);
34276da39aSCy Schubert
35276da39aSCy Schubert struct kod_entry* result;
36276da39aSCy Schubert
37276da39aSCy Schubert TEST_ASSERT_EQUAL(1, search_entry(HOST, &result));
38276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING(HOST, result->hostname);
39276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING(REASON, result->type);
40276da39aSCy Schubert }
41276da39aSCy Schubert
42276da39aSCy Schubert
439034852cSGleb Smirnoff void
test_MultipleEntryHandling(void)449034852cSGleb Smirnoff test_MultipleEntryHandling(void) {
459034852cSGleb Smirnoff const char HOST1[] = "192.0.2.3";
469034852cSGleb Smirnoff const char REASON1[] = "DENY";
47276da39aSCy Schubert
489034852cSGleb Smirnoff const char HOST2[] = "192.0.5.5";
499034852cSGleb Smirnoff const char REASON2[] = "RATE";
509034852cSGleb Smirnoff
519034852cSGleb Smirnoff const char HOST3[] = "192.0.10.1";
529034852cSGleb Smirnoff const char REASON3[] = "DENY";
53276da39aSCy Schubert
54276da39aSCy Schubert add_entry(HOST1, REASON1);
55276da39aSCy Schubert add_entry(HOST2, REASON2);
56276da39aSCy Schubert add_entry(HOST3, REASON3);
57276da39aSCy Schubert
58276da39aSCy Schubert struct kod_entry* result;
59276da39aSCy Schubert
60276da39aSCy Schubert TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result));
61276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING(HOST1, result->hostname);
62276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING(REASON1, result->type);
63276da39aSCy Schubert
64276da39aSCy Schubert TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result));
65276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING(HOST2, result->hostname);
66276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING(REASON2, result->type);
67276da39aSCy Schubert
68276da39aSCy Schubert TEST_ASSERT_EQUAL(1, search_entry(HOST3, &result));
69276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING(HOST3, result->hostname);
70276da39aSCy Schubert TEST_ASSERT_EQUAL_STRING(REASON3, result->type);
71276da39aSCy Schubert
72276da39aSCy Schubert free(result);
73276da39aSCy Schubert }
74276da39aSCy Schubert
759034852cSGleb Smirnoff
769034852cSGleb Smirnoff void
test_NoMatchInSearch(void)779034852cSGleb Smirnoff test_NoMatchInSearch(void) {
789034852cSGleb Smirnoff const char HOST_ADD[] = "192.0.2.6";
799034852cSGleb Smirnoff const char HOST_NOTADD[] = "192.0.6.1";
809034852cSGleb Smirnoff const char REASON[] = "DENY";
81276da39aSCy Schubert
82276da39aSCy Schubert add_entry(HOST_ADD, REASON);
83276da39aSCy Schubert
84276da39aSCy Schubert struct kod_entry* result;
85276da39aSCy Schubert
86276da39aSCy Schubert TEST_ASSERT_EQUAL(0, search_entry(HOST_NOTADD, &result));
87276da39aSCy Schubert TEST_ASSERT_TRUE(result == NULL);
88276da39aSCy Schubert }
89276da39aSCy Schubert
909034852cSGleb Smirnoff
919034852cSGleb Smirnoff void
test_AddDuplicate(void)929034852cSGleb Smirnoff test_AddDuplicate(void) {
939034852cSGleb Smirnoff const char HOST[] = "192.0.2.3";
949034852cSGleb Smirnoff const char REASON1[] = "RATE";
959034852cSGleb Smirnoff const char REASON2[] = "DENY";
96276da39aSCy Schubert
97276da39aSCy Schubert add_entry(HOST, REASON1);
98276da39aSCy Schubert struct kod_entry* result1;
99276da39aSCy Schubert TEST_ASSERT_EQUAL(1, search_entry(HOST, &result1));
100276da39aSCy Schubert
101276da39aSCy Schubert /*
102276da39aSCy Schubert * Sleeps for two seconds since we want to ensure that
103276da39aSCy Schubert * the timestamp is updated to a new value.
104276da39aSCy Schubert */
105276da39aSCy Schubert sleep(2);
106276da39aSCy Schubert
107276da39aSCy Schubert add_entry(HOST, REASON2);
108276da39aSCy Schubert struct kod_entry* result2;
109276da39aSCy Schubert TEST_ASSERT_EQUAL(1, search_entry(HOST, &result2));
110276da39aSCy Schubert
111276da39aSCy Schubert TEST_ASSERT_FALSE(result1->timestamp == result2->timestamp);
112276da39aSCy Schubert
113276da39aSCy Schubert free(result1);
114276da39aSCy Schubert free(result2);
115276da39aSCy Schubert }
116276da39aSCy Schubert
1179034852cSGleb Smirnoff
1189034852cSGleb Smirnoff void
test_DeleteEntry(void)1199034852cSGleb Smirnoff test_DeleteEntry(void) {
1209034852cSGleb Smirnoff const char HOST1[] = "192.0.2.1";
1219034852cSGleb Smirnoff const char HOST2[] = "192.0.2.2";
1229034852cSGleb Smirnoff const char HOST3[] = "192.0.2.3";
1239034852cSGleb Smirnoff const char REASON[] = "DENY";
124276da39aSCy Schubert
125276da39aSCy Schubert add_entry(HOST1, REASON);
126276da39aSCy Schubert add_entry(HOST2, REASON);
127276da39aSCy Schubert add_entry(HOST3, REASON);
128276da39aSCy Schubert
129276da39aSCy Schubert struct kod_entry* result;
130276da39aSCy Schubert
131276da39aSCy Schubert TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result));
132276da39aSCy Schubert free(result);
133276da39aSCy Schubert
134276da39aSCy Schubert delete_entry(HOST2, REASON);
135276da39aSCy Schubert
136276da39aSCy Schubert TEST_ASSERT_EQUAL(0, search_entry(HOST2, &result));
137276da39aSCy Schubert
138276da39aSCy Schubert // Ensure that the other entry is still there.
139276da39aSCy Schubert TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result));
140276da39aSCy Schubert free(result);
141276da39aSCy Schubert }
142