2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-23 10:39:16 +00:00
bind/lib/dns/tests/dst_test.c

309 lines
7.0 KiB
C
Raw Normal View History

/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
*
* 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.
*/
2018-11-08 18:00:15 +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 */
#include <setjmp.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
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 <stdlib.h>
#include <unistd.h>
2018-11-08 18:00:15 +07:00
#define UNIT_TESTING
#include <cmocka.h>
#include <isc/file.h>
#include <isc/hex.h>
#include <isc/stdio.h>
#include <isc/string.h>
#include <isc/util.h>
#include <dst/dst.h>
#include <dst/result.h>
#include "../dst_internal.h"
#include "dnstest.h"
2018-11-08 18:00:15 +07:00
static int
2020-02-13 14:44:37 -08:00
_setup(void **state) {
2018-11-08 18:00:15 +07:00
isc_result_t result;
UNUSED(state);
result = dns_test_begin(NULL, false);
assert_int_equal(result, ISC_R_SUCCESS);
return (0);
}
2018-11-08 18:00:15 +07:00
static int
2020-02-13 14:44:37 -08:00
_teardown(void **state) {
2018-11-08 18:00:15 +07:00
UNUSED(state);
dns_test_end();
return (0);
}
/* Read sig in file at path to buf. Check signature ineffability */
static isc_result_t
2020-02-13 14:44:37 -08:00
sig_fromfile(const char *path, isc_buffer_t *buf) {
isc_result_t result;
size_t rval, len;
FILE *fp = NULL;
unsigned char val;
2020-02-13 14:44:37 -08:00
char *p, *data;
off_t size;
result = isc_stdio_open(path, "rb", &fp);
2018-11-08 18:00:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_file_getsizefd(fileno(fp), &size);
2018-11-08 18:00:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
data = isc_mem_get(dt_mctx, (size + 1));
2018-11-08 18:00:15 +07:00
assert_non_null(data);
len = (size_t)size;
p = data;
while (len != 0U) {
result = isc_stdio_read(p, 1, len, fp, &rval);
2018-11-08 18:00:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
len -= rval;
p += rval;
}
isc_stdio_close(fp);
p = data;
len = size;
while (len > 0U) {
if ((*p == '\r') || (*p == '\n')) {
++p;
--len;
continue;
2018-11-08 18:00:15 +07:00
} else if (len < 2U) {
goto err;
2018-11-08 18:00:15 +07:00
}
if (('0' <= *p) && (*p <= '9')) {
val = *p - '0';
} else if (('A' <= *p) && (*p <= 'F')) {
val = *p - 'A' + 10;
} else {
result = ISC_R_BADHEX;
goto err;
}
++p;
val <<= 4;
--len;
if (('0' <= *p) && (*p <= '9')) {
val |= (*p - '0');
} else if (('A' <= *p) && (*p <= 'F')) {
val |= (*p - 'A' + 10);
} else {
result = ISC_R_BADHEX;
goto err;
}
++p;
--len;
isc_buffer_putuint8(buf, val);
}
result = ISC_R_SUCCESS;
err:
isc_mem_put(dt_mctx, data, size + 1);
return (result);
}
static void
check_sig(const char *datapath, const char *sigpath, const char *keyname,
2020-02-13 14:44:37 -08:00
dns_keytag_t id, dns_secalg_t alg, int type, bool expect) {
isc_result_t result;
size_t rval, len;
FILE *fp;
dst_key_t *key = NULL;
unsigned char sig[512];
unsigned char *p;
unsigned char *data;
off_t size;
isc_buffer_t b;
isc_buffer_t databuf, sigbuf;
isc_region_t datareg, sigreg;
dns_fixedname_t fname;
2020-02-13 14:44:37 -08:00
dns_name_t *name;
dst_context_t *ctx = NULL;
/*
* Read data from file in a form usable by dst_verify.
*/
result = isc_stdio_open(datapath, "rb", &fp);
2018-11-08 18:00:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
result = isc_file_getsizefd(fileno(fp), &size);
2018-11-08 18:00:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
data = isc_mem_get(dt_mctx, (size + 1));
2018-11-08 18:00:15 +07:00
assert_non_null(data);
p = data;
len = (size_t)size;
do {
result = isc_stdio_read(p, 1, len, fp, &rval);
2018-11-08 18:00:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
len -= rval;
p += rval;
} while (len);
isc_stdio_close(fp);
/*
* Read key from file in a form usable by dst_verify.
*/
name = dns_fixedname_initname(&fname);
isc_buffer_constinit(&b, keyname, strlen(keyname));
isc_buffer_add(&b, strlen(keyname));
result = dns_name_fromtext(name, &b, dns_rootname, 0, NULL);
2018-11-08 18:00:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
result = dst_key_fromfile(name, id, alg, type, "testdata/dst", dt_mctx,
&key);
2018-11-08 18:00:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
isc_buffer_init(&databuf, data, (unsigned int)size);
isc_buffer_add(&databuf, (unsigned int)size);
isc_buffer_usedregion(&databuf, &datareg);
memset(sig, 0, sizeof(sig));
isc_buffer_init(&sigbuf, sig, sizeof(sig));
/*
* Read precomputed signature from file in a form usable by dst_verify.
*/
result = sig_fromfile(sigpath, &sigbuf);
2018-11-08 18:00:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
/*
* Verify that the key signed the data.
*/
isc_buffer_remainingregion(&sigbuf, &sigreg);
result = dst_context_create(key, dt_mctx, DNS_LOGCATEGORY_GENERAL,
false, 0, &ctx);
2018-11-08 18:00:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
result = dst_context_adddata(ctx, &datareg);
2018-11-08 18:00:15 +07:00
assert_int_equal(result, ISC_R_SUCCESS);
result = dst_context_verify(ctx, &sigreg);
/*
* Compute the expected signature and emit it
* so the precomputed signature can be updated.
* This should only be done if the covered data
* is updated.
*/
if (expect && result != ISC_R_SUCCESS) {
isc_result_t result2;
dst_context_destroy(&ctx);
result2 = dst_context_create(
key, dt_mctx, DNS_LOGCATEGORY_GENERAL, false, 0, &ctx);
2018-11-08 18:00:15 +07:00
assert_int_equal(result2, ISC_R_SUCCESS);
result2 = dst_context_adddata(ctx, &datareg);
2018-11-08 18:00:15 +07:00
assert_int_equal(result2, ISC_R_SUCCESS);
2020-02-13 14:44:37 -08:00
char sigbuf2[4096];
isc_buffer_t sigb;
isc_buffer_init(&sigb, sigbuf2, sizeof(sigbuf2));
result2 = dst_context_sign(ctx, &sigb);
2018-11-08 18:00:15 +07:00
assert_int_equal(result2, ISC_R_SUCCESS);
isc_region_t r;
isc_buffer_usedregion(&sigb, &r);
2020-02-13 14:44:37 -08:00
char hexbuf[4096] = { 0 };
isc_buffer_t hb;
isc_buffer_init(&hb, hexbuf, sizeof(hexbuf));
isc_hex_totext(&r, 0, "", &hb);
fprintf(stderr, "# %s:\n# %s\n", sigpath, hexbuf);
}
isc_mem_put(dt_mctx, data, size + 1);
dst_context_destroy(&ctx);
dst_key_free(&key);
assert_true((expect && (result == ISC_R_SUCCESS)) ||
(!expect && (result != ISC_R_SUCCESS)));
return;
}
2018-11-08 18:00:15 +07:00
static void
2020-02-13 14:44:37 -08:00
sig_test(void **state) {
2018-11-08 18:00:15 +07:00
UNUSED(state);
struct {
2020-02-13 14:44:37 -08:00
const char *datapath;
const char *sigpath;
const char *keyname;
dns_keytag_t keyid;
dns_secalg_t alg;
2020-02-13 14:44:37 -08:00
bool expect;
} testcases[] = {
{ "testdata/dst/test1.data", "testdata/dst/test1.ecdsa256sig",
"test.", 49130, DST_ALG_ECDSA256, true },
{ "testdata/dst/test1.data", "testdata/dst/test1.rsasha256sig",
"test.", 11349, DST_ALG_RSASHA256, true },
{ /* wrong sig */
"testdata/dst/test1.data", "testdata/dst/test1.ecdsa256sig",
"test.", 11349, DST_ALG_RSASHA256, false },
{ /* wrong data */
"testdata/dst/test2.data", "testdata/dst/test1.ecdsa256sig",
"test.", 49130, DST_ALG_ECDSA256, false },
};
unsigned int i;
for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) {
if (!dst_algorithm_supported(testcases[i].alg)) {
continue;
}
check_sig(testcases[i].datapath, testcases[i].sigpath,
testcases[i].keyname, testcases[i].keyid,
testcases[i].alg, DST_TYPE_PRIVATE | DST_TYPE_PUBLIC,
testcases[i].expect);
}
2018-11-08 18:00:15 +07:00
}
2018-11-08 18:00:15 +07:00
int
2020-02-13 14:44:37 -08:00
main(void) {
2018-11-08 18:00:15 +07:00
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(sig_test, _setup, _teardown),
};
return (cmocka_run_group_tests(tests, NULL, NULL));
}
2018-11-08 18:00:15 +07:00
#else /* HAVE_CMOCKA */
#include <stdio.h>
2018-11-08 18:00:15 +07:00
int
2020-02-13 14:44:37 -08:00
main(void) {
2018-11-08 18:00:15 +07:00
printf("1..0 # Skipped: cmocka not available\n");
return (0);
}
2018-11-08 18:00:15 +07:00
#endif /* if HAVE_CMOCKA */