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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
#include "config.h"
#include "ntpd.h"
#include "ntp_lists.h"
#include "unity.h"
/* Helper functions */
sockaddr_u
create_sockaddr_u(short sin_family, unsigned short sin_port, char* ip_addr) {
sockaddr_u sockaddr;
sockaddr.sa4.sin_family = AF_INET;
sockaddr.sa4.sin_port = htons(sin_port);
memset(sockaddr.sa4.sin_zero, 0, 8);
sockaddr.sa4.sin_addr.s_addr = inet_addr(ip_addr);
return sockaddr;
}
void
setUp(void) {
init_restrict();
}
tearDown(void) {
restrict_u *empty_restrict = malloc(sizeof(restrict_u));
memset(empty_restrict, 0, sizeof(restrict_u));
restrict_u *current;
do {
UNLINK_HEAD_SLIST(current, restrictlist4, link);
if (current != NULL)
{
*current = *empty_restrict;
}
} while (current != NULL);
do {
UNLINK_HEAD_SLIST(current, restrictlist6, link);
if (current != NULL)
{
*current = *empty_restrict;
}
} while (current != NULL);
free(empty_restrict);
}
/* Tests */
void
test_RestrictionsAreEmptyAfterInit(void) {
restrict_u *rl4 = malloc(sizeof(restrict_u));
restrict_u *rl6 = malloc(sizeof(restrict_u));
memset(rl4, 0, sizeof(restrict_u));
memset(rl6, 0, sizeof(restrict_u));
TEST_ASSERT_EQUAL(rl4->count, restrictlist4->count);
TEST_ASSERT_EQUAL(rl4->flags, restrictlist4->flags);
TEST_ASSERT_EQUAL(rl4->mflags, restrictlist4->mflags);
TEST_ASSERT_EQUAL(rl4->expire, restrictlist4->expire);
TEST_ASSERT_EQUAL(rl4->u.v4.addr, restrictlist4->u.v4.addr);
TEST_ASSERT_EQUAL(rl4->u.v4.mask, restrictlist4->u.v4.mask);
TEST_ASSERT_EQUAL(rl6->count, restrictlist6->count);
TEST_ASSERT_EQUAL(rl6->flags, restrictlist6->flags);
TEST_ASSERT_EQUAL(rl6->mflags, restrictlist6->mflags);
TEST_ASSERT_EQUAL(rl6->expire, restrictlist6->expire);
free(rl4);
free(rl6);
}
void
test_ReturnsCorrectDefaultRestrictions(void) {
sockaddr_u sockaddr = create_sockaddr_u(AF_INET,
54321, "63.161.169.137");
u_short retval = restrictions(&sockaddr);
TEST_ASSERT_EQUAL(0, retval);
}
void
test_HackingDefaultRestriction(void) {
/*
* We change the flag of the default restriction,
* and check if restriction() returns that flag
*/
const u_short flags = 42;
sockaddr_u resaddr = create_sockaddr_u(AF_INET,
54321, "0.0.0.0");
sockaddr_u resmask = create_sockaddr_u(AF_INET,
54321, "0.0.0.0");
hack_restrict(RESTRICT_FLAGS, &resaddr, &resmask, 0, flags, 0);
sockaddr_u sockaddr = create_sockaddr_u(AF_INET,
54321, "111.123.251.124");
TEST_ASSERT_EQUAL(flags, restrictions(&sockaddr));
}
void
test_CantRemoveDefaultEntry(void) {
sockaddr_u resaddr = create_sockaddr_u(AF_INET, 54321, "0.0.0.0");
sockaddr_u resmask = create_sockaddr_u(AF_INET, 54321, "0.0.0.0");
hack_restrict(RESTRICT_REMOVE, &resaddr, &resmask, 0, 0, 0);
TEST_ASSERT_EQUAL(0, restrictions(&resaddr));
}
void
test_AddingNewRestriction(void) {
sockaddr_u resaddr = create_sockaddr_u(AF_INET, 54321, "11.22.33.44");
sockaddr_u resmask = create_sockaddr_u(AF_INET, 54321, "128.0.0.0");
const u_short flags = 42;
hack_restrict(RESTRICT_FLAGS, &resaddr, &resmask, 0, flags, 0);
TEST_ASSERT_EQUAL(flags, restrictions(&resaddr));
}
void
test_TheMostFittingRestrictionIsMatched(void) {
sockaddr_u resaddr_target = create_sockaddr_u(AF_INET, 54321, "11.22.33.44");
sockaddr_u resaddr_not_matching = create_sockaddr_u(AF_INET, 54321, "11.99.33.44");
sockaddr_u resmask_not_matching = create_sockaddr_u(AF_INET, 54321, "255.255.0.0");
sockaddr_u resaddr_best_match = create_sockaddr_u(AF_INET, 54321, "11.22.30.20");
sockaddr_u resmask_best_match = create_sockaddr_u(AF_INET, 54321, "255.255.0.0");
/* it also matches, but we prefer the one above, as it's more specific */
sockaddr_u resaddr_second_match = create_sockaddr_u(AF_INET, 54321, "11.99.33.44");
sockaddr_u resmask_second_match = create_sockaddr_u(AF_INET, 54321, "255.0.0.0");
hack_restrict(RESTRICT_FLAGS, &resaddr_not_matching, &resmask_not_matching, 0, 11, 0);
hack_restrict(RESTRICT_FLAGS, &resaddr_best_match, &resmask_best_match, 0, 22, 0);
hack_restrict(RESTRICT_FLAGS, &resaddr_second_match, &resmask_second_match, 0, 128, 0);
TEST_ASSERT_EQUAL(22, restrictions(&resaddr_target));
}
void
test_DeletedRestrictionIsNotMatched(void) {
sockaddr_u resaddr_target = create_sockaddr_u(AF_INET, 54321, "11.22.33.44");
sockaddr_u resaddr_not_matching = create_sockaddr_u(AF_INET, 54321, "11.99.33.44");
sockaddr_u resmask_not_matching = create_sockaddr_u(AF_INET, 54321, "255.255.0.0");
sockaddr_u resaddr_best_match = create_sockaddr_u(AF_INET, 54321, "11.22.30.20");
sockaddr_u resmask_best_match = create_sockaddr_u(AF_INET, 54321, "255.255.0.0");
sockaddr_u resaddr_second_match = create_sockaddr_u(AF_INET, 54321, "11.99.33.44");
sockaddr_u resmask_second_match = create_sockaddr_u(AF_INET, 54321, "255.0.0.0");
hack_restrict(RESTRICT_FLAGS, &resaddr_not_matching, &resmask_not_matching, 0, 11, 0);
hack_restrict(RESTRICT_FLAGS, &resaddr_best_match, &resmask_best_match, 0, 22, 0);
hack_restrict(RESTRICT_FLAGS, &resaddr_second_match, &resmask_second_match, 0, 128, 0);
/* deleting the best match*/
hack_restrict(RESTRICT_REMOVE, &resaddr_best_match, &resmask_best_match, 0, 22, 0);
TEST_ASSERT_EQUAL(128, restrictions(&resaddr_target));
}
void
test_RestrictUnflagWorks(void) {
sockaddr_u resaddr = create_sockaddr_u(AF_INET, 54321, "11.22.30.20");
sockaddr_u resmask = create_sockaddr_u(AF_INET, 54321, "255.255.0.0");
hack_restrict(RESTRICT_FLAGS, &resaddr, &resmask, 0, 11, 0);
hack_restrict(RESTRICT_UNFLAG, &resaddr, &resmask, 0, 10, 0);
TEST_ASSERT_EQUAL(1, restrictions(&resaddr));
}
|