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
buf_to_hex(uint8_t * b,size_t blen,char * s,size_t slen)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
rr_transform(char * txt_in,char * wire1,char * txt_out,char * wire2,size_t bufs)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
rr_checks(char * wire_chk,char * txt_chk,char * txt_out,char * wire_out,char * back)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
rr_test_file(const char * input,const char * check)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 unit_assert(0);
178 }
179 if(!fgets(txt_chk, (int)bufs, chf)) {
180 printf("%s too short\n", check);
181 unit_assert(0);
182 }
183 chlineno += 2;
184 if(vbmp) printf("%s:%d %s", check, chlineno-1, wire_chk);
185 if(vbmp) printf("%s:%d %s", check, chlineno, txt_chk);
186 /* generate results */
187 rr_transform(txt_in, wire_out, txt_out, back, bufs);
188 /* checks */
189 if(of) {
190 fprintf(of, "%s%s", wire_out, txt_out);
191 } else {
192 rr_checks(wire_chk, txt_chk, txt_out, wire_out, back);
193 }
194 }
195
196 if(of) fclose(of);
197 fclose(inf);
198 fclose(chf);
199 free(txt_in);
200 free(txt_out);
201 free(txt_chk);
202 free(wire_out);
203 free(wire_chk);
204 free(back);
205 }
206
207 #define xstr(s) str(s)
208 #define str(s) #s
209
210 #define SRCDIRSTR xstr(SRCDIR)
211
212 /** read rrs to and from string, to and from wireformat */
213 static void
rr_tests(void)214 rr_tests(void)
215 {
216 rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.1",
217 SRCDIRSTR "/testdata/test_ldnsrr.c1");
218 rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.2",
219 SRCDIRSTR "/testdata/test_ldnsrr.c2");
220 rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.3",
221 SRCDIRSTR "/testdata/test_ldnsrr.c3");
222 rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.4",
223 SRCDIRSTR "/testdata/test_ldnsrr.c4");
224 rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.5",
225 SRCDIRSTR "/testdata/test_ldnsrr.c5");
226 }
227
228 /** test various base64 decoding options */
229 static void
b64_test(void)230 b64_test(void)
231 {
232 /* "normal" b64 alphabet, with padding */
233 char* p1 = "aGVsbG8="; /* "hello" */
234 char* p2 = "aGVsbG8+"; /* "hello>" */
235 char* p3 = "aGVsbG8/IQ=="; /* "hello?!" */
236 char* p4 = "aGVsbG8"; /* "hel" + extra garbage */
237
238 /* base64 url, without padding */
239 char* u1 = "aGVsbG8"; /* "hello" */
240 char* u2 = "aGVsbG8-"; /* "hello>" */
241 char* u3 = "aGVsbG8_IQ"; /* "hello?!" */
242 char* u4 = "aaaaa"; /* garbage */
243
244 char target[128];
245 size_t tarsize = 128;
246 int result;
247
248 memset(target, 0, sizeof(target));
249 result = sldns_b64_pton(p1, (uint8_t*)target, tarsize);
250 unit_assert(result == (int)strlen("hello") && strcmp(target, "hello") == 0);
251 memset(target, 0, sizeof(target));
252 result = sldns_b64_pton(p2, (uint8_t*)target, tarsize);
253 unit_assert(result == (int)strlen("hello>") && strcmp(target, "hello>") == 0);
254 memset(target, 0, sizeof(target));
255 result = sldns_b64_pton(p3, (uint8_t*)target, tarsize);
256 unit_assert(result == (int)strlen("hello?!") && strcmp(target, "hello?!") == 0);
257 memset(target, 0, sizeof(target));
258 result = sldns_b64_pton(p4, (uint8_t*)target, tarsize);
259 /* when padding is used everything that is not a block of 4 will be
260 * ignored */
261 unit_assert(result == (int)strlen("hel") && strcmp(target, "hel") == 0);
262
263 memset(target, 0, sizeof(target));
264 result = sldns_b64url_pton(u1, strlen(u1), (uint8_t*)target, tarsize);
265 unit_assert(result == (int)strlen("hello") && strcmp(target, "hello") == 0);
266 memset(target, 0, sizeof(target));
267 result = sldns_b64url_pton(u2, strlen(u2), (uint8_t*)target, tarsize);
268 unit_assert(result == (int)strlen("hello>") && strcmp(target, "hello>") == 0);
269 memset(target, 0, sizeof(target));
270 result = sldns_b64url_pton(u3, strlen(u3), (uint8_t*)target, tarsize);
271 unit_assert(result == (int)strlen("hello+/") && strcmp(target, "hello?!") == 0);
272 /* one item in block of four is not allowed */
273 memset(target, 0, sizeof(target));
274 result = sldns_b64url_pton(u4, strlen(u4), (uint8_t*)target, tarsize);
275 unit_assert(result == -1);
276 }
277
278 void
ldns_test(void)279 ldns_test(void)
280 {
281 unit_show_feature("sldns");
282 rr_tests();
283 b64_test();
284 }
285