2012-01-27 00:49:42 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2012-01-27 00:49:42 +00:00
|
|
|
*
|
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-01-27 00:49:42 +00:00
|
|
|
*/
|
|
|
|
|
2018-10-24 21:09:06 -07:00
|
|
|
#if HAVE_CMOCKA
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <sched.h> /* IWYU pragma: keep */
|
|
|
|
#include <setjmp.h>
|
2018-10-24 21:09:06 -07:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-01-27 00:49:42 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2018-10-24 21:09:06 -07:00
|
|
|
#define UNIT_TESTING
|
|
|
|
#include <cmocka.h>
|
|
|
|
|
|
|
|
#include <isc/string.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <isc/util.h>
|
2018-10-24 21:09:06 -07:00
|
|
|
|
2012-01-27 00:49:42 +00:00
|
|
|
#include <dns/db.h>
|
|
|
|
#include <dns/nsec3.h>
|
|
|
|
|
|
|
|
#include "dnstest.h"
|
|
|
|
|
2018-10-24 21:09:06 -07:00
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_setup(void **state) {
|
2018-10-24 21:09:06 -07:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
result = dns_test_begin(NULL, false);
|
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-02-13 14:44:37 -08:00
|
|
|
_teardown(void **state) {
|
2018-10-24 21:09:06 -07:00
|
|
|
UNUSED(state);
|
|
|
|
|
|
|
|
dns_test_end();
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
2012-01-27 00:49:42 +00:00
|
|
|
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
iteration_test(const char *file, unsigned int expected) {
|
2012-01-27 00:49:42 +00:00
|
|
|
isc_result_t result;
|
2020-02-13 14:44:37 -08:00
|
|
|
dns_db_t *db = NULL;
|
2012-01-27 00:49:42 +00:00
|
|
|
unsigned int iterations;
|
|
|
|
|
|
|
|
result = dns_test_loaddb(&db, dns_dbtype_zone, "test", file);
|
2018-10-24 21:09:06 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2012-01-27 00:49:42 +00:00
|
|
|
|
2021-04-19 16:32:54 +10:00
|
|
|
iterations = dns_nsec3_maxiterations();
|
2012-01-27 00:49:42 +00:00
|
|
|
|
2018-10-24 21:09:06 -07:00
|
|
|
assert_int_equal(iterations, expected);
|
2012-01-27 00:49:42 +00:00
|
|
|
|
|
|
|
dns_db_detach(&db);
|
|
|
|
}
|
|
|
|
|
2017-10-25 09:21:57 +02:00
|
|
|
/*%
|
|
|
|
* Structure containing parameters for nsec3param_salttotext_test().
|
|
|
|
*/
|
|
|
|
typedef struct {
|
2020-02-12 13:59:18 +01:00
|
|
|
const char *nsec3param_text; /* NSEC3PARAM RDATA in text form */
|
|
|
|
const char *expected_salt; /* string expected in target buffer */
|
2017-10-25 09:21:57 +02:00
|
|
|
} nsec3param_salttotext_test_params_t;
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* Check whether dns_nsec3param_salttotext() handles supplied text form
|
|
|
|
* NSEC3PARAM RDATA correctly: test whether the result of calling the former is
|
|
|
|
* as expected and whether it properly checks available buffer space.
|
|
|
|
*
|
|
|
|
* Assumes supplied text form NSEC3PARAM RDATA is valid as testing handling of
|
|
|
|
* invalid NSEC3PARAM RDATA is out of scope of this unit test.
|
|
|
|
*/
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
nsec3param_salttotext_test(const nsec3param_salttotext_test_params_t *params) {
|
|
|
|
dns_rdata_t rdata = DNS_RDATA_INIT;
|
2017-10-25 09:21:57 +02:00
|
|
|
dns_rdata_nsec3param_t nsec3param;
|
2020-02-13 14:44:37 -08:00
|
|
|
unsigned char buf[1024];
|
|
|
|
isc_result_t result;
|
|
|
|
char salt[64];
|
|
|
|
size_t length;
|
2017-10-25 09:21:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare a dns_rdata_nsec3param_t structure for testing.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
result = dns_test_rdatafromstring(
|
|
|
|
&rdata, dns_rdataclass_in, dns_rdatatype_nsec3param, buf,
|
|
|
|
sizeof(buf), params->nsec3param_text, false);
|
2018-10-24 21:09:06 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-10-25 09:21:57 +02:00
|
|
|
result = dns_rdata_tostruct(&rdata, &nsec3param, NULL);
|
2018-10-24 21:09:06 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-10-25 09:21:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check typical use.
|
|
|
|
*/
|
|
|
|
result = dns_nsec3param_salttotext(&nsec3param, salt, sizeof(salt));
|
2018-10-24 21:09:06 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
|
|
|
assert_string_equal(salt, params->expected_salt);
|
2017-10-25 09:21:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure available space in the buffer is checked before the salt is
|
|
|
|
* printed to it and that the amount of space checked for includes the
|
|
|
|
* terminating NULL byte.
|
|
|
|
*/
|
|
|
|
length = strlen(params->expected_salt);
|
2020-02-12 13:59:18 +01:00
|
|
|
assert_true(length < sizeof(salt) - 1); /* prevent buffer overwrite */
|
2018-10-24 21:09:06 -07:00
|
|
|
assert_true(length > 0U); /* prevent length underflow */
|
2017-10-25 09:21:57 +02:00
|
|
|
|
|
|
|
result = dns_nsec3param_salttotext(&nsec3param, salt, length - 1);
|
2018-10-24 21:09:06 -07:00
|
|
|
assert_int_equal(result, ISC_R_NOSPACE);
|
|
|
|
|
2017-10-25 09:21:57 +02:00
|
|
|
result = dns_nsec3param_salttotext(&nsec3param, salt, length);
|
2018-10-24 21:09:06 -07:00
|
|
|
assert_int_equal(result, ISC_R_NOSPACE);
|
|
|
|
|
2017-10-25 09:21:57 +02:00
|
|
|
result = dns_nsec3param_salttotext(&nsec3param, salt, length + 1);
|
2018-10-24 21:09:06 -07:00
|
|
|
assert_int_equal(result, ISC_R_SUCCESS);
|
2017-10-25 09:21:57 +02:00
|
|
|
}
|
|
|
|
|
2012-01-27 00:49:42 +00:00
|
|
|
/*
|
2018-10-24 21:09:06 -07:00
|
|
|
* check that appropriate max iterations is returned for different
|
|
|
|
* key size mixes
|
2012-01-27 00:49:42 +00:00
|
|
|
*/
|
2018-10-24 21:09:06 -07:00
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
max_iterations(void **state) {
|
2018-10-24 21:09:06 -07:00
|
|
|
UNUSED(state);
|
2015-12-07 17:52:37 +11:00
|
|
|
|
2012-01-27 00:49:42 +00:00
|
|
|
iteration_test("testdata/nsec3/1024.db", 150);
|
2021-04-19 16:32:54 +10:00
|
|
|
iteration_test("testdata/nsec3/2048.db", 150);
|
|
|
|
iteration_test("testdata/nsec3/4096.db", 150);
|
2012-01-27 00:49:42 +00:00
|
|
|
iteration_test("testdata/nsec3/min-1024.db", 150);
|
2021-04-19 16:32:54 +10:00
|
|
|
iteration_test("testdata/nsec3/min-2048.db", 150);
|
2012-01-27 00:49:42 +00:00
|
|
|
}
|
2017-10-25 09:21:57 +02:00
|
|
|
|
2018-10-24 21:09:06 -07:00
|
|
|
/* check dns_nsec3param_salttotext() */
|
|
|
|
static void
|
2020-02-13 14:44:37 -08:00
|
|
|
nsec3param_salttotext(void **state) {
|
2017-10-25 09:21:57 +02:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
const nsec3param_salttotext_test_params_t tests[] = {
|
|
|
|
/*
|
|
|
|
* Tests with non-empty salts.
|
|
|
|
*/
|
|
|
|
{ "0 0 10 0123456789abcdef", "0123456789ABCDEF" },
|
|
|
|
{ "0 1 11 0123456789abcdef", "0123456789ABCDEF" },
|
|
|
|
{ "1 0 12 42", "42" },
|
|
|
|
{ "1 1 13 42", "42" },
|
|
|
|
/*
|
|
|
|
* Test with empty salt.
|
|
|
|
*/
|
|
|
|
{ "0 0 0 -", "-" },
|
|
|
|
};
|
|
|
|
|
2018-10-24 21:09:06 -07:00
|
|
|
UNUSED(state);
|
2017-10-25 09:21:57 +02:00
|
|
|
|
|
|
|
for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
|
|
|
|
nsec3param_salttotext_test(&tests[i]);
|
|
|
|
}
|
2018-10-24 21:09:06 -07:00
|
|
|
}
|
2017-10-25 09:21:57 +02:00
|
|
|
|
2018-10-24 21:09:06 -07:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-24 21:09:06 -07:00
|
|
|
const struct CMUnitTest tests[] = {
|
2020-02-12 13:59:18 +01:00
|
|
|
cmocka_unit_test_setup_teardown(max_iterations, _setup,
|
|
|
|
_teardown),
|
|
|
|
cmocka_unit_test_setup_teardown(nsec3param_salttotext, _setup,
|
|
|
|
_teardown),
|
2018-10-24 21:09:06 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
return (cmocka_run_group_tests(tests, NULL, NULL));
|
2017-10-25 09:21:57 +02:00
|
|
|
}
|
2012-01-27 00:49:42 +00:00
|
|
|
|
2018-10-24 21:09:06 -07:00
|
|
|
#else /* HAVE_CMOCKA */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-01-27 00:49:42 +00:00
|
|
|
|
2018-10-24 21:09:06 -07:00
|
|
|
int
|
2020-02-13 14:44:37 -08:00
|
|
|
main(void) {
|
2018-10-24 21:09:06 -07:00
|
|
|
printf("1..0 # Skipped: cmocka not available\n");
|
2021-01-18 19:15:44 +01:00
|
|
|
return (SKIPPED_TEST_EXIT_CODE);
|
2012-01-27 00:49:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-13 21:48:23 +01:00
|
|
|
#endif /* if HAVE_CMOCKA */
|