1 /* $NetBSD: netaddr_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 <stdio.h> 23 #include <stdlib.h> 24 #include <string.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 isc_netaddr_isnetzero() */ 36 ISC_RUN_TEST_IMPL(netaddr_isnetzero) { 37 unsigned int i; 38 struct in_addr ina; 39 struct { 40 const char *address; 41 bool expect; 42 } tests[] = { { "0.0.0.0", true }, { "0.0.0.1", true }, 43 { "0.0.1.2", true }, { "0.1.2.3", true }, 44 { "10.0.0.0", false }, { "10.9.0.0", false }, 45 { "10.9.8.0", false }, { "10.9.8.7", false }, 46 { "127.0.0.0", false }, { "127.0.0.1", false } }; 47 isc_netaddr_t netaddr; 48 49 UNUSED(state); 50 51 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { 52 bool result; 53 ina.s_addr = inet_addr(tests[i].address); 54 isc_netaddr_fromin(&netaddr, &ina); 55 result = isc_netaddr_isnetzero(&netaddr); 56 assert_int_equal(result, tests[i].expect); 57 } 58 } 59 60 /* test isc_netaddr_masktoprefixlen() calculates correct prefix lengths */ 61 ISC_RUN_TEST_IMPL(netaddr_masktoprefixlen) { 62 struct in_addr na_a; 63 struct in_addr na_b; 64 struct in_addr na_c; 65 struct in_addr na_d; 66 isc_netaddr_t ina_a; 67 isc_netaddr_t ina_b; 68 isc_netaddr_t ina_c; 69 isc_netaddr_t ina_d; 70 unsigned int plen; 71 72 UNUSED(state); 73 74 assert_true(inet_pton(AF_INET, "0.0.0.0", &na_a) >= 0); 75 assert_true(inet_pton(AF_INET, "255.255.255.254", &na_b) >= 0); 76 assert_true(inet_pton(AF_INET, "255.255.255.255", &na_c) >= 0); 77 assert_true(inet_pton(AF_INET, "255.255.255.0", &na_d) >= 0); 78 79 isc_netaddr_fromin(&ina_a, &na_a); 80 isc_netaddr_fromin(&ina_b, &na_b); 81 isc_netaddr_fromin(&ina_c, &na_c); 82 isc_netaddr_fromin(&ina_d, &na_d); 83 84 assert_int_equal(isc_netaddr_masktoprefixlen(&ina_a, &plen), 85 ISC_R_SUCCESS); 86 assert_int_equal(plen, 0); 87 88 assert_int_equal(isc_netaddr_masktoprefixlen(&ina_b, &plen), 89 ISC_R_SUCCESS); 90 assert_int_equal(plen, 31); 91 92 assert_int_equal(isc_netaddr_masktoprefixlen(&ina_c, &plen), 93 ISC_R_SUCCESS); 94 assert_int_equal(plen, 32); 95 96 assert_int_equal(isc_netaddr_masktoprefixlen(&ina_d, &plen), 97 ISC_R_SUCCESS); 98 assert_int_equal(plen, 24); 99 } 100 101 /* check multicast addresses are detected properly */ 102 ISC_RUN_TEST_IMPL(netaddr_multicast) { 103 unsigned int i; 104 struct { 105 int family; 106 const char *addr; 107 bool is_multicast; 108 } tests[] = { 109 { AF_INET, "1.2.3.4", false }, { AF_INET, "4.3.2.1", false }, 110 { AF_INET, "224.1.1.1", true }, { AF_INET, "1.1.1.244", false }, 111 { AF_INET6, "::1", false }, { AF_INET6, "ff02::1", true } 112 }; 113 114 UNUSED(state); 115 116 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { 117 isc_netaddr_t na; 118 struct in_addr in; 119 struct in6_addr in6; 120 int r; 121 122 if (tests[i].family == AF_INET) { 123 r = inet_pton(AF_INET, tests[i].addr, 124 (unsigned char *)&in); 125 assert_int_equal(r, 1); 126 isc_netaddr_fromin(&na, &in); 127 } else { 128 r = inet_pton(AF_INET6, tests[i].addr, 129 (unsigned char *)&in6); 130 assert_int_equal(r, 1); 131 isc_netaddr_fromin6(&na, &in6); 132 } 133 134 assert_int_equal(isc_netaddr_ismulticast(&na), 135 tests[i].is_multicast); 136 } 137 } 138 139 ISC_TEST_LIST_START 140 141 ISC_TEST_ENTRY(netaddr_isnetzero) 142 ISC_TEST_ENTRY(netaddr_masktoprefixlen) 143 ISC_TEST_ENTRY(netaddr_multicast) 144 145 ISC_TEST_LIST_END 146 147 ISC_TEST_MAIN 148