Annotation of src/external/mpl/bind/dist/lib/dns/tests/private_test.c, Revision 1.3.4.1
1.3.4.1 ! martin 1: /* $NetBSD: private_test.c,v 1.4 2019/09/05 19:32:58 christos Exp $ */
1.1 christos 2:
3: /*
4: * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
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 http://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:
15: #include <config.h>
16:
1.3 christos 17: #if HAVE_CMOCKA
1.1 christos 18:
1.3 christos 19: #include <stdarg.h>
20: #include <stddef.h>
21: #include <setjmp.h>
22:
23: #include <inttypes.h>
1.3.4.1 ! martin 24: #include <sched.h> /* IWYU pragma: keep */
1.3 christos 25: #include <stdbool.h>
26: #include <stdlib.h>
1.1 christos 27: #include <unistd.h>
28:
1.3 christos 29: #define UNIT_TESTING
30: #include <cmocka.h>
31:
32: #include <isc/util.h>
1.1 christos 33: #include <isc/buffer.h>
34:
35: #include <dns/nsec3.h>
36: #include <dns/private.h>
37: #include <dns/rdataclass.h>
38: #include <dns/rdatatype.h>
39:
40: #include <dst/dst.h>
41:
42: #include "dnstest.h"
43:
44: static dns_rdatatype_t privatetype = 65534;
45:
1.3 christos 46: static int
47: _setup(void **state) {
48: isc_result_t result;
49:
50: UNUSED(state);
51:
52: result = dns_test_begin(NULL, false);
53: assert_int_equal(result, ISC_R_SUCCESS);
54:
55: return (0);
56: }
57:
58: static int
59: _teardown(void **state) {
60: UNUSED(state);
61:
62: dns_test_end();
63:
64: return (0);
65: }
66:
1.1 christos 67: typedef struct {
68: unsigned char alg;
69: dns_keytag_t keyid;
1.3 christos 70: bool remove;
71: bool complete;
1.1 christos 72: } signing_testcase_t;
73:
74: typedef struct {
75: unsigned char hash;
76: unsigned char flags;
77: unsigned int iterations;
78: unsigned long salt;
1.3 christos 79: bool remove;
80: bool pending;
81: bool nonsec;
1.1 christos 82: } nsec3_testcase_t;
83:
84: static void
85: make_signing(signing_testcase_t *testcase, dns_rdata_t *private,
86: unsigned char *buf, size_t len)
87: {
88: dns_rdata_init(private);
89:
90: buf[0] = testcase->alg;
91: buf[1] = (testcase->keyid & 0xff00) >> 8;
92: buf[2] = (testcase->keyid & 0xff);
93: buf[3] = testcase->remove;
94: buf[4] = testcase->complete;
95: private->data = buf;
96: private->length = len;
97: private->type = privatetype;
98: private->rdclass = dns_rdataclass_in;
99: }
100:
101: static void
102: make_nsec3(nsec3_testcase_t *testcase, dns_rdata_t *private,
103: unsigned char *pbuf)
104: {
105: dns_rdata_nsec3param_t params;
106: dns_rdata_t nsec3param = DNS_RDATA_INIT;
107: unsigned char bufdata[BUFSIZ];
108: isc_buffer_t buf;
1.3 christos 109: uint32_t salt;
1.1 christos 110: unsigned char *sp;
111: int slen = 4;
112:
113: /* for simplicity, we're using a maximum salt length of 4 */
114: salt = htonl(testcase->salt);
115: sp = (unsigned char *) &salt;
1.3.4.1 ! martin 116: while (slen > 0 && *sp == '\0') {
1.1 christos 117: slen--;
118: sp++;
119: }
120:
121: params.common.rdclass = dns_rdataclass_in;
122: params.common.rdtype = dns_rdatatype_nsec3param;
123: params.hash = testcase->hash;
124: params.iterations = testcase->iterations;
125: params.salt = sp;
126: params.salt_length = slen;
127:
128: params.flags = testcase->flags;
129: if (testcase->remove) {
130: params.flags |= DNS_NSEC3FLAG_REMOVE;
1.3 christos 131: if (testcase->nonsec) {
1.1 christos 132: params.flags |= DNS_NSEC3FLAG_NONSEC;
1.3 christos 133: }
1.1 christos 134: } else {
135: params.flags |= DNS_NSEC3FLAG_CREATE;
1.3 christos 136: if (testcase->pending) {
1.1 christos 137: params.flags |= DNS_NSEC3FLAG_INITIAL;
1.3 christos 138: }
1.1 christos 139: }
140:
141: isc_buffer_init(&buf, bufdata, sizeof(bufdata));
142: dns_rdata_fromstruct(&nsec3param, dns_rdataclass_in,
143: dns_rdatatype_nsec3param, ¶ms, &buf);
144:
145: dns_rdata_init(private);
146:
147: dns_nsec3param_toprivate(&nsec3param, private, privatetype,
148: pbuf, DNS_NSEC3PARAM_BUFFERSIZE + 1);
149: }
150:
1.3 christos 151: /* convert private signing records to text */
152: static void
153: private_signing_totext_test(void **state) {
1.1 christos 154: dns_rdata_t private;
155: int i;
156:
157: signing_testcase_t testcases[] = {
158: { DST_ALG_RSASHA512, 12345, 0, 0 },
159: { DST_ALG_RSASHA256, 54321, 1, 0 },
160: { DST_ALG_NSEC3RSASHA1, 22222, 0, 1 },
161: { DST_ALG_RSASHA1, 33333, 1, 1 }
162: };
163: const char *results[] = {
164: "Signing with key 12345/RSASHA512",
165: "Removing signatures for key 54321/RSASHA256",
166: "Done signing with key 22222/NSEC3RSASHA1",
167: "Done removing signatures for key 33333/RSASHA1"
168: };
169: int ncases = 4;
170:
1.3 christos 171: UNUSED(state);
1.1 christos 172:
173: for (i = 0; i < ncases; i++) {
174: unsigned char data[5];
175: char output[BUFSIZ];
176: isc_buffer_t buf;
177:
178: isc_buffer_init(&buf, output, sizeof(output));
179:
180: make_signing(&testcases[i], &private, data, sizeof(data));
181: dns_private_totext(&private, &buf);
1.3 christos 182: assert_string_equal(output, results[i]);
1.1 christos 183: }
184:
185: }
186:
1.3 christos 187: /* convert private chain records to text */
188: static void
189: private_nsec3_totext_test(void **state) {
1.1 christos 190: dns_rdata_t private;
191: int i;
192:
193: nsec3_testcase_t testcases[] = {
194: { 1, 0, 1, 0xbeef, 0, 0, 0 },
195: { 1, 1, 10, 0xdadd, 0, 0, 0 },
196: { 1, 0, 20, 0xbead, 0, 1, 0 },
197: { 1, 0, 30, 0xdeaf, 1, 0, 0 },
198: { 1, 0, 100, 0xfeedabee, 1, 0, 1 },
199: };
200: const char *results[] = {
201: "Creating NSEC3 chain 1 0 1 BEEF",
202: "Creating NSEC3 chain 1 1 10 DADD",
203: "Pending NSEC3 chain 1 0 20 BEAD",
204: "Removing NSEC3 chain 1 0 30 DEAF / creating NSEC chain",
205: "Removing NSEC3 chain 1 0 100 FEEDABEE"
206: };
207: int ncases = 5;
208:
1.3 christos 209: UNUSED(state);
1.1 christos 210:
211: for (i = 0; i < ncases; i++) {
212: unsigned char data[DNS_NSEC3PARAM_BUFFERSIZE + 1];
213: char output[BUFSIZ];
214: isc_buffer_t buf;
215:
216: isc_buffer_init(&buf, output, sizeof(output));
217:
218: make_nsec3(&testcases[i], &private, data);
219: dns_private_totext(&private, &buf);
1.3 christos 220: assert_string_equal(output, results[i]);
1.1 christos 221: }
1.3 christos 222: }
223:
224: int
225: main(void) {
226: const struct CMUnitTest tests[] = {
227: cmocka_unit_test_setup_teardown(private_signing_totext_test,
228: _setup, _teardown),
229: cmocka_unit_test_setup_teardown(private_nsec3_totext_test,
230: _setup, _teardown),
231: };
1.1 christos 232:
1.3 christos 233: return (cmocka_run_group_tests(tests, NULL, NULL));
1.1 christos 234: }
235:
1.3 christos 236: #else /* HAVE_CMOCKA */
237:
238: #include <stdio.h>
239:
240: int
241: main(void) {
242: printf("1..0 # Skipped: cmocka not available\n");
243: return (0);
1.1 christos 244: }
245:
1.3 christos 246: #endif
CVSweb <webmaster@jp.NetBSD.org>