2012-10-23 22:04:06 -07:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2012-10-23 22:04:06 -07:00
|
|
|
*
|
2021-06-03 08:37:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*
|
2016-06-27 14:56:38 +10:00
|
|
|
* 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
|
2020-09-14 16:20:40 -07:00
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2012-10-23 22:04:06 -07:00
|
|
|
*/
|
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
Include <sched.h> where necessary for musl libc
All unit tests define the UNIT_TESTING macro, which causes <cmocka.h> to
replace malloc(), calloc(), realloc(), and free() with its own functions
tracking memory allocations. In order for this not to break
compilation, the system header declaring the prototypes for these
standard functions must be included before <cmocka.h>.
Normally, these prototypes are only present in <stdlib.h>, so we make
sure it is included before <cmocka.h>. However, musl libc also defines
the prototypes for calloc() and free() in <sched.h>, which is included
by <pthread.h>, which is included e.g. by <isc/mutex.h>. Thus, unit
tests including "dnstest.h" (which includes <isc/mem.h>, which includes
<isc/mutex.h>) after <cmocka.h> will not compile with musl libc as for
these programs, <sched.h> will be included after <cmocka.h>.
Always including <cmocka.h> after all other header files is not a
feasible solution as that causes the mock assertion macros defined in
<isc/util.h> to mangle the contents of <cmocka.h>, thus breaking
compilation. We cannot really use the __noreturn__ or analyzer_noreturn
attributes with cmocka assertion functions because they do return if the
tested condition is true. The problem is that what BIND unit tests do
is incompatible with Clang Static Analyzer's assumptions: since we use
cmocka, our custom assertion handlers are present in a shared library
(i.e. it is the cmocka library that checks the assertion condition, not
a macro in unit test code). Redefining cmocka's assertion macros in
<isc/util.h> is an ugly hack to overcome that problem - unfortunately,
this is the only way we can think of to make Clang Static Analyzer
properly process unit test code. Giving up on Clang Static Analyzer
being able to properly process unit test code is not a satisfactory
solution.
Undefining _GNU_SOURCE for unit test code could work around the problem
(musl libc's <sched.h> only defines the prototypes for calloc() and
free() when _GNU_SOURCE is defined), but doing that could introduce
discrepancies for unit tests including entire *.c files, so it is also
not a good solution.
All in all, including <sched.h> before <cmocka.h> for all affected unit
tests seems to be the most benign way of working around this musl libc
quirk. While quite an ugly solution, it achieves our goals here, which
are to keep the benefit of proper static analysis of unit test code and
to fix compilation against musl libc.
2019-07-30 21:08:40 +02:00
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
2018-04-17 08:29:14 -07:00
|
|
|
#include <stdbool.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <stddef.h>
|
2018-10-23 23:19:44 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-10-23 22:04:06 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
2018-02-26 22:44:39 -08:00
|
|
|
#include <isc/netaddr.h>
|
2012-10-23 22:04:06 -07:00
|
|
|
#include <isc/print.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <isc/sockaddr.h>
|
2018-10-23 23:19:44 -07:00
|
|
|
#include <isc/util.h>
|
2012-10-23 22:04:06 -07:00
|
|
|
|
|
|
|
#include "isctest.h"
|
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_setup(void **state) {
|
2018-10-23 23:19:44 -07:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = isc_test_begin(NULL, true, 0);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2012-10-23 22:04:06 -07:00
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
return (0);
|
2012-10-23 22:04:06 -07:00
|
|
|
}
|
2018-10-23 23:19:44 -07:00
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_teardown(void **state) {
|
2018-10-23 23:19:44 -07:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
isc_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test sockaddr hash */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
sockaddr_hash(void **state) {
|
|
|
|
isc_sockaddr_t addr;
|
|
|
|
struct in_addr in;
|
2012-10-23 22:04:06 -07:00
|
|
|
struct in6_addr in6;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned int h1, h2, h3, h4;
|
|
|
|
int ret;
|
2012-10-23 22:04:06 -07:00
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
UNUSED(state);
|
2012-10-23 22:04:06 -07:00
|
|
|
|
|
|
|
in.s_addr = inet_addr("127.0.0.1");
|
|
|
|
isc_sockaddr_fromin(&addr, &in, 1);
|
2018-04-17 08:29:14 -07:00
|
|
|
h1 = isc_sockaddr_hash(&addr, true);
|
|
|
|
h2 = isc_sockaddr_hash(&addr, false);
|
2018-10-23 23:19:44 -07:00
|
|
|
assert_int_not_equal(h1, h2);
|
2012-10-23 22:04:06 -07:00
|
|
|
|
|
|
|
ret = inet_pton(AF_INET6, "::ffff:127.0.0.1", &in6);
|
2018-10-23 23:19:44 -07:00
|
|
|
assert_int_equal(ret, 1);
|
2012-10-23 22:04:06 -07:00
|
|
|
isc_sockaddr_fromin6(&addr, &in6, 1);
|
2018-04-17 08:29:14 -07:00
|
|
|
h3 = isc_sockaddr_hash(&addr, true);
|
|
|
|
h4 = isc_sockaddr_hash(&addr, false);
|
2018-10-23 23:19:44 -07:00
|
|
|
assert_int_equal(h1, h3);
|
|
|
|
assert_int_equal(h2, h4);
|
2012-10-23 22:04:06 -07:00
|
|
|
}
|
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
/* test isc_sockaddr_isnetzero() */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
sockaddr_isnetzero(void **state) {
|
|
|
|
isc_sockaddr_t addr;
|
|
|
|
struct in_addr in;
|
2015-10-16 08:00:15 +11:00
|
|
|
struct in6_addr in6;
|
2020-02-13 14:44:37 -08:00
|
|
|
bool r;
|
|
|
|
int ret;
|
|
|
|
size_t i;
|
2015-10-16 08:00:15 +11:00
|
|
|
struct {
|
|
|
|
const char *string;
|
2020-02-13 14:44:37 -08:00
|
|
|
bool expect;
|
2015-10-16 08:00:15 +11:00
|
|
|
} data4[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
{ "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 },
|
2018-04-17 08:29:14 -07:00
|
|
|
{ "255.255.255.255", false },
|
2015-10-16 08:00:15 +11:00
|
|
|
};
|
|
|
|
/*
|
|
|
|
* Mapped addresses are currently not netzero.
|
|
|
|
*/
|
|
|
|
struct {
|
|
|
|
const char *string;
|
2020-02-13 14:44:37 -08:00
|
|
|
bool expect;
|
2015-10-16 08:00:15 +11:00
|
|
|
} data6[] = {
|
2018-04-17 08:29:14 -07:00
|
|
|
{ "::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 },
|
2015-10-16 08:00:15 +11:00
|
|
|
};
|
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
UNUSED(state);
|
2015-10-16 08:00:15 +11:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
for (i = 0; i < sizeof(data4) / sizeof(data4[0]); i++) {
|
2017-05-02 13:40:49 -07:00
|
|
|
in.s_addr = inet_addr(data4[i].string);
|
2015-10-16 08:00:15 +11:00
|
|
|
isc_sockaddr_fromin(&addr, &in, 1);
|
|
|
|
r = isc_sockaddr_isnetzero(&addr);
|
2018-10-23 23:19:44 -07:00
|
|
|
assert_int_equal(r, data4[i].expect);
|
2015-10-16 08:00:15 +11:00
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
for (i = 0; i < sizeof(data6) / sizeof(data6[0]); i++) {
|
2017-05-02 13:40:49 -07:00
|
|
|
ret = inet_pton(AF_INET6, data6[i].string, &in6);
|
2018-10-23 23:19:44 -07:00
|
|
|
assert_int_equal(ret, 1);
|
2015-10-16 08:00:15 +11:00
|
|
|
isc_sockaddr_fromin6(&addr, &in6, 1);
|
|
|
|
r = isc_sockaddr_isnetzero(&addr);
|
2018-10-23 23:19:44 -07:00
|
|
|
assert_int_equal(r, data6[i].expect);
|
2015-10-16 08:00:15 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
/*
|
|
|
|
* test that isc_sockaddr_eqaddrprefix() returns true when prefixes of a
|
|
|
|
* and b are equal, and false when they are not equal
|
|
|
|
*/
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
sockaddr_eqaddrprefix(void **state) {
|
2018-02-26 22:44:39 -08:00
|
|
|
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;
|
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
UNUSED(state);
|
2018-02-26 22:44:39 -08:00
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
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);
|
2018-02-26 22:44:39 -08:00
|
|
|
|
|
|
|
isc_sockaddr_fromin(&isa_a, &ina_a, 0);
|
|
|
|
isc_sockaddr_fromin(&isa_b, &ina_b, 42);
|
|
|
|
isc_sockaddr_fromin(&isa_c, &ina_c, 0);
|
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
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));
|
2018-02-26 22:44:39 -08:00
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
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));
|
2018-02-26 22:44:39 -08:00
|
|
|
}
|
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-23 23:19:44 -07:00
|
|
|
const struct CMUnitTest tests[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
cmocka_unit_test_setup_teardown(sockaddr_hash, _setup,
|
|
|
|
_teardown),
|
2018-10-23 23:19:44 -07:00
|
|
|
cmocka_unit_test(sockaddr_isnetzero),
|
|
|
|
cmocka_unit_test(sockaddr_eqaddrprefix),
|
|
|
|
};
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-10-23 22:04:06 -07:00
|
|
|
|
2018-10-23 23:19:44 -07:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-23 23:19:44 -07:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
2021-01-18 19:15:44 +01:00
|
|
|
return (SKIPPED_TEST_EXIT_CODE);
|
2012-10-23 22:04:06 -07:00
|
|
|
}
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_CMOCKA */
|