summaryrefslogtreecommitdiffstats
path: root/tests/knot/test_unreachable.c
blob: 826544d5e6ad4e3f45a4214bcc2e138fde9f2dfa (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
/*  Copyright (C) 2022 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <tap/basic.h>

#include "knot/common/unreachable.h"

#include "contrib/sockaddr.h"

#define UR_TEST_ADDRS 32
struct sockaddr_storage ur_test_addrs[UR_TEST_ADDRS] = { { 0 } };
struct sockaddr_storage ur_test_via[2] = { { 0 } };

int main(int argc, char *argv[])
{
	plan_lazy();

	global_unreachables = knot_unreachables_init(10);
	ok(global_unreachables != NULL, "unreachables: init");

	// ur_test_via[0] left empty - AF_UNSPEC
	sockaddr_set(&ur_test_via[1], AF_INET6, "::1", 0);

	for (int i = 0; i < UR_TEST_ADDRS; i++) {
		struct sockaddr_storage *s = &ur_test_addrs[i];
		sockaddr_set(s, AF_INET6, "::2", i + 1);
		struct sockaddr_storage *via = &ur_test_via[i % 2];
		struct sockaddr_storage *not_via = &ur_test_via[1 - i % 2];

		ok(!knot_unreachable_is(global_unreachables, s, via), "unreachables: pre[%d]", i);
		knot_unreachable_add(global_unreachables, s, via);
		ok(knot_unreachable_is(global_unreachables, s, via), "unreachables: post[%d]", i);
		ok(!knot_unreachable_is(global_unreachables, s, not_via), "unreachables: via[%d]", i);

		usleep(1000);
		if (i >= 10) {
			ok(!knot_unreachable_is(global_unreachables, &ur_test_addrs[i - 10], via),
			   "unreachables: expired[%d]", i - 10);
		}
	}

	knot_unreachables_deinit(&global_unreachables);
	ok(global_unreachables == NULL, "unreachables: deinit");

	return 0;
}