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