xref: /minix3/external/bsd/bind/dist/lib/dns/tests/private_test.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: private_test.c,v 1.1.1.4 2014/12/10 03:34:43 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2011, 2012  Internet Systems Consortium, Inc. ("ISC")
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /* Id */
20 
21 /*! \file */
22 
23 #include <config.h>
24 
25 #include <atf-c.h>
26 
27 #include <unistd.h>
28 
29 #include <isc/buffer.h>
30 
31 #include <dns/nsec3.h>
32 #include <dns/private.h>
33 #include <dns/rdataclass.h>
34 #include <dns/rdatatype.h>
35 
36 #include <dst/dst.h>
37 
38 #include "dnstest.h"
39 
40 static dns_rdatatype_t privatetype = 65534;
41 
42 typedef struct {
43 	unsigned char alg;
44 	dns_keytag_t keyid;
45 	isc_boolean_t remove;
46 	isc_boolean_t complete;
47 } signing_testcase_t;
48 
49 typedef struct {
50 	unsigned char hash;
51 	unsigned char flags;
52 	unsigned int iterations;
53 	unsigned long salt;
54 	isc_boolean_t remove;
55 	isc_boolean_t pending;
56 	isc_boolean_t nonsec;
57 } nsec3_testcase_t;
58 
59 /*
60  * Helper functions
61  */
62 static void
make_signing(signing_testcase_t * testcase,dns_rdata_t * private,unsigned char * buf,size_t len)63 make_signing(signing_testcase_t *testcase, dns_rdata_t *private,
64 	     unsigned char *buf, size_t len)
65 {
66 	dns_rdata_init(private);
67 
68 	buf[0] = testcase->alg;
69 	buf[1] = (testcase->keyid & 0xff00) >> 8;
70 	buf[2] = (testcase->keyid & 0xff);
71 	buf[3] = testcase->remove;
72 	buf[4] = testcase->complete;
73 	private->data = buf;
74 	private->length = len;
75 	private->type = privatetype;
76 	private->rdclass = dns_rdataclass_in;
77 }
78 
79 static void
make_nsec3(nsec3_testcase_t * testcase,dns_rdata_t * private,unsigned char * pbuf)80 make_nsec3(nsec3_testcase_t *testcase, dns_rdata_t *private,
81 	   unsigned char *pbuf)
82 {
83 	dns_rdata_nsec3param_t params;
84 	dns_rdata_t nsec3param = DNS_RDATA_INIT;
85 	unsigned char bufdata[BUFSIZ];
86 	isc_buffer_t buf;
87 	isc_uint32_t salt;
88 	unsigned char *sp;
89 	int slen = 4;
90 
91 	/* for simplicity, we're using a maximum salt length of 4 */
92 	salt = htonl(testcase->salt);
93 	sp = (unsigned char *) &salt;
94 	while (*sp == '\0' && slen > 0) {
95 		slen--;
96 		sp++;
97 	}
98 
99 	params.common.rdclass = dns_rdataclass_in;
100 	params.common.rdtype = dns_rdatatype_nsec3param;
101 	params.hash = testcase->hash;
102 	params.iterations = testcase->iterations;
103 	params.salt = sp;
104 	params.salt_length = slen;
105 
106 	params.flags = testcase->flags;
107 	if (testcase->remove) {
108 		params.flags |= DNS_NSEC3FLAG_REMOVE;
109 		if (testcase->nonsec)
110 			params.flags |= DNS_NSEC3FLAG_NONSEC;
111 	} else {
112 		params.flags |= DNS_NSEC3FLAG_CREATE;
113 		if (testcase->pending)
114 			params.flags |= DNS_NSEC3FLAG_INITIAL;
115 	}
116 
117 	isc_buffer_init(&buf, bufdata, sizeof(bufdata));
118 	dns_rdata_fromstruct(&nsec3param, dns_rdataclass_in,
119 			     dns_rdatatype_nsec3param, &params, &buf);
120 
121 	dns_rdata_init(private);
122 
123 	dns_nsec3param_toprivate(&nsec3param, private, privatetype,
124 				 pbuf, DNS_NSEC3PARAM_BUFFERSIZE + 1);
125 }
126 
127 /*
128  * Individual unit tests
129  */
130 ATF_TC(private_signing_totext);
ATF_TC_HEAD(private_signing_totext,tc)131 ATF_TC_HEAD(private_signing_totext, tc) {
132 	atf_tc_set_md_var(tc, "descr",
133 			  "convert private signing records to text");
134 }
ATF_TC_BODY(private_signing_totext,tc)135 ATF_TC_BODY(private_signing_totext, tc) {
136 	isc_result_t result;
137 	dns_rdata_t private;
138 	int i;
139 
140 	signing_testcase_t testcases[] = {
141 		{ DST_ALG_RSASHA512, 12345, 0, 0 },
142 		{ DST_ALG_RSASHA256, 54321, 1, 0 },
143 		{ DST_ALG_NSEC3RSASHA1, 22222, 0, 1 },
144 		{ DST_ALG_RSASHA1, 33333, 1, 1 }
145 	};
146 	const char *results[] = {
147 		"Signing with key 12345/RSASHA512",
148 		"Removing signatures for key 54321/RSASHA256",
149 		"Done signing with key 22222/NSEC3RSASHA1",
150 		"Done removing signatures for key 33333/RSASHA1"
151 	};
152 	int ncases = 4;
153 
154 	UNUSED(tc);
155 
156 	result = dns_test_begin(NULL, ISC_TRUE);
157 	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
158 
159 	for (i = 0; i < ncases; i++) {
160 		unsigned char data[5];
161 		char output[BUFSIZ];
162 		isc_buffer_t buf;
163 
164 		isc_buffer_init(&buf, output, sizeof(output));
165 
166 		make_signing(&testcases[i], &private, data, sizeof(data));
167 		dns_private_totext(&private, &buf);
168 		ATF_CHECK_STREQ(output, results[i]);
169 	}
170 
171 	dns_test_end();
172 }
173 
174 ATF_TC(private_nsec3_totext);
ATF_TC_HEAD(private_nsec3_totext,tc)175 ATF_TC_HEAD(private_nsec3_totext, tc) {
176 	atf_tc_set_md_var(tc, "descr", "convert private chain records to text");
177 }
ATF_TC_BODY(private_nsec3_totext,tc)178 ATF_TC_BODY(private_nsec3_totext, tc) {
179 	isc_result_t result;
180 	dns_rdata_t private;
181 	int i;
182 
183 	nsec3_testcase_t testcases[] = {
184 		{ 1, 0, 1, 0xbeef, 0, 0, 0 },
185 		{ 1, 1, 10, 0xdadd, 0, 0, 0 },
186 		{ 1, 0, 20, 0xbead, 0, 1, 0 },
187 		{ 1, 0, 30, 0xdeaf, 1, 0, 0 },
188 		{ 1, 0, 100, 0xfeedabee, 1, 0, 1 },
189 	};
190 	const char *results[] = {
191 		"Creating NSEC3 chain 1 0 1 BEEF",
192 		"Creating NSEC3 chain 1 1 10 DADD",
193 		"Pending NSEC3 chain 1 0 20 BEAD",
194 		"Removing NSEC3 chain 1 0 30 DEAF / creating NSEC chain",
195 		"Removing NSEC3 chain 1 0 100 FEEDABEE"
196 	};
197 	int ncases = 5;
198 
199 	UNUSED(tc);
200 
201 	result = dns_test_begin(NULL, ISC_TRUE);
202 	ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
203 
204 	for (i = 0; i < ncases; i++) {
205 		unsigned char data[DNS_NSEC3PARAM_BUFFERSIZE + 1];
206 		char output[BUFSIZ];
207 		isc_buffer_t buf;
208 
209 		isc_buffer_init(&buf, output, sizeof(output));
210 
211 		make_nsec3(&testcases[i], &private, data);
212 		dns_private_totext(&private, &buf);
213 		ATF_CHECK_STREQ(output, results[i]);
214 	}
215 
216 	dns_test_end();
217 }
218 
219 /*
220  * Main
221  */
ATF_TP_ADD_TCS(tp)222 ATF_TP_ADD_TCS(tp) {
223 	ATF_TP_ADD_TC(tp, private_signing_totext);
224 	ATF_TP_ADD_TC(tp, private_nsec3_totext);
225 	return (atf_no_error());
226 }
227 
228