Debian Codegraph @

tests/isc/sockaddr_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <inttypes.h>
15 #include <sched.h> /* IWYU pragma: keep */
16 #include <setjmp.h>
17 #include <stdarg.h>
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #define UNIT_TESTING
25 #include <cmocka.h>
26
27 #include <isc/netaddr.h>
28 #include <isc/sockaddr.h>
29 #include <isc/util.h>
30
31 #include <tests/isc.h>
32
33 /* test sockaddr hash */
34 ISC_RUN_TEST_IMPL(sockaddr_hash) {
35 isc_sockaddr_t addr;
36 struct in_addr in;
37 struct in6_addr in6;
38 unsigned int h1, h2, h3, h4;
39 int ret;
40
41 UNUSED(state);
42
43 in.s_addr = inet_addr("127.0.0.1");
44 isc_sockaddr_fromin(&addr, &in, 1);
45 h1 = isc_sockaddr_hash(&addr, true);
46 h2 = isc_sockaddr_hash(&addr, false);
47 assert_int_not_equal(h1, h2);
48
49 ret = inet_pton(AF_INET6, "::ffff:127.0.0.1", &in6);
50 assert_int_equal(ret, 1);
51 isc_sockaddr_fromin6(&addr, &in6, 1);
52 h3 = isc_sockaddr_hash(&addr, true);
53 h4 = isc_sockaddr_hash(&addr, false);
54 assert_int_equal(h1, h3);
55 assert_int_equal(h2, h4);
56 }
57
58 /* test isc_sockaddr_isnetzero() */
59 ISC_RUN_TEST_IMPL(sockaddr_isnetzero) {
60 isc_sockaddr_t addr;
61 struct in_addr in;
62 struct in6_addr in6;
63 bool r;
64
65 size_t i;
66 struct {
67 const char *string;
68 bool expect;
69 } data4[] = {
70 { "0.0.0.0", true }, { "0.0.0.1", true },
71 { "0.0.1.0", true }, { "0.1.0.0", true },
72 { "1.0.0.0", false }, { "0.0.0.127", true },
73 { "0.0.0.255", true }, { "127.0.0.1", false },
74 { "255.255.255.255", false },
75 };
76 /*
77 * Mapped addresses are currently not netzero.
78 */
79 struct {
80 const char *string;
81 bool expect;
82 } data6[] = {
83 { "::ffff:0.0.0.0", false },
84 { "::ffff:0.0.0.1", false },
85 { "::ffff:0.0.0.127", false },
86 { "::ffff:0.0.0.255", false },
87 { "::ffff:127.0.0.1", false },
88 { "::ffff:255.255.255.255", false },
89 };
90
91 UNUSED(state);
92
93 for (i = 0; i < sizeof(data4) / sizeof(data4[0]); i++) {
94 in.s_addr = inet_addr(data4[i].string);
95 isc_sockaddr_fromin(&addr, &in, 1);
96 r = isc_sockaddr_isnetzero(&addr);
97 assert_int_equal(r, data4[i].expect);
98 }
99
100 for (i = 0; i < sizeof(data6) / sizeof(data6[0]); i++) {
101 int ret = inet_pton(AF_INET6, data6[i].string, &in6);
102 assert_int_equal(ret, 1);
103 isc_sockaddr_fromin6(&addr, &in6, 1);
104 r = isc_sockaddr_isnetzero(&addr);
105 assert_int_equal(r, data6[i].expect);
106 }
107 }
108
109 /*
110 * test that isc_sockaddr_eqaddrprefix() returns true when prefixes of a
111 * and b are equal, and false when they are not equal
112 */
113 ISC_RUN_TEST_IMPL(sockaddr_eqaddrprefix) {
114 struct in_addr ina_a;
115 struct in_addr ina_b;
116 struct in_addr ina_c;
117 isc_sockaddr_t isa_a;
118 isc_sockaddr_t isa_b;
119 isc_sockaddr_t isa_c;
120
121 UNUSED(state);
122
123 assert_true(inet_pton(AF_INET, "194.100.32.87", &ina_a) >= 0);
124 assert_true(inet_pton(AF_INET, "194.100.32.80", &ina_b) >= 0);
125 assert_true(inet_pton(AF_INET, "194.101.32.87", &ina_c) >= 0);
126
127 isc_sockaddr_fromin(&isa_a, &ina_a, 0);
128 isc_sockaddr_fromin(&isa_b, &ina_b, 42);
129 isc_sockaddr_fromin(&isa_c, &ina_c, 0);
130
131 assert_true(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 0));
132 assert_true(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 29));
133 assert_true(isc_sockaddr_eqaddrprefix(&isa_a, &isa_c, 8));
134
135 assert_false(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 30));
136 assert_false(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 32));
137 assert_false(isc_sockaddr_eqaddrprefix(&isa_a, &isa_c, 16));
138 }
139
140 ISC_TEST_LIST_START
141
142 ISC_TEST_ENTRY(sockaddr_hash)
143 ISC_TEST_ENTRY(sockaddr_isnetzero)
144 ISC_TEST_ENTRY(sockaddr_eqaddrprefix)
145
146 ISC_TEST_LIST_END
147
148 ISC_TEST_MAIN