mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-08-22 10:10:06 +00:00
The unit tests contain a lot of duplicated code and here's an attempt to reduce code duplication. This commit does several things: 1. Remove #ifdef HAVE_CMOCKA - we already solve this with automake conditionals. 2. Create a set of ISC_TEST_* and ISC_*_TEST_ macros to wrap the test implementations, test lists, and the main test routine, so we don't have to repeat this all over again. The macros were modeled after libuv test suite but adapted to cmocka as the test driver. A simple example of a unit test would be: ISC_RUN_TEST_IMPL(test1) { assert_true(true); } ISC_TEST_LIST_START ISC_TEST_ENTRY(test1) ISC_TEST_LIST_END ISC_TEST_MAIN (Discussion: Should this be ISC_TEST_RUN ?) For more complicated examples including group setup and teardown functions, and per-test setup and teardown functions. 3. The macros prefix the test functions and cmocka entries, so the name of the test can now match the tested function name, and we don't have to append `_test` because `run_test_` is automatically prepended to the main test function, and `setup_test_` and `teardown_test_` is prepended to setup and teardown function. 4. Update all the unit tests to use the new syntax and fix a few bits here and there. 5. In the future, we can separate the test declarations and test implementations which are going to greatly help with uncluttering the bigger unit tests like doh_test and netmgr_test, because the test implementations are not declared static (see `ISC_RUN_TEST_DECLARE` and `ISC_RUN_TEST_IMPL` for more details. NOTE: This heavily relies on preprocessor macros, but the result greatly outweighs all the negatives of using the macros. There's less duplicated code, the tests are more uniform and the implementation can be more flexible.
145 lines
3.7 KiB
C
145 lines
3.7 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.
|
|
*/
|
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
|
#include <setjmp.h>
|
|
#include <stdarg.h>
|
|
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define UNIT_TESTING
|
|
#include <cmocka.h>
|
|
|
|
#include <isc/netaddr.h>
|
|
#include <isc/sockaddr.h>
|
|
#include <isc/util.h>
|
|
|
|
#include <isc/test.h>
|
|
|
|
/* test isc_netaddr_isnetzero() */
|
|
ISC_RUN_TEST_IMPL(netaddr_isnetzero) {
|
|
unsigned int i;
|
|
struct in_addr ina;
|
|
struct {
|
|
const char *address;
|
|
bool expect;
|
|
} tests[] = { { "0.0.0.0", true }, { "0.0.0.1", true },
|
|
{ "0.0.1.2", true }, { "0.1.2.3", true },
|
|
{ "10.0.0.0", false }, { "10.9.0.0", false },
|
|
{ "10.9.8.0", false }, { "10.9.8.7", false },
|
|
{ "127.0.0.0", false }, { "127.0.0.1", false } };
|
|
isc_netaddr_t netaddr;
|
|
|
|
UNUSED(state);
|
|
|
|
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
|
|
bool result;
|
|
ina.s_addr = inet_addr(tests[i].address);
|
|
isc_netaddr_fromin(&netaddr, &ina);
|
|
result = isc_netaddr_isnetzero(&netaddr);
|
|
assert_int_equal(result, tests[i].expect);
|
|
}
|
|
}
|
|
|
|
/* test isc_netaddr_masktoprefixlen() calculates correct prefix lengths */
|
|
ISC_RUN_TEST_IMPL(netaddr_masktoprefixlen) {
|
|
struct in_addr na_a;
|
|
struct in_addr na_b;
|
|
struct in_addr na_c;
|
|
struct in_addr na_d;
|
|
isc_netaddr_t ina_a;
|
|
isc_netaddr_t ina_b;
|
|
isc_netaddr_t ina_c;
|
|
isc_netaddr_t ina_d;
|
|
unsigned int plen;
|
|
|
|
UNUSED(state);
|
|
|
|
assert_true(inet_pton(AF_INET, "0.0.0.0", &na_a) >= 0);
|
|
assert_true(inet_pton(AF_INET, "255.255.255.254", &na_b) >= 0);
|
|
assert_true(inet_pton(AF_INET, "255.255.255.255", &na_c) >= 0);
|
|
assert_true(inet_pton(AF_INET, "255.255.255.0", &na_d) >= 0);
|
|
|
|
isc_netaddr_fromin(&ina_a, &na_a);
|
|
isc_netaddr_fromin(&ina_b, &na_b);
|
|
isc_netaddr_fromin(&ina_c, &na_c);
|
|
isc_netaddr_fromin(&ina_d, &na_d);
|
|
|
|
assert_int_equal(isc_netaddr_masktoprefixlen(&ina_a, &plen),
|
|
ISC_R_SUCCESS);
|
|
assert_int_equal(plen, 0);
|
|
|
|
assert_int_equal(isc_netaddr_masktoprefixlen(&ina_b, &plen),
|
|
ISC_R_SUCCESS);
|
|
assert_int_equal(plen, 31);
|
|
|
|
assert_int_equal(isc_netaddr_masktoprefixlen(&ina_c, &plen),
|
|
ISC_R_SUCCESS);
|
|
assert_int_equal(plen, 32);
|
|
|
|
assert_int_equal(isc_netaddr_masktoprefixlen(&ina_d, &plen),
|
|
ISC_R_SUCCESS);
|
|
assert_int_equal(plen, 24);
|
|
}
|
|
|
|
/* check multicast addresses are detected properly */
|
|
ISC_RUN_TEST_IMPL(netaddr_multicast) {
|
|
unsigned int i;
|
|
struct {
|
|
int family;
|
|
const char *addr;
|
|
bool is_multicast;
|
|
} tests[] = {
|
|
{ AF_INET, "1.2.3.4", false }, { AF_INET, "4.3.2.1", false },
|
|
{ AF_INET, "224.1.1.1", true }, { AF_INET, "1.1.1.244", false },
|
|
{ AF_INET6, "::1", false }, { AF_INET6, "ff02::1", true }
|
|
};
|
|
|
|
UNUSED(state);
|
|
|
|
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
|
|
isc_netaddr_t na;
|
|
struct in_addr in;
|
|
struct in6_addr in6;
|
|
int r;
|
|
|
|
if (tests[i].family == AF_INET) {
|
|
r = inet_pton(AF_INET, tests[i].addr,
|
|
(unsigned char *)&in);
|
|
assert_int_equal(r, 1);
|
|
isc_netaddr_fromin(&na, &in);
|
|
} else {
|
|
r = inet_pton(AF_INET6, tests[i].addr,
|
|
(unsigned char *)&in6);
|
|
assert_int_equal(r, 1);
|
|
isc_netaddr_fromin6(&na, &in6);
|
|
}
|
|
|
|
assert_int_equal(isc_netaddr_ismulticast(&na),
|
|
tests[i].is_multicast);
|
|
}
|
|
}
|
|
|
|
ISC_TEST_LIST_START
|
|
|
|
ISC_TEST_ENTRY(netaddr_isnetzero)
|
|
ISC_TEST_ENTRY(netaddr_masktoprefixlen)
|
|
ISC_TEST_ENTRY(netaddr_multicast)
|
|
|
|
ISC_TEST_LIST_END
|
|
|
|
ISC_TEST_MAIN
|