summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/sntp/tests/kodDatabase.c
blob: 3c056947151555a2348d905abeb7a0e59a61064f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "config.h"

#include "ntp_workimpl.h"
#include "ntp_types.h"
#include "sntptest.h"
#include "ntp_stdlib.h"
#include "sntp-opts.h"
#include "kod_management.h"
#include "ntp_io.h"

#include "unity.h"

void setUp(void);
void test_SingleEntryHandling(void);
void test_MultipleEntryHandling(void);
void test_NoMatchInSearch(void);
void test_AddDuplicate(void);
void test_DeleteEntry(void);


void
setUp(void) {
	kod_init_kod_db("/dev/null", TRUE);
	init_lib();
}


void
test_SingleEntryHandling(void) {
	const char HOST[] = "192.0.2.5";
	const char REASON[] = "DENY";

	add_entry(HOST, REASON);

	struct kod_entry* result;

	TEST_ASSERT_EQUAL(1, search_entry(HOST, &result));
	TEST_ASSERT_EQUAL_STRING(HOST, result->hostname);
	TEST_ASSERT_EQUAL_STRING(REASON, result->type);
}


void
test_MultipleEntryHandling(void) {
	const char HOST1[] = "192.0.2.3";
	const char REASON1[] = "DENY";

	const char HOST2[] = "192.0.5.5";
	const char REASON2[] = "RATE";

	const char HOST3[] = "192.0.10.1";
	const char REASON3[] = "DENY";

	add_entry(HOST1, REASON1);
	add_entry(HOST2, REASON2);
	add_entry(HOST3, REASON3);

	struct kod_entry* result;

	TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result));
	TEST_ASSERT_EQUAL_STRING(HOST1, result->hostname);
	TEST_ASSERT_EQUAL_STRING(REASON1, result->type);

	TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result));
	TEST_ASSERT_EQUAL_STRING(HOST2, result->hostname);
	TEST_ASSERT_EQUAL_STRING(REASON2, result->type);

	TEST_ASSERT_EQUAL(1, search_entry(HOST3, &result));
	TEST_ASSERT_EQUAL_STRING(HOST3, result->hostname);
	TEST_ASSERT_EQUAL_STRING(REASON3, result->type);

	free(result);
}


void
test_NoMatchInSearch(void) {
	const char HOST_ADD[] = "192.0.2.6";
	const char HOST_NOTADD[] = "192.0.6.1";
	const char REASON[] = "DENY";

	add_entry(HOST_ADD, REASON);

	struct kod_entry* result;

	TEST_ASSERT_EQUAL(0, search_entry(HOST_NOTADD, &result));
	TEST_ASSERT_TRUE(result == NULL);
}


void
test_AddDuplicate(void) {
	const char HOST[] = "192.0.2.3";
	const char REASON1[] = "RATE";
	const char REASON2[] = "DENY";

	add_entry(HOST, REASON1);
	struct kod_entry* result1;
	TEST_ASSERT_EQUAL(1, search_entry(HOST, &result1));

	/* 
	 * Sleeps for two seconds since we want to ensure that
	 * the timestamp is updated to a new value.
	 */
	sleep(2);

	add_entry(HOST, REASON2);
	struct kod_entry* result2;
	TEST_ASSERT_EQUAL(1, search_entry(HOST, &result2));

	TEST_ASSERT_FALSE(result1->timestamp == result2->timestamp);

	free(result1);
	free(result2);
}


void
test_DeleteEntry(void) {
	const char HOST1[] = "192.0.2.1";
	const char HOST2[] = "192.0.2.2";
	const char HOST3[] = "192.0.2.3";
	const char REASON[] = "DENY";

	add_entry(HOST1, REASON);
	add_entry(HOST2, REASON);
	add_entry(HOST3, REASON);

	struct kod_entry* result;
	
	TEST_ASSERT_EQUAL(1, search_entry(HOST2, &result));
	free(result);

	delete_entry(HOST2, REASON);

	TEST_ASSERT_EQUAL(0, search_entry(HOST2, &result));

	// Ensure that the other entry is still there.
	TEST_ASSERT_EQUAL(1, search_entry(HOST1, &result));
	free(result);
}
OpenPOWER on IntegriCloud