Debian Codegraph @

tests/api/netacl.c

raw symbols SCIP
1 /*
2 * ProFTPD - FTP server testsuite
3 * Copyright (c) 2008-2023 The ProFTPD Project team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18 *
19 * As a special exemption, The ProFTPD Project team and other respective
20 * copyright holders give permission to link this program with OpenSSL, and
21 * distribute the resulting executable, without including the source code for
22 * OpenSSL in the source distribution.
23 */
24
25 /* NetACL API tests */
26
27 #include "tests.h"
28
29 static pool *p = NULL;
30
31 /* Fixtures */
32
33 static void set_up(void) {
34 if (p == NULL) {
35 p = permanent_pool = make_sub_pool(NULL);
36 }
37
38 init_netaddr();
39
40 if (getenv("TEST_VERBOSE") != NULL) {
41 pr_trace_set_levels("dns", 1, 20);
42 pr_trace_set_levels("netacl", 1, 20);
43 }
44 }
45
46 static void tear_down(void) {
47 if (getenv("TEST_VERBOSE") != NULL) {
48 pr_trace_set_levels("dns", 0, 0);
49 pr_trace_set_levels("netacl", 0, 0);
50 }
51
52 if (p) {
53 destroy_pool(p);
54 p = permanent_pool = NULL;
55 }
56 }
57
58 /* Tests */
59
60 START_TEST (netacl_create_test) {
61 pr_netacl_t *res;
62 pr_netacl_type_t acl_type;
63 char *acl_str;
64
65 res = pr_netacl_create(NULL, NULL);
66 ck_assert_msg(res == NULL, "Failed to handle NULL arguments");
67 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
68
69 res = pr_netacl_create(NULL, "");
70 ck_assert_msg(res == NULL, "Failed to handle NULL pool");
71 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
72
73 res = pr_netacl_create(p, NULL);
74 ck_assert_msg(res == NULL, "Failed to handle NULL ACL string");
75 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
76
77 res = pr_netacl_create(p, "");
78 ck_assert_msg(res == NULL, "Failed to handle empty ACL string");
79 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
80
81 acl_str = "ALL";
82 res = pr_netacl_create(p, acl_str);
83 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s'", acl_str);
84
85 acl_type = pr_netacl_get_type(res);
86 ck_assert_msg(acl_type == PR_NETACL_TYPE_ALL,
87 "Failed to have ALL type for ACL string '%s'", acl_str);
88
89 acl_str = "none";
90 res = pr_netacl_create(p, acl_str);
91 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s'", acl_str);
92
93 acl_type = pr_netacl_get_type(res);
94 ck_assert_msg(acl_type == PR_NETACL_TYPE_NONE,
95 "Failed to have NONE type for ACL string '%s'", acl_str);
96
97 acl_str = pstrdup(p, "localhost/24");
98 res = pr_netacl_create(p, acl_str);
99 ck_assert_msg(res == NULL, "Failed to handle bad ACL string '%s': %s",
100 acl_str, strerror(errno));
101 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
102
103 acl_str = pstrdup(p, "127.0.0.1/24");
104 res = pr_netacl_create(p, acl_str);
105 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
106 strerror(errno));
107
108 acl_type = pr_netacl_get_type(res);
109 ck_assert_msg(acl_type == PR_NETACL_TYPE_IPMASK,
110 "Failed to have IPMASK type for ACL string '%s'", acl_str);
111
112 acl_str = pstrdup(p, "127.0.0.1/36");
113 res = pr_netacl_create(p, acl_str);
114 ck_assert_msg(res == NULL, "Failed to handle bad ACL string '%s': %s", acl_str,
115 strerror(errno));
116
117 acl_str = pstrdup(p, "0.0.0.0/0");
118 res = pr_netacl_create(p, acl_str);
119 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
120 strerror(errno));
121
122 #ifdef PR_USE_IPV6
123 acl_str = pstrdup(p, "::1/36");
124 res = pr_netacl_create(p, acl_str);
125 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
126 strerror(errno));
127
128 acl_type = pr_netacl_get_type(res);
129 ck_assert_msg(acl_type == PR_NETACL_TYPE_IPMASK,
130 "Failed to have IPMASK type for ACL string '%s'", acl_str);
131
132 acl_str = pstrdup(p, "::ffff:127.0.0.1/111");
133 res = pr_netacl_create(p, acl_str);
134 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
135 strerror(errno));
136
137 acl_type = pr_netacl_get_type(res);
138 ck_assert_msg(acl_type == PR_NETACL_TYPE_IPMASK,
139 "Failed to have IPMASK type for ACL string '%s'", acl_str);
140
141 acl_str = pstrdup(p, "::1/136");
142 res = pr_netacl_create(p, acl_str);
143 ck_assert_msg(res == NULL, "Failed to handle bad ACL string '%s': %s", acl_str,
144 strerror(errno));
145
146 acl_str = pstrdup(p, "::ffff:127.0.0.1/136");
147 res = pr_netacl_create(p, acl_str);
148 ck_assert_msg(res == NULL, "Failed to handle bad ACL string '%s': %s", acl_str,
149 strerror(errno));
150
151 acl_str = pstrdup(p, "::1");
152 res = pr_netacl_create(p, acl_str);
153 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
154 strerror(errno));
155
156 acl_type = pr_netacl_get_type(res);
157 ck_assert_msg(acl_type == PR_NETACL_TYPE_IPMATCH,
158 "Failed to have IPMATCH type for ACL string '%s'", acl_str);
159
160 acl_str = pstrdup(p, "!::1");
161 res = pr_netacl_create(p, acl_str);
162 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
163 strerror(errno));
164
165 acl_type = pr_netacl_get_type(res);
166 ck_assert_msg(acl_type == PR_NETACL_TYPE_IPMATCH,
167 "Failed to have IPMATCH type for ACL string '%s'", acl_str);
168 #endif
169
170 acl_str = pstrdup(p, "127.0.0.1/0");
171 res = pr_netacl_create(p, acl_str);
172 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
173 strerror(errno));
174
175 acl_type = pr_netacl_get_type(res);
176 ck_assert_msg(acl_type == PR_NETACL_TYPE_IPMASK,
177 "Failed to have IPMASK type for ACL string '%s'", acl_str);
178
179 acl_str = pstrdup(p, "127.0.0.1/-1");
180 res = pr_netacl_create(p, acl_str);
181 ck_assert_msg(res == NULL, "Failed to handle bad ACL string '%s': %s", acl_str,
182 strerror(errno));
183
184 acl_str = pstrdup(p, "127.0.0.1.2/24");
185 res = pr_netacl_create(p, acl_str);
186 ck_assert_msg(res == NULL, "Failed to handle bad ACL string '%s': %s", acl_str,
187 strerror(errno));
188
189 acl_str = pstrdup(p, "127.0.0.1/25f");
190 res = pr_netacl_create(p, acl_str);
191 ck_assert_msg(res == NULL, "Failed to handle bad ACL string '%s': %s", acl_str,
192 strerror(errno));
193
194 acl_str = pstrdup(p, "127.0.0.");
195 res = pr_netacl_create(p, acl_str);
196 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
197 strerror(errno));
198
199 acl_type = pr_netacl_get_type(res);
200 ck_assert_msg(acl_type == PR_NETACL_TYPE_IPGLOB,
201 "Failed to have IPGLOB type for ACL string '%s'", acl_str);
202
203 acl_str = pstrdup(p, "127.0.0.1");
204 res = pr_netacl_create(p, acl_str);
205 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
206 strerror(errno));
207
208 acl_type = pr_netacl_get_type(res);
209 ck_assert_msg(acl_type == PR_NETACL_TYPE_IPMATCH,
210 "Failed to have IPMATCH type for ACL string '%s'", acl_str);
211
212 acl_str = pstrdup(p, "!127.0.0.1");
213 res = pr_netacl_create(p, acl_str);
214 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
215 strerror(errno));
216
217 acl_type = pr_netacl_get_type(res);
218 ck_assert_msg(acl_type == PR_NETACL_TYPE_IPMATCH,
219 "Failed to have IPMATCH type for ACL string '%s'", acl_str);
220
221 acl_str = pstrdup(p, "127.0.0.1.1");
222 res = pr_netacl_create(p, acl_str);
223 ck_assert_msg(res == NULL, "Failed to handle bad ACL string '%s': %s", acl_str,
224 strerror(errno));
225
226 acl_str = pstrdup(p, ".0.0.1");
227 res = pr_netacl_create(p, acl_str);
228 ck_assert_msg(res == NULL, "Failed to handle bad ACL string '%s': %s", acl_str,
229 strerror(errno));
230
231 acl_str = pstrdup(p, "*.0.0.1");
232 res = pr_netacl_create(p, acl_str);
233 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
234 strerror(errno));
235
236 acl_type = pr_netacl_get_type(res);
237 ck_assert_msg(acl_type == PR_NETACL_TYPE_IPGLOB,
238 "Failed to have IPGLOB type for ACL string '%s'", acl_str);
239
240 acl_str = pstrdup(p, ".edu");
241 res = pr_netacl_create(p, acl_str);
242 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
243 strerror(errno));
244
245 acl_type = pr_netacl_get_type(res);
246 ck_assert_msg(acl_type == PR_NETACL_TYPE_DNSGLOB,
247 "Failed to have DNSGLOB type for ACL string '%s'", acl_str);
248
249 acl_str = pstrdup(p, "localhost");
250 res = pr_netacl_create(p, acl_str);
251 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
252 strerror(errno));
253
254 acl_type = pr_netacl_get_type(res);
255 ck_assert_msg(acl_type == PR_NETACL_TYPE_DNSMATCH,
256 "Failed to have DNSMATCH type for ACL string '%s'", acl_str);
257
258 acl_str = pstrdup(p, "foobar");
259 res = pr_netacl_create(p, acl_str);
260 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
261 strerror(errno));
262
263 acl_type = pr_netacl_get_type(res);
264 ck_assert_msg(acl_type == PR_NETACL_TYPE_DNSMATCH,
265 "Failed to have DNSMATCH type for ACL string '%s'", acl_str);
266
267 acl_str = pstrdup(p, "!foobar");
268 res = pr_netacl_create(p, acl_str);
269 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
270 strerror(errno));
271
272 acl_type = pr_netacl_get_type(res);
273 ck_assert_msg(acl_type == PR_NETACL_TYPE_DNSMATCH,
274 "Failed to have DNSMATCH type for ACL string '%s'", acl_str);
275
276 acl_str = pstrdup(p, "!fo?bar");
277 res = pr_netacl_create(p, acl_str);
278 ck_assert_msg(res != NULL, "Failed to handle ACL string '%s': %s", acl_str,
279 strerror(errno));
280
281 acl_type = pr_netacl_get_type(res);
282 ck_assert_msg(acl_type == PR_NETACL_TYPE_DNSGLOB,
283 "Failed to have DNSGLOB type for ACL string '%s'", acl_str);
284 }
285 END_TEST
286
287 START_TEST (netacl_get_str_test) {
288 pr_netacl_t *acl;
289 char *acl_str, *ok;
290 const char *res;
291
292 res = pr_netacl_get_str(NULL, NULL);
293 ck_assert_msg(res == NULL, "Failed to handle NULL arguments");
294 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
295
296 res = pr_netacl_get_str(p, NULL);
297 ck_assert_msg(res == NULL, "Failed to handle NULL ACL");
298 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
299
300 acl_str = "all";
301 acl = pr_netacl_create(p, acl_str);
302 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
303
304 res = pr_netacl_get_str(NULL, acl);
305 ck_assert_msg(res == NULL, "Failed to handle NULL pool");
306 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
307
308 ok = "all <all>";
309 res = pr_netacl_get_str(p, acl);
310 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
311 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
312
313 acl_str = "AlL";
314 acl = pr_netacl_create(p, acl_str);
315 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
316
317 res = pr_netacl_get_str(p, acl);
318 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
319 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
320
321 acl_str = "None";
322 acl = pr_netacl_create(p, acl_str);
323 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
324
325 ok = "none <none>";
326 res = pr_netacl_get_str(p, acl);
327 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
328 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
329
330 acl_str = pstrdup(p, "127.0.0.1");
331 acl = pr_netacl_create(p, acl_str);
332 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
333
334 ok = "127.0.0.1 <IP address match>";
335 res = pr_netacl_get_str(p, acl);
336 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
337 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
338
339 acl_str = pstrdup(p, "!127.0.0.1");
340 acl = pr_netacl_create(p, acl_str);
341 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
342
343 ok = "!127.0.0.1 <IP address match, inverted>";
344 res = pr_netacl_get_str(p, acl);
345 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
346 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
347
348 acl_str = pstrdup(p, "127.0.0.");
349 acl = pr_netacl_create(p, acl_str);
350 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
351
352 ok = "127.0.0.* <IP address glob>";
353 res = pr_netacl_get_str(p, acl);
354 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
355 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
356
357 acl_str = pstrdup(p, "localhost");
358 acl = pr_netacl_create(p, acl_str);
359 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
360
361 ok = "localhost <DNS hostname match>";
362 res = pr_netacl_get_str(p, acl);
363 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
364 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
365
366 acl_str = pstrdup(p, ".castaglia.org");
367 acl = pr_netacl_create(p, acl_str);
368 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
369
370 ok = "*.castaglia.org <DNS hostname glob>";
371 res = pr_netacl_get_str(p, acl);
372 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
373 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
374
375 acl_str = pstrdup(p, "127.0.0.1/24");
376 acl = pr_netacl_create(p, acl_str);
377 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
378
379 ok = "127.0.0.1/24 <IP address mask, 24-bit mask>";
380 res = pr_netacl_get_str(p, acl);
381 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
382 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
383
384 acl_str = pstrdup(p, "127.0.0.1/0");
385 acl = pr_netacl_create(p, acl_str);
386 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
387
388 ok = "127.0.0.1/0 <IP address mask, 0-bit mask>";
389 res = pr_netacl_get_str(p, acl);
390 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
391 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
392
393 acl_str = pstrdup(p, "0.0.0.0/0");
394 acl = pr_netacl_create(p, acl_str);
395 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
396
397 ok = "0.0.0.0/0 <IP address mask, 0-bit mask>";
398 res = pr_netacl_get_str(p, acl);
399 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
400 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
401
402 acl_str = pstrdup(p, "!127.0.0.1/24");
403 acl = pr_netacl_create(p, acl_str);
404 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
405
406 ok = "!127.0.0.1/24 <IP address mask, 24-bit mask, inverted>";
407 res = pr_netacl_get_str(p, acl);
408 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
409 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
410
411 #ifdef PR_USE_IPV6
412 acl_str = pstrdup(p, "::1/24");
413 acl = pr_netacl_create(p, acl_str);
414 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
415
416 ok = "::1/24 <IP address mask, 24-bit mask>";
417 res = pr_netacl_get_str(p, acl);
418 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
419 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
420
421 acl_str = pstrdup(p, "::1/127");
422 acl = pr_netacl_create(p, acl_str);
423 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
424
425 ok = "::1/127 <IP address mask, 127-bit mask>";
426 res = pr_netacl_get_str(p, acl);
427 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
428 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
429
430 acl_str = pstrdup(p, "::ffff:127.0.0.1/127");
431 acl = pr_netacl_create(p, acl_str);
432 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
433
434 ok = "::ffff:127.0.0.1/127 <IP address mask, 127-bit mask>";
435 res = pr_netacl_get_str(p, acl);
436 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
437 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
438 #endif
439 }
440 END_TEST
441
442 START_TEST (netacl_get_str2_test) {
443 pr_netacl_t *acl;
444 char *acl_str, *ok;
445 const char *res;
446 int flags = PR_NETACL_FL_STR_NO_DESC;
447
448 res = pr_netacl_get_str2(NULL, NULL, 0);
449 ck_assert_msg(res == NULL, "Failed to handle NULL arguments");
450 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
451
452 res = pr_netacl_get_str2(p, NULL, 0);
453 ck_assert_msg(res == NULL, "Failed to handle NULL ACL");
454 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
455
456 acl_str = "all";
457 acl = pr_netacl_create(p, acl_str);
458 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
459
460 res = pr_netacl_get_str2(NULL, acl, flags);
461 ck_assert_msg(res == NULL, "Failed to handle NULL pool");
462 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
463
464 ok = "all";
465 res = pr_netacl_get_str2(p, acl, flags);
466 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
467 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
468
469 acl_str = "AlL";
470 acl = pr_netacl_create(p, acl_str);
471 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
472
473 res = pr_netacl_get_str2(p, acl, flags);
474 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
475 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
476
477 acl_str = "None";
478 acl = pr_netacl_create(p, acl_str);
479 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
480
481 ok = "none";
482 res = pr_netacl_get_str2(p, acl, flags);
483 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
484 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
485
486 acl_str = pstrdup(p, "127.0.0.1");
487 acl = pr_netacl_create(p, acl_str);
488 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
489
490 ok = "127.0.0.1";
491 res = pr_netacl_get_str2(p, acl, flags);
492 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
493 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
494
495 acl_str = pstrdup(p, "!127.0.0.1");
496 acl = pr_netacl_create(p, acl_str);
497 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
498
499 ok = "!127.0.0.1";
500 res = pr_netacl_get_str2(p, acl, flags);
501 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
502 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
503
504 acl_str = pstrdup(p, "127.0.0.");
505 acl = pr_netacl_create(p, acl_str);
506 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
507
508 ok = "127.0.0.*";
509 res = pr_netacl_get_str2(p, acl, flags);
510 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
511 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
512
513 acl_str = pstrdup(p, "localhost");
514 acl = pr_netacl_create(p, acl_str);
515 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
516
517 ok = "localhost";
518 res = pr_netacl_get_str2(p, acl, flags);
519 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
520 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
521
522 acl_str = pstrdup(p, ".castaglia.org");
523 acl = pr_netacl_create(p, acl_str);
524 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
525
526 ok = "*.castaglia.org";
527 res = pr_netacl_get_str2(p, acl, flags);
528 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
529 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
530
531 acl_str = pstrdup(p, "127.0.0.1/24");
532 acl = pr_netacl_create(p, acl_str);
533 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
534
535 ok = "127.0.0.1/24";
536 res = pr_netacl_get_str2(p, acl, flags);
537 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
538 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
539
540 acl_str = pstrdup(p, "127.0.0.1/0");
541 acl = pr_netacl_create(p, acl_str);
542 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
543
544 ok = "127.0.0.1/0";
545 res = pr_netacl_get_str2(p, acl, flags);
546 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
547 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
548
549 acl_str = pstrdup(p, "0.0.0.0/0");
550 acl = pr_netacl_create(p, acl_str);
551 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
552
553 ok = "0.0.0.0/0";
554 res = pr_netacl_get_str2(p, acl, flags);
555 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
556 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
557
558 acl_str = pstrdup(p, "!127.0.0.1/24");
559 acl = pr_netacl_create(p, acl_str);
560 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
561
562 ok = "!127.0.0.1/24";
563 res = pr_netacl_get_str2(p, acl, flags);
564 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
565 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
566
567 #ifdef PR_USE_IPV6
568 acl_str = pstrdup(p, "::1/24");
569 acl = pr_netacl_create(p, acl_str);
570 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
571
572 ok = "::1/24";
573 res = pr_netacl_get_str2(p, acl, flags);
574 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
575 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
576
577 acl_str = pstrdup(p, "::1/127");
578 acl = pr_netacl_create(p, acl_str);
579 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
580
581 ok = "::1/127";
582 res = pr_netacl_get_str2(p, acl, flags);
583 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
584 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
585
586 acl_str = pstrdup(p, "::ffff:127.0.0.1/127");
587 acl = pr_netacl_create(p, acl_str);
588 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
589
590 ok = "::ffff:127.0.0.1/127";
591 res = pr_netacl_get_str2(p, acl, flags);
592 ck_assert_msg(res != NULL, "Failed to get ACL string: %s", strerror(errno));
593 ck_assert_msg(strcmp(res, ok) == 0, "Expected '%s', got '%s'", ok, res);
594 #endif
595 }
596 END_TEST
597
598 START_TEST (netacl_dup_test) {
599 pr_netacl_t *acl, *res;
600
601 res = pr_netacl_dup(NULL, NULL);
602 ck_assert_msg(res == NULL, "Failed to handle NULL arguments");
603 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
604
605 res = pr_netacl_dup(p, NULL);
606 ck_assert_msg(res == NULL, "Failed to handle NULL ACL argument");
607 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
608
609 acl = pr_netacl_create(p, "ALL");
610 ck_assert_msg(acl != NULL, "Failed to create ALL ACL");
611
612 res = pr_netacl_dup(NULL, acl);
613 ck_assert_msg(res == NULL, "Failed to handle NULL pool");
614 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
615
616 res = pr_netacl_dup(p, acl);
617 ck_assert_msg(res != NULL, "Failed to dup ACL: %s", strerror(errno));
618 ck_assert_msg(strcmp(pr_netacl_get_str(p, res), pr_netacl_get_str(p, acl)) == 0,
619 "Expected '%s', got '%s'", pr_netacl_get_str(p, acl),
620 pr_netacl_get_str(p, res));
621 }
622 END_TEST
623
624 START_TEST (netacl_match_test) {
625 pr_netacl_t *acl;
626 const pr_netaddr_t *addr;
627 char *acl_str;
628 int have_localdomain = FALSE, res, reverse_dns;
629
630 res = pr_netacl_match(NULL, NULL);
631 ck_assert_msg(res == -2, "Failed to handle NULL arguments");
632 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
633
634 acl_str = "all";
635 acl = pr_netacl_create(p, acl_str);
636 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
637 strerror(errno));
638
639 res = pr_netacl_match(acl, NULL);
640 ck_assert_msg(res == -2, "Failed to handle NULL addr");
641 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
642
643 addr = pr_netaddr_get_addr(p, "localhost", NULL);
644 ck_assert_msg(addr != NULL, "Failed to get addr for '%s': %s", "localhost",
645 strerror(errno));
646
647 if (getenv("CI") == NULL) {
648 /* It's possible that the DNS name for 'localhost' that is used will
649 * actually be 'localhost.localdomain', depending on the contents of
650 * the host's /etc/hosts file.
651 */
652 if (strcmp(pr_netaddr_get_dnsstr(addr), "localhost.localdomain") == 0) {
653 have_localdomain = TRUE;
654 }
655 }
656
657 res = pr_netacl_match(NULL, addr);
658 ck_assert_msg(res == -2, "Failed to handle NULL ACL");
659 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
660
661 res = pr_netacl_match(acl, addr);
662 ck_assert_msg(res == 1, "Failed to positively match ACL to addr: %s",
663 strerror(errno));
664
665 acl_str = "none";
666 acl = pr_netacl_create(p, acl_str);
667 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
668 strerror(errno));
669
670 res = pr_netacl_match(acl, addr);
671 ck_assert_msg(res == -1, "Failed to negatively match ACL to addr: %s",
672 strerror(errno));
673
674 acl_str = pstrdup(p, "127.0.0.1");
675 acl = pr_netacl_create(p, acl_str);
676 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
677 strerror(errno));
678
679 res = pr_netacl_match(acl, addr);
680 ck_assert_msg(res == 1, "Failed to positively match ACL to addr: %s",
681 strerror(errno));
682
683 acl_str = pstrdup(p, "!127.0.0.1");
684 acl = pr_netacl_create(p, acl_str);
685 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
686 strerror(errno));
687
688 res = pr_netacl_match(acl, addr);
689 ck_assert_msg(res == -1, "Failed to negatively match ACL to addr: %s",
690 strerror(errno));
691
692 acl_str = pstrdup(p, "192.168.0.1");
693 acl = pr_netacl_create(p, acl_str);
694 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
695 strerror(errno));
696
697 res = pr_netacl_match(acl, addr);
698 ck_assert_msg(res == 0, "Failed to match ACL to addr: %s", strerror(errno));
699
700 acl_str = pstrdup(p, "!192.168.0.1");
701 acl = pr_netacl_create(p, acl_str);
702 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
703 strerror(errno));
704
705 res = pr_netacl_match(acl, addr);
706 ck_assert_msg(res == 1, "Failed to positively match ACL to addr: %s",
707 strerror(errno));
708
709 acl_str = pstrdup(p, "127.0.0.0/24");
710 acl = pr_netacl_create(p, acl_str);
711 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
712 strerror(errno));
713
714 res = pr_netacl_match(acl, addr);
715 ck_assert_msg(res == 1, "Failed to positively match ACL to addr: %s",
716 strerror(errno));
717
718 acl_str = pstrdup(p, "!127.0.0.0/24");
719 acl = pr_netacl_create(p, acl_str);
720 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
721 strerror(errno));
722
723 res = pr_netacl_match(acl, addr);
724 ck_assert_msg(res == -1, "Failed to negatively match ACL to addr: %s",
725 strerror(errno));
726
727 acl_str = pstrdup(p, "!1.2.3.4/24");
728 acl = pr_netacl_create(p, acl_str);
729 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
730 strerror(errno));
731
732 res = pr_netacl_match(acl, addr);
733 ck_assert_msg(res == 1, "Failed to positively match ACL to addr: %s",
734 strerror(errno));
735
736 acl_str = pstrdup(p, "127.0.0.");
737 acl = pr_netacl_create(p, acl_str);
738 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
739 strerror(errno));
740
741 res = pr_netacl_match(acl, addr);
742 ck_assert_msg(res == 1, "Failed to positively match ACL to addr: %s",
743 strerror(errno));
744
745 acl_str = pstrdup(p, "!127.0.0.");
746 acl = pr_netacl_create(p, acl_str);
747 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
748 strerror(errno));
749
750 res = pr_netacl_match(acl, addr);
751 ck_assert_msg(res == -1, "Failed to negatively match ACL to addr: %s",
752 strerror(errno));
753
754 acl_str = pstrdup(p, "!1.2.3.");
755 acl = pr_netacl_create(p, acl_str);
756 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
757 strerror(errno));
758
759 res = pr_netacl_match(acl, addr);
760 ck_assert_msg(res == 1, "Failed to positively match ACL to addr: %s",
761 strerror(errno));
762
763 if (have_localdomain == TRUE) {
764 acl_str = pstrdup(p, "localhost.localdomain");
765
766 } else {
767 acl_str = pstrdup(p, "localhost");
768 }
769
770 acl = pr_netacl_create(p, acl_str);
771 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
772 strerror(errno));
773
774 res = pr_netacl_match(acl, addr);
775 if (getenv("CI") == NULL) {
776 ck_assert_msg(res == 1, "Failed to positively match ACL to addr: %s",
777 strerror(errno));
778 }
779
780 if (have_localdomain == TRUE) {
781 acl_str = pstrdup(p, "!localhost.localdomain");
782
783 } else {
784 acl_str = pstrdup(p, "!localhost");
785 }
786
787 acl = pr_netacl_create(p, acl_str);
788 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
789 strerror(errno));
790
791 res = pr_netacl_match(acl, addr);
792 if (getenv("CI") == NULL) {
793 ck_assert_msg(res == -1, "Failed to negatively match ACL to addr: %s",
794 strerror(errno));
795 }
796
797 acl_str = "!www.google.com";
798 acl = pr_netacl_create(p, acl_str);
799 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
800 strerror(errno));
801
802 res = pr_netacl_match(acl, addr);
803 ck_assert_msg(res == 1, "Failed to positively match ACL to addr: %s",
804 strerror(errno));
805
806 if (have_localdomain == TRUE) {
807 acl_str = pstrdup(p, "loc*st.loc*in");
808
809 } else {
810 acl_str = pstrdup(p, "loc*st");
811 }
812
813 acl = pr_netacl_create(p, acl_str);
814 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
815 strerror(errno));
816
817 res = pr_netacl_match(acl, addr);
818 if (getenv("CI") == NULL) {
819 ck_assert_msg(res == 1, "Failed to positively match ACL to addr: %s",
820 strerror(errno));
821 }
822
823 if (have_localdomain == TRUE) {
824 acl_str = pstrdup(p, "!loc*st.loc*in");
825
826 } else {
827 acl_str = pstrdup(p, "!loc*st");
828 }
829
830 acl = pr_netacl_create(p, acl_str);
831 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
832 strerror(errno));
833
834 res = pr_netacl_match(acl, addr);
835 if (getenv("CI") == NULL) {
836 ck_assert_msg(res == -1, "Failed to negatively match ACL to addr: %s",
837 strerror(errno));
838 }
839
840 acl_str = "!www.g*g.com";
841 acl = pr_netacl_create(p, acl_str);
842 ck_assert_msg(acl != NULL, "Failed to handle ACL string '%s': %s", acl_str,
843 strerror(errno));
844
845 res = pr_netacl_match(acl, addr);
846 ck_assert_msg(res == 1, "Failed to positively match ACL to addr: %s",
847 strerror(errno));
848
849 reverse_dns = ServerUseReverseDNS;
850 ServerUseReverseDNS = FALSE;
851
852 res = pr_netacl_match(acl, addr);
853 ck_assert_msg(res == 0, "Matched DNS glob ACL to addr unexpectedly");
854
855 ServerUseReverseDNS = reverse_dns;
856 }
857 END_TEST
858
859 START_TEST (netacl_get_negated_test) {
860 pr_netacl_t *acl;
861 int res;
862
863 res = pr_netacl_get_negated(NULL);
864 ck_assert_msg(res == -1, "Failed to handle NULL argument");
865 ck_assert_msg(errno == EINVAL, "Failed to set errno to EINVAL");
866
867 acl = pr_netacl_create(p, "127.0.0.1");
868 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
869
870 res = pr_netacl_get_negated(acl);
871 ck_assert_msg(res == 0, "Expected %d, got %d", 0, res);
872
873 acl = pr_netacl_create(p, "!127.0.0.1");
874 ck_assert_msg(acl != NULL, "Failed to create ACL: %s", strerror(errno));
875
876 res = pr_netacl_get_negated(acl);
877 ck_assert_msg(res == 1, "Expected %d, got %d", 1, res);
878 }
879 END_TEST
880
881 Suite *tests_get_netacl_suite(void) {
882 Suite *suite;
883 TCase *testcase;
884
885 suite = suite_create("netacl");
886
887 testcase = tcase_create("base");
888 tcase_add_checked_fixture(testcase, set_up, tear_down);
889
890 tcase_add_test(testcase, netacl_create_test);
891 tcase_add_test(testcase, netacl_get_str_test);
892 tcase_add_test(testcase, netacl_get_str2_test);
893 tcase_add_test(testcase, netacl_dup_test);
894 tcase_add_test(testcase, netacl_match_test);
895 tcase_add_test(testcase, netacl_get_negated_test);
896
897 /* Some of the network-related tests may take a little longer. */
898 tcase_set_timeout(testcase, 30);
899
900 suite_add_tcase(suite, testcase);
901 return suite;
902 }