2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 01:59:26 +00:00
bind/tests/dns/rbtdb_test.c
Ondřej Surý 2c3b2dabe9 Move all the unit tests to /tests/<libname>/
The unit tests are now using a common base, which means that
lib/dns/tests/ code now has to include lib/isc/include/isc/test.h and
link with lib/isc/test.c and lib/ns/tests has to include both libisc and
libdns parts.

Instead of cross-linking code between the directories, move the
/lib/<foo>/test.c to /tests/<foo>.c and /lib/<foo>/include/<foo>test.h
to /tests/include/tests/<foo>.h and create a single libtest.la
convenience library in /tests/.

At the same time, move the /lib/<foo>/tests/ to /tests/<foo>/ (but keep
it symlinked to the old location) and adjust paths accordingly.  In few
places, we are now using absolute paths instead of relative paths,
because the directory level has changed.  By moving the directories
under the /tests/ directory, the test-related code is kept in a single
place and we can avoid referencing files between libns->libdns->libisc
which is unhealthy because they live in a separate Makefile-space.

In the future, the /bin/tests/ should be merged to /tests/ and symlink
kept, and the /fuzz/ directory moved to /tests/fuzz/.
2022-05-28 14:53:02 -07:00

212 lines
5.2 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 <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/util.h>
#include <dns/rbt.h>
#include <dns/rdataset.h>
#include <dns/rdatastruct.h>
#define KEEP_BEFORE
/* Include the main file */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wshadow"
#undef CHECK
#include "rbtdb.c"
#pragma GCC diagnostic pop
#undef CHECK
#include <tests/dns.h>
const char *ownercase_vectors[12][2] = {
{
"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz",
"aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz",
},
{
"aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz",
"AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ",
},
{
"AABBCCDDEEFFGGHHIIJJKKLLMMNNOOPPQQRRSSTTUUVVWWXXYYZZ",
"aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz",
},
{
"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ",
"aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz",
},
{
"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVxXyYzZ",
"aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvxxyyzz",
},
{
"WwW.ExAmPlE.OrG",
"wWw.eXaMpLe.oRg",
},
{
"_SIP.tcp.example.org",
"_sip.TCP.example.org",
},
{
"bind-USERS.lists.example.org",
"bind-users.lists.example.org",
},
{
"a0123456789.example.org",
"A0123456789.example.org",
},
{
"\\000.example.org",
"\\000.example.org",
},
{
"wWw.\\000.isc.org",
"www.\\000.isc.org",
},
{
"\255.example.org",
"\255.example.ORG",
}
};
static bool
ownercase_test_one(const char *str1, const char *str2) {
isc_result_t result;
rbtdb_nodelock_t node_locks[1];
dns_rbtdb_t rbtdb = { .node_locks = node_locks };
dns_rbtnode_t rbtnode = { .locknum = 0 };
rdatasetheader_t header = { 0 };
unsigned char *raw = (unsigned char *)(&header) + sizeof(header);
dns_rdataset_t rdataset = {
.magic = DNS_RDATASET_MAGIC,
.private1 = &rbtdb,
.private2 = &rbtnode,
.private3 = raw,
.methods = &rdataset_methods,
};
isc_buffer_t b;
dns_fixedname_t fname1, fname2;
dns_name_t *name1, *name2;
memset(node_locks, 0, sizeof(node_locks));
/* Minimal initialization of the mock objects */
NODE_INITLOCK(&rbtdb.node_locks[0].lock);
name1 = dns_fixedname_initname(&fname1);
isc_buffer_constinit(&b, str1, strlen(str1));
isc_buffer_add(&b, strlen(str1));
result = dns_name_fromtext(name1, &b, dns_rootname, 0, NULL);
assert_int_equal(result, ISC_R_SUCCESS);
name2 = dns_fixedname_initname(&fname2);
isc_buffer_constinit(&b, str2, strlen(str2));
isc_buffer_add(&b, strlen(str2));
result = dns_name_fromtext(name2, &b, dns_rootname, 0, NULL);
assert_int_equal(result, ISC_R_SUCCESS);
/* Store the case from name1 */
dns_rdataset_setownercase(&rdataset, name1);
assert_true(CASESET(&header));
/* Retrieve the case to name2 */
dns_rdataset_getownercase(&rdataset, name2);
NODE_DESTROYLOCK(&rbtdb.node_locks[0].lock);
return (dns_name_caseequal(name1, name2));
}
ISC_RUN_TEST_IMPL(ownercase) {
UNUSED(state);
for (size_t n = 0; n < ARRAY_SIZE(ownercase_vectors); n++) {
assert_true(ownercase_test_one(ownercase_vectors[n][0],
ownercase_vectors[n][1]));
}
assert_false(ownercase_test_one("W.example.org", "\\000.example.org"));
/* Ö and ö in ISO Latin 1 */
assert_false(ownercase_test_one("\\216", "\\246"));
}
ISC_RUN_TEST_IMPL(setownercase) {
isc_result_t result;
rbtdb_nodelock_t node_locks[1];
dns_rbtdb_t rbtdb = { .node_locks = node_locks };
dns_rbtnode_t rbtnode = { .locknum = 0 };
rdatasetheader_t header = { 0 };
unsigned char *raw = (unsigned char *)(&header) + sizeof(header);
dns_rdataset_t rdataset = {
.magic = DNS_RDATASET_MAGIC,
.private1 = &rbtdb,
.private2 = &rbtnode,
.private3 = raw,
.methods = &rdataset_methods,
};
const char *str1 =
"AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
isc_buffer_t b;
dns_fixedname_t fname1, fname2;
dns_name_t *name1, *name2;
UNUSED(state);
/* Minimal initialization of the mock objects */
memset(node_locks, 0, sizeof(node_locks));
NODE_INITLOCK(&rbtdb.node_locks[0].lock);
name1 = dns_fixedname_initname(&fname1);
isc_buffer_constinit(&b, str1, strlen(str1));
isc_buffer_add(&b, strlen(str1));
result = dns_name_fromtext(name1, &b, dns_rootname, 0, NULL);
assert_int_equal(result, ISC_R_SUCCESS);
name2 = dns_fixedname_initname(&fname2);
isc_buffer_constinit(&b, str1, strlen(str1));
isc_buffer_add(&b, strlen(str1));
result = dns_name_fromtext(name2, &b, dns_rootname, 0, NULL);
assert_int_equal(result, ISC_R_SUCCESS);
assert_false(CASESET(&header));
/* Retrieve the case to name2 */
dns_rdataset_getownercase(&rdataset, name2);
NODE_DESTROYLOCK(&rbtdb.node_locks[0].lock);
assert_true(dns_name_caseequal(name1, name2));
}
ISC_TEST_LIST_START
ISC_TEST_ENTRY(ownercase)
ISC_TEST_ENTRY(setownercase)
ISC_TEST_LIST_END
ISC_TEST_MAIN