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