1 /*
2  * testcode/unitldns.c - unit test for ldns routines.
3  *
4  * Copyright (c) 2014, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36 /**
37  * \file
38  * Calls ldns unit tests. Exits with code 1 on a failure.
39  */
40 
41 #include "config.h"
42 #include "util/log.h"
43 #include "testcode/unitmain.h"
44 #include "sldns/sbuffer.h"
45 #include "sldns/str2wire.h"
46 #include "sldns/wire2str.h"
47 #include "sldns/parseutil.h"
48 
49 /** verbose this unit test */
50 static int vbmp = 0;
51 
52 /** print buffer to hex into string */
53 static void
54 buf_to_hex(uint8_t* b, size_t blen, char* s, size_t slen)
55 {
56 	const char* h = "0123456789ABCDEF";
57 	size_t i;
58 	if(slen < blen*2+2 && vbmp) printf("hexstring buffer too small\n");
59 	unit_assert(slen >= blen*2+2);
60 	for(i=0; i<blen; i++) {
61 		s[i*2] = h[(b[i]&0xf0)>>4];
62 		s[i*2+1] = h[b[i]&0x0f];
63 	}
64 	s[blen*2] = '\n';
65 	s[blen*2+1] = 0;
66 }
67 
68 /** Transform input.
69  * @param txt_in: input text format.
70  * @param wire1: output wireformat in hex (txt_in converted to wire).
71  * @param txt_out: output text format (converted from wire_out).
72  * @param wire2: output wireformat in hex, txt_out converted back to wireformat.
73  * @param bufs: size of the text buffers.
74  */
75 static void
76 rr_transform(char* txt_in, char* wire1, char* txt_out, char* wire2,
77 	size_t bufs)
78 {
79 	uint8_t b[65536];
80 	size_t len;
81 	int err;
82 
83 	len = sizeof(b);
84 	err = sldns_str2wire_rr_buf(txt_in, b, &len, NULL, 3600,
85 		NULL, 0, NULL, 0);
86 	if(err != 0) {
87 		if(vbmp) printf("sldns_str2wire_rr_buf, pos %d: %s\n",
88 			LDNS_WIREPARSE_OFFSET(err),
89 			sldns_get_errorstr_parse(err));
90 	}
91 	unit_assert(err == 0);
92 	buf_to_hex(b, len, wire1, bufs);
93 	if(vbmp) printf("wire1: %s", wire1);
94 
95 	err = sldns_wire2str_rr_buf(b, len, txt_out, bufs);
96 	unit_assert(err < (int)bufs && err > 0);
97 	if(vbmp) printf("txt: %s", txt_out);
98 
99 	len = sizeof(b);
100 	err = sldns_str2wire_rr_buf(txt_out, b, &len, NULL, 3600,
101 		NULL, 0, NULL, 0);
102 	if(err != 0) {
103 		if(vbmp) printf("sldns_str2wire_rr_buf-2, pos %d: %s\n",
104 			LDNS_WIREPARSE_OFFSET(err),
105 			sldns_get_errorstr_parse(err));
106 	}
107 	unit_assert(err == 0);
108 	buf_to_hex(b, len, wire2, bufs);
109 	if(vbmp) printf("wire2: %s", wire2);
110 }
111 
112 /** Check if results are correct */
113 static void
114 rr_checks(char* wire_chk, char* txt_chk, char* txt_out, char* wire_out,
115 	char* back)
116 {
117 #ifdef __APPLE__
118 	/* the wiretostr on ipv6 is weird on apple, we cannot check it.
119 	 * skip AAAA on OSX */
120 	if(strstr(txt_out, "IN	AAAA"))
121 		txt_out = txt_chk; /* skip this test, but test wirefmt */
122 			/* so we know that txt_out back to wire is the same */
123 #endif
124 
125 	if(strcmp(txt_chk, txt_out) != 0 && vbmp)
126 		printf("txt different\n");
127 	if(strcmp(wire_chk, wire_out) != 0 && vbmp)
128 		printf("wire1 different\n");
129 	if(strcmp(wire_chk, back) != 0 && vbmp)
130 		printf("wire2 different\n");
131 
132 	unit_assert(strcmp(txt_chk, txt_out) == 0);
133 	unit_assert(strcmp(wire_chk, wire_out) == 0);
134 	unit_assert(strcmp(wire_chk, back) == 0);
135 }
136 
137 /** read rrs to and from string, and wireformat
138  * Skips empty lines and comments.
139  * @param input: input file with text format.
140  * @param check: check file with hex and then textformat
141  */
142 static void
143 rr_test_file(const char* input, const char* check)
144 {
145 	size_t bufs = 131072;
146 	FILE* inf, *chf, *of;
147 	int lineno = 0, chlineno = 0;
148 	char* txt_in = (char*)malloc(bufs);
149 	char* txt_out = (char*)malloc(bufs);
150 	char* txt_chk = (char*)malloc(bufs);
151 	char* wire_out = (char*)malloc(bufs);
152 	char* wire_chk = (char*)malloc(bufs);
153 	char* back = (char*)malloc(bufs);
154 	if(!txt_in || !txt_out || !txt_chk || !wire_out || !wire_chk || !back)
155 		fatal_exit("malloc failure");
156 	inf = fopen(input, "r");
157 	if(!inf) fatal_exit("cannot open %s: %s", input, strerror(errno));
158 	chf = fopen(check, "r");
159 	if(!chf) fatal_exit("cannot open %s: %s", check, strerror(errno));
160 
161 	of = NULL;
162 	if(0) {
163 		/* debug: create check file */
164 		of = fopen("outputfile", "w");
165 		if(!of) fatal_exit("cannot write output: %s", strerror(errno));
166 	}
167 
168 	while(fgets(txt_in, (int)bufs, inf)) {
169 		lineno++;
170 		if(vbmp) printf("\n%s:%d %s", input, lineno, txt_in);
171 		/* skip empty lines and comments */
172 		if(txt_in[0] == 0 || txt_in[0] == '\n' || txt_in[0] == ';')
173 			continue;
174 		/* read check lines */
175 		if(!fgets(wire_chk, (int)bufs, chf))
176 			printf("%s too short\n", check);
177 		if(!fgets(txt_chk, (int)bufs, chf))
178 			printf("%s too short\n", check);
179 		chlineno += 2;
180 		if(vbmp) printf("%s:%d %s", check, chlineno-1, wire_chk);
181 		if(vbmp) printf("%s:%d %s", check, chlineno, txt_chk);
182 		/* generate results */
183 		rr_transform(txt_in, wire_out, txt_out, back, bufs);
184 		/* checks */
185 		if(of) {
186 			fprintf(of, "%s%s", wire_out, txt_out);
187 		} else {
188 			rr_checks(wire_chk, txt_chk, txt_out, wire_out, back);
189 		}
190 	}
191 
192 	if(of) fclose(of);
193 	fclose(inf);
194 	fclose(chf);
195 	free(txt_in);
196 	free(txt_out);
197 	free(txt_chk);
198 	free(wire_out);
199 	free(wire_chk);
200 	free(back);
201 }
202 
203 #define xstr(s) str(s)
204 #define str(s) #s
205 
206 #define SRCDIRSTR xstr(SRCDIR)
207 
208 /** read rrs to and from string, to and from wireformat */
209 static void
210 rr_tests(void)
211 {
212 	rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.1",
213 		SRCDIRSTR "/testdata/test_ldnsrr.c1");
214 	rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.2",
215 		SRCDIRSTR "/testdata/test_ldnsrr.c2");
216 	rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.3",
217 		SRCDIRSTR "/testdata/test_ldnsrr.c3");
218 	rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.4",
219 		SRCDIRSTR "/testdata/test_ldnsrr.c4");
220 	rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.5",
221 		SRCDIRSTR "/testdata/test_ldnsrr.c5");
222 }
223 
224 /** test various base64 decoding options */
225 static void
226 b64_test(void)
227 {
228 	/* "normal" b64 alphabet, with padding */
229 	char* p1 = "aGVsbG8="; /* "hello" */
230 	char* p2 = "aGVsbG8+"; /* "hello>" */
231 	char* p3 = "aGVsbG8/IQ=="; /* "hello?!" */
232 	char* p4 = "aGVsbG8"; /* "hel" + extra garbage */
233 
234 	/* base64 url, without padding */
235 	char* u1 = "aGVsbG8"; /* "hello" */
236 	char* u2 = "aGVsbG8-"; /* "hello>" */
237 	char* u3 = "aGVsbG8_IQ"; /* "hello?!" */
238 	char* u4 = "aaaaa"; /* garbage */
239 
240 	char target[128];
241 	size_t tarsize = 128;
242 	int result;
243 
244 	memset(target, 0, sizeof(target));
245 	result = sldns_b64_pton(p1, (uint8_t*)target, tarsize);
246 	unit_assert(result == strlen("hello") && strcmp(target, "hello") == 0);
247 	memset(target, 0, sizeof(target));
248 	result = sldns_b64_pton(p2, (uint8_t*)target, tarsize);
249 	unit_assert(result == strlen("hello>") && strcmp(target, "hello>") == 0);
250 	memset(target, 0, sizeof(target));
251 	result = sldns_b64_pton(p3, (uint8_t*)target, tarsize);
252 	unit_assert(result == strlen("hello?!") && strcmp(target, "hello?!") == 0);
253 	memset(target, 0, sizeof(target));
254 	result = sldns_b64_pton(p4, (uint8_t*)target, tarsize);
255 	/* when padding is used everything that is not a block of 4 will be
256 	 * ignored */
257 	unit_assert(result == strlen("hel") && strcmp(target, "hel") == 0);
258 
259 	memset(target, 0, sizeof(target));
260 	result = sldns_b64url_pton(u1, strlen(u1), (uint8_t*)target, tarsize);
261 	unit_assert(result == strlen("hello") && strcmp(target, "hello") == 0);
262 	memset(target, 0, sizeof(target));
263 	result = sldns_b64url_pton(u2, strlen(u2), (uint8_t*)target, tarsize);
264 	unit_assert(result == strlen("hello>") && strcmp(target, "hello>") == 0);
265 	memset(target, 0, sizeof(target));
266 	result = sldns_b64url_pton(u3, strlen(u3), (uint8_t*)target, tarsize);
267 	unit_assert(result == strlen("hello+/") && strcmp(target, "hello?!") == 0);
268 	/* one item in block of four is not allowed */
269 	memset(target, 0, sizeof(target));
270 	result = sldns_b64url_pton(u4, strlen(u4), (uint8_t*)target, tarsize);
271 	unit_assert(result == -1);
272 }
273 
274 void
275 ldns_test(void)
276 {
277 	unit_show_feature("sldns");
278 	rr_tests();
279 	b64_test();
280 }
281