1712b2f30Ssthen /*
2712b2f30Ssthen * testcode/unitldns.c - unit test for ldns routines.
3712b2f30Ssthen *
4712b2f30Ssthen * Copyright (c) 2014, NLnet Labs. All rights reserved.
5712b2f30Ssthen *
6712b2f30Ssthen * This software is open source.
7712b2f30Ssthen *
8712b2f30Ssthen * Redistribution and use in source and binary forms, with or without
9712b2f30Ssthen * modification, are permitted provided that the following conditions
10712b2f30Ssthen * are met:
11712b2f30Ssthen *
12712b2f30Ssthen * Redistributions of source code must retain the above copyright notice,
13712b2f30Ssthen * this list of conditions and the following disclaimer.
14712b2f30Ssthen *
15712b2f30Ssthen * Redistributions in binary form must reproduce the above copyright notice,
16712b2f30Ssthen * this list of conditions and the following disclaimer in the documentation
17712b2f30Ssthen * and/or other materials provided with the distribution.
18712b2f30Ssthen *
19712b2f30Ssthen * Neither the name of the NLNET LABS nor the names of its contributors may
20712b2f30Ssthen * be used to endorse or promote products derived from this software without
21712b2f30Ssthen * specific prior written permission.
22712b2f30Ssthen *
23712b2f30Ssthen * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24712b2f30Ssthen * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25712b2f30Ssthen * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26712b2f30Ssthen * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27712b2f30Ssthen * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28712b2f30Ssthen * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29712b2f30Ssthen * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30712b2f30Ssthen * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31712b2f30Ssthen * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32712b2f30Ssthen * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33712b2f30Ssthen * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34712b2f30Ssthen *
35712b2f30Ssthen */
36712b2f30Ssthen /**
37712b2f30Ssthen * \file
38712b2f30Ssthen * Calls ldns unit tests. Exits with code 1 on a failure.
39712b2f30Ssthen */
40712b2f30Ssthen
41712b2f30Ssthen #include "config.h"
42712b2f30Ssthen #include "util/log.h"
43712b2f30Ssthen #include "testcode/unitmain.h"
44712b2f30Ssthen #include "sldns/sbuffer.h"
45712b2f30Ssthen #include "sldns/str2wire.h"
46712b2f30Ssthen #include "sldns/wire2str.h"
47e2a0f313Ssthen #include "sldns/parseutil.h"
48712b2f30Ssthen
49712b2f30Ssthen /** verbose this unit test */
50712b2f30Ssthen static int vbmp = 0;
51712b2f30Ssthen
52712b2f30Ssthen /** print buffer to hex into string */
53712b2f30Ssthen static void
buf_to_hex(uint8_t * b,size_t blen,char * s,size_t slen)54712b2f30Ssthen buf_to_hex(uint8_t* b, size_t blen, char* s, size_t slen)
55712b2f30Ssthen {
56712b2f30Ssthen const char* h = "0123456789ABCDEF";
57712b2f30Ssthen size_t i;
58712b2f30Ssthen if(slen < blen*2+2 && vbmp) printf("hexstring buffer too small\n");
59712b2f30Ssthen unit_assert(slen >= blen*2+2);
60712b2f30Ssthen for(i=0; i<blen; i++) {
61712b2f30Ssthen s[i*2] = h[(b[i]&0xf0)>>4];
62712b2f30Ssthen s[i*2+1] = h[b[i]&0x0f];
63712b2f30Ssthen }
64712b2f30Ssthen s[blen*2] = '\n';
65712b2f30Ssthen s[blen*2+1] = 0;
66712b2f30Ssthen }
67712b2f30Ssthen
68712b2f30Ssthen /** Transform input.
69712b2f30Ssthen * @param txt_in: input text format.
70712b2f30Ssthen * @param wire1: output wireformat in hex (txt_in converted to wire).
71712b2f30Ssthen * @param txt_out: output text format (converted from wire_out).
72712b2f30Ssthen * @param wire2: output wireformat in hex, txt_out converted back to wireformat.
73712b2f30Ssthen * @param bufs: size of the text buffers.
74712b2f30Ssthen */
75712b2f30Ssthen static void
rr_transform(char * txt_in,char * wire1,char * txt_out,char * wire2,size_t bufs)76712b2f30Ssthen rr_transform(char* txt_in, char* wire1, char* txt_out, char* wire2,
77712b2f30Ssthen size_t bufs)
78712b2f30Ssthen {
79712b2f30Ssthen uint8_t b[65536];
80712b2f30Ssthen size_t len;
81712b2f30Ssthen int err;
82712b2f30Ssthen
83712b2f30Ssthen len = sizeof(b);
84712b2f30Ssthen err = sldns_str2wire_rr_buf(txt_in, b, &len, NULL, 3600,
85712b2f30Ssthen NULL, 0, NULL, 0);
86712b2f30Ssthen if(err != 0) {
87712b2f30Ssthen if(vbmp) printf("sldns_str2wire_rr_buf, pos %d: %s\n",
88712b2f30Ssthen LDNS_WIREPARSE_OFFSET(err),
89712b2f30Ssthen sldns_get_errorstr_parse(err));
90712b2f30Ssthen }
91712b2f30Ssthen unit_assert(err == 0);
92712b2f30Ssthen buf_to_hex(b, len, wire1, bufs);
93712b2f30Ssthen if(vbmp) printf("wire1: %s", wire1);
94712b2f30Ssthen
95712b2f30Ssthen err = sldns_wire2str_rr_buf(b, len, txt_out, bufs);
96712b2f30Ssthen unit_assert(err < (int)bufs && err > 0);
97712b2f30Ssthen if(vbmp) printf("txt: %s", txt_out);
98712b2f30Ssthen
99712b2f30Ssthen len = sizeof(b);
100712b2f30Ssthen err = sldns_str2wire_rr_buf(txt_out, b, &len, NULL, 3600,
101712b2f30Ssthen NULL, 0, NULL, 0);
102712b2f30Ssthen if(err != 0) {
103712b2f30Ssthen if(vbmp) printf("sldns_str2wire_rr_buf-2, pos %d: %s\n",
104712b2f30Ssthen LDNS_WIREPARSE_OFFSET(err),
105712b2f30Ssthen sldns_get_errorstr_parse(err));
106712b2f30Ssthen }
107712b2f30Ssthen unit_assert(err == 0);
108712b2f30Ssthen buf_to_hex(b, len, wire2, bufs);
109712b2f30Ssthen if(vbmp) printf("wire2: %s", wire2);
110712b2f30Ssthen }
111712b2f30Ssthen
112712b2f30Ssthen /** Check if results are correct */
113712b2f30Ssthen static void
rr_checks(char * wire_chk,char * txt_chk,char * txt_out,char * wire_out,char * back)114712b2f30Ssthen rr_checks(char* wire_chk, char* txt_chk, char* txt_out, char* wire_out,
115712b2f30Ssthen char* back)
116712b2f30Ssthen {
117712b2f30Ssthen #ifdef __APPLE__
118712b2f30Ssthen /* the wiretostr on ipv6 is weird on apple, we cannot check it.
119712b2f30Ssthen * skip AAAA on OSX */
120712b2f30Ssthen if(strstr(txt_out, "IN AAAA"))
121712b2f30Ssthen txt_out = txt_chk; /* skip this test, but test wirefmt */
122712b2f30Ssthen /* so we know that txt_out back to wire is the same */
123712b2f30Ssthen #endif
124712b2f30Ssthen
125712b2f30Ssthen if(strcmp(txt_chk, txt_out) != 0 && vbmp)
126712b2f30Ssthen printf("txt different\n");
127712b2f30Ssthen if(strcmp(wire_chk, wire_out) != 0 && vbmp)
128712b2f30Ssthen printf("wire1 different\n");
129712b2f30Ssthen if(strcmp(wire_chk, back) != 0 && vbmp)
130712b2f30Ssthen printf("wire2 different\n");
131712b2f30Ssthen
132712b2f30Ssthen unit_assert(strcmp(txt_chk, txt_out) == 0);
133712b2f30Ssthen unit_assert(strcmp(wire_chk, wire_out) == 0);
134712b2f30Ssthen unit_assert(strcmp(wire_chk, back) == 0);
135712b2f30Ssthen }
136712b2f30Ssthen
137712b2f30Ssthen /** read rrs to and from string, and wireformat
138712b2f30Ssthen * Skips empty lines and comments.
139712b2f30Ssthen * @param input: input file with text format.
140712b2f30Ssthen * @param check: check file with hex and then textformat
141712b2f30Ssthen */
142712b2f30Ssthen static void
rr_test_file(const char * input,const char * check)143712b2f30Ssthen rr_test_file(const char* input, const char* check)
144712b2f30Ssthen {
145712b2f30Ssthen size_t bufs = 131072;
146712b2f30Ssthen FILE* inf, *chf, *of;
147712b2f30Ssthen int lineno = 0, chlineno = 0;
148712b2f30Ssthen char* txt_in = (char*)malloc(bufs);
149712b2f30Ssthen char* txt_out = (char*)malloc(bufs);
150712b2f30Ssthen char* txt_chk = (char*)malloc(bufs);
151712b2f30Ssthen char* wire_out = (char*)malloc(bufs);
152712b2f30Ssthen char* wire_chk = (char*)malloc(bufs);
153712b2f30Ssthen char* back = (char*)malloc(bufs);
154712b2f30Ssthen if(!txt_in || !txt_out || !txt_chk || !wire_out || !wire_chk || !back)
155712b2f30Ssthen fatal_exit("malloc failure");
156712b2f30Ssthen inf = fopen(input, "r");
157712b2f30Ssthen if(!inf) fatal_exit("cannot open %s: %s", input, strerror(errno));
158712b2f30Ssthen chf = fopen(check, "r");
159712b2f30Ssthen if(!chf) fatal_exit("cannot open %s: %s", check, strerror(errno));
160712b2f30Ssthen
161712b2f30Ssthen of = NULL;
162712b2f30Ssthen if(0) {
163712b2f30Ssthen /* debug: create check file */
164712b2f30Ssthen of = fopen("outputfile", "w");
165712b2f30Ssthen if(!of) fatal_exit("cannot write output: %s", strerror(errno));
166712b2f30Ssthen }
167712b2f30Ssthen
168712b2f30Ssthen while(fgets(txt_in, (int)bufs, inf)) {
169712b2f30Ssthen lineno++;
170712b2f30Ssthen if(vbmp) printf("\n%s:%d %s", input, lineno, txt_in);
171712b2f30Ssthen /* skip empty lines and comments */
172712b2f30Ssthen if(txt_in[0] == 0 || txt_in[0] == '\n' || txt_in[0] == ';')
173712b2f30Ssthen continue;
174712b2f30Ssthen /* read check lines */
175712b2f30Ssthen if(!fgets(wire_chk, (int)bufs, chf))
176712b2f30Ssthen printf("%s too short\n", check);
177712b2f30Ssthen if(!fgets(txt_chk, (int)bufs, chf))
178712b2f30Ssthen printf("%s too short\n", check);
179712b2f30Ssthen chlineno += 2;
180712b2f30Ssthen if(vbmp) printf("%s:%d %s", check, chlineno-1, wire_chk);
181712b2f30Ssthen if(vbmp) printf("%s:%d %s", check, chlineno, txt_chk);
182712b2f30Ssthen /* generate results */
183712b2f30Ssthen rr_transform(txt_in, wire_out, txt_out, back, bufs);
184712b2f30Ssthen /* checks */
185712b2f30Ssthen if(of) {
186712b2f30Ssthen fprintf(of, "%s%s", wire_out, txt_out);
187712b2f30Ssthen } else {
188712b2f30Ssthen rr_checks(wire_chk, txt_chk, txt_out, wire_out, back);
189712b2f30Ssthen }
190712b2f30Ssthen }
191712b2f30Ssthen
192712b2f30Ssthen if(of) fclose(of);
193712b2f30Ssthen fclose(inf);
194712b2f30Ssthen fclose(chf);
195712b2f30Ssthen free(txt_in);
196712b2f30Ssthen free(txt_out);
197712b2f30Ssthen free(txt_chk);
198712b2f30Ssthen free(wire_out);
199712b2f30Ssthen free(wire_chk);
200712b2f30Ssthen free(back);
201712b2f30Ssthen }
202712b2f30Ssthen
2037bc20e6dSsthen #define xstr(s) str(s)
2047bc20e6dSsthen #define str(s) #s
2057bc20e6dSsthen
2067bc20e6dSsthen #define SRCDIRSTR xstr(SRCDIR)
2077bc20e6dSsthen
208712b2f30Ssthen /** read rrs to and from string, to and from wireformat */
209712b2f30Ssthen static void
rr_tests(void)210712b2f30Ssthen rr_tests(void)
211712b2f30Ssthen {
2127bc20e6dSsthen rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.1",
2137bc20e6dSsthen SRCDIRSTR "/testdata/test_ldnsrr.c1");
2147bc20e6dSsthen rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.2",
2157bc20e6dSsthen SRCDIRSTR "/testdata/test_ldnsrr.c2");
2167bc20e6dSsthen rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.3",
2177bc20e6dSsthen SRCDIRSTR "/testdata/test_ldnsrr.c3");
2187bc20e6dSsthen rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.4",
2197bc20e6dSsthen SRCDIRSTR "/testdata/test_ldnsrr.c4");
2207bc20e6dSsthen rr_test_file(SRCDIRSTR "/testdata/test_ldnsrr.5",
2217bc20e6dSsthen SRCDIRSTR "/testdata/test_ldnsrr.c5");
222712b2f30Ssthen }
223712b2f30Ssthen
224e2a0f313Ssthen /** test various base64 decoding options */
225e2a0f313Ssthen static void
b64_test(void)226e2a0f313Ssthen b64_test(void)
227e2a0f313Ssthen {
228e2a0f313Ssthen /* "normal" b64 alphabet, with padding */
229e2a0f313Ssthen char* p1 = "aGVsbG8="; /* "hello" */
230e2a0f313Ssthen char* p2 = "aGVsbG8+"; /* "hello>" */
231e2a0f313Ssthen char* p3 = "aGVsbG8/IQ=="; /* "hello?!" */
232e2a0f313Ssthen char* p4 = "aGVsbG8"; /* "hel" + extra garbage */
233e2a0f313Ssthen
234e2a0f313Ssthen /* base64 url, without padding */
235e2a0f313Ssthen char* u1 = "aGVsbG8"; /* "hello" */
236e2a0f313Ssthen char* u2 = "aGVsbG8-"; /* "hello>" */
237e2a0f313Ssthen char* u3 = "aGVsbG8_IQ"; /* "hello?!" */
238e2a0f313Ssthen char* u4 = "aaaaa"; /* garbage */
239e2a0f313Ssthen
240e2a0f313Ssthen char target[128];
241e2a0f313Ssthen size_t tarsize = 128;
242e2a0f313Ssthen int result;
243e2a0f313Ssthen
244e2a0f313Ssthen memset(target, 0, sizeof(target));
245e2a0f313Ssthen result = sldns_b64_pton(p1, (uint8_t*)target, tarsize);
246*c2862f41Ssthen unit_assert(result == (int)strlen("hello") && strcmp(target, "hello") == 0);
247e2a0f313Ssthen memset(target, 0, sizeof(target));
248e2a0f313Ssthen result = sldns_b64_pton(p2, (uint8_t*)target, tarsize);
249*c2862f41Ssthen unit_assert(result == (int)strlen("hello>") && strcmp(target, "hello>") == 0);
250e2a0f313Ssthen memset(target, 0, sizeof(target));
251e2a0f313Ssthen result = sldns_b64_pton(p3, (uint8_t*)target, tarsize);
252*c2862f41Ssthen unit_assert(result == (int)strlen("hello?!") && strcmp(target, "hello?!") == 0);
253e2a0f313Ssthen memset(target, 0, sizeof(target));
254e2a0f313Ssthen result = sldns_b64_pton(p4, (uint8_t*)target, tarsize);
255e2a0f313Ssthen /* when padding is used everything that is not a block of 4 will be
256e2a0f313Ssthen * ignored */
257*c2862f41Ssthen unit_assert(result == (int)strlen("hel") && strcmp(target, "hel") == 0);
258e2a0f313Ssthen
259e2a0f313Ssthen memset(target, 0, sizeof(target));
260e2a0f313Ssthen result = sldns_b64url_pton(u1, strlen(u1), (uint8_t*)target, tarsize);
261*c2862f41Ssthen unit_assert(result == (int)strlen("hello") && strcmp(target, "hello") == 0);
262e2a0f313Ssthen memset(target, 0, sizeof(target));
263e2a0f313Ssthen result = sldns_b64url_pton(u2, strlen(u2), (uint8_t*)target, tarsize);
264*c2862f41Ssthen unit_assert(result == (int)strlen("hello>") && strcmp(target, "hello>") == 0);
265e2a0f313Ssthen memset(target, 0, sizeof(target));
266e2a0f313Ssthen result = sldns_b64url_pton(u3, strlen(u3), (uint8_t*)target, tarsize);
267*c2862f41Ssthen unit_assert(result == (int)strlen("hello+/") && strcmp(target, "hello?!") == 0);
268e2a0f313Ssthen /* one item in block of four is not allowed */
269e2a0f313Ssthen memset(target, 0, sizeof(target));
270e2a0f313Ssthen result = sldns_b64url_pton(u4, strlen(u4), (uint8_t*)target, tarsize);
271e2a0f313Ssthen unit_assert(result == -1);
272e2a0f313Ssthen }
273e2a0f313Ssthen
274712b2f30Ssthen void
ldns_test(void)275712b2f30Ssthen ldns_test(void)
276712b2f30Ssthen {
277712b2f30Ssthen unit_show_feature("sldns");
278712b2f30Ssthen rr_tests();
279e2a0f313Ssthen b64_test();
280712b2f30Ssthen }
281