xref: /freebsd/contrib/unbound/testcode/unitdname.c (revision be771a7b7f4580a30d99e41a5bb1b93a385a119d)
1*be771a7bSCy Schubert /*
2*be771a7bSCy Schubert  * testcode/unitdname.c - unit test for dname routines.
3*be771a7bSCy Schubert  *
4*be771a7bSCy Schubert  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5*be771a7bSCy Schubert  *
6*be771a7bSCy Schubert  * This software is open source.
7*be771a7bSCy Schubert  *
8*be771a7bSCy Schubert  * Redistribution and use in source and binary forms, with or without
9*be771a7bSCy Schubert  * modification, are permitted provided that the following conditions
10*be771a7bSCy Schubert  * are met:
11*be771a7bSCy Schubert  *
12*be771a7bSCy Schubert  * Redistributions of source code must retain the above copyright notice,
13*be771a7bSCy Schubert  * this list of conditions and the following disclaimer.
14*be771a7bSCy Schubert  *
15*be771a7bSCy Schubert  * Redistributions in binary form must reproduce the above copyright notice,
16*be771a7bSCy Schubert  * this list of conditions and the following disclaimer in the documentation
17*be771a7bSCy Schubert  * and/or other materials provided with the distribution.
18*be771a7bSCy Schubert  *
19*be771a7bSCy Schubert  * Neither the name of the NLNET LABS nor the names of its contributors may
20*be771a7bSCy Schubert  * be used to endorse or promote products derived from this software without
21*be771a7bSCy Schubert  * specific prior written permission.
22*be771a7bSCy Schubert  *
23*be771a7bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*be771a7bSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*be771a7bSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26*be771a7bSCy Schubert  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27*be771a7bSCy Schubert  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*be771a7bSCy Schubert  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29*be771a7bSCy Schubert  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30*be771a7bSCy Schubert  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31*be771a7bSCy Schubert  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32*be771a7bSCy Schubert  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*be771a7bSCy Schubert  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*be771a7bSCy Schubert  *
35*be771a7bSCy Schubert  */
36*be771a7bSCy Schubert /**
37*be771a7bSCy Schubert  * \file
38*be771a7bSCy Schubert  * Calls dname unit tests. Exits with code 1 on a failure.
39*be771a7bSCy Schubert  */
40*be771a7bSCy Schubert 
41*be771a7bSCy Schubert #include "config.h"
42*be771a7bSCy Schubert #include <ctype.h>
43*be771a7bSCy Schubert #include "util/log.h"
44*be771a7bSCy Schubert #include "testcode/unitmain.h"
45*be771a7bSCy Schubert #include "util/data/dname.h"
46*be771a7bSCy Schubert #include "sldns/sbuffer.h"
47*be771a7bSCy Schubert #include "sldns/str2wire.h"
48*be771a7bSCy Schubert 
49*be771a7bSCy Schubert /** put dname into buffer */
50*be771a7bSCy Schubert static sldns_buffer*
51*be771a7bSCy Schubert dname_to_buf(sldns_buffer* b, const char* str)
52*be771a7bSCy Schubert {
53*be771a7bSCy Schubert 	int e;
54*be771a7bSCy Schubert 	size_t len = sldns_buffer_capacity(b);
55*be771a7bSCy Schubert 	sldns_buffer_clear(b);
56*be771a7bSCy Schubert 	e = sldns_str2wire_dname_buf(str, sldns_buffer_begin(b), &len);
57*be771a7bSCy Schubert 	if(e != 0)
58*be771a7bSCy Schubert 		fatal_exit("%s ldns: %s", __func__,
59*be771a7bSCy Schubert 			sldns_get_errorstr_parse(e));
60*be771a7bSCy Schubert 	sldns_buffer_set_position(b, len);
61*be771a7bSCy Schubert 	sldns_buffer_flip(b);
62*be771a7bSCy Schubert 	return b;
63*be771a7bSCy Schubert }
64*be771a7bSCy Schubert 
65*be771a7bSCy Schubert /** test query_dname_len function */
66*be771a7bSCy Schubert static void
67*be771a7bSCy Schubert dname_test_qdl(sldns_buffer* buff)
68*be771a7bSCy Schubert {
69*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "query_dname_len");
70*be771a7bSCy Schubert 	unit_assert( query_dname_len(buff) == 0);
71*be771a7bSCy Schubert 	unit_assert( query_dname_len(dname_to_buf(buff, ".")) == 1 );
72*be771a7bSCy Schubert 	unit_assert( query_dname_len(dname_to_buf(buff, "bla.foo.")) == 9 );
73*be771a7bSCy Schubert 	unit_assert( query_dname_len(dname_to_buf(buff, "x.y.z.example.com."
74*be771a7bSCy Schubert 		)) == 19 );
75*be771a7bSCy Schubert }
76*be771a7bSCy Schubert 
77*be771a7bSCy Schubert /** test query_dname_tolower */
78*be771a7bSCy Schubert static void
79*be771a7bSCy Schubert dname_test_qdtl(sldns_buffer* buff)
80*be771a7bSCy Schubert {
81*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "query_dname_tolower");
82*be771a7bSCy Schubert 	sldns_buffer_write_at(buff, 0, "\012abCDeaBCde\003cOm\000", 16);
83*be771a7bSCy Schubert 	query_dname_tolower(sldns_buffer_begin(buff));
84*be771a7bSCy Schubert 	unit_assert( memcmp(sldns_buffer_begin(buff),
85*be771a7bSCy Schubert 		"\012abcdeabcde\003com\000", 16) == 0);
86*be771a7bSCy Schubert 
87*be771a7bSCy Schubert 	sldns_buffer_write_at(buff, 0, "\001+\012abC{e-ZYXe\003NET\000", 18);
88*be771a7bSCy Schubert 	query_dname_tolower(sldns_buffer_begin(buff));
89*be771a7bSCy Schubert 	unit_assert( memcmp(sldns_buffer_begin(buff),
90*be771a7bSCy Schubert 		"\001+\012abc{e-zyxe\003net\000", 18) == 0);
91*be771a7bSCy Schubert 
92*be771a7bSCy Schubert 	sldns_buffer_write_at(buff, 0, "\000", 1);
93*be771a7bSCy Schubert 	query_dname_tolower(sldns_buffer_begin(buff));
94*be771a7bSCy Schubert 	unit_assert( memcmp(sldns_buffer_begin(buff), "\000", 1) == 0);
95*be771a7bSCy Schubert 
96*be771a7bSCy Schubert 	sldns_buffer_write_at(buff, 0, "\002NL\000", 4);
97*be771a7bSCy Schubert 	query_dname_tolower(sldns_buffer_begin(buff));
98*be771a7bSCy Schubert 	unit_assert( memcmp(sldns_buffer_begin(buff), "\002nl\000", 4) == 0);
99*be771a7bSCy Schubert }
100*be771a7bSCy Schubert 
101*be771a7bSCy Schubert /** test query_dname_compare */
102*be771a7bSCy Schubert static void
103*be771a7bSCy Schubert dname_test_query_dname_compare(void)
104*be771a7bSCy Schubert {
105*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "query_dname_compare");
106*be771a7bSCy Schubert 	unit_assert(query_dname_compare((uint8_t*)"", (uint8_t*)"") == 0);
107*be771a7bSCy Schubert 	unit_assert(query_dname_compare((uint8_t*)"\001a",
108*be771a7bSCy Schubert 					(uint8_t*)"\001a") == 0);
109*be771a7bSCy Schubert 	unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
110*be771a7bSCy Schubert 					(uint8_t*)"\003abc\001a") == 0);
111*be771a7bSCy Schubert 	unit_assert(query_dname_compare((uint8_t*)"\003aBc\001a",
112*be771a7bSCy Schubert 					(uint8_t*)"\003AbC\001A") == 0);
113*be771a7bSCy Schubert 	unit_assert(query_dname_compare((uint8_t*)"\003abc",
114*be771a7bSCy Schubert 					(uint8_t*)"\003abc\001a") == -1);
115*be771a7bSCy Schubert 	unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
116*be771a7bSCy Schubert 					(uint8_t*)"\003abc") == +1);
117*be771a7bSCy Schubert 	unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
118*be771a7bSCy Schubert 					(uint8_t*)"") == +1);
119*be771a7bSCy Schubert 	unit_assert(query_dname_compare((uint8_t*)"",
120*be771a7bSCy Schubert 					(uint8_t*)"\003abc\001a") == -1);
121*be771a7bSCy Schubert 	unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
122*be771a7bSCy Schubert 					(uint8_t*)"\003xxx\001a") == -1);
123*be771a7bSCy Schubert 	unit_assert(query_dname_compare((uint8_t*)"\003axx\001a",
124*be771a7bSCy Schubert 					(uint8_t*)"\003abc\001a") == 1);
125*be771a7bSCy Schubert 	unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
126*be771a7bSCy Schubert 					(uint8_t*)"\003abc\001Z") == -1);
127*be771a7bSCy Schubert 	unit_assert(query_dname_compare((uint8_t*)"\003abc\001Z",
128*be771a7bSCy Schubert 					(uint8_t*)"\003abc\001a") == 1);
129*be771a7bSCy Schubert }
130*be771a7bSCy Schubert 
131*be771a7bSCy Schubert /** test dname_count_labels */
132*be771a7bSCy Schubert static void
133*be771a7bSCy Schubert dname_test_count_labels(void)
134*be771a7bSCy Schubert {
135*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_count_labels");
136*be771a7bSCy Schubert 	unit_assert(dname_count_labels((uint8_t*)"") == 1);
137*be771a7bSCy Schubert 	unit_assert(dname_count_labels((uint8_t*)"\003com") == 2);
138*be771a7bSCy Schubert 	unit_assert(dname_count_labels((uint8_t*)"\003org") == 2);
139*be771a7bSCy Schubert 	unit_assert(dname_count_labels((uint8_t*)"\007example\003com") == 3);
140*be771a7bSCy Schubert 	unit_assert(dname_count_labels((uint8_t*)"\003bla\007example\003com")
141*be771a7bSCy Schubert 		== 4);
142*be771a7bSCy Schubert }
143*be771a7bSCy Schubert 
144*be771a7bSCy Schubert /** test dname_count_size_labels */
145*be771a7bSCy Schubert static void
146*be771a7bSCy Schubert dname_test_count_size_labels(void)
147*be771a7bSCy Schubert {
148*be771a7bSCy Schubert 	size_t sz = 0;
149*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_count_size_labels");
150*be771a7bSCy Schubert 	unit_assert(dname_count_size_labels((uint8_t*)"", &sz) == 1);
151*be771a7bSCy Schubert 	unit_assert(sz == 1);
152*be771a7bSCy Schubert 	unit_assert(dname_count_size_labels((uint8_t*)"\003com", &sz) == 2);
153*be771a7bSCy Schubert 	unit_assert(sz == 5);
154*be771a7bSCy Schubert 	unit_assert(dname_count_size_labels((uint8_t*)"\003org", &sz) == 2);
155*be771a7bSCy Schubert 	unit_assert(sz == 5);
156*be771a7bSCy Schubert 	unit_assert(dname_count_size_labels((uint8_t*)"\007example\003com",
157*be771a7bSCy Schubert 		&sz) == 3);
158*be771a7bSCy Schubert 	unit_assert(sz == 13);
159*be771a7bSCy Schubert 	unit_assert(dname_count_size_labels((uint8_t*)"\003bla\007example"
160*be771a7bSCy Schubert 		"\003com", &sz) == 4);
161*be771a7bSCy Schubert 	unit_assert(sz == 17);
162*be771a7bSCy Schubert }
163*be771a7bSCy Schubert 
164*be771a7bSCy Schubert 
165*be771a7bSCy Schubert /** test pkt_dname_len */
166*be771a7bSCy Schubert static void
167*be771a7bSCy Schubert dname_test_pkt_dname_len(sldns_buffer* buff)
168*be771a7bSCy Schubert {
169*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "pkt_dname_len");
170*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
171*be771a7bSCy Schubert 	sldns_buffer_write(buff, "\000", 1);
172*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
173*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 1 );
174*be771a7bSCy Schubert 	unit_assert( sldns_buffer_position(buff) == 1);
175*be771a7bSCy Schubert 
176*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
177*be771a7bSCy Schubert 	sldns_buffer_write(buff, "\003org\000", 5);
178*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
179*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 5 );
180*be771a7bSCy Schubert 	unit_assert( sldns_buffer_position(buff) == 5);
181*be771a7bSCy Schubert 
182*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
183*be771a7bSCy Schubert 	sldns_buffer_write(buff, "\002os\007example\003org\000", 16);
184*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
185*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 16 );
186*be771a7bSCy Schubert 	unit_assert( sldns_buffer_position(buff) == 16);
187*be771a7bSCy Schubert 
188*be771a7bSCy Schubert 	/* invalid compression pointer: to self */
189*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
190*be771a7bSCy Schubert 	sldns_buffer_write(buff, "\300\000os\007example\003org\000", 17);
191*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
192*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 0 );
193*be771a7bSCy Schubert 
194*be771a7bSCy Schubert 	/* valid compression pointer */
195*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
196*be771a7bSCy Schubert 	sldns_buffer_write(buff, "\003com\000\040\300\000", 8);
197*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
198*be771a7bSCy Schubert 	sldns_buffer_set_position(buff, 6);
199*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 5 );
200*be771a7bSCy Schubert 	unit_assert( sldns_buffer_position(buff) == 8);
201*be771a7bSCy Schubert 
202*be771a7bSCy Schubert 	/* unknown label type */
203*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
204*be771a7bSCy Schubert 	sldns_buffer_write(buff, "\002os\107example\003org\000", 16);
205*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
206*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 0 );
207*be771a7bSCy Schubert 
208*be771a7bSCy Schubert 	/* label too long */
209*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
210*be771a7bSCy Schubert 	sldns_buffer_write(buff, "\002os\047example\003org\000", 16);
211*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
212*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 0 );
213*be771a7bSCy Schubert 
214*be771a7bSCy Schubert 	/* label exceeds packet */
215*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
216*be771a7bSCy Schubert 	sldns_buffer_write(buff, "\002os\007example\007org\004", 16);
217*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
218*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 0 );
219*be771a7bSCy Schubert 
220*be771a7bSCy Schubert 	/* name very long */
221*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
222*be771a7bSCy Schubert 	sldns_buffer_write(buff,
223*be771a7bSCy Schubert 		"\020a1cdef5555544444"
224*be771a7bSCy Schubert 		"\020a2cdef5555544444"
225*be771a7bSCy Schubert 		"\020a3cdef5555544444"
226*be771a7bSCy Schubert 		"\020a4cdef5555544444"
227*be771a7bSCy Schubert 		"\020a5cdef5555544444"
228*be771a7bSCy Schubert 		"\020a6cdef5555544444"
229*be771a7bSCy Schubert 		"\020a7cdef5555544444"
230*be771a7bSCy Schubert 		"\020a8cdef5555544444"
231*be771a7bSCy Schubert 		"\020a9cdef5555544444"
232*be771a7bSCy Schubert 		"\020aAcdef5555544444"
233*be771a7bSCy Schubert 		"\020aBcdef5555544444"
234*be771a7bSCy Schubert 		"\020aCcdef5555544444"
235*be771a7bSCy Schubert 		"\020aDcdef5555544444"
236*be771a7bSCy Schubert 		"\020aEcdef5555544444"	/* 238 up to here */
237*be771a7bSCy Schubert 		"\007aabbccd"		/* 246 up to here */
238*be771a7bSCy Schubert 		"\007example\000"	/* 255 to here */
239*be771a7bSCy Schubert 		, 255);
240*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
241*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 255 );
242*be771a7bSCy Schubert 	unit_assert( sldns_buffer_position(buff) == 255);
243*be771a7bSCy Schubert 
244*be771a7bSCy Schubert 	/* name too long */
245*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
246*be771a7bSCy Schubert 	sldns_buffer_write(buff,
247*be771a7bSCy Schubert 		"\020a1cdef5555544444"
248*be771a7bSCy Schubert 		"\020a2cdef5555544444"
249*be771a7bSCy Schubert 		"\020a3cdef5555544444"
250*be771a7bSCy Schubert 		"\020a4cdef5555544444"
251*be771a7bSCy Schubert 		"\020a5cdef5555544444"
252*be771a7bSCy Schubert 		"\020a6cdef5555544444"
253*be771a7bSCy Schubert 		"\020a7cdef5555544444"
254*be771a7bSCy Schubert 		"\020a8cdef5555544444"
255*be771a7bSCy Schubert 		"\020a9cdef5555544444"
256*be771a7bSCy Schubert 		"\020aAcdef5555544444"
257*be771a7bSCy Schubert 		"\020aBcdef5555544444"
258*be771a7bSCy Schubert 		"\020aCcdef5555544444"
259*be771a7bSCy Schubert 		"\020aXcdef5555544444"
260*be771a7bSCy Schubert 		"\020aXcdef5555544444"
261*be771a7bSCy Schubert 		"\020aXcdef5555544444"
262*be771a7bSCy Schubert 		"\020aDcdef5555544444"
263*be771a7bSCy Schubert 		"\020aEcdef5555544444"	/* 238 up to here */
264*be771a7bSCy Schubert 		"\007aabbccd"		/* 246 up to here */
265*be771a7bSCy Schubert 		"\007example\000"	/* 255 to here */
266*be771a7bSCy Schubert 		, 255);
267*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
268*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 0 );
269*be771a7bSCy Schubert }
270*be771a7bSCy Schubert 
271*be771a7bSCy Schubert /** test dname_lab_cmp */
272*be771a7bSCy Schubert static void
273*be771a7bSCy Schubert dname_test_dname_lab_cmp(void)
274*be771a7bSCy Schubert {
275*be771a7bSCy Schubert 	int ml = 0; /* number of labels that matched exactly */
276*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_lab_cmp");
277*be771a7bSCy Schubert 
278*be771a7bSCy Schubert 	/* test for equality succeeds */
279*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp((uint8_t*)"", 1, (uint8_t*)"", 1, &ml) == 0);
280*be771a7bSCy Schubert 	unit_assert(ml == 1);
281*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
282*be771a7bSCy Schubert 		(uint8_t*)"\003net", 2,
283*be771a7bSCy Schubert 		(uint8_t*)"\003net", 2,
284*be771a7bSCy Schubert 		&ml) == 0);
285*be771a7bSCy Schubert 	unit_assert(ml == 2);
286*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
287*be771a7bSCy Schubert 		(uint8_t*)"\007example\003net", 3,
288*be771a7bSCy Schubert 		(uint8_t*)"\007example\003net", 3,
289*be771a7bSCy Schubert 		&ml) == 0);
290*be771a7bSCy Schubert 	unit_assert(ml == 3);
291*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
292*be771a7bSCy Schubert 		(uint8_t*)"\004test\007example\003net", 4,
293*be771a7bSCy Schubert 		(uint8_t*)"\004test\007example\003net", 4,
294*be771a7bSCy Schubert 		&ml) == 0);
295*be771a7bSCy Schubert 	unit_assert(ml == 4);
296*be771a7bSCy Schubert 
297*be771a7bSCy Schubert 	/* root is smaller than anything else */
298*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
299*be771a7bSCy Schubert 		(uint8_t*)"", 1,
300*be771a7bSCy Schubert 		(uint8_t*)"\003net", 2,
301*be771a7bSCy Schubert 		&ml) == -1);
302*be771a7bSCy Schubert 	unit_assert(ml == 1);
303*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
304*be771a7bSCy Schubert 		(uint8_t*)"\003net", 2,
305*be771a7bSCy Schubert 		(uint8_t*)"", 1,
306*be771a7bSCy Schubert 		&ml) == 1);
307*be771a7bSCy Schubert 	unit_assert(ml == 1);
308*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
309*be771a7bSCy Schubert 		(uint8_t*)"", 1,
310*be771a7bSCy Schubert 		(uint8_t*)"\007example\003net", 3,
311*be771a7bSCy Schubert 		&ml) == -1);
312*be771a7bSCy Schubert 	unit_assert(ml == 1);
313*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
314*be771a7bSCy Schubert 		(uint8_t*)"\007example\003net", 3,
315*be771a7bSCy Schubert 		(uint8_t*)"", 1,
316*be771a7bSCy Schubert 		&ml) == 1);
317*be771a7bSCy Schubert 	unit_assert(ml == 1);
318*be771a7bSCy Schubert 
319*be771a7bSCy Schubert 	/* label length makes a difference */
320*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
321*be771a7bSCy Schubert 		(uint8_t*)"\004neta", 2,
322*be771a7bSCy Schubert 		(uint8_t*)"\003net", 2,
323*be771a7bSCy Schubert 		&ml) != 0);
324*be771a7bSCy Schubert 	unit_assert(ml == 1);
325*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
326*be771a7bSCy Schubert 		(uint8_t*)"\002ne", 2,
327*be771a7bSCy Schubert 		(uint8_t*)"\004neta", 2,
328*be771a7bSCy Schubert 		&ml) != 0);
329*be771a7bSCy Schubert 	unit_assert(ml == 1);
330*be771a7bSCy Schubert 
331*be771a7bSCy Schubert 	/* contents follow the zone apex */
332*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
333*be771a7bSCy Schubert 		(uint8_t*)"\003bla\007example\003net", 4,
334*be771a7bSCy Schubert 		(uint8_t*)"\007example\003net", 3,
335*be771a7bSCy Schubert 		&ml) == 1);
336*be771a7bSCy Schubert 	unit_assert(ml == 3);
337*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
338*be771a7bSCy Schubert 		(uint8_t*)"\007example\003net", 3,
339*be771a7bSCy Schubert 		(uint8_t*)"\003bla\007example\003net", 4,
340*be771a7bSCy Schubert 		&ml) == -1);
341*be771a7bSCy Schubert 	unit_assert(ml == 3);
342*be771a7bSCy Schubert 
343*be771a7bSCy Schubert 	/* label content makes a difference */
344*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
345*be771a7bSCy Schubert 		(uint8_t*)"\003aag\007example\003net", 4,
346*be771a7bSCy Schubert 		(uint8_t*)"\003bla\007example\003net", 4,
347*be771a7bSCy Schubert 		&ml) == -1);
348*be771a7bSCy Schubert 	unit_assert(ml == 3);
349*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
350*be771a7bSCy Schubert 		(uint8_t*)"\003aag\007example\003net", 4,
351*be771a7bSCy Schubert 		(uint8_t*)"\003bla\007example\003net", 4,
352*be771a7bSCy Schubert 		&ml) == -1);
353*be771a7bSCy Schubert 	unit_assert(ml == 3);
354*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
355*be771a7bSCy Schubert 		(uint8_t*)"\003bla\003aag\007example\003net", 5,
356*be771a7bSCy Schubert 		(uint8_t*)"\003aag\003bla\007example\003net", 5,
357*be771a7bSCy Schubert 		&ml) == -1);
358*be771a7bSCy Schubert 	unit_assert(ml == 3);
359*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
360*be771a7bSCy Schubert 		(uint8_t*)"\02sn\003opt\003aag\007example\003net", 6,
361*be771a7bSCy Schubert 		(uint8_t*)"\02sn\003opt\003bla\007example\003net", 6,
362*be771a7bSCy Schubert 		&ml) == -1);
363*be771a7bSCy Schubert 	unit_assert(ml == 3);
364*be771a7bSCy Schubert 
365*be771a7bSCy Schubert 	/* but lowercase/uppercase does not make a difference. */
366*be771a7bSCy Schubert 	unit_assert(dname_lab_cmp(
367*be771a7bSCy Schubert 		(uint8_t*)"\003bLa\007examPLe\003net", 4,
368*be771a7bSCy Schubert 		(uint8_t*)"\003bla\007eXAmple\003nET", 4,
369*be771a7bSCy Schubert 		&ml) == 0);
370*be771a7bSCy Schubert 	unit_assert(ml == 4);
371*be771a7bSCy Schubert }
372*be771a7bSCy Schubert 
373*be771a7bSCy Schubert /** test dname_subdomain_c */
374*be771a7bSCy Schubert static void
375*be771a7bSCy Schubert dname_test_subdomain(void)
376*be771a7bSCy Schubert {
377*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_subdomain");
378*be771a7bSCy Schubert 	unit_assert(dname_subdomain_c(
379*be771a7bSCy Schubert 		(uint8_t*)"",
380*be771a7bSCy Schubert 		(uint8_t*)""));
381*be771a7bSCy Schubert 	unit_assert(dname_subdomain_c(
382*be771a7bSCy Schubert 		(uint8_t*)"\003com",
383*be771a7bSCy Schubert 		(uint8_t*)""));
384*be771a7bSCy Schubert 	unit_assert(!dname_subdomain_c(
385*be771a7bSCy Schubert 		(uint8_t*)"",
386*be771a7bSCy Schubert 		(uint8_t*)"\003com"));
387*be771a7bSCy Schubert 	unit_assert(dname_subdomain_c(
388*be771a7bSCy Schubert 		(uint8_t*)"\007example\003com",
389*be771a7bSCy Schubert 		(uint8_t*)"\003com"));
390*be771a7bSCy Schubert 	unit_assert(!dname_subdomain_c(
391*be771a7bSCy Schubert 		(uint8_t*)"\003com",
392*be771a7bSCy Schubert 		(uint8_t*)"\007example\003com"));
393*be771a7bSCy Schubert 	unit_assert(dname_subdomain_c(
394*be771a7bSCy Schubert 		(uint8_t*)"\007example\003com",
395*be771a7bSCy Schubert 		(uint8_t*)""));
396*be771a7bSCy Schubert 	unit_assert(!dname_subdomain_c(
397*be771a7bSCy Schubert 		(uint8_t*)"\003net",
398*be771a7bSCy Schubert 		(uint8_t*)"\003com"));
399*be771a7bSCy Schubert 	unit_assert(!dname_subdomain_c(
400*be771a7bSCy Schubert 		(uint8_t*)"\003net",
401*be771a7bSCy Schubert 		(uint8_t*)"\003org"));
402*be771a7bSCy Schubert 	unit_assert(!dname_subdomain_c(
403*be771a7bSCy Schubert 		(uint8_t*)"\007example\003net",
404*be771a7bSCy Schubert 		(uint8_t*)"\003org"));
405*be771a7bSCy Schubert 	unit_assert(!dname_subdomain_c(
406*be771a7bSCy Schubert 		(uint8_t*)"\003net",
407*be771a7bSCy Schubert 		(uint8_t*)"\007example\003org"));
408*be771a7bSCy Schubert }
409*be771a7bSCy Schubert 
410*be771a7bSCy Schubert /** test dname_strict_subdomain */
411*be771a7bSCy Schubert static void
412*be771a7bSCy Schubert dname_test_strict_subdomain(void)
413*be771a7bSCy Schubert {
414*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_strict_subdomain");
415*be771a7bSCy Schubert 	unit_assert(!dname_strict_subdomain(
416*be771a7bSCy Schubert 		(uint8_t*)"", 1,
417*be771a7bSCy Schubert 		(uint8_t*)"", 1));
418*be771a7bSCy Schubert 	unit_assert(dname_strict_subdomain(
419*be771a7bSCy Schubert 		(uint8_t*)"\003com", 2,
420*be771a7bSCy Schubert 		(uint8_t*)"", 1));
421*be771a7bSCy Schubert 	unit_assert(!dname_strict_subdomain(
422*be771a7bSCy Schubert 		(uint8_t*)"", 1,
423*be771a7bSCy Schubert 		(uint8_t*)"\003com", 2));
424*be771a7bSCy Schubert 	unit_assert(dname_strict_subdomain(
425*be771a7bSCy Schubert 		(uint8_t*)"\007example\003com", 3,
426*be771a7bSCy Schubert 		(uint8_t*)"\003com", 2));
427*be771a7bSCy Schubert 	unit_assert(!dname_strict_subdomain(
428*be771a7bSCy Schubert 		(uint8_t*)"\003com", 2,
429*be771a7bSCy Schubert 		(uint8_t*)"\007example\003com", 3));
430*be771a7bSCy Schubert 	unit_assert(dname_strict_subdomain(
431*be771a7bSCy Schubert 		(uint8_t*)"\007example\003com", 3,
432*be771a7bSCy Schubert 		(uint8_t*)"", 1));
433*be771a7bSCy Schubert 	unit_assert(!dname_strict_subdomain(
434*be771a7bSCy Schubert 		(uint8_t*)"\003net", 2,
435*be771a7bSCy Schubert 		(uint8_t*)"\003com", 2));
436*be771a7bSCy Schubert 	unit_assert(!dname_strict_subdomain(
437*be771a7bSCy Schubert 		(uint8_t*)"\003net", 2,
438*be771a7bSCy Schubert 		(uint8_t*)"\003org", 2));
439*be771a7bSCy Schubert 	unit_assert(!dname_strict_subdomain(
440*be771a7bSCy Schubert 		(uint8_t*)"\007example\003net", 3,
441*be771a7bSCy Schubert 		(uint8_t*)"\003org", 2));
442*be771a7bSCy Schubert 	unit_assert(!dname_strict_subdomain(
443*be771a7bSCy Schubert 		(uint8_t*)"\003net", 2,
444*be771a7bSCy Schubert 		(uint8_t*)"\007example\003org", 3));
445*be771a7bSCy Schubert }
446*be771a7bSCy Schubert 
447*be771a7bSCy Schubert /** test dname_is_root */
448*be771a7bSCy Schubert static void
449*be771a7bSCy Schubert dname_test_isroot(void)
450*be771a7bSCy Schubert {
451*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_isroot");
452*be771a7bSCy Schubert 	unit_assert(dname_is_root((uint8_t*)"\000"));
453*be771a7bSCy Schubert 	unit_assert(!dname_is_root((uint8_t*)"\001a\000"));
454*be771a7bSCy Schubert 	unit_assert(!dname_is_root((uint8_t*)"\005abvcd\003com\000"));
455*be771a7bSCy Schubert 	/* malformed dname in this test, but should work */
456*be771a7bSCy Schubert 	unit_assert(!dname_is_root((uint8_t*)"\077a\000"));
457*be771a7bSCy Schubert 	unit_assert(dname_is_root((uint8_t*)"\000"));
458*be771a7bSCy Schubert }
459*be771a7bSCy Schubert 
460*be771a7bSCy Schubert /** test dname_remove_label */
461*be771a7bSCy Schubert static void
462*be771a7bSCy Schubert dname_test_removelabel(void)
463*be771a7bSCy Schubert {
464*be771a7bSCy Schubert 	uint8_t* orig = (uint8_t*)"\007example\003com\000";
465*be771a7bSCy Schubert 	uint8_t* n = orig;
466*be771a7bSCy Schubert 	size_t l = 13;
467*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_remove_label");
468*be771a7bSCy Schubert 	dname_remove_label(&n, &l);
469*be771a7bSCy Schubert 	unit_assert( n == orig+8 );
470*be771a7bSCy Schubert 	unit_assert( l == 5 );
471*be771a7bSCy Schubert 	dname_remove_label(&n, &l);
472*be771a7bSCy Schubert 	unit_assert( n == orig+12 );
473*be771a7bSCy Schubert 	unit_assert( l == 1 );
474*be771a7bSCy Schubert 	dname_remove_label(&n, &l);
475*be771a7bSCy Schubert 	unit_assert( n == orig+12 );
476*be771a7bSCy Schubert 	unit_assert( l == 1 );
477*be771a7bSCy Schubert }
478*be771a7bSCy Schubert 
479*be771a7bSCy Schubert /** test dname_signame_label_count */
480*be771a7bSCy Schubert static void
481*be771a7bSCy Schubert dname_test_sigcount(void)
482*be771a7bSCy Schubert {
483*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_signame_label_count");
484*be771a7bSCy Schubert 	unit_assert(dname_signame_label_count((uint8_t*)"\000") == 0);
485*be771a7bSCy Schubert 	unit_assert(dname_signame_label_count((uint8_t*)"\001*\000") == 0);
486*be771a7bSCy Schubert 	unit_assert(dname_signame_label_count((uint8_t*)"\003xom\000") == 1);
487*be771a7bSCy Schubert 	unit_assert(dname_signame_label_count(
488*be771a7bSCy Schubert 		(uint8_t*)"\001*\003xom\000") == 1);
489*be771a7bSCy Schubert 	unit_assert(dname_signame_label_count(
490*be771a7bSCy Schubert 		(uint8_t*)"\007example\003xom\000") == 2);
491*be771a7bSCy Schubert 	unit_assert(dname_signame_label_count(
492*be771a7bSCy Schubert 		(uint8_t*)"\001*\007example\003xom\000") == 2);
493*be771a7bSCy Schubert 	unit_assert(dname_signame_label_count(
494*be771a7bSCy Schubert 		(uint8_t*)"\003www\007example\003xom\000") == 3);
495*be771a7bSCy Schubert 	unit_assert(dname_signame_label_count(
496*be771a7bSCy Schubert 		(uint8_t*)"\001*\003www\007example\003xom\000") == 3);
497*be771a7bSCy Schubert }
498*be771a7bSCy Schubert 
499*be771a7bSCy Schubert /** test dname_is_wild routine */
500*be771a7bSCy Schubert static void
501*be771a7bSCy Schubert dname_test_iswild(void)
502*be771a7bSCy Schubert {
503*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_iswild");
504*be771a7bSCy Schubert 	unit_assert( !dname_is_wild((uint8_t*)"\000") );
505*be771a7bSCy Schubert 	unit_assert( dname_is_wild((uint8_t*)"\001*\000") );
506*be771a7bSCy Schubert 	unit_assert( !dname_is_wild((uint8_t*)"\003net\000") );
507*be771a7bSCy Schubert 	unit_assert( dname_is_wild((uint8_t*)"\001*\003net\000") );
508*be771a7bSCy Schubert }
509*be771a7bSCy Schubert 
510*be771a7bSCy Schubert /** test dname_canonical_compare */
511*be771a7bSCy Schubert static void
512*be771a7bSCy Schubert dname_test_canoncmp(void)
513*be771a7bSCy Schubert {
514*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_canonical_compare");
515*be771a7bSCy Schubert 	/* equality */
516*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
517*be771a7bSCy Schubert 		(uint8_t*)"\000",
518*be771a7bSCy Schubert 		(uint8_t*)"\000"
519*be771a7bSCy Schubert 		) == 0);
520*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
521*be771a7bSCy Schubert 		(uint8_t*)"\003net\000",
522*be771a7bSCy Schubert 		(uint8_t*)"\003net\000"
523*be771a7bSCy Schubert 		) == 0);
524*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
525*be771a7bSCy Schubert 		(uint8_t*)"\007example\003net\000",
526*be771a7bSCy Schubert 		(uint8_t*)"\007example\003net\000"
527*be771a7bSCy Schubert 		) == 0);
528*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
529*be771a7bSCy Schubert 		(uint8_t*)"\004test\007example\003net\000",
530*be771a7bSCy Schubert 		(uint8_t*)"\004test\007example\003net\000"
531*be771a7bSCy Schubert 		) == 0);
532*be771a7bSCy Schubert 
533*be771a7bSCy Schubert 	/* subdomains */
534*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
535*be771a7bSCy Schubert 		(uint8_t*)"\003com",
536*be771a7bSCy Schubert 		(uint8_t*)"\000"
537*be771a7bSCy Schubert 		) == 1);
538*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
539*be771a7bSCy Schubert 		(uint8_t*)"\000",
540*be771a7bSCy Schubert 		(uint8_t*)"\003com"
541*be771a7bSCy Schubert 		) == -1);
542*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
543*be771a7bSCy Schubert 		(uint8_t*)"\007example\003com",
544*be771a7bSCy Schubert 		(uint8_t*)"\003com"
545*be771a7bSCy Schubert 		) == 1);
546*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
547*be771a7bSCy Schubert 		(uint8_t*)"\003com",
548*be771a7bSCy Schubert 		(uint8_t*)"\007example\003com"
549*be771a7bSCy Schubert 		) == -1);
550*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
551*be771a7bSCy Schubert 		(uint8_t*)"\007example\003com",
552*be771a7bSCy Schubert 		(uint8_t*)"\000"
553*be771a7bSCy Schubert 		) == 1);
554*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
555*be771a7bSCy Schubert 		(uint8_t*)"\000",
556*be771a7bSCy Schubert 		(uint8_t*)"\007example\003com"
557*be771a7bSCy Schubert 		) == -1);
558*be771a7bSCy Schubert 
559*be771a7bSCy Schubert 	/* compare rightmost label */
560*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
561*be771a7bSCy Schubert 		(uint8_t*)"\003com",
562*be771a7bSCy Schubert 		(uint8_t*)"\003net"
563*be771a7bSCy Schubert 		) == -1);
564*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
565*be771a7bSCy Schubert 		(uint8_t*)"\003net",
566*be771a7bSCy Schubert 		(uint8_t*)"\003com"
567*be771a7bSCy Schubert 		) == 1);
568*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
569*be771a7bSCy Schubert 		(uint8_t*)"\003net",
570*be771a7bSCy Schubert 		(uint8_t*)"\003org"
571*be771a7bSCy Schubert 		) == -1);
572*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
573*be771a7bSCy Schubert 		(uint8_t*)"\007example\003net",
574*be771a7bSCy Schubert 		(uint8_t*)"\003org"
575*be771a7bSCy Schubert 		) == -1);
576*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
577*be771a7bSCy Schubert 		(uint8_t*)"\003org",
578*be771a7bSCy Schubert 		(uint8_t*)"\007example\003net"
579*be771a7bSCy Schubert 		) == 1);
580*be771a7bSCy Schubert 
581*be771a7bSCy Schubert 	/* label length makes a difference; but only if rest is equal */
582*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
583*be771a7bSCy Schubert 		(uint8_t*)"\004neta",
584*be771a7bSCy Schubert 		(uint8_t*)"\003net"
585*be771a7bSCy Schubert 		) == 1);
586*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
587*be771a7bSCy Schubert 		(uint8_t*)"\002ne",
588*be771a7bSCy Schubert 		(uint8_t*)"\004neta"
589*be771a7bSCy Schubert 		) == -1);
590*be771a7bSCy Schubert 
591*be771a7bSCy Schubert 	/* label content */
592*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
593*be771a7bSCy Schubert 		(uint8_t*)"\003aag\007example\003net",
594*be771a7bSCy Schubert 		(uint8_t*)"\003bla\007example\003net"
595*be771a7bSCy Schubert 		) == -1);
596*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
597*be771a7bSCy Schubert 		(uint8_t*)"\003bla\007example\003net",
598*be771a7bSCy Schubert 		(uint8_t*)"\003aag\007example\003net"
599*be771a7bSCy Schubert 		) == 1);
600*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
601*be771a7bSCy Schubert 		(uint8_t*)"\003bla\003aag\007example\003net",
602*be771a7bSCy Schubert 		(uint8_t*)"\003aag\003bla\007example\003net"
603*be771a7bSCy Schubert 		) == -1);
604*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
605*be771a7bSCy Schubert 		(uint8_t*)"\02sn\003opt\003aag\007example\003net",
606*be771a7bSCy Schubert 		(uint8_t*)"\02sn\003opt\003bla\007example\003net"
607*be771a7bSCy Schubert 		) == -1);
608*be771a7bSCy Schubert 
609*be771a7bSCy Schubert 	/* lowercase during compare */
610*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
611*be771a7bSCy Schubert 		(uint8_t*)"\003bLa\007examPLe\003net",
612*be771a7bSCy Schubert 		(uint8_t*)"\003bla\007eXAmple\003nET"
613*be771a7bSCy Schubert 		) == 0);
614*be771a7bSCy Schubert 
615*be771a7bSCy Schubert 	/* example from 4034 */
616*be771a7bSCy Schubert 	/* example a.example yljkjljk.a.example Z.a.example zABC.a.EXAMPLE
617*be771a7bSCy Schubert 	 z.example \001.z.example *.z.example \200.z.example */
618*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
619*be771a7bSCy Schubert 		(uint8_t*)"",
620*be771a7bSCy Schubert 		(uint8_t*)"\007example"
621*be771a7bSCy Schubert 		) == -1);
622*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
623*be771a7bSCy Schubert 		(uint8_t*)"\007example",
624*be771a7bSCy Schubert 		(uint8_t*)"\001a\007example"
625*be771a7bSCy Schubert 		) == -1);
626*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
627*be771a7bSCy Schubert 		(uint8_t*)"\001a\007example",
628*be771a7bSCy Schubert 		(uint8_t*)"\010yljkjljk\001a\007example"
629*be771a7bSCy Schubert 		) == -1);
630*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
631*be771a7bSCy Schubert 		(uint8_t*)"\010yljkjljk\001a\007example",
632*be771a7bSCy Schubert 		(uint8_t*)"\001Z\001a\007example"
633*be771a7bSCy Schubert 		) == -1);
634*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
635*be771a7bSCy Schubert 		(uint8_t*)"\001Z\001a\007example",
636*be771a7bSCy Schubert 		(uint8_t*)"\004zABC\001a\007EXAMPLE"
637*be771a7bSCy Schubert 		) == -1);
638*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
639*be771a7bSCy Schubert 		(uint8_t*)"\004zABC\001a\007EXAMPLE",
640*be771a7bSCy Schubert 		(uint8_t*)"\001z\007example"
641*be771a7bSCy Schubert 		) == -1);
642*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
643*be771a7bSCy Schubert 		(uint8_t*)"\001z\007example",
644*be771a7bSCy Schubert 		(uint8_t*)"\001\001\001z\007example"
645*be771a7bSCy Schubert 		) == -1);
646*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
647*be771a7bSCy Schubert 		(uint8_t*)"\001\001\001z\007example",
648*be771a7bSCy Schubert 		(uint8_t*)"\001*\001z\007example"
649*be771a7bSCy Schubert 		) == -1);
650*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
651*be771a7bSCy Schubert 		(uint8_t*)"\001*\001z\007example",
652*be771a7bSCy Schubert 		(uint8_t*)"\001\200\001z\007example"
653*be771a7bSCy Schubert 		) == -1);
654*be771a7bSCy Schubert 	/* same example in reverse */
655*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
656*be771a7bSCy Schubert 		(uint8_t*)"\007example",
657*be771a7bSCy Schubert 		(uint8_t*)""
658*be771a7bSCy Schubert 		) == 1);
659*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
660*be771a7bSCy Schubert 		(uint8_t*)"\001a\007example",
661*be771a7bSCy Schubert 		(uint8_t*)"\007example"
662*be771a7bSCy Schubert 		) == 1);
663*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
664*be771a7bSCy Schubert 		(uint8_t*)"\010yljkjljk\001a\007example",
665*be771a7bSCy Schubert 		(uint8_t*)"\001a\007example"
666*be771a7bSCy Schubert 		) == 1);
667*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
668*be771a7bSCy Schubert 		(uint8_t*)"\001Z\001a\007example",
669*be771a7bSCy Schubert 		(uint8_t*)"\010yljkjljk\001a\007example"
670*be771a7bSCy Schubert 		) == 1);
671*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
672*be771a7bSCy Schubert 		(uint8_t*)"\004zABC\001a\007EXAMPLE",
673*be771a7bSCy Schubert 		(uint8_t*)"\001Z\001a\007example"
674*be771a7bSCy Schubert 		) == 1);
675*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
676*be771a7bSCy Schubert 		(uint8_t*)"\001z\007example",
677*be771a7bSCy Schubert 		(uint8_t*)"\004zABC\001a\007EXAMPLE"
678*be771a7bSCy Schubert 		) == 1);
679*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
680*be771a7bSCy Schubert 		(uint8_t*)"\001\001\001z\007example",
681*be771a7bSCy Schubert 		(uint8_t*)"\001z\007example"
682*be771a7bSCy Schubert 		) == 1);
683*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
684*be771a7bSCy Schubert 		(uint8_t*)"\001*\001z\007example",
685*be771a7bSCy Schubert 		(uint8_t*)"\001\001\001z\007example"
686*be771a7bSCy Schubert 		) == 1);
687*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
688*be771a7bSCy Schubert 		(uint8_t*)"\001\200\001z\007example",
689*be771a7bSCy Schubert 		(uint8_t*)"\001*\001z\007example"
690*be771a7bSCy Schubert 		) == 1);
691*be771a7bSCy Schubert 	/* same example for equality */
692*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
693*be771a7bSCy Schubert 		(uint8_t*)"\007example",
694*be771a7bSCy Schubert 		(uint8_t*)"\007example"
695*be771a7bSCy Schubert 		) == 0);
696*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
697*be771a7bSCy Schubert 		(uint8_t*)"\001a\007example",
698*be771a7bSCy Schubert 		(uint8_t*)"\001a\007example"
699*be771a7bSCy Schubert 		) == 0);
700*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
701*be771a7bSCy Schubert 		(uint8_t*)"\010yljkjljk\001a\007example",
702*be771a7bSCy Schubert 		(uint8_t*)"\010yljkjljk\001a\007example"
703*be771a7bSCy Schubert 		) == 0);
704*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
705*be771a7bSCy Schubert 		(uint8_t*)"\001Z\001a\007example",
706*be771a7bSCy Schubert 		(uint8_t*)"\001Z\001a\007example"
707*be771a7bSCy Schubert 		) == 0);
708*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
709*be771a7bSCy Schubert 		(uint8_t*)"\004zABC\001a\007EXAMPLE",
710*be771a7bSCy Schubert 		(uint8_t*)"\004zABC\001a\007EXAMPLE"
711*be771a7bSCy Schubert 		) == 0);
712*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
713*be771a7bSCy Schubert 		(uint8_t*)"\001z\007example",
714*be771a7bSCy Schubert 		(uint8_t*)"\001z\007example"
715*be771a7bSCy Schubert 		) == 0);
716*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
717*be771a7bSCy Schubert 		(uint8_t*)"\001\001\001z\007example",
718*be771a7bSCy Schubert 		(uint8_t*)"\001\001\001z\007example"
719*be771a7bSCy Schubert 		) == 0);
720*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
721*be771a7bSCy Schubert 		(uint8_t*)"\001*\001z\007example",
722*be771a7bSCy Schubert 		(uint8_t*)"\001*\001z\007example"
723*be771a7bSCy Schubert 		) == 0);
724*be771a7bSCy Schubert 	unit_assert( dname_canonical_compare(
725*be771a7bSCy Schubert 		(uint8_t*)"\001\200\001z\007example",
726*be771a7bSCy Schubert 		(uint8_t*)"\001\200\001z\007example"
727*be771a7bSCy Schubert 		) == 0);
728*be771a7bSCy Schubert }
729*be771a7bSCy Schubert 
730*be771a7bSCy Schubert /** Test dname_get_shared_topdomain */
731*be771a7bSCy Schubert static void
732*be771a7bSCy Schubert dname_test_topdomain(void)
733*be771a7bSCy Schubert {
734*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_get_shared_topdomain");
735*be771a7bSCy Schubert 	unit_assert( query_dname_compare(
736*be771a7bSCy Schubert 		dname_get_shared_topdomain(
737*be771a7bSCy Schubert 			(uint8_t*)"",
738*be771a7bSCy Schubert 			(uint8_t*)""),
739*be771a7bSCy Schubert 		(uint8_t*)"") == 0);
740*be771a7bSCy Schubert 	unit_assert( query_dname_compare(
741*be771a7bSCy Schubert 		dname_get_shared_topdomain(
742*be771a7bSCy Schubert 			(uint8_t*)"\003www\007example\003com",
743*be771a7bSCy Schubert 			(uint8_t*)"\003www\007example\003com"),
744*be771a7bSCy Schubert 		(uint8_t*)"\003www\007example\003com") == 0);
745*be771a7bSCy Schubert 	unit_assert( query_dname_compare(
746*be771a7bSCy Schubert 		dname_get_shared_topdomain(
747*be771a7bSCy Schubert 			(uint8_t*)"\003www\007example\003com",
748*be771a7bSCy Schubert 			(uint8_t*)"\003bla\007example\003com"),
749*be771a7bSCy Schubert 		(uint8_t*)"\007example\003com") == 0);
750*be771a7bSCy Schubert }
751*be771a7bSCy Schubert 
752*be771a7bSCy Schubert /** Test dname_valid */
753*be771a7bSCy Schubert static void
754*be771a7bSCy Schubert dname_test_valid(void)
755*be771a7bSCy Schubert {
756*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_valid");
757*be771a7bSCy Schubert 	unit_assert( dname_valid(
758*be771a7bSCy Schubert 			(uint8_t*)"\003www\007example\003com", 255) == 17);
759*be771a7bSCy Schubert 	unit_assert( dname_valid((uint8_t*)"", 255) == 1);
760*be771a7bSCy Schubert 	unit_assert( dname_valid( (uint8_t*)
761*be771a7bSCy Schubert 		"\020a1cdef5555544444"
762*be771a7bSCy Schubert 		"\020a2cdef5555544444"
763*be771a7bSCy Schubert 		"\020a3cdef5555544444"
764*be771a7bSCy Schubert 		"\020a4cdef5555544444"
765*be771a7bSCy Schubert 		"\020a5cdef5555544444"
766*be771a7bSCy Schubert 		"\020a6cdef5555544444"
767*be771a7bSCy Schubert 		"\020a7cdef5555544444"
768*be771a7bSCy Schubert 		"\020a8cdef5555544444"
769*be771a7bSCy Schubert 		"\020a9cdef5555544444"
770*be771a7bSCy Schubert 		"\020aAcdef5555544444"
771*be771a7bSCy Schubert 		"\020aBcdef5555544444"
772*be771a7bSCy Schubert 		"\020aCcdef5555544444"
773*be771a7bSCy Schubert 		"\020aDcdef5555544444"
774*be771a7bSCy Schubert 		"\020aEcdef5555544444"	/* 238 up to here */
775*be771a7bSCy Schubert 		"\007aabbccd"		/* 246 up to here */
776*be771a7bSCy Schubert 		"\007example\000"	/* 255 to here */
777*be771a7bSCy Schubert 		, 255) == 255);
778*be771a7bSCy Schubert 	unit_assert( dname_valid( (uint8_t*)
779*be771a7bSCy Schubert 		"\020a1cdef5555544444"
780*be771a7bSCy Schubert 		"\020a2cdef5555544444"
781*be771a7bSCy Schubert 		"\020a3cdef5555544444"
782*be771a7bSCy Schubert 		"\020a4cdef5555544444"
783*be771a7bSCy Schubert 		"\020a5cdef5555544444"
784*be771a7bSCy Schubert 		"\020a6cdef5555544444"
785*be771a7bSCy Schubert 		"\020a7cdef5555544444"
786*be771a7bSCy Schubert 		"\020a8cdef5555544444"
787*be771a7bSCy Schubert 		"\020a9cdef5555544444"
788*be771a7bSCy Schubert 		"\020aAcdef5555544444"
789*be771a7bSCy Schubert 		"\020aBcdef5555544444"
790*be771a7bSCy Schubert 		"\020aCcdef5555544444"
791*be771a7bSCy Schubert 		"\020aDcdef5555544444"
792*be771a7bSCy Schubert 		"\020aEcdef5555544444"	/* 238 up to here */
793*be771a7bSCy Schubert 		"\007aabbccd"		/* 246 up to here */
794*be771a7bSCy Schubert 		"\010exampleX\000"	/* 256 to here */
795*be771a7bSCy Schubert 		, 4096) == 0);
796*be771a7bSCy Schubert }
797*be771a7bSCy Schubert 
798*be771a7bSCy Schubert /** Test dname_has_label */
799*be771a7bSCy Schubert static void
800*be771a7bSCy Schubert dname_test_has_label(void)
801*be771a7bSCy Schubert {
802*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_has_label");
803*be771a7bSCy Schubert 	/* label past root label */
804*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\01a\0\01c", 5, (uint8_t*)"\01c") == 0);
805*be771a7bSCy Schubert 	/* label not found */
806*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\02ab\01c\0", 6, (uint8_t*)"\01e") == 0);
807*be771a7bSCy Schubert 	/* buffer too short */
808*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\02ab\01c\0", 5, (uint8_t*)"\0") == 0);
809*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\1a\0", 2, (uint8_t*)"\0") == 0);
810*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\0", 0, (uint8_t*)"\0") == 0);
811*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\02ab\01c", 4, (uint8_t*)"\01c") == 0);
812*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\02ab\03qwe\06oqieur\03def\01c\0", 19, (uint8_t*)"\01c") == 0);
813*be771a7bSCy Schubert 
814*be771a7bSCy Schubert 	/* positive cases  */
815*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\0", 1, (uint8_t*)"\0") == 1);
816*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\1a\0", 3, (uint8_t*)"\0") == 1);
817*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\01a\0\01c", 5, (uint8_t*)"\0") == 1);
818*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\02ab\01c", 5, (uint8_t*)"\01c") == 1);
819*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\02ab\01c\0", 10, (uint8_t*)"\0") == 1);
820*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\02ab\01c\0", 7, (uint8_t*)"\0") == 1);
821*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\02ab\03qwe\06oqieur\03def\01c\0", 22, (uint8_t*)"\03def") == 1);
822*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\02ab\03qwe\06oqieur\03def\01c\0", 22, (uint8_t*)"\02ab") == 1);
823*be771a7bSCy Schubert 	unit_assert(dname_has_label((uint8_t*)"\02ab\03qwe\06oqieur\03def\01c\0", 22, (uint8_t*)"\01c") == 1);
824*be771a7bSCy Schubert }
825*be771a7bSCy Schubert 
826*be771a7bSCy Schubert /** test pkt_dname_tolower */
827*be771a7bSCy Schubert static void
828*be771a7bSCy Schubert dname_test_pdtl(sldns_buffer* loopbuf, sldns_buffer* boundbuf)
829*be771a7bSCy Schubert {
830*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "pkt_dname_tolower");
831*be771a7bSCy Schubert 	pkt_dname_tolower(loopbuf, sldns_buffer_at(loopbuf, 12));
832*be771a7bSCy Schubert 	pkt_dname_tolower(boundbuf, sldns_buffer_at(boundbuf, 12));
833*be771a7bSCy Schubert }
834*be771a7bSCy Schubert 
835*be771a7bSCy Schubert /** setup looped dname and out-of-bounds dname ptr */
836*be771a7bSCy Schubert static void
837*be771a7bSCy Schubert dname_setup_bufs(sldns_buffer* loopbuf, sldns_buffer* boundbuf)
838*be771a7bSCy Schubert {
839*be771a7bSCy Schubert 	sldns_buffer_write_u16(loopbuf, 0xd54d);  /* id */
840*be771a7bSCy Schubert 	sldns_buffer_write_u16(loopbuf, 0x12);    /* flags  */
841*be771a7bSCy Schubert 	sldns_buffer_write_u16(loopbuf, 1);       /* qdcount */
842*be771a7bSCy Schubert 	sldns_buffer_write_u16(loopbuf, 0);       /* ancount */
843*be771a7bSCy Schubert 	sldns_buffer_write_u16(loopbuf, 0);       /* nscount */
844*be771a7bSCy Schubert 	sldns_buffer_write_u16(loopbuf, 0);       /* arcount */
845*be771a7bSCy Schubert 	sldns_buffer_write_u8(loopbuf, 0xc0); /* PTR back at itself */
846*be771a7bSCy Schubert 	sldns_buffer_write_u8(loopbuf, 0x0c);
847*be771a7bSCy Schubert 	sldns_buffer_flip(loopbuf);
848*be771a7bSCy Schubert 
849*be771a7bSCy Schubert 	sldns_buffer_write_u16(boundbuf, 0xd54d);  /* id */
850*be771a7bSCy Schubert 	sldns_buffer_write_u16(boundbuf, 0x12);    /* flags  */
851*be771a7bSCy Schubert 	sldns_buffer_write_u16(boundbuf, 1);       /* qdcount */
852*be771a7bSCy Schubert 	sldns_buffer_write_u16(boundbuf, 0);       /* ancount */
853*be771a7bSCy Schubert 	sldns_buffer_write_u16(boundbuf, 0);       /* nscount */
854*be771a7bSCy Schubert 	sldns_buffer_write_u16(boundbuf, 0);       /* arcount */
855*be771a7bSCy Schubert 	sldns_buffer_write_u8(boundbuf, 0x01); /* len=1 */
856*be771a7bSCy Schubert 	sldns_buffer_write_u8(boundbuf, (uint8_t)'A'); /* A. label */
857*be771a7bSCy Schubert 	sldns_buffer_write_u8(boundbuf, 0xc0); /* PTR out of bounds */
858*be771a7bSCy Schubert 	sldns_buffer_write_u8(boundbuf, 0xcc);
859*be771a7bSCy Schubert 	sldns_buffer_flip(boundbuf);
860*be771a7bSCy Schubert }
861*be771a7bSCy Schubert 
862*be771a7bSCy Schubert static void
863*be771a7bSCy Schubert dname_test_str(sldns_buffer* buff)
864*be771a7bSCy Schubert {
865*be771a7bSCy Schubert 	char result[LDNS_MAX_DOMAINLEN], expect[LDNS_MAX_DOMAINLEN], *e;
866*be771a7bSCy Schubert 	size_t i;
867*be771a7bSCy Schubert 	unit_show_func("util/data/dname.c", "dname_str");
868*be771a7bSCy Schubert 
869*be771a7bSCy Schubert 	/* root ; expected OK */
870*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
871*be771a7bSCy Schubert 	sldns_buffer_write(buff, "\000", 1);
872*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
873*be771a7bSCy Schubert 	unit_assert( sldns_buffer_limit(buff) == 1 );
874*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 1 );
875*be771a7bSCy Schubert 	dname_str(sldns_buffer_begin(buff), result);
876*be771a7bSCy Schubert 	unit_assert( strcmp( ".", result) == 0 );
877*be771a7bSCy Schubert 
878*be771a7bSCy Schubert 	/* LDNS_MAX_DOMAINLEN - 1 ; expected OK */
879*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
880*be771a7bSCy Schubert 	sldns_buffer_write(buff,
881*be771a7bSCy Schubert 		"\077abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"  /*  64 up to here */
882*be771a7bSCy Schubert 		"\077abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"  /* 128 up to here */
883*be771a7bSCy Schubert 		"\077abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"  /* 192 up to here */
884*be771a7bSCy Schubert 		"\074abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567"     /* 253 up to here */
885*be771a7bSCy Schubert 		"\000"                                                                 /* 254 up to here */
886*be771a7bSCy Schubert 		, 254);
887*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
888*be771a7bSCy Schubert 	unit_assert( sldns_buffer_limit(buff) == 254 );
889*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 254 );
890*be771a7bSCy Schubert 	dname_str(sldns_buffer_begin(buff), result);
891*be771a7bSCy Schubert 	unit_assert( strcmp(
892*be771a7bSCy Schubert 		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890."
893*be771a7bSCy Schubert 		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890."
894*be771a7bSCy Schubert 		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890."
895*be771a7bSCy Schubert 		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567"
896*be771a7bSCy Schubert 		".", result) == 0 );
897*be771a7bSCy Schubert 
898*be771a7bSCy Schubert 	/* LDNS_MAX_DOMAINLEN ; expected OK */
899*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
900*be771a7bSCy Schubert 	sldns_buffer_write(buff,
901*be771a7bSCy Schubert 		"\077abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"  /*  64 up to here */
902*be771a7bSCy Schubert 		"\077abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"  /* 128 up to here */
903*be771a7bSCy Schubert 		"\077abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"  /* 192 up to here */
904*be771a7bSCy Schubert 		"\075abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678"    /* 254 up to here */
905*be771a7bSCy Schubert 		"\000"                                                                 /* 255 up to here */
906*be771a7bSCy Schubert 		, 255);
907*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
908*be771a7bSCy Schubert 	unit_assert( sldns_buffer_limit(buff) == 255 );
909*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 255 );
910*be771a7bSCy Schubert 	dname_str(sldns_buffer_begin(buff), result);
911*be771a7bSCy Schubert 	unit_assert( strcmp(
912*be771a7bSCy Schubert 		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890."
913*be771a7bSCy Schubert 		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890."
914*be771a7bSCy Schubert 		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890."
915*be771a7bSCy Schubert 		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678"
916*be771a7bSCy Schubert 		".", result) == 0 );
917*be771a7bSCy Schubert 
918*be771a7bSCy Schubert 	/* LDNS_MAX_DOMAINLEN + 1 ; expected to fail, output uses '&' on the latest label */
919*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
920*be771a7bSCy Schubert 	sldns_buffer_write(buff,
921*be771a7bSCy Schubert 		"\077abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"  /*  64 up to here */
922*be771a7bSCy Schubert 		"\077abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"  /* 128 up to here */
923*be771a7bSCy Schubert 		"\077abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"  /* 192 up to here */
924*be771a7bSCy Schubert 		"\076abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"   /* 255 up to here */
925*be771a7bSCy Schubert 		"\000"                                                                 /* 256 up to here */
926*be771a7bSCy Schubert 		, 256);
927*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
928*be771a7bSCy Schubert 	unit_assert( sldns_buffer_limit(buff) == 256 );
929*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 0 );
930*be771a7bSCy Schubert 	dname_str(sldns_buffer_begin(buff), result);
931*be771a7bSCy Schubert 	unit_assert( strcmp(
932*be771a7bSCy Schubert 		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890."
933*be771a7bSCy Schubert 		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890."
934*be771a7bSCy Schubert 		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890."
935*be771a7bSCy Schubert 		"&", result) == 0 );
936*be771a7bSCy Schubert 
937*be771a7bSCy Schubert 	/* LDNS_MAX_LABELLEN + 1 ; expected to fail, output uses '#' on the offending label */
938*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
939*be771a7bSCy Schubert 	sldns_buffer_write(buff,
940*be771a7bSCy Schubert 		"\077abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890"  /*  64 up to here */
941*be771a7bSCy Schubert 		"\100abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890a" /* 129 up to here */
942*be771a7bSCy Schubert 		"\000"                                                                 /* 130 up to here */
943*be771a7bSCy Schubert 		, 130);
944*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
945*be771a7bSCy Schubert 	unit_assert( sldns_buffer_limit(buff) == 130 );
946*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 0 );
947*be771a7bSCy Schubert 	dname_str(sldns_buffer_begin(buff), result);
948*be771a7bSCy Schubert 	unit_assert( strcmp(
949*be771a7bSCy Schubert 		"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890."
950*be771a7bSCy Schubert 		"#", result) == 0 );
951*be771a7bSCy Schubert 
952*be771a7bSCy Schubert 	/* LDNS_MAX_DOMAINLEN with single labels; expected OK */
953*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
954*be771a7bSCy Schubert 	for(i=0; i<=252; i+=2)
955*be771a7bSCy Schubert 		sldns_buffer_write(buff, "\001a", 2);
956*be771a7bSCy Schubert 	sldns_buffer_write_u8(buff, 0);
957*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
958*be771a7bSCy Schubert 	unit_assert( sldns_buffer_limit(buff) == 255 );
959*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 255 );
960*be771a7bSCy Schubert 	dname_str(sldns_buffer_begin(buff), result);
961*be771a7bSCy Schubert 	e = expect;
962*be771a7bSCy Schubert 	for(i=0; i<=252; i+=2) {
963*be771a7bSCy Schubert 		*e++ = 'a';
964*be771a7bSCy Schubert 		*e++ = '.';
965*be771a7bSCy Schubert 	}
966*be771a7bSCy Schubert 	*e = '\0';
967*be771a7bSCy Schubert 	unit_assert( strcmp(expect, result) == 0 );
968*be771a7bSCy Schubert 
969*be771a7bSCy Schubert 	/* LDNS_MAX_DOMAINLEN + 1 with single labels; expected to fail, output uses '&' on the latest label */
970*be771a7bSCy Schubert 	sldns_buffer_clear(buff);
971*be771a7bSCy Schubert 	for(i=0; i<=250; i+=2)
972*be771a7bSCy Schubert 		sldns_buffer_write(buff, "\001a", 2);
973*be771a7bSCy Schubert 	sldns_buffer_write(buff, "\002ab", 3);
974*be771a7bSCy Schubert 	sldns_buffer_write_u8(buff, 0);
975*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
976*be771a7bSCy Schubert 	unit_assert( sldns_buffer_limit(buff) == 256 );
977*be771a7bSCy Schubert 	unit_assert( pkt_dname_len(buff) == 0 );
978*be771a7bSCy Schubert 	dname_str(sldns_buffer_begin(buff), result);
979*be771a7bSCy Schubert 	e = expect;
980*be771a7bSCy Schubert 	for(i=0; i<=250; i+=2) {
981*be771a7bSCy Schubert 		*e++ = 'a';
982*be771a7bSCy Schubert 		*e++ = '.';
983*be771a7bSCy Schubert 	}
984*be771a7bSCy Schubert 	*e++ = '&';
985*be771a7bSCy Schubert 	*e = '\0';
986*be771a7bSCy Schubert 	unit_assert( strcmp(expect, result) == 0 );
987*be771a7bSCy Schubert 
988*be771a7bSCy Schubert 	/* Only alphas, numericals and '-', '_' and '*' are allowed in the output */
989*be771a7bSCy Schubert 	for(i=1; i<=255; i++) {
990*be771a7bSCy Schubert 		if(isalnum(i) || i == '-' || i == '_' || i == '*')
991*be771a7bSCy Schubert 			continue;
992*be771a7bSCy Schubert 		sldns_buffer_clear(buff);
993*be771a7bSCy Schubert 		sldns_buffer_write_u8(buff, 1);
994*be771a7bSCy Schubert 		sldns_buffer_write_u8(buff, (uint8_t)i);
995*be771a7bSCy Schubert 		sldns_buffer_write_u8(buff, 0);
996*be771a7bSCy Schubert 		sldns_buffer_flip(buff);
997*be771a7bSCy Schubert 		unit_assert( sldns_buffer_limit(buff) == 3 );
998*be771a7bSCy Schubert 		unit_assert( pkt_dname_len(buff) == 3);
999*be771a7bSCy Schubert 		dname_str(sldns_buffer_begin(buff), result);
1000*be771a7bSCy Schubert 		if(strcmp( "?.", result) != 0 ) {
1001*be771a7bSCy Schubert 			log_err("ASCII value '0x%lX' allowed in string output", (unsigned long)i);
1002*be771a7bSCy Schubert 			unit_assert(0);
1003*be771a7bSCy Schubert 		}
1004*be771a7bSCy Schubert 	}
1005*be771a7bSCy Schubert }
1006*be771a7bSCy Schubert 
1007*be771a7bSCy Schubert void dname_test(void)
1008*be771a7bSCy Schubert {
1009*be771a7bSCy Schubert 	sldns_buffer* loopbuf = sldns_buffer_new(14);
1010*be771a7bSCy Schubert 	sldns_buffer* boundbuf = sldns_buffer_new(16);
1011*be771a7bSCy Schubert 	sldns_buffer* buff = sldns_buffer_new(65800);
1012*be771a7bSCy Schubert 	unit_assert(loopbuf && boundbuf && buff);
1013*be771a7bSCy Schubert 	sldns_buffer_flip(buff);
1014*be771a7bSCy Schubert 	dname_setup_bufs(loopbuf, boundbuf);
1015*be771a7bSCy Schubert 	dname_test_qdl(buff);
1016*be771a7bSCy Schubert 	dname_test_qdtl(buff);
1017*be771a7bSCy Schubert 	dname_test_pdtl(loopbuf, boundbuf);
1018*be771a7bSCy Schubert 	dname_test_query_dname_compare();
1019*be771a7bSCy Schubert 	dname_test_count_labels();
1020*be771a7bSCy Schubert 	dname_test_count_size_labels();
1021*be771a7bSCy Schubert 	dname_test_dname_lab_cmp();
1022*be771a7bSCy Schubert 	dname_test_pkt_dname_len(buff);
1023*be771a7bSCy Schubert 	dname_test_strict_subdomain();
1024*be771a7bSCy Schubert 	dname_test_subdomain();
1025*be771a7bSCy Schubert 	dname_test_isroot();
1026*be771a7bSCy Schubert 	dname_test_removelabel();
1027*be771a7bSCy Schubert 	dname_test_sigcount();
1028*be771a7bSCy Schubert 	dname_test_iswild();
1029*be771a7bSCy Schubert 	dname_test_canoncmp();
1030*be771a7bSCy Schubert 	dname_test_topdomain();
1031*be771a7bSCy Schubert 	dname_test_valid();
1032*be771a7bSCy Schubert 	dname_test_has_label();
1033*be771a7bSCy Schubert 	dname_test_str(buff);
1034*be771a7bSCy Schubert 	sldns_buffer_free(buff);
1035*be771a7bSCy Schubert 	sldns_buffer_free(loopbuf);
1036*be771a7bSCy Schubert 	sldns_buffer_free(boundbuf);
1037*be771a7bSCy Schubert }
1038