xref: /netbsd-src/external/mpl/bind/dist/tests/isc/sockaddr_test.c (revision bcda20f65a8566e103791ec395f7f499ef322704)
1 /*	$NetBSD: sockaddr_test.c,v 1.3 2025/01/26 16:25:50 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/sockaddr.h>
31 #include <isc/util.h>
32 
33 #include <tests/isc.h>
34 
35 /* test sockaddr hash */
36 ISC_RUN_TEST_IMPL(sockaddr_hash) {
37 	isc_sockaddr_t addr;
38 	struct in_addr in;
39 	struct in6_addr in6;
40 	unsigned int h1, h2, h3, h4;
41 	int ret;
42 
43 	UNUSED(state);
44 
45 	in.s_addr = inet_addr("127.0.0.1");
46 	isc_sockaddr_fromin(&addr, &in, 1);
47 	h1 = isc_sockaddr_hash(&addr, true);
48 	h2 = isc_sockaddr_hash(&addr, false);
49 	assert_int_not_equal(h1, h2);
50 
51 	ret = inet_pton(AF_INET6, "::ffff:127.0.0.1", &in6);
52 	assert_int_equal(ret, 1);
53 	isc_sockaddr_fromin6(&addr, &in6, 1);
54 	h3 = isc_sockaddr_hash(&addr, true);
55 	h4 = isc_sockaddr_hash(&addr, false);
56 	assert_int_equal(h1, h3);
57 	assert_int_equal(h2, h4);
58 }
59 
60 /* test isc_sockaddr_isnetzero() */
61 ISC_RUN_TEST_IMPL(sockaddr_isnetzero) {
62 	isc_sockaddr_t addr;
63 	struct in_addr in;
64 	struct in6_addr in6;
65 	bool r;
66 
67 	size_t i;
68 	struct {
69 		const char *string;
70 		bool expect;
71 	} data4[] = {
72 		{ "0.0.0.0", true },	      { "0.0.0.1", true },
73 		{ "0.0.1.0", true },	      { "0.1.0.0", true },
74 		{ "1.0.0.0", false },	      { "0.0.0.127", true },
75 		{ "0.0.0.255", true },	      { "127.0.0.1", false },
76 		{ "255.255.255.255", false },
77 	};
78 	/*
79 	 * Mapped addresses are currently not netzero.
80 	 */
81 	struct {
82 		const char *string;
83 		bool expect;
84 	} data6[] = {
85 		{ "::ffff:0.0.0.0", false },
86 		{ "::ffff:0.0.0.1", false },
87 		{ "::ffff:0.0.0.127", false },
88 		{ "::ffff:0.0.0.255", false },
89 		{ "::ffff:127.0.0.1", false },
90 		{ "::ffff:255.255.255.255", false },
91 	};
92 
93 	UNUSED(state);
94 
95 	for (i = 0; i < sizeof(data4) / sizeof(data4[0]); i++) {
96 		in.s_addr = inet_addr(data4[i].string);
97 		isc_sockaddr_fromin(&addr, &in, 1);
98 		r = isc_sockaddr_isnetzero(&addr);
99 		assert_int_equal(r, data4[i].expect);
100 	}
101 
102 	for (i = 0; i < sizeof(data6) / sizeof(data6[0]); i++) {
103 		int ret = inet_pton(AF_INET6, data6[i].string, &in6);
104 		assert_int_equal(ret, 1);
105 		isc_sockaddr_fromin6(&addr, &in6, 1);
106 		r = isc_sockaddr_isnetzero(&addr);
107 		assert_int_equal(r, data6[i].expect);
108 	}
109 }
110 
111 /*
112  * test that isc_sockaddr_eqaddrprefix() returns true when prefixes of a
113  * and b are equal, and false when they are not equal
114  */
115 ISC_RUN_TEST_IMPL(sockaddr_eqaddrprefix) {
116 	struct in_addr ina_a;
117 	struct in_addr ina_b;
118 	struct in_addr ina_c;
119 	isc_sockaddr_t isa_a;
120 	isc_sockaddr_t isa_b;
121 	isc_sockaddr_t isa_c;
122 
123 	UNUSED(state);
124 
125 	assert_true(inet_pton(AF_INET, "194.100.32.87", &ina_a) >= 0);
126 	assert_true(inet_pton(AF_INET, "194.100.32.80", &ina_b) >= 0);
127 	assert_true(inet_pton(AF_INET, "194.101.32.87", &ina_c) >= 0);
128 
129 	isc_sockaddr_fromin(&isa_a, &ina_a, 0);
130 	isc_sockaddr_fromin(&isa_b, &ina_b, 42);
131 	isc_sockaddr_fromin(&isa_c, &ina_c, 0);
132 
133 	assert_true(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 0));
134 	assert_true(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 29));
135 	assert_true(isc_sockaddr_eqaddrprefix(&isa_a, &isa_c, 8));
136 
137 	assert_false(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 30));
138 	assert_false(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 32));
139 	assert_false(isc_sockaddr_eqaddrprefix(&isa_a, &isa_c, 16));
140 }
141 
142 ISC_TEST_LIST_START
143 
144 ISC_TEST_ENTRY(sockaddr_hash)
145 ISC_TEST_ENTRY(sockaddr_isnetzero)
146 ISC_TEST_ENTRY(sockaddr_eqaddrprefix)
147 
148 ISC_TEST_LIST_END
149 
150 ISC_TEST_MAIN
151