| 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 <stddef.h> |
| 19 |
#include <stdlib.h> |
| 20 |
#include <string.h> |
| 21 |
#include <unistd.h> |
| 22 |
|
| 23 |
#define UNIT_TESTING |
| 24 |
#include <cmocka.h> |
| 25 |
|
| 26 |
#include <isc/netaddr.h> |
| 27 |
#include <isc/result.h> |
| 28 |
#include <isc/string.h> |
| 29 |
#include <isc/util.h> |
| 30 |
|
| 31 |
#include <dns/dns64.h> |
| 32 |
#include <dns/rdata.h> |
| 33 |
#include <dns/rdatalist.h> |
| 34 |
#include <dns/rdataset.h> |
| 35 |
|
| 36 |
#include <tests/dns.h> |
| 37 |
|
| 38 |
static void |
| 39 |
multiple_prefixes(void) { |
| 40 |
size_t i, count; |
| 41 |
/* |
| 42 |
* Two prefix, non consectutive. |
| 43 |
*/ |
| 44 |
unsigned char aaaa[4][16] = { |
| 45 |
{ 0, 0, 0, 0, 192, 0, 0, 170, 0, 0, 0, 0, 192, 0, 0, 171 }, |
| 46 |
{ 0, 0, 0, 0, 192, 55, 0, 170, 0, 0, 0, 0, 192, 0, 0, 170 }, |
| 47 |
{ 0, 0, 0, 0, 192, 0, 0, 170, 0, 0, 0, 0, 192, 0, 0, 170 }, |
| 48 |
{ 0, 0, 0, 0, 192, 55, 0, 170, 0, 0, 0, 0, 192, 0, 0, 171 }, |
| 49 |
}; |
| 50 |
dns_rdataset_t rdataset; |
| 51 |
dns_rdatalist_t rdatalist; |
| 52 |
dns_rdata_t rdata[4] = { DNS_RDATA_INIT, DNS_RDATA_INIT, DNS_RDATA_INIT, |
| 53 |
DNS_RDATA_INIT }; |
| 54 |
isc_netprefix_t prefix[2]; |
| 55 |
unsigned char p1[] = { 0, 0, 0, 0, 192, 0, 0, 170, 0, 0, 0, 0 }; |
| 56 |
unsigned char p2[] = { 0, 0, 0, 0, 192, 55, 0, 170, 0, 0, 0, 0 }; |
| 57 |
isc_result_t result; |
| 58 |
bool have_p1, have_p2; |
| 59 |
|
| 60 |
/* |
| 61 |
* Construct AAAA rdataset containing 2 prefixes. |
| 62 |
*/ |
| 63 |
dns_rdatalist_init(&rdatalist); |
| 64 |
for (i = 0; i < 4; i++) { |
| 65 |
isc_region_t region; |
| 66 |
region.base = aaaa[i]; |
| 67 |
region.length = 16; |
| 68 |
dns_rdata_fromregion(&rdata[i], dns_rdataclass_in, |
| 69 |
dns_rdatatype_aaaa, ®ion); |
| 70 |
ISC_LIST_APPEND(rdatalist.rdata, &rdata[i], link); |
| 71 |
} |
| 72 |
rdatalist.type = rdata[0].type; |
| 73 |
rdatalist.rdclass = rdata[0].rdclass; |
| 74 |
rdatalist.ttl = 0; |
| 75 |
dns_rdataset_init(&rdataset); |
| 76 |
dns_rdatalist_tordataset(&rdatalist, &rdataset); |
| 77 |
|
| 78 |
count = ARRAY_SIZE(prefix); |
| 79 |
memset(&prefix, 0, sizeof(prefix)); |
| 80 |
result = dns_dns64_findprefix(&rdataset, prefix, &count); |
| 81 |
assert_int_equal(result, ISC_R_SUCCESS); |
| 82 |
assert_int_equal(count, 2); |
| 83 |
have_p1 = have_p2 = false; |
| 84 |
for (i = 0; i < count; i++) { |
| 85 |
assert_int_equal(prefix[i].prefixlen, 96); |
| 86 |
assert_int_equal(prefix[i].addr.family, AF_INET6); |
| 87 |
if (memcmp(prefix[i].addr.type.in6.s6_addr, p1, 12) == 0) { |
| 88 |
have_p1 = true; |
| 89 |
} |
| 90 |
if (memcmp(prefix[i].addr.type.in6.s6_addr, p2, 12) == 0) { |
| 91 |
have_p2 = true; |
| 92 |
} |
| 93 |
} |
| 94 |
assert_true(have_p1); |
| 95 |
assert_true(have_p2); |
| 96 |
|
| 97 |
/* |
| 98 |
* Check that insufficient prefix space returns ISC_R_NOSPACE |
| 99 |
* and that the prefix is populated. |
| 100 |
*/ |
| 101 |
count = 1; |
| 102 |
memset(&prefix, 0, sizeof(prefix)); |
| 103 |
result = dns_dns64_findprefix(&rdataset, prefix, &count); |
| 104 |
assert_int_equal(result, ISC_R_NOSPACE); |
| 105 |
assert_int_equal(count, 2); |
| 106 |
have_p1 = have_p2 = false; |
| 107 |
assert_int_equal(prefix[0].prefixlen, 96); |
| 108 |
assert_int_equal(prefix[0].addr.family, AF_INET6); |
| 109 |
if (memcmp(prefix[0].addr.type.in6.s6_addr, p1, 12) == 0) { |
| 110 |
have_p1 = true; |
| 111 |
} |
| 112 |
if (memcmp(prefix[0].addr.type.in6.s6_addr, p2, 12) == 0) { |
| 113 |
have_p2 = true; |
| 114 |
} |
| 115 |
if (!have_p2) { |
| 116 |
assert_true(have_p1); |
| 117 |
} |
| 118 |
if (!have_p1) { |
| 119 |
assert_true(have_p2); |
| 120 |
} |
| 121 |
assert_true(have_p1 != have_p2); |
| 122 |
} |
| 123 |
|
| 124 |
ISC_RUN_TEST_IMPL(dns64_findprefix) { |
| 125 |
unsigned int i, j, o; |
| 126 |
isc_result_t result; |
| 127 |
struct { |
| 128 |
unsigned char prefix[12]; |
| 129 |
unsigned int prefixlen; |
| 130 |
isc_result_t result; |
| 131 |
} tests[] = { |
| 132 |
/* The WKP with various lengths. */ |
| 133 |
{ { 0, 0x64, 0xff, 0x9b, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 134 |
32, |
| 135 |
ISC_R_SUCCESS }, |
| 136 |
{ { 0, 0x64, 0xff, 0x9b, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 137 |
40, |
| 138 |
ISC_R_SUCCESS }, |
| 139 |
{ { 0, 0x64, 0xff, 0x9b, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 140 |
48, |
| 141 |
ISC_R_SUCCESS }, |
| 142 |
{ { 0, 0x64, 0xff, 0x9b, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 143 |
56, |
| 144 |
ISC_R_SUCCESS }, |
| 145 |
{ { 0, 0x64, 0xff, 0x9b, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 146 |
64, |
| 147 |
ISC_R_SUCCESS }, |
| 148 |
{ { 0, 0x64, 0xff, 0x9b, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| 149 |
96, |
| 150 |
ISC_R_SUCCESS }, |
| 151 |
/* |
| 152 |
* Prefix with the mapped addresses also appearing in the |
| 153 |
* prefix. |
| 154 |
*/ |
| 155 |
{ { 0, 0, 0, 0, 192, 0, 0, 170, 0, 0, 0, 0 }, |
| 156 |
96, |
| 157 |
ISC_R_SUCCESS }, |
| 158 |
{ { 0, 0, 0, 0, 192, 0, 0, 171, 0, 0, 0, 0 }, |
| 159 |
96, |
| 160 |
ISC_R_SUCCESS }, |
| 161 |
/* Bad prefix, MBZ != 0. */ |
| 162 |
{ { 0, 0x64, 0xff, 0x9b, 0, 0, 0, 0, 1, 0, 0, 0 }, |
| 163 |
96, |
| 164 |
ISC_R_NOTFOUND }, |
| 165 |
}; |
| 166 |
|
| 167 |
for (i = 0; i < ARRAY_SIZE(tests); i++) { |
| 168 |
size_t count = 2; |
| 169 |
dns_rdataset_t rdataset; |
| 170 |
dns_rdatalist_t rdatalist; |
| 171 |
dns_rdata_t rdata[2] = { DNS_RDATA_INIT, DNS_RDATA_INIT }; |
| 172 |
struct in6_addr ina6[2]; |
| 173 |
isc_netprefix_t prefix[2]; |
| 174 |
unsigned char aa[] = { 192, 0, 0, 170 }; |
| 175 |
unsigned char ab[] = { 192, 0, 0, 171 }; |
| 176 |
isc_region_t region; |
| 177 |
|
| 178 |
/* |
| 179 |
* Construct rdata. |
| 180 |
*/ |
| 181 |
memset(ina6[0].s6_addr, 0, sizeof(ina6[0].s6_addr)); |
| 182 |
memset(ina6[1].s6_addr, 0, sizeof(ina6[1].s6_addr)); |
| 183 |
memmove(ina6[0].s6_addr, tests[i].prefix, 12); |
| 184 |
memmove(ina6[1].s6_addr, tests[i].prefix, 12); |
| 185 |
o = tests[i].prefixlen / 8; |
| 186 |
for (j = 0; j < 4; j++) { |
| 187 |
if ((o + j) == 8U) { |
| 188 |
o++; /* skip mbz */ |
| 189 |
} |
| 190 |
ina6[0].s6_addr[j + o] = aa[j]; |
| 191 |
ina6[1].s6_addr[j + o] = ab[j]; |
| 192 |
} |
| 193 |
region.base = ina6[0].s6_addr; |
| 194 |
region.length = sizeof(ina6[0].s6_addr); |
| 195 |
dns_rdata_fromregion(&rdata[0], dns_rdataclass_in, |
| 196 |
dns_rdatatype_aaaa, ®ion); |
| 197 |
region.base = ina6[1].s6_addr; |
| 198 |
region.length = sizeof(ina6[1].s6_addr); |
| 199 |
dns_rdata_fromregion(&rdata[1], dns_rdataclass_in, |
| 200 |
dns_rdatatype_aaaa, ®ion); |
| 201 |
|
| 202 |
dns_rdatalist_init(&rdatalist); |
| 203 |
rdatalist.type = rdata[0].type; |
| 204 |
rdatalist.rdclass = rdata[0].rdclass; |
| 205 |
rdatalist.ttl = 0; |
| 206 |
ISC_LIST_APPEND(rdatalist.rdata, &rdata[0], link); |
| 207 |
ISC_LIST_APPEND(rdatalist.rdata, &rdata[1], link); |
| 208 |
dns_rdataset_init(&rdataset); |
| 209 |
dns_rdatalist_tordataset(&rdatalist, &rdataset); |
| 210 |
|
| 211 |
result = dns_dns64_findprefix(&rdataset, prefix, &count); |
| 212 |
assert_int_equal(result, tests[i].result); |
| 213 |
if (tests[i].result == ISC_R_SUCCESS) { |
| 214 |
assert_int_equal(count, 1); |
| 215 |
assert_int_equal(prefix[0].prefixlen, |
| 216 |
tests[i].prefixlen); |
| 217 |
assert_int_equal(prefix[0].addr.family, AF_INET6); |
| 218 |
assert_memory_equal(prefix[0].addr.type.in6.s6_addr, |
| 219 |
tests[i].prefix, |
| 220 |
tests[i].prefixlen / 8); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/* |
| 225 |
* Test multiple prefixes. |
| 226 |
*/ |
| 227 |
multiple_prefixes(); |
| 228 |
} |
| 229 |
|
| 230 |
ISC_TEST_LIST_START |
| 231 |
ISC_TEST_ENTRY(dns64_findprefix) |
| 232 |
ISC_TEST_LIST_END |
| 233 |
|
| 234 |
ISC_TEST_MAIN |