mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-08-25 19:47:42 +00:00
This commit converts the license handling to adhere to the REUSE specification. It specifically: 1. Adds used licnses to LICENSES/ directory 2. Add "isc" template for adding the copyright boilerplate 3. Changes all source files to include copyright and SPDX license header, this includes all the C sources, documentation, zone files, configuration files. There are notes in the doc/dev/copyrights file on how to add correct headers to the new files. 4. Handle the rest that can't be modified via .reuse/dep5 file. The binary (or otherwise unmodifiable) files could have license places next to them in <foo>.license file, but this would lead to cluttered repository and most of the files handled in the .reuse/dep5 file are system test files.
189 lines
4.4 KiB
C
189 lines
4.4 KiB
C
/*
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
*
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
|
*
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
* information regarding copyright ownership.
|
|
*/
|
|
|
|
#if HAVE_CMOCKA
|
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
|
#include <setjmp.h>
|
|
#include <stdarg.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#define UNIT_TESTING
|
|
#include <cmocka.h>
|
|
|
|
#include <isc/netaddr.h>
|
|
#include <isc/print.h>
|
|
#include <isc/sockaddr.h>
|
|
#include <isc/util.h>
|
|
|
|
#include "isctest.h"
|
|
|
|
static int
|
|
_setup(void **state) {
|
|
isc_result_t result;
|
|
|
|
UNUSED(state);
|
|
|
|
result = isc_test_begin(NULL, true, 0);
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
return (0);
|
|
}
|
|
|
|
static int
|
|
_teardown(void **state) {
|
|
UNUSED(state);
|
|
|
|
isc_test_end();
|
|
|
|
return (0);
|
|
}
|
|
|
|
/* test sockaddr hash */
|
|
static void
|
|
sockaddr_hash(void **state) {
|
|
isc_sockaddr_t addr;
|
|
struct in_addr in;
|
|
struct in6_addr in6;
|
|
unsigned int h1, h2, h3, h4;
|
|
int ret;
|
|
|
|
UNUSED(state);
|
|
|
|
in.s_addr = inet_addr("127.0.0.1");
|
|
isc_sockaddr_fromin(&addr, &in, 1);
|
|
h1 = isc_sockaddr_hash(&addr, true);
|
|
h2 = isc_sockaddr_hash(&addr, false);
|
|
assert_int_not_equal(h1, h2);
|
|
|
|
ret = inet_pton(AF_INET6, "::ffff:127.0.0.1", &in6);
|
|
assert_int_equal(ret, 1);
|
|
isc_sockaddr_fromin6(&addr, &in6, 1);
|
|
h3 = isc_sockaddr_hash(&addr, true);
|
|
h4 = isc_sockaddr_hash(&addr, false);
|
|
assert_int_equal(h1, h3);
|
|
assert_int_equal(h2, h4);
|
|
}
|
|
|
|
/* test isc_sockaddr_isnetzero() */
|
|
static void
|
|
sockaddr_isnetzero(void **state) {
|
|
isc_sockaddr_t addr;
|
|
struct in_addr in;
|
|
struct in6_addr in6;
|
|
bool r;
|
|
int ret;
|
|
size_t i;
|
|
struct {
|
|
const char *string;
|
|
bool expect;
|
|
} data4[] = {
|
|
{ "0.0.0.0", true }, { "0.0.0.1", true },
|
|
{ "0.0.1.0", true }, { "0.1.0.0", true },
|
|
{ "1.0.0.0", false }, { "0.0.0.127", true },
|
|
{ "0.0.0.255", true }, { "127.0.0.1", false },
|
|
{ "255.255.255.255", false },
|
|
};
|
|
/*
|
|
* Mapped addresses are currently not netzero.
|
|
*/
|
|
struct {
|
|
const char *string;
|
|
bool expect;
|
|
} data6[] = {
|
|
{ "::ffff:0.0.0.0", false },
|
|
{ "::ffff:0.0.0.1", false },
|
|
{ "::ffff:0.0.0.127", false },
|
|
{ "::ffff:0.0.0.255", false },
|
|
{ "::ffff:127.0.0.1", false },
|
|
{ "::ffff:255.255.255.255", false },
|
|
};
|
|
|
|
UNUSED(state);
|
|
|
|
for (i = 0; i < sizeof(data4) / sizeof(data4[0]); i++) {
|
|
in.s_addr = inet_addr(data4[i].string);
|
|
isc_sockaddr_fromin(&addr, &in, 1);
|
|
r = isc_sockaddr_isnetzero(&addr);
|
|
assert_int_equal(r, data4[i].expect);
|
|
}
|
|
|
|
for (i = 0; i < sizeof(data6) / sizeof(data6[0]); i++) {
|
|
ret = inet_pton(AF_INET6, data6[i].string, &in6);
|
|
assert_int_equal(ret, 1);
|
|
isc_sockaddr_fromin6(&addr, &in6, 1);
|
|
r = isc_sockaddr_isnetzero(&addr);
|
|
assert_int_equal(r, data6[i].expect);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* test that isc_sockaddr_eqaddrprefix() returns true when prefixes of a
|
|
* and b are equal, and false when they are not equal
|
|
*/
|
|
static void
|
|
sockaddr_eqaddrprefix(void **state) {
|
|
struct in_addr ina_a;
|
|
struct in_addr ina_b;
|
|
struct in_addr ina_c;
|
|
isc_sockaddr_t isa_a;
|
|
isc_sockaddr_t isa_b;
|
|
isc_sockaddr_t isa_c;
|
|
|
|
UNUSED(state);
|
|
|
|
assert_true(inet_pton(AF_INET, "194.100.32.87", &ina_a) >= 0);
|
|
assert_true(inet_pton(AF_INET, "194.100.32.80", &ina_b) >= 0);
|
|
assert_true(inet_pton(AF_INET, "194.101.32.87", &ina_c) >= 0);
|
|
|
|
isc_sockaddr_fromin(&isa_a, &ina_a, 0);
|
|
isc_sockaddr_fromin(&isa_b, &ina_b, 42);
|
|
isc_sockaddr_fromin(&isa_c, &ina_c, 0);
|
|
|
|
assert_true(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 0));
|
|
assert_true(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 29));
|
|
assert_true(isc_sockaddr_eqaddrprefix(&isa_a, &isa_c, 8));
|
|
|
|
assert_false(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 30));
|
|
assert_false(isc_sockaddr_eqaddrprefix(&isa_a, &isa_b, 32));
|
|
assert_false(isc_sockaddr_eqaddrprefix(&isa_a, &isa_c, 16));
|
|
}
|
|
|
|
int
|
|
main(void) {
|
|
const struct CMUnitTest tests[] = {
|
|
cmocka_unit_test_setup_teardown(sockaddr_hash, _setup,
|
|
_teardown),
|
|
cmocka_unit_test(sockaddr_isnetzero),
|
|
cmocka_unit_test(sockaddr_eqaddrprefix),
|
|
};
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
|
}
|
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
#include <stdio.h>
|
|
|
|
int
|
|
main(void) {
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
|
return (SKIPPED_TEST_EXIT_CODE);
|
|
}
|
|
|
|
#endif /* if HAVE_CMOCKA */
|