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