xref: /openbsd-src/usr.sbin/unbound/testcode/unitdname.c (revision d5d2b63dc0f2a73f863461bee27367e62ff8957a)
1712b2f30Ssthen /*
2712b2f30Ssthen  * testcode/unitdname.c - unit test for dname routines.
3712b2f30Ssthen  *
4712b2f30Ssthen  * Copyright (c) 2007, 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 dname 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 "util/data/dname.h"
45712b2f30Ssthen #include "sldns/sbuffer.h"
46712b2f30Ssthen #include "sldns/str2wire.h"
47712b2f30Ssthen 
48712b2f30Ssthen /** put dname into buffer */
49712b2f30Ssthen static sldns_buffer*
dname_to_buf(sldns_buffer * b,const char * str)50712b2f30Ssthen dname_to_buf(sldns_buffer* b, const char* str)
51712b2f30Ssthen {
52712b2f30Ssthen 	int e;
53712b2f30Ssthen 	size_t len = sldns_buffer_capacity(b);
54712b2f30Ssthen 	sldns_buffer_clear(b);
55712b2f30Ssthen 	e = sldns_str2wire_dname_buf(str, sldns_buffer_begin(b), &len);
56712b2f30Ssthen 	if(e != 0)
57712b2f30Ssthen 		fatal_exit("%s ldns: %s", __func__,
58712b2f30Ssthen 			sldns_get_errorstr_parse(e));
59712b2f30Ssthen 	sldns_buffer_set_position(b, len);
60712b2f30Ssthen 	sldns_buffer_flip(b);
61712b2f30Ssthen 	return b;
62712b2f30Ssthen }
63712b2f30Ssthen 
64712b2f30Ssthen /** test query_dname_len function */
65712b2f30Ssthen static void
dname_test_qdl(sldns_buffer * buff)66712b2f30Ssthen dname_test_qdl(sldns_buffer* buff)
67712b2f30Ssthen {
68712b2f30Ssthen 	unit_show_func("util/data/dname.c", "query_dname_len");
69712b2f30Ssthen 	unit_assert( query_dname_len(buff) == 0);
70712b2f30Ssthen 	unit_assert( query_dname_len(dname_to_buf(buff, ".")) == 1 );
71712b2f30Ssthen 	unit_assert( query_dname_len(dname_to_buf(buff, "bla.foo.")) == 9 );
72712b2f30Ssthen 	unit_assert( query_dname_len(dname_to_buf(buff, "x.y.z.example.com."
73712b2f30Ssthen 		)) == 19 );
74712b2f30Ssthen }
75712b2f30Ssthen 
76712b2f30Ssthen /** test query_dname_tolower */
77712b2f30Ssthen static void
dname_test_qdtl(sldns_buffer * buff)78712b2f30Ssthen dname_test_qdtl(sldns_buffer* buff)
79712b2f30Ssthen {
80712b2f30Ssthen 	unit_show_func("util/data/dname.c", "query_dname_tolower");
81712b2f30Ssthen 	sldns_buffer_write_at(buff, 0, "\012abCDeaBCde\003cOm\000", 16);
82712b2f30Ssthen 	query_dname_tolower(sldns_buffer_begin(buff));
83712b2f30Ssthen 	unit_assert( memcmp(sldns_buffer_begin(buff),
84712b2f30Ssthen 		"\012abcdeabcde\003com\000", 16) == 0);
85712b2f30Ssthen 
86712b2f30Ssthen 	sldns_buffer_write_at(buff, 0, "\001+\012abC{e-ZYXe\003NET\000", 18);
87712b2f30Ssthen 	query_dname_tolower(sldns_buffer_begin(buff));
88712b2f30Ssthen 	unit_assert( memcmp(sldns_buffer_begin(buff),
89712b2f30Ssthen 		"\001+\012abc{e-zyxe\003net\000", 18) == 0);
90712b2f30Ssthen 
91712b2f30Ssthen 	sldns_buffer_write_at(buff, 0, "\000", 1);
92712b2f30Ssthen 	query_dname_tolower(sldns_buffer_begin(buff));
93712b2f30Ssthen 	unit_assert( memcmp(sldns_buffer_begin(buff), "\000", 1) == 0);
94712b2f30Ssthen 
95712b2f30Ssthen 	sldns_buffer_write_at(buff, 0, "\002NL\000", 4);
96712b2f30Ssthen 	query_dname_tolower(sldns_buffer_begin(buff));
97712b2f30Ssthen 	unit_assert( memcmp(sldns_buffer_begin(buff), "\002nl\000", 4) == 0);
98712b2f30Ssthen }
99712b2f30Ssthen 
100712b2f30Ssthen /** test query_dname_compare */
101712b2f30Ssthen static void
dname_test_query_dname_compare(void)102712b2f30Ssthen dname_test_query_dname_compare(void)
103712b2f30Ssthen {
104712b2f30Ssthen 	unit_show_func("util/data/dname.c", "query_dname_compare");
105712b2f30Ssthen 	unit_assert(query_dname_compare((uint8_t*)"", (uint8_t*)"") == 0);
106712b2f30Ssthen 	unit_assert(query_dname_compare((uint8_t*)"\001a",
107712b2f30Ssthen 					(uint8_t*)"\001a") == 0);
108712b2f30Ssthen 	unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
109712b2f30Ssthen 					(uint8_t*)"\003abc\001a") == 0);
110712b2f30Ssthen 	unit_assert(query_dname_compare((uint8_t*)"\003aBc\001a",
111712b2f30Ssthen 					(uint8_t*)"\003AbC\001A") == 0);
112712b2f30Ssthen 	unit_assert(query_dname_compare((uint8_t*)"\003abc",
113712b2f30Ssthen 					(uint8_t*)"\003abc\001a") == -1);
114712b2f30Ssthen 	unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
115712b2f30Ssthen 					(uint8_t*)"\003abc") == +1);
116712b2f30Ssthen 	unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
117712b2f30Ssthen 					(uint8_t*)"") == +1);
118712b2f30Ssthen 	unit_assert(query_dname_compare((uint8_t*)"",
119712b2f30Ssthen 					(uint8_t*)"\003abc\001a") == -1);
120712b2f30Ssthen 	unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
121712b2f30Ssthen 					(uint8_t*)"\003xxx\001a") == -1);
122712b2f30Ssthen 	unit_assert(query_dname_compare((uint8_t*)"\003axx\001a",
123712b2f30Ssthen 					(uint8_t*)"\003abc\001a") == 1);
124712b2f30Ssthen 	unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
125712b2f30Ssthen 					(uint8_t*)"\003abc\001Z") == -1);
126712b2f30Ssthen 	unit_assert(query_dname_compare((uint8_t*)"\003abc\001Z",
127712b2f30Ssthen 					(uint8_t*)"\003abc\001a") == 1);
128712b2f30Ssthen }
129712b2f30Ssthen 
130712b2f30Ssthen /** test dname_count_labels */
131712b2f30Ssthen static void
dname_test_count_labels(void)132712b2f30Ssthen dname_test_count_labels(void)
133712b2f30Ssthen {
134712b2f30Ssthen 	unit_show_func("util/data/dname.c", "dname_count_labels");
135712b2f30Ssthen 	unit_assert(dname_count_labels((uint8_t*)"") == 1);
136712b2f30Ssthen 	unit_assert(dname_count_labels((uint8_t*)"\003com") == 2);
137712b2f30Ssthen 	unit_assert(dname_count_labels((uint8_t*)"\003org") == 2);
138712b2f30Ssthen 	unit_assert(dname_count_labels((uint8_t*)"\007example\003com") == 3);
139712b2f30Ssthen 	unit_assert(dname_count_labels((uint8_t*)"\003bla\007example\003com")
140712b2f30Ssthen 		== 4);
141712b2f30Ssthen }
142712b2f30Ssthen 
143712b2f30Ssthen /** test dname_count_size_labels */
144712b2f30Ssthen static void
dname_test_count_size_labels(void)145712b2f30Ssthen dname_test_count_size_labels(void)
146712b2f30Ssthen {
147712b2f30Ssthen 	size_t sz = 0;
148712b2f30Ssthen 	unit_show_func("util/data/dname.c", "dname_count_size_labels");
149712b2f30Ssthen 	unit_assert(dname_count_size_labels((uint8_t*)"", &sz) == 1);
150712b2f30Ssthen 	unit_assert(sz == 1);
151712b2f30Ssthen 	unit_assert(dname_count_size_labels((uint8_t*)"\003com", &sz) == 2);
152712b2f30Ssthen 	unit_assert(sz == 5);
153712b2f30Ssthen 	unit_assert(dname_count_size_labels((uint8_t*)"\003org", &sz) == 2);
154712b2f30Ssthen 	unit_assert(sz == 5);
155712b2f30Ssthen 	unit_assert(dname_count_size_labels((uint8_t*)"\007example\003com",
156712b2f30Ssthen 		&sz) == 3);
157712b2f30Ssthen 	unit_assert(sz == 13);
158712b2f30Ssthen 	unit_assert(dname_count_size_labels((uint8_t*)"\003bla\007example"
159712b2f30Ssthen 		"\003com", &sz) == 4);
160712b2f30Ssthen 	unit_assert(sz == 17);
161712b2f30Ssthen }
162712b2f30Ssthen 
163712b2f30Ssthen 
164712b2f30Ssthen /** test pkt_dname_len */
165712b2f30Ssthen static void
dname_test_pkt_dname_len(sldns_buffer * buff)166712b2f30Ssthen dname_test_pkt_dname_len(sldns_buffer* buff)
167712b2f30Ssthen {
168712b2f30Ssthen 	unit_show_func("util/data/dname.c", "pkt_dname_len");
169712b2f30Ssthen 	sldns_buffer_clear(buff);
170712b2f30Ssthen 	sldns_buffer_write(buff, "\000", 1);
171712b2f30Ssthen 	sldns_buffer_flip(buff);
172712b2f30Ssthen 	unit_assert( pkt_dname_len(buff) == 1 );
173712b2f30Ssthen 	unit_assert( sldns_buffer_position(buff) == 1);
174712b2f30Ssthen 
175712b2f30Ssthen 	sldns_buffer_clear(buff);
176712b2f30Ssthen 	sldns_buffer_write(buff, "\003org\000", 5);
177712b2f30Ssthen 	sldns_buffer_flip(buff);
178712b2f30Ssthen 	unit_assert( pkt_dname_len(buff) == 5 );
179712b2f30Ssthen 	unit_assert( sldns_buffer_position(buff) == 5);
180712b2f30Ssthen 
181712b2f30Ssthen 	sldns_buffer_clear(buff);
182712b2f30Ssthen 	sldns_buffer_write(buff, "\002os\007example\003org\000", 16);
183712b2f30Ssthen 	sldns_buffer_flip(buff);
184712b2f30Ssthen 	unit_assert( pkt_dname_len(buff) == 16 );
185712b2f30Ssthen 	unit_assert( sldns_buffer_position(buff) == 16);
186712b2f30Ssthen 
187712b2f30Ssthen 	/* invalid compression pointer: to self */
188712b2f30Ssthen 	sldns_buffer_clear(buff);
189712b2f30Ssthen 	sldns_buffer_write(buff, "\300\000os\007example\003org\000", 17);
190712b2f30Ssthen 	sldns_buffer_flip(buff);
191712b2f30Ssthen 	unit_assert( pkt_dname_len(buff) == 0 );
192712b2f30Ssthen 
193712b2f30Ssthen 	/* valid compression pointer */
194712b2f30Ssthen 	sldns_buffer_clear(buff);
195712b2f30Ssthen 	sldns_buffer_write(buff, "\003com\000\040\300\000", 8);
196712b2f30Ssthen 	sldns_buffer_flip(buff);
197712b2f30Ssthen 	sldns_buffer_set_position(buff, 6);
198712b2f30Ssthen 	unit_assert( pkt_dname_len(buff) == 5 );
199712b2f30Ssthen 	unit_assert( sldns_buffer_position(buff) == 8);
200712b2f30Ssthen 
201712b2f30Ssthen 	/* unknown label type */
202712b2f30Ssthen 	sldns_buffer_clear(buff);
203712b2f30Ssthen 	sldns_buffer_write(buff, "\002os\107example\003org\000", 16);
204712b2f30Ssthen 	sldns_buffer_flip(buff);
205712b2f30Ssthen 	unit_assert( pkt_dname_len(buff) == 0 );
206712b2f30Ssthen 
207712b2f30Ssthen 	/* label too long */
208712b2f30Ssthen 	sldns_buffer_clear(buff);
209712b2f30Ssthen 	sldns_buffer_write(buff, "\002os\047example\003org\000", 16);
210712b2f30Ssthen 	sldns_buffer_flip(buff);
211712b2f30Ssthen 	unit_assert( pkt_dname_len(buff) == 0 );
212712b2f30Ssthen 
213712b2f30Ssthen 	/* label exceeds packet */
214712b2f30Ssthen 	sldns_buffer_clear(buff);
215712b2f30Ssthen 	sldns_buffer_write(buff, "\002os\007example\007org\004", 16);
216712b2f30Ssthen 	sldns_buffer_flip(buff);
217712b2f30Ssthen 	unit_assert( pkt_dname_len(buff) == 0 );
218712b2f30Ssthen 
219712b2f30Ssthen 	/* name very long */
220712b2f30Ssthen 	sldns_buffer_clear(buff);
221712b2f30Ssthen 	sldns_buffer_write(buff,
222712b2f30Ssthen 		"\020a1cdef5555544444"
223712b2f30Ssthen 		"\020a2cdef5555544444"
224712b2f30Ssthen 		"\020a3cdef5555544444"
225712b2f30Ssthen 		"\020a4cdef5555544444"
226712b2f30Ssthen 		"\020a5cdef5555544444"
227712b2f30Ssthen 		"\020a6cdef5555544444"
228712b2f30Ssthen 		"\020a7cdef5555544444"
229712b2f30Ssthen 		"\020a8cdef5555544444"
230712b2f30Ssthen 		"\020a9cdef5555544444"
231712b2f30Ssthen 		"\020aAcdef5555544444"
232712b2f30Ssthen 		"\020aBcdef5555544444"
233712b2f30Ssthen 		"\020aCcdef5555544444"
234712b2f30Ssthen 		"\020aDcdef5555544444"
235712b2f30Ssthen 		"\020aEcdef5555544444"	/* 238 up to here */
236712b2f30Ssthen 		"\007aabbccd"		/* 246 up to here */
237712b2f30Ssthen 		"\007example\000"	/* 255 to here */
238712b2f30Ssthen 		, 255);
239712b2f30Ssthen 	sldns_buffer_flip(buff);
240712b2f30Ssthen 	unit_assert( pkt_dname_len(buff) == 255 );
241712b2f30Ssthen 	unit_assert( sldns_buffer_position(buff) == 255);
242712b2f30Ssthen 
243712b2f30Ssthen 	/* name too long */
244712b2f30Ssthen 	sldns_buffer_clear(buff);
245712b2f30Ssthen 	sldns_buffer_write(buff,
246712b2f30Ssthen 		"\020a1cdef5555544444"
247712b2f30Ssthen 		"\020a2cdef5555544444"
248712b2f30Ssthen 		"\020a3cdef5555544444"
249712b2f30Ssthen 		"\020a4cdef5555544444"
250712b2f30Ssthen 		"\020a5cdef5555544444"
251712b2f30Ssthen 		"\020a6cdef5555544444"
252712b2f30Ssthen 		"\020a7cdef5555544444"
253712b2f30Ssthen 		"\020a8cdef5555544444"
254712b2f30Ssthen 		"\020a9cdef5555544444"
255712b2f30Ssthen 		"\020aAcdef5555544444"
256712b2f30Ssthen 		"\020aBcdef5555544444"
257712b2f30Ssthen 		"\020aCcdef5555544444"
258712b2f30Ssthen 		"\020aXcdef5555544444"
259712b2f30Ssthen 		"\020aXcdef5555544444"
260712b2f30Ssthen 		"\020aXcdef5555544444"
261712b2f30Ssthen 		"\020aDcdef5555544444"
262712b2f30Ssthen 		"\020aEcdef5555544444"	/* 238 up to here */
263712b2f30Ssthen 		"\007aabbccd"		/* 246 up to here */
264712b2f30Ssthen 		"\007example\000"	/* 255 to here */
265712b2f30Ssthen 		, 255);
266712b2f30Ssthen 	sldns_buffer_flip(buff);
267712b2f30Ssthen 	unit_assert( pkt_dname_len(buff) == 0 );
268712b2f30Ssthen }
269712b2f30Ssthen 
270712b2f30Ssthen /** test dname_lab_cmp */
271712b2f30Ssthen static void
dname_test_dname_lab_cmp(void)272712b2f30Ssthen dname_test_dname_lab_cmp(void)
273712b2f30Ssthen {
274712b2f30Ssthen 	int ml = 0; /* number of labels that matched exactly */
275712b2f30Ssthen 	unit_show_func("util/data/dname.c", "dname_lab_cmp");
276712b2f30Ssthen 
277712b2f30Ssthen 	/* test for equality succeeds */
278712b2f30Ssthen 	unit_assert(dname_lab_cmp((uint8_t*)"", 1, (uint8_t*)"", 1, &ml) == 0);
279712b2f30Ssthen 	unit_assert(ml == 1);
280712b2f30Ssthen 	unit_assert(dname_lab_cmp(
281712b2f30Ssthen 		(uint8_t*)"\003net", 2,
282712b2f30Ssthen 		(uint8_t*)"\003net", 2,
283712b2f30Ssthen 		&ml) == 0);
284712b2f30Ssthen 	unit_assert(ml == 2);
285712b2f30Ssthen 	unit_assert(dname_lab_cmp(
286712b2f30Ssthen 		(uint8_t*)"\007example\003net", 3,
287712b2f30Ssthen 		(uint8_t*)"\007example\003net", 3,
288712b2f30Ssthen 		&ml) == 0);
289712b2f30Ssthen 	unit_assert(ml == 3);
290712b2f30Ssthen 	unit_assert(dname_lab_cmp(
291712b2f30Ssthen 		(uint8_t*)"\004test\007example\003net", 4,
292712b2f30Ssthen 		(uint8_t*)"\004test\007example\003net", 4,
293712b2f30Ssthen 		&ml) == 0);
294712b2f30Ssthen 	unit_assert(ml == 4);
295712b2f30Ssthen 
296712b2f30Ssthen 	/* root is smaller than anything else */
297712b2f30Ssthen 	unit_assert(dname_lab_cmp(
298712b2f30Ssthen 		(uint8_t*)"", 1,
299712b2f30Ssthen 		(uint8_t*)"\003net", 2,
300712b2f30Ssthen 		&ml) == -1);
301712b2f30Ssthen 	unit_assert(ml == 1);
302712b2f30Ssthen 	unit_assert(dname_lab_cmp(
303712b2f30Ssthen 		(uint8_t*)"\003net", 2,
304712b2f30Ssthen 		(uint8_t*)"", 1,
305712b2f30Ssthen 		&ml) == 1);
306712b2f30Ssthen 	unit_assert(ml == 1);
307712b2f30Ssthen 	unit_assert(dname_lab_cmp(
308712b2f30Ssthen 		(uint8_t*)"", 1,
309712b2f30Ssthen 		(uint8_t*)"\007example\003net", 3,
310712b2f30Ssthen 		&ml) == -1);
311712b2f30Ssthen 	unit_assert(ml == 1);
312712b2f30Ssthen 	unit_assert(dname_lab_cmp(
313712b2f30Ssthen 		(uint8_t*)"\007example\003net", 3,
314712b2f30Ssthen 		(uint8_t*)"", 1,
315712b2f30Ssthen 		&ml) == 1);
316712b2f30Ssthen 	unit_assert(ml == 1);
317712b2f30Ssthen 
318712b2f30Ssthen 	/* label length makes a difference */
319712b2f30Ssthen 	unit_assert(dname_lab_cmp(
320712b2f30Ssthen 		(uint8_t*)"\004neta", 2,
321712b2f30Ssthen 		(uint8_t*)"\003net", 2,
322712b2f30Ssthen 		&ml) != 0);
323712b2f30Ssthen 	unit_assert(ml == 1);
324712b2f30Ssthen 	unit_assert(dname_lab_cmp(
325712b2f30Ssthen 		(uint8_t*)"\002ne", 2,
326712b2f30Ssthen 		(uint8_t*)"\004neta", 2,
327712b2f30Ssthen 		&ml) != 0);
328712b2f30Ssthen 	unit_assert(ml == 1);
329712b2f30Ssthen 
330712b2f30Ssthen 	/* contents follow the zone apex */
331712b2f30Ssthen 	unit_assert(dname_lab_cmp(
332712b2f30Ssthen 		(uint8_t*)"\003bla\007example\003net", 4,
333712b2f30Ssthen 		(uint8_t*)"\007example\003net", 3,
334712b2f30Ssthen 		&ml) == 1);
335712b2f30Ssthen 	unit_assert(ml == 3);
336712b2f30Ssthen 	unit_assert(dname_lab_cmp(
337712b2f30Ssthen 		(uint8_t*)"\007example\003net", 3,
338712b2f30Ssthen 		(uint8_t*)"\003bla\007example\003net", 4,
339712b2f30Ssthen 		&ml) == -1);
340712b2f30Ssthen 	unit_assert(ml == 3);
341712b2f30Ssthen 
342712b2f30Ssthen 	/* label content makes a difference */
343712b2f30Ssthen 	unit_assert(dname_lab_cmp(
344712b2f30Ssthen 		(uint8_t*)"\003aag\007example\003net", 4,
345712b2f30Ssthen 		(uint8_t*)"\003bla\007example\003net", 4,
346712b2f30Ssthen 		&ml) == -1);
347712b2f30Ssthen 	unit_assert(ml == 3);
348712b2f30Ssthen 	unit_assert(dname_lab_cmp(
349712b2f30Ssthen 		(uint8_t*)"\003aag\007example\003net", 4,
350712b2f30Ssthen 		(uint8_t*)"\003bla\007example\003net", 4,
351712b2f30Ssthen 		&ml) == -1);
352712b2f30Ssthen 	unit_assert(ml == 3);
353712b2f30Ssthen 	unit_assert(dname_lab_cmp(
354712b2f30Ssthen 		(uint8_t*)"\003bla\003aag\007example\003net", 5,
355712b2f30Ssthen 		(uint8_t*)"\003aag\003bla\007example\003net", 5,
356712b2f30Ssthen 		&ml) == -1);
357712b2f30Ssthen 	unit_assert(ml == 3);
358712b2f30Ssthen 	unit_assert(dname_lab_cmp(
359712b2f30Ssthen 		(uint8_t*)"\02sn\003opt\003aag\007example\003net", 6,
360712b2f30Ssthen 		(uint8_t*)"\02sn\003opt\003bla\007example\003net", 6,
361712b2f30Ssthen 		&ml) == -1);
362712b2f30Ssthen 	unit_assert(ml == 3);
363712b2f30Ssthen 
364712b2f30Ssthen 	/* but lowercase/uppercase does not make a difference. */
365712b2f30Ssthen 	unit_assert(dname_lab_cmp(
366712b2f30Ssthen 		(uint8_t*)"\003bLa\007examPLe\003net", 4,
367712b2f30Ssthen 		(uint8_t*)"\003bla\007eXAmple\003nET", 4,
368712b2f30Ssthen 		&ml) == 0);
369712b2f30Ssthen 	unit_assert(ml == 4);
370712b2f30Ssthen }
371712b2f30Ssthen 
372712b2f30Ssthen /** test dname_subdomain_c */
373712b2f30Ssthen static void
dname_test_subdomain(void)374712b2f30Ssthen dname_test_subdomain(void)
375712b2f30Ssthen {
376712b2f30Ssthen 	unit_show_func("util/data/dname.c", "dname_subdomain");
377712b2f30Ssthen 	unit_assert(dname_subdomain_c(
378712b2f30Ssthen 		(uint8_t*)"",
379712b2f30Ssthen 		(uint8_t*)""));
380712b2f30Ssthen 	unit_assert(dname_subdomain_c(
381712b2f30Ssthen 		(uint8_t*)"\003com",
382712b2f30Ssthen 		(uint8_t*)""));
383712b2f30Ssthen 	unit_assert(!dname_subdomain_c(
384712b2f30Ssthen 		(uint8_t*)"",
385712b2f30Ssthen 		(uint8_t*)"\003com"));
386712b2f30Ssthen 	unit_assert(dname_subdomain_c(
387712b2f30Ssthen 		(uint8_t*)"\007example\003com",
388712b2f30Ssthen 		(uint8_t*)"\003com"));
389712b2f30Ssthen 	unit_assert(!dname_subdomain_c(
390712b2f30Ssthen 		(uint8_t*)"\003com",
391712b2f30Ssthen 		(uint8_t*)"\007example\003com"));
392712b2f30Ssthen 	unit_assert(dname_subdomain_c(
393712b2f30Ssthen 		(uint8_t*)"\007example\003com",
394712b2f30Ssthen 		(uint8_t*)""));
395712b2f30Ssthen 	unit_assert(!dname_subdomain_c(
396712b2f30Ssthen 		(uint8_t*)"\003net",
397712b2f30Ssthen 		(uint8_t*)"\003com"));
398712b2f30Ssthen 	unit_assert(!dname_subdomain_c(
399712b2f30Ssthen 		(uint8_t*)"\003net",
400712b2f30Ssthen 		(uint8_t*)"\003org"));
401712b2f30Ssthen 	unit_assert(!dname_subdomain_c(
402712b2f30Ssthen 		(uint8_t*)"\007example\003net",
403712b2f30Ssthen 		(uint8_t*)"\003org"));
404712b2f30Ssthen 	unit_assert(!dname_subdomain_c(
405712b2f30Ssthen 		(uint8_t*)"\003net",
406712b2f30Ssthen 		(uint8_t*)"\007example\003org"));
407712b2f30Ssthen }
408712b2f30Ssthen 
409712b2f30Ssthen /** test dname_strict_subdomain */
410712b2f30Ssthen static void
dname_test_strict_subdomain(void)411712b2f30Ssthen dname_test_strict_subdomain(void)
412712b2f30Ssthen {
413712b2f30Ssthen 	unit_show_func("util/data/dname.c", "dname_strict_subdomain");
414712b2f30Ssthen 	unit_assert(!dname_strict_subdomain(
415712b2f30Ssthen 		(uint8_t*)"", 1,
416712b2f30Ssthen 		(uint8_t*)"", 1));
417712b2f30Ssthen 	unit_assert(dname_strict_subdomain(
418712b2f30Ssthen 		(uint8_t*)"\003com", 2,
419712b2f30Ssthen 		(uint8_t*)"", 1));
420712b2f30Ssthen 	unit_assert(!dname_strict_subdomain(
421712b2f30Ssthen 		(uint8_t*)"", 1,
422712b2f30Ssthen 		(uint8_t*)"\003com", 2));
423712b2f30Ssthen 	unit_assert(dname_strict_subdomain(
424712b2f30Ssthen 		(uint8_t*)"\007example\003com", 3,
425712b2f30Ssthen 		(uint8_t*)"\003com", 2));
426712b2f30Ssthen 	unit_assert(!dname_strict_subdomain(
427712b2f30Ssthen 		(uint8_t*)"\003com", 2,
428712b2f30Ssthen 		(uint8_t*)"\007example\003com", 3));
429712b2f30Ssthen 	unit_assert(dname_strict_subdomain(
430712b2f30Ssthen 		(uint8_t*)"\007example\003com", 3,
431712b2f30Ssthen 		(uint8_t*)"", 1));
432712b2f30Ssthen 	unit_assert(!dname_strict_subdomain(
433712b2f30Ssthen 		(uint8_t*)"\003net", 2,
434712b2f30Ssthen 		(uint8_t*)"\003com", 2));
435712b2f30Ssthen 	unit_assert(!dname_strict_subdomain(
436712b2f30Ssthen 		(uint8_t*)"\003net", 2,
437712b2f30Ssthen 		(uint8_t*)"\003org", 2));
438712b2f30Ssthen 	unit_assert(!dname_strict_subdomain(
439712b2f30Ssthen 		(uint8_t*)"\007example\003net", 3,
440712b2f30Ssthen 		(uint8_t*)"\003org", 2));
441712b2f30Ssthen 	unit_assert(!dname_strict_subdomain(
442712b2f30Ssthen 		(uint8_t*)"\003net", 2,
443712b2f30Ssthen 		(uint8_t*)"\007example\003org", 3));
444712b2f30Ssthen }
445712b2f30Ssthen 
446712b2f30Ssthen /** test dname_is_root */
447712b2f30Ssthen static void
dname_test_isroot(void)448712b2f30Ssthen dname_test_isroot(void)
449712b2f30Ssthen {
450712b2f30Ssthen 	unit_show_func("util/data/dname.c", "dname_isroot");
451712b2f30Ssthen 	unit_assert(dname_is_root((uint8_t*)"\000"));
452712b2f30Ssthen 	unit_assert(!dname_is_root((uint8_t*)"\001a\000"));
453712b2f30Ssthen 	unit_assert(!dname_is_root((uint8_t*)"\005abvcd\003com\000"));
454712b2f30Ssthen 	/* malformed dname in this test, but should work */
455712b2f30Ssthen 	unit_assert(!dname_is_root((uint8_t*)"\077a\000"));
456712b2f30Ssthen 	unit_assert(dname_is_root((uint8_t*)"\000"));
457712b2f30Ssthen }
458712b2f30Ssthen 
459712b2f30Ssthen /** test dname_remove_label */
460712b2f30Ssthen static void
dname_test_removelabel(void)461712b2f30Ssthen dname_test_removelabel(void)
462712b2f30Ssthen {
463712b2f30Ssthen 	uint8_t* orig = (uint8_t*)"\007example\003com\000";
464712b2f30Ssthen 	uint8_t* n = orig;
465712b2f30Ssthen 	size_t l = 13;
466712b2f30Ssthen 	unit_show_func("util/data/dname.c", "dname_remove_label");
467712b2f30Ssthen 	dname_remove_label(&n, &l);
468712b2f30Ssthen 	unit_assert( n == orig+8 );
469712b2f30Ssthen 	unit_assert( l == 5 );
470712b2f30Ssthen 	dname_remove_label(&n, &l);
471712b2f30Ssthen 	unit_assert( n == orig+12 );
472712b2f30Ssthen 	unit_assert( l == 1 );
473712b2f30Ssthen 	dname_remove_label(&n, &l);
474712b2f30Ssthen 	unit_assert( n == orig+12 );
475712b2f30Ssthen 	unit_assert( l == 1 );
476712b2f30Ssthen }
477712b2f30Ssthen 
478712b2f30Ssthen /** test dname_signame_label_count */
479712b2f30Ssthen static void
dname_test_sigcount(void)480712b2f30Ssthen dname_test_sigcount(void)
481712b2f30Ssthen {
482712b2f30Ssthen 	unit_show_func("util/data/dname.c", "dname_signame_label_count");
483712b2f30Ssthen 	unit_assert(dname_signame_label_count((uint8_t*)"\000") == 0);
484712b2f30Ssthen 	unit_assert(dname_signame_label_count((uint8_t*)"\001*\000") == 0);
485712b2f30Ssthen 	unit_assert(dname_signame_label_count((uint8_t*)"\003xom\000") == 1);
486712b2f30Ssthen 	unit_assert(dname_signame_label_count(
487712b2f30Ssthen 		(uint8_t*)"\001*\003xom\000") == 1);
488712b2f30Ssthen 	unit_assert(dname_signame_label_count(
489712b2f30Ssthen 		(uint8_t*)"\007example\003xom\000") == 2);
490712b2f30Ssthen 	unit_assert(dname_signame_label_count(
491712b2f30Ssthen 		(uint8_t*)"\001*\007example\003xom\000") == 2);
492712b2f30Ssthen 	unit_assert(dname_signame_label_count(
493712b2f30Ssthen 		(uint8_t*)"\003www\007example\003xom\000") == 3);
494712b2f30Ssthen 	unit_assert(dname_signame_label_count(
495712b2f30Ssthen 		(uint8_t*)"\001*\003www\007example\003xom\000") == 3);
496712b2f30Ssthen }
497712b2f30Ssthen 
498712b2f30Ssthen /** test dname_is_wild routine */
499712b2f30Ssthen static void
dname_test_iswild(void)500712b2f30Ssthen dname_test_iswild(void)
501712b2f30Ssthen {
502712b2f30Ssthen 	unit_show_func("util/data/dname.c", "dname_iswild");
503712b2f30Ssthen 	unit_assert( !dname_is_wild((uint8_t*)"\000") );
504712b2f30Ssthen 	unit_assert( dname_is_wild((uint8_t*)"\001*\000") );
505712b2f30Ssthen 	unit_assert( !dname_is_wild((uint8_t*)"\003net\000") );
506712b2f30Ssthen 	unit_assert( dname_is_wild((uint8_t*)"\001*\003net\000") );
507712b2f30Ssthen }
508712b2f30Ssthen 
509712b2f30Ssthen /** test dname_canonical_compare */
510712b2f30Ssthen static void
dname_test_canoncmp(void)511712b2f30Ssthen dname_test_canoncmp(void)
512712b2f30Ssthen {
513712b2f30Ssthen 	unit_show_func("util/data/dname.c", "dname_canonical_compare");
514712b2f30Ssthen 	/* equality */
515712b2f30Ssthen 	unit_assert( dname_canonical_compare(
516712b2f30Ssthen 		(uint8_t*)"\000",
517712b2f30Ssthen 		(uint8_t*)"\000"
518712b2f30Ssthen 		) == 0);
519712b2f30Ssthen 	unit_assert( dname_canonical_compare(
520712b2f30Ssthen 		(uint8_t*)"\003net\000",
521712b2f30Ssthen 		(uint8_t*)"\003net\000"
522712b2f30Ssthen 		) == 0);
523712b2f30Ssthen 	unit_assert( dname_canonical_compare(
524712b2f30Ssthen 		(uint8_t*)"\007example\003net\000",
525712b2f30Ssthen 		(uint8_t*)"\007example\003net\000"
526712b2f30Ssthen 		) == 0);
527712b2f30Ssthen 	unit_assert( dname_canonical_compare(
528712b2f30Ssthen 		(uint8_t*)"\004test\007example\003net\000",
529712b2f30Ssthen 		(uint8_t*)"\004test\007example\003net\000"
530712b2f30Ssthen 		) == 0);
531712b2f30Ssthen 
532712b2f30Ssthen 	/* subdomains */
533712b2f30Ssthen 	unit_assert( dname_canonical_compare(
534712b2f30Ssthen 		(uint8_t*)"\003com",
535712b2f30Ssthen 		(uint8_t*)"\000"
536712b2f30Ssthen 		) == 1);
537712b2f30Ssthen 	unit_assert( dname_canonical_compare(
538712b2f30Ssthen 		(uint8_t*)"\000",
539712b2f30Ssthen 		(uint8_t*)"\003com"
540712b2f30Ssthen 		) == -1);
541712b2f30Ssthen 	unit_assert( dname_canonical_compare(
542712b2f30Ssthen 		(uint8_t*)"\007example\003com",
543712b2f30Ssthen 		(uint8_t*)"\003com"
544712b2f30Ssthen 		) == 1);
545712b2f30Ssthen 	unit_assert( dname_canonical_compare(
546712b2f30Ssthen 		(uint8_t*)"\003com",
547712b2f30Ssthen 		(uint8_t*)"\007example\003com"
548712b2f30Ssthen 		) == -1);
549712b2f30Ssthen 	unit_assert( dname_canonical_compare(
550712b2f30Ssthen 		(uint8_t*)"\007example\003com",
551712b2f30Ssthen 		(uint8_t*)"\000"
552712b2f30Ssthen 		) == 1);
553712b2f30Ssthen 	unit_assert( dname_canonical_compare(
554712b2f30Ssthen 		(uint8_t*)"\000",
555712b2f30Ssthen 		(uint8_t*)"\007example\003com"
556712b2f30Ssthen 		) == -1);
557712b2f30Ssthen 
558712b2f30Ssthen 	/* compare rightmost label */
559712b2f30Ssthen 	unit_assert( dname_canonical_compare(
560712b2f30Ssthen 		(uint8_t*)"\003com",
561712b2f30Ssthen 		(uint8_t*)"\003net"
562712b2f30Ssthen 		) == -1);
563712b2f30Ssthen 	unit_assert( dname_canonical_compare(
564712b2f30Ssthen 		(uint8_t*)"\003net",
565712b2f30Ssthen 		(uint8_t*)"\003com"
566712b2f30Ssthen 		) == 1);
567712b2f30Ssthen 	unit_assert( dname_canonical_compare(
568712b2f30Ssthen 		(uint8_t*)"\003net",
569712b2f30Ssthen 		(uint8_t*)"\003org"
570712b2f30Ssthen 		) == -1);
571712b2f30Ssthen 	unit_assert( dname_canonical_compare(
572712b2f30Ssthen 		(uint8_t*)"\007example\003net",
573712b2f30Ssthen 		(uint8_t*)"\003org"
574712b2f30Ssthen 		) == -1);
575712b2f30Ssthen 	unit_assert( dname_canonical_compare(
576712b2f30Ssthen 		(uint8_t*)"\003org",
577712b2f30Ssthen 		(uint8_t*)"\007example\003net"
578712b2f30Ssthen 		) == 1);
579712b2f30Ssthen 
580712b2f30Ssthen 	/* label length makes a difference; but only if rest is equal */
581712b2f30Ssthen 	unit_assert( dname_canonical_compare(
582712b2f30Ssthen 		(uint8_t*)"\004neta",
583712b2f30Ssthen 		(uint8_t*)"\003net"
584712b2f30Ssthen 		) == 1);
585712b2f30Ssthen 	unit_assert( dname_canonical_compare(
586712b2f30Ssthen 		(uint8_t*)"\002ne",
587712b2f30Ssthen 		(uint8_t*)"\004neta"
588712b2f30Ssthen 		) == -1);
589712b2f30Ssthen 
590712b2f30Ssthen 	/* label content */
591712b2f30Ssthen 	unit_assert( dname_canonical_compare(
592712b2f30Ssthen 		(uint8_t*)"\003aag\007example\003net",
593712b2f30Ssthen 		(uint8_t*)"\003bla\007example\003net"
594712b2f30Ssthen 		) == -1);
595712b2f30Ssthen 	unit_assert( dname_canonical_compare(
596712b2f30Ssthen 		(uint8_t*)"\003bla\007example\003net",
597712b2f30Ssthen 		(uint8_t*)"\003aag\007example\003net"
598712b2f30Ssthen 		) == 1);
599712b2f30Ssthen 	unit_assert( dname_canonical_compare(
600712b2f30Ssthen 		(uint8_t*)"\003bla\003aag\007example\003net",
601712b2f30Ssthen 		(uint8_t*)"\003aag\003bla\007example\003net"
602712b2f30Ssthen 		) == -1);
603712b2f30Ssthen 	unit_assert( dname_canonical_compare(
604712b2f30Ssthen 		(uint8_t*)"\02sn\003opt\003aag\007example\003net",
605712b2f30Ssthen 		(uint8_t*)"\02sn\003opt\003bla\007example\003net"
606712b2f30Ssthen 		) == -1);
607712b2f30Ssthen 
608712b2f30Ssthen 	/* lowercase during compare */
609712b2f30Ssthen 	unit_assert( dname_canonical_compare(
610712b2f30Ssthen 		(uint8_t*)"\003bLa\007examPLe\003net",
611712b2f30Ssthen 		(uint8_t*)"\003bla\007eXAmple\003nET"
612712b2f30Ssthen 		) == 0);
613712b2f30Ssthen 
614712b2f30Ssthen 	/* example from 4034 */
615712b2f30Ssthen 	/* example a.example yljkjljk.a.example Z.a.example zABC.a.EXAMPLE
616712b2f30Ssthen 	 z.example \001.z.example *.z.example \200.z.example */
617712b2f30Ssthen 	unit_assert( dname_canonical_compare(
618712b2f30Ssthen 		(uint8_t*)"",
619712b2f30Ssthen 		(uint8_t*)"\007example"
620712b2f30Ssthen 		) == -1);
621712b2f30Ssthen 	unit_assert( dname_canonical_compare(
622712b2f30Ssthen 		(uint8_t*)"\007example",
623712b2f30Ssthen 		(uint8_t*)"\001a\007example"
624712b2f30Ssthen 		) == -1);
625712b2f30Ssthen 	unit_assert( dname_canonical_compare(
626712b2f30Ssthen 		(uint8_t*)"\001a\007example",
627712b2f30Ssthen 		(uint8_t*)"\010yljkjljk\001a\007example"
628712b2f30Ssthen 		) == -1);
629712b2f30Ssthen 	unit_assert( dname_canonical_compare(
630712b2f30Ssthen 		(uint8_t*)"\010yljkjljk\001a\007example",
631712b2f30Ssthen 		(uint8_t*)"\001Z\001a\007example"
632712b2f30Ssthen 		) == -1);
633712b2f30Ssthen 	unit_assert( dname_canonical_compare(
634712b2f30Ssthen 		(uint8_t*)"\001Z\001a\007example",
635712b2f30Ssthen 		(uint8_t*)"\004zABC\001a\007EXAMPLE"
636712b2f30Ssthen 		) == -1);
637712b2f30Ssthen 	unit_assert( dname_canonical_compare(
638712b2f30Ssthen 		(uint8_t*)"\004zABC\001a\007EXAMPLE",
639712b2f30Ssthen 		(uint8_t*)"\001z\007example"
640712b2f30Ssthen 		) == -1);
641712b2f30Ssthen 	unit_assert( dname_canonical_compare(
642712b2f30Ssthen 		(uint8_t*)"\001z\007example",
643712b2f30Ssthen 		(uint8_t*)"\001\001\001z\007example"
644712b2f30Ssthen 		) == -1);
645712b2f30Ssthen 	unit_assert( dname_canonical_compare(
646712b2f30Ssthen 		(uint8_t*)"\001\001\001z\007example",
647712b2f30Ssthen 		(uint8_t*)"\001*\001z\007example"
648712b2f30Ssthen 		) == -1);
649712b2f30Ssthen 	unit_assert( dname_canonical_compare(
650712b2f30Ssthen 		(uint8_t*)"\001*\001z\007example",
651712b2f30Ssthen 		(uint8_t*)"\001\200\001z\007example"
652712b2f30Ssthen 		) == -1);
653712b2f30Ssthen 	/* same example in reverse */
654712b2f30Ssthen 	unit_assert( dname_canonical_compare(
655712b2f30Ssthen 		(uint8_t*)"\007example",
656712b2f30Ssthen 		(uint8_t*)""
657712b2f30Ssthen 		) == 1);
658712b2f30Ssthen 	unit_assert( dname_canonical_compare(
659712b2f30Ssthen 		(uint8_t*)"\001a\007example",
660712b2f30Ssthen 		(uint8_t*)"\007example"
661712b2f30Ssthen 		) == 1);
662712b2f30Ssthen 	unit_assert( dname_canonical_compare(
663712b2f30Ssthen 		(uint8_t*)"\010yljkjljk\001a\007example",
664712b2f30Ssthen 		(uint8_t*)"\001a\007example"
665712b2f30Ssthen 		) == 1);
666712b2f30Ssthen 	unit_assert( dname_canonical_compare(
667712b2f30Ssthen 		(uint8_t*)"\001Z\001a\007example",
668712b2f30Ssthen 		(uint8_t*)"\010yljkjljk\001a\007example"
669712b2f30Ssthen 		) == 1);
670712b2f30Ssthen 	unit_assert( dname_canonical_compare(
671712b2f30Ssthen 		(uint8_t*)"\004zABC\001a\007EXAMPLE",
672712b2f30Ssthen 		(uint8_t*)"\001Z\001a\007example"
673712b2f30Ssthen 		) == 1);
674712b2f30Ssthen 	unit_assert( dname_canonical_compare(
675712b2f30Ssthen 		(uint8_t*)"\001z\007example",
676712b2f30Ssthen 		(uint8_t*)"\004zABC\001a\007EXAMPLE"
677712b2f30Ssthen 		) == 1);
678712b2f30Ssthen 	unit_assert( dname_canonical_compare(
679712b2f30Ssthen 		(uint8_t*)"\001\001\001z\007example",
680712b2f30Ssthen 		(uint8_t*)"\001z\007example"
681712b2f30Ssthen 		) == 1);
682712b2f30Ssthen 	unit_assert( dname_canonical_compare(
683712b2f30Ssthen 		(uint8_t*)"\001*\001z\007example",
684712b2f30Ssthen 		(uint8_t*)"\001\001\001z\007example"
685712b2f30Ssthen 		) == 1);
686712b2f30Ssthen 	unit_assert( dname_canonical_compare(
687712b2f30Ssthen 		(uint8_t*)"\001\200\001z\007example",
688712b2f30Ssthen 		(uint8_t*)"\001*\001z\007example"
689712b2f30Ssthen 		) == 1);
690712b2f30Ssthen 	/* same example for equality */
691712b2f30Ssthen 	unit_assert( dname_canonical_compare(
692712b2f30Ssthen 		(uint8_t*)"\007example",
693712b2f30Ssthen 		(uint8_t*)"\007example"
694712b2f30Ssthen 		) == 0);
695712b2f30Ssthen 	unit_assert( dname_canonical_compare(
696712b2f30Ssthen 		(uint8_t*)"\001a\007example",
697712b2f30Ssthen 		(uint8_t*)"\001a\007example"
698712b2f30Ssthen 		) == 0);
699712b2f30Ssthen 	unit_assert( dname_canonical_compare(
700712b2f30Ssthen 		(uint8_t*)"\010yljkjljk\001a\007example",
701712b2f30Ssthen 		(uint8_t*)"\010yljkjljk\001a\007example"
702712b2f30Ssthen 		) == 0);
703712b2f30Ssthen 	unit_assert( dname_canonical_compare(
704712b2f30Ssthen 		(uint8_t*)"\001Z\001a\007example",
705712b2f30Ssthen 		(uint8_t*)"\001Z\001a\007example"
706712b2f30Ssthen 		) == 0);
707712b2f30Ssthen 	unit_assert( dname_canonical_compare(
708712b2f30Ssthen 		(uint8_t*)"\004zABC\001a\007EXAMPLE",
709712b2f30Ssthen 		(uint8_t*)"\004zABC\001a\007EXAMPLE"
710712b2f30Ssthen 		) == 0);
711712b2f30Ssthen 	unit_assert( dname_canonical_compare(
712712b2f30Ssthen 		(uint8_t*)"\001z\007example",
713712b2f30Ssthen 		(uint8_t*)"\001z\007example"
714712b2f30Ssthen 		) == 0);
715712b2f30Ssthen 	unit_assert( dname_canonical_compare(
716712b2f30Ssthen 		(uint8_t*)"\001\001\001z\007example",
717712b2f30Ssthen 		(uint8_t*)"\001\001\001z\007example"
718712b2f30Ssthen 		) == 0);
719712b2f30Ssthen 	unit_assert( dname_canonical_compare(
720712b2f30Ssthen 		(uint8_t*)"\001*\001z\007example",
721712b2f30Ssthen 		(uint8_t*)"\001*\001z\007example"
722712b2f30Ssthen 		) == 0);
723712b2f30Ssthen 	unit_assert( dname_canonical_compare(
724712b2f30Ssthen 		(uint8_t*)"\001\200\001z\007example",
725712b2f30Ssthen 		(uint8_t*)"\001\200\001z\007example"
726712b2f30Ssthen 		) == 0);
727712b2f30Ssthen }
728712b2f30Ssthen 
729712b2f30Ssthen /** Test dname_get_shared_topdomain */
730712b2f30Ssthen static void
dname_test_topdomain(void)731712b2f30Ssthen dname_test_topdomain(void)
732712b2f30Ssthen {
733712b2f30Ssthen 	unit_show_func("util/data/dname.c", "dname_get_shared_topdomain");
734712b2f30Ssthen 	unit_assert( query_dname_compare(
735712b2f30Ssthen 		dname_get_shared_topdomain(
736712b2f30Ssthen 			(uint8_t*)"",
737712b2f30Ssthen 			(uint8_t*)""),
738712b2f30Ssthen 		(uint8_t*)"") == 0);
739712b2f30Ssthen 	unit_assert( query_dname_compare(
740712b2f30Ssthen 		dname_get_shared_topdomain(
741712b2f30Ssthen 			(uint8_t*)"\003www\007example\003com",
742712b2f30Ssthen 			(uint8_t*)"\003www\007example\003com"),
743712b2f30Ssthen 		(uint8_t*)"\003www\007example\003com") == 0);
744712b2f30Ssthen 	unit_assert( query_dname_compare(
745712b2f30Ssthen 		dname_get_shared_topdomain(
746712b2f30Ssthen 			(uint8_t*)"\003www\007example\003com",
747712b2f30Ssthen 			(uint8_t*)"\003bla\007example\003com"),
748712b2f30Ssthen 		(uint8_t*)"\007example\003com") == 0);
749712b2f30Ssthen }
750712b2f30Ssthen 
751712b2f30Ssthen /** Test dname_valid */
752712b2f30Ssthen static void
dname_test_valid(void)753712b2f30Ssthen dname_test_valid(void)
754712b2f30Ssthen {
755712b2f30Ssthen 	unit_show_func("util/data/dname.c", "dname_valid");
756712b2f30Ssthen 	unit_assert( dname_valid(
757712b2f30Ssthen 			(uint8_t*)"\003www\007example\003com", 255) == 17);
758712b2f30Ssthen 	unit_assert( dname_valid((uint8_t*)"", 255) == 1);
759712b2f30Ssthen 	unit_assert( dname_valid( (uint8_t*)
760712b2f30Ssthen 		"\020a1cdef5555544444"
761712b2f30Ssthen 		"\020a2cdef5555544444"
762712b2f30Ssthen 		"\020a3cdef5555544444"
763712b2f30Ssthen 		"\020a4cdef5555544444"
764712b2f30Ssthen 		"\020a5cdef5555544444"
765712b2f30Ssthen 		"\020a6cdef5555544444"
766712b2f30Ssthen 		"\020a7cdef5555544444"
767712b2f30Ssthen 		"\020a8cdef5555544444"
768712b2f30Ssthen 		"\020a9cdef5555544444"
769712b2f30Ssthen 		"\020aAcdef5555544444"
770712b2f30Ssthen 		"\020aBcdef5555544444"
771712b2f30Ssthen 		"\020aCcdef5555544444"
772712b2f30Ssthen 		"\020aDcdef5555544444"
773712b2f30Ssthen 		"\020aEcdef5555544444"	/* 238 up to here */
774712b2f30Ssthen 		"\007aabbccd"		/* 246 up to here */
775712b2f30Ssthen 		"\007example\000"	/* 255 to here */
776712b2f30Ssthen 		, 255) == 255);
777712b2f30Ssthen 	unit_assert( dname_valid( (uint8_t*)
778712b2f30Ssthen 		"\020a1cdef5555544444"
779712b2f30Ssthen 		"\020a2cdef5555544444"
780712b2f30Ssthen 		"\020a3cdef5555544444"
781712b2f30Ssthen 		"\020a4cdef5555544444"
782712b2f30Ssthen 		"\020a5cdef5555544444"
783712b2f30Ssthen 		"\020a6cdef5555544444"
784712b2f30Ssthen 		"\020a7cdef5555544444"
785712b2f30Ssthen 		"\020a8cdef5555544444"
786712b2f30Ssthen 		"\020a9cdef5555544444"
787712b2f30Ssthen 		"\020aAcdef5555544444"
788712b2f30Ssthen 		"\020aBcdef5555544444"
789712b2f30Ssthen 		"\020aCcdef5555544444"
790712b2f30Ssthen 		"\020aDcdef5555544444"
791712b2f30Ssthen 		"\020aEcdef5555544444"	/* 238 up to here */
792712b2f30Ssthen 		"\007aabbccd"		/* 246 up to here */
793712b2f30Ssthen 		"\010exampleX\000"	/* 256 to here */
794712b2f30Ssthen 		, 4096) == 0);
795712b2f30Ssthen }
796712b2f30Ssthen 
797*d5d2b63dSsthen /** Test dname_has_label */
798*d5d2b63dSsthen static void
dname_test_has_label(void)799*d5d2b63dSsthen dname_test_has_label(void)
800*d5d2b63dSsthen {
801*d5d2b63dSsthen 	unit_show_func("util/data/dname.c", "dname_has_label");
802*d5d2b63dSsthen 	/* label past root label */
803*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\01a\0\01c", 5, (uint8_t*)"\01c") == 0);
804*d5d2b63dSsthen 	/* label not found */
805*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\02ab\01c\0", 6, (uint8_t*)"\01e") == 0);
806*d5d2b63dSsthen 	/* buffer too short */
807*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\02ab\01c\0", 5, (uint8_t*)"\0") == 0);
808*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\1a\0", 2, (uint8_t*)"\0") == 0);
809*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\0", 0, (uint8_t*)"\0") == 0);
810*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\02ab\01c", 4, (uint8_t*)"\01c") == 0);
811*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\02ab\03qwe\06oqieur\03def\01c\0", 19, (uint8_t*)"\01c") == 0);
812*d5d2b63dSsthen 
813*d5d2b63dSsthen 	/* positive cases  */
814*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\0", 1, (uint8_t*)"\0") == 1);
815*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\1a\0", 3, (uint8_t*)"\0") == 1);
816*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\01a\0\01c", 5, (uint8_t*)"\0") == 1);
817*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\02ab\01c", 5, (uint8_t*)"\01c") == 1);
818*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\02ab\01c\0", 10, (uint8_t*)"\0") == 1);
819*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\02ab\01c\0", 7, (uint8_t*)"\0") == 1);
820*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\02ab\03qwe\06oqieur\03def\01c\0", 22, (uint8_t*)"\03def") == 1);
821*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\02ab\03qwe\06oqieur\03def\01c\0", 22, (uint8_t*)"\02ab") == 1);
822*d5d2b63dSsthen 	unit_assert(dname_has_label((uint8_t*)"\02ab\03qwe\06oqieur\03def\01c\0", 22, (uint8_t*)"\01c") == 1);
823*d5d2b63dSsthen }
824*d5d2b63dSsthen 
825712b2f30Ssthen /** test pkt_dname_tolower */
826712b2f30Ssthen static void
dname_test_pdtl(sldns_buffer * loopbuf,sldns_buffer * boundbuf)827712b2f30Ssthen dname_test_pdtl(sldns_buffer* loopbuf, sldns_buffer* boundbuf)
828712b2f30Ssthen {
829712b2f30Ssthen 	unit_show_func("util/data/dname.c", "pkt_dname_tolower");
830712b2f30Ssthen 	pkt_dname_tolower(loopbuf, sldns_buffer_at(loopbuf, 12));
831712b2f30Ssthen 	pkt_dname_tolower(boundbuf, sldns_buffer_at(boundbuf, 12));
832712b2f30Ssthen }
833712b2f30Ssthen 
834712b2f30Ssthen /** setup looped dname and out-of-bounds dname ptr */
835712b2f30Ssthen static void
dname_setup_bufs(sldns_buffer * loopbuf,sldns_buffer * boundbuf)836712b2f30Ssthen dname_setup_bufs(sldns_buffer* loopbuf, sldns_buffer* boundbuf)
837712b2f30Ssthen {
838712b2f30Ssthen 	sldns_buffer_write_u16(loopbuf, 0xd54d);  /* id */
839712b2f30Ssthen 	sldns_buffer_write_u16(loopbuf, 0x12);    /* flags  */
840712b2f30Ssthen 	sldns_buffer_write_u16(loopbuf, 1);       /* qdcount */
841712b2f30Ssthen 	sldns_buffer_write_u16(loopbuf, 0);       /* ancount */
842712b2f30Ssthen 	sldns_buffer_write_u16(loopbuf, 0);       /* nscount */
843712b2f30Ssthen 	sldns_buffer_write_u16(loopbuf, 0);       /* arcount */
844712b2f30Ssthen 	sldns_buffer_write_u8(loopbuf, 0xc0); /* PTR back at itself */
845712b2f30Ssthen 	sldns_buffer_write_u8(loopbuf, 0x0c);
846712b2f30Ssthen 	sldns_buffer_flip(loopbuf);
847712b2f30Ssthen 
848712b2f30Ssthen 	sldns_buffer_write_u16(boundbuf, 0xd54d);  /* id */
849712b2f30Ssthen 	sldns_buffer_write_u16(boundbuf, 0x12);    /* flags  */
850712b2f30Ssthen 	sldns_buffer_write_u16(boundbuf, 1);       /* qdcount */
851712b2f30Ssthen 	sldns_buffer_write_u16(boundbuf, 0);       /* ancount */
852712b2f30Ssthen 	sldns_buffer_write_u16(boundbuf, 0);       /* nscount */
853712b2f30Ssthen 	sldns_buffer_write_u16(boundbuf, 0);       /* arcount */
854712b2f30Ssthen 	sldns_buffer_write_u8(boundbuf, 0x01); /* len=1 */
855712b2f30Ssthen 	sldns_buffer_write_u8(boundbuf, (uint8_t)'A'); /* A. label */
856712b2f30Ssthen 	sldns_buffer_write_u8(boundbuf, 0xc0); /* PTR out of bounds */
857712b2f30Ssthen 	sldns_buffer_write_u8(boundbuf, 0xcc);
858712b2f30Ssthen 	sldns_buffer_flip(boundbuf);
859712b2f30Ssthen }
860712b2f30Ssthen 
dname_test(void)861712b2f30Ssthen void dname_test(void)
862712b2f30Ssthen {
863712b2f30Ssthen 	sldns_buffer* loopbuf = sldns_buffer_new(14);
864712b2f30Ssthen 	sldns_buffer* boundbuf = sldns_buffer_new(16);
865712b2f30Ssthen 	sldns_buffer* buff = sldns_buffer_new(65800);
866712b2f30Ssthen 	unit_assert(loopbuf && boundbuf && buff);
867712b2f30Ssthen 	sldns_buffer_flip(buff);
868712b2f30Ssthen 	dname_setup_bufs(loopbuf, boundbuf);
869712b2f30Ssthen 	dname_test_qdl(buff);
870712b2f30Ssthen 	dname_test_qdtl(buff);
871712b2f30Ssthen 	dname_test_pdtl(loopbuf, boundbuf);
872712b2f30Ssthen 	dname_test_query_dname_compare();
873712b2f30Ssthen 	dname_test_count_labels();
874712b2f30Ssthen 	dname_test_count_size_labels();
875712b2f30Ssthen 	dname_test_dname_lab_cmp();
876712b2f30Ssthen 	dname_test_pkt_dname_len(buff);
877712b2f30Ssthen 	dname_test_strict_subdomain();
878712b2f30Ssthen 	dname_test_subdomain();
879712b2f30Ssthen 	dname_test_isroot();
880712b2f30Ssthen 	dname_test_removelabel();
881712b2f30Ssthen 	dname_test_sigcount();
882712b2f30Ssthen 	dname_test_iswild();
883712b2f30Ssthen 	dname_test_canoncmp();
884712b2f30Ssthen 	dname_test_topdomain();
885712b2f30Ssthen 	dname_test_valid();
886*d5d2b63dSsthen 	dname_test_has_label();
887712b2f30Ssthen 	sldns_buffer_free(buff);
888712b2f30Ssthen 	sldns_buffer_free(loopbuf);
889712b2f30Ssthen 	sldns_buffer_free(boundbuf);
890712b2f30Ssthen }
891