Debian Codegraph @

tests/dns/nsec3_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <inttypes.h>
15 #include <sched.h> /* IWYU pragma: keep */
16 #include <setjmp.h>
17 #include <stdarg.h>
18 #include <stddef.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #define UNIT_TESTING
24 #include <cmocka.h>
25
26 #include <isc/string.h>
27 #include <isc/util.h>
28
29 #include <dns/db.h>
30 #include <dns/nsec3.h>
31
32 #include <tests/dns.h>
33
34 static void
35 iteration_test(const char *file, unsigned int expected) {
36 isc_result_t result;
37 dns_db_t *db = NULL;
38 unsigned int iterations;
39
40 result = dns_test_loaddb(&db, dns_dbtype_zone, "test", file);
41 assert_int_equal(result, ISC_R_SUCCESS);
42
43 iterations = dns_nsec3_maxiterations();
44
45 assert_int_equal(iterations, expected);
46
47 dns_db_detach(&db);
48 }
49
50 /*%
51 * Structure containing parameters for nsec3param_salttotext_test().
52 */
53 typedef struct {
54 const char *nsec3param_text; /* NSEC3PARAM RDATA in text form */
55 const char *expected_salt; /* string expected in target buffer */
56 } nsec3param_salttotext_test_params_t;
57
58 /*%
59 * Check whether dns_nsec3param_salttotext() handles supplied text form
60 * NSEC3PARAM RDATA correctly: test whether the result of calling the former is
61 * as expected and whether it properly checks available buffer space.
62 *
63 * Assumes supplied text form NSEC3PARAM RDATA is valid as testing handling of
64 * invalid NSEC3PARAM RDATA is out of scope of this unit test.
65 */
66 static void
67 nsec3param_salttotext_test(const nsec3param_salttotext_test_params_t *params) {
68 dns_rdata_t rdata = DNS_RDATA_INIT;
69 dns_rdata_nsec3param_t nsec3param;
70 unsigned char buf[1024];
71 isc_result_t result;
72 char salt[64];
73 size_t length;
74
75 /*
76 * Prepare a dns_rdata_nsec3param_t structure for testing.
77 */
78 result = dns_test_rdatafromstring(
79 &rdata, dns_rdataclass_in, dns_rdatatype_nsec3param, buf,
80 sizeof(buf), params->nsec3param_text, false);
81 assert_int_equal(result, ISC_R_SUCCESS);
82 result = dns_rdata_tostruct(&rdata, &nsec3param, NULL);
83 assert_int_equal(result, ISC_R_SUCCESS);
84
85 /*
86 * Check typical use.
87 */
88 result = dns_nsec3param_salttotext(&nsec3param, salt, sizeof(salt));
89 assert_int_equal(result, ISC_R_SUCCESS);
90 assert_string_equal(salt, params->expected_salt);
91
92 /*
93 * Ensure available space in the buffer is checked before the salt is
94 * printed to it and that the amount of space checked for includes the
95 * terminating NULL byte.
96 */
97 length = strlen(params->expected_salt);
98 assert_true(length < sizeof(salt) - 1); /* prevent buffer overwrite */
99 assert_true(length > 0U); /* prevent length underflow */
100
101 result = dns_nsec3param_salttotext(&nsec3param, salt, length - 1);
102 assert_int_equal(result, ISC_R_NOSPACE);
103
104 result = dns_nsec3param_salttotext(&nsec3param, salt, length);
105 assert_int_equal(result, ISC_R_NOSPACE);
106
107 result = dns_nsec3param_salttotext(&nsec3param, salt, length + 1);
108 assert_int_equal(result, ISC_R_SUCCESS);
109 }
110
111 /*
112 * check that appropriate max iterations is returned for different
113 * key size mixes
114 */
115 ISC_RUN_TEST_IMPL(max_iterations) {
116 UNUSED(state);
117
118 iteration_test(TESTS_DIR "/testdata/nsec3/1024.db", 50);
119 iteration_test(TESTS_DIR "/testdata/nsec3/2048.db", 50);
120 iteration_test(TESTS_DIR "/testdata/nsec3/4096.db", 50);
121 iteration_test(TESTS_DIR "/testdata/nsec3/min-1024.db", 50);
122 iteration_test(TESTS_DIR "/testdata/nsec3/min-2048.db", 50);
123 }
124
125 /* check dns_nsec3param_salttotext() */
126 ISC_RUN_TEST_IMPL(nsec3param_salttotext) {
127 size_t i;
128
129 const nsec3param_salttotext_test_params_t tests[] = {
130 /*
131 * Tests with non-empty salts.
132 */
133 { "0 0 10 0123456789abcdef", "0123456789ABCDEF" },
134 { "0 1 11 0123456789abcdef", "0123456789ABCDEF" },
135 { "1 0 12 42", "42" },
136 { "1 1 13 42", "42" },
137 /*
138 * Test with empty salt.
139 */
140 { "0 0 0 -", "-" },
141 };
142
143 UNUSED(state);
144
145 for (i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
146 nsec3param_salttotext_test(&tests[i]);
147 }
148 }
149
150 ISC_TEST_LIST_START
151 ISC_TEST_ENTRY(max_iterations)
152 ISC_TEST_ENTRY(nsec3param_salttotext)
153 ISC_TEST_LIST_END
154
155 ISC_TEST_MAIN