]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/testcode/unitdname.c
2 * testcode/unitdname.c - unit test for dname routines.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Calls dname unit tests. Exits with code 1 on a failure.
43 #include "testcode/unitmain.h"
44 #include "util/data/dname.h"
45 #include "ldns/sbuffer.h"
46 #include "ldns/str2wire.h"
48 /** put dname into buffer */
50 dname_to_buf(sldns_buffer
* b
, const char* str
)
53 size_t len
= sldns_buffer_capacity(b
);
54 sldns_buffer_clear(b
);
55 e
= sldns_str2wire_dname_buf(str
, sldns_buffer_begin(b
), &len
);
57 fatal_exit("%s ldns: %s", __func__
,
58 sldns_get_errorstr_parse(e
));
59 sldns_buffer_set_position(b
, len
);
64 /** test query_dname_len function */
66 dname_test_qdl(sldns_buffer
* buff
)
68 unit_show_func("util/data/dname.c", "query_dname_len");
69 unit_assert( query_dname_len(buff
) == 0);
70 unit_assert( query_dname_len(dname_to_buf(buff
, ".")) == 1 );
71 unit_assert( query_dname_len(dname_to_buf(buff
, "bla.foo.")) == 9 );
72 unit_assert( query_dname_len(dname_to_buf(buff
, "x.y.z.example.com."
76 /** test query_dname_tolower */
78 dname_test_qdtl(sldns_buffer
* buff
)
80 unit_show_func("util/data/dname.c", "query_dname_tolower");
81 sldns_buffer_write_at(buff
, 0, "\012abCDeaBCde\003cOm\000", 16);
82 query_dname_tolower(sldns_buffer_begin(buff
));
83 unit_assert( memcmp(sldns_buffer_begin(buff
),
84 "\012abcdeabcde\003com\000", 16) == 0);
86 sldns_buffer_write_at(buff
, 0, "\001+\012abC{e-ZYXe\003NET\000", 18);
87 query_dname_tolower(sldns_buffer_begin(buff
));
88 unit_assert( memcmp(sldns_buffer_begin(buff
),
89 "\001+\012abc{e-zyxe\003net\000", 18) == 0);
91 sldns_buffer_write_at(buff
, 0, "\000", 1);
92 query_dname_tolower(sldns_buffer_begin(buff
));
93 unit_assert( memcmp(sldns_buffer_begin(buff
), "\000", 1) == 0);
95 sldns_buffer_write_at(buff
, 0, "\002NL\000", 4);
96 query_dname_tolower(sldns_buffer_begin(buff
));
97 unit_assert( memcmp(sldns_buffer_begin(buff
), "\002nl\000", 4) == 0);
100 /** test query_dname_compare */
102 dname_test_query_dname_compare(void)
104 unit_show_func("util/data/dname.c", "query_dname_compare");
105 unit_assert(query_dname_compare((uint8_t*)"", (uint8_t*)"") == 0);
106 unit_assert(query_dname_compare((uint8_t*)"\001a",
107 (uint8_t*)"\001a") == 0);
108 unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
109 (uint8_t*)"\003abc\001a") == 0);
110 unit_assert(query_dname_compare((uint8_t*)"\003aBc\001a",
111 (uint8_t*)"\003AbC\001A") == 0);
112 unit_assert(query_dname_compare((uint8_t*)"\003abc",
113 (uint8_t*)"\003abc\001a") == -1);
114 unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
115 (uint8_t*)"\003abc") == +1);
116 unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
117 (uint8_t*)"") == +1);
118 unit_assert(query_dname_compare((uint8_t*)"",
119 (uint8_t*)"\003abc\001a") == -1);
120 unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
121 (uint8_t*)"\003xxx\001a") == -1);
122 unit_assert(query_dname_compare((uint8_t*)"\003axx\001a",
123 (uint8_t*)"\003abc\001a") == 1);
124 unit_assert(query_dname_compare((uint8_t*)"\003abc\001a",
125 (uint8_t*)"\003abc\001Z") == -1);
126 unit_assert(query_dname_compare((uint8_t*)"\003abc\001Z",
127 (uint8_t*)"\003abc\001a") == 1);
130 /** test dname_count_labels */
132 dname_test_count_labels(void)
134 unit_show_func("util/data/dname.c", "dname_count_labels");
135 unit_assert(dname_count_labels((uint8_t*)"") == 1);
136 unit_assert(dname_count_labels((uint8_t*)"\003com") == 2);
137 unit_assert(dname_count_labels((uint8_t*)"\003org") == 2);
138 unit_assert(dname_count_labels((uint8_t*)"\007example\003com") == 3);
139 unit_assert(dname_count_labels((uint8_t*)"\003bla\007example\003com")
143 /** test dname_count_size_labels */
145 dname_test_count_size_labels(void)
148 unit_show_func("util/data/dname.c", "dname_count_size_labels");
149 unit_assert(dname_count_size_labels((uint8_t*)"", &sz
) == 1);
150 unit_assert(sz
== 1);
151 unit_assert(dname_count_size_labels((uint8_t*)"\003com", &sz
) == 2);
152 unit_assert(sz
== 5);
153 unit_assert(dname_count_size_labels((uint8_t*)"\003org", &sz
) == 2);
154 unit_assert(sz
== 5);
155 unit_assert(dname_count_size_labels((uint8_t*)"\007example\003com",
157 unit_assert(sz
== 13);
158 unit_assert(dname_count_size_labels((uint8_t*)"\003bla\007example"
159 "\003com", &sz
) == 4);
160 unit_assert(sz
== 17);
164 /** test pkt_dname_len */
166 dname_test_pkt_dname_len(sldns_buffer
* buff
)
168 unit_show_func("util/data/dname.c", "pkt_dname_len");
169 sldns_buffer_clear(buff
);
170 sldns_buffer_write(buff
, "\000", 1);
171 sldns_buffer_flip(buff
);
172 unit_assert( pkt_dname_len(buff
) == 1 );
173 unit_assert( sldns_buffer_position(buff
) == 1);
175 sldns_buffer_clear(buff
);
176 sldns_buffer_write(buff
, "\003org\000", 5);
177 sldns_buffer_flip(buff
);
178 unit_assert( pkt_dname_len(buff
) == 5 );
179 unit_assert( sldns_buffer_position(buff
) == 5);
181 sldns_buffer_clear(buff
);
182 sldns_buffer_write(buff
, "\002os\007example\003org\000", 16);
183 sldns_buffer_flip(buff
);
184 unit_assert( pkt_dname_len(buff
) == 16 );
185 unit_assert( sldns_buffer_position(buff
) == 16);
187 /* invalid compression pointer: to self */
188 sldns_buffer_clear(buff
);
189 sldns_buffer_write(buff
, "\300\000os\007example\003org\000", 17);
190 sldns_buffer_flip(buff
);
191 unit_assert( pkt_dname_len(buff
) == 0 );
193 /* valid compression pointer */
194 sldns_buffer_clear(buff
);
195 sldns_buffer_write(buff
, "\003com\000\040\300\000", 8);
196 sldns_buffer_flip(buff
);
197 sldns_buffer_set_position(buff
, 6);
198 unit_assert( pkt_dname_len(buff
) == 5 );
199 unit_assert( sldns_buffer_position(buff
) == 8);
201 /* unknown label type */
202 sldns_buffer_clear(buff
);
203 sldns_buffer_write(buff
, "\002os\107example\003org\000", 16);
204 sldns_buffer_flip(buff
);
205 unit_assert( pkt_dname_len(buff
) == 0 );
208 sldns_buffer_clear(buff
);
209 sldns_buffer_write(buff
, "\002os\047example\003org\000", 16);
210 sldns_buffer_flip(buff
);
211 unit_assert( pkt_dname_len(buff
) == 0 );
213 /* label exceeds packet */
214 sldns_buffer_clear(buff
);
215 sldns_buffer_write(buff
, "\002os\007example\007org\004", 16);
216 sldns_buffer_flip(buff
);
217 unit_assert( pkt_dname_len(buff
) == 0 );
220 sldns_buffer_clear(buff
);
221 sldns_buffer_write(buff
,
222 "\020a1cdef5555544444"
223 "\020a2cdef5555544444"
224 "\020a3cdef5555544444"
225 "\020a4cdef5555544444"
226 "\020a5cdef5555544444"
227 "\020a6cdef5555544444"
228 "\020a7cdef5555544444"
229 "\020a8cdef5555544444"
230 "\020a9cdef5555544444"
231 "\020aAcdef5555544444"
232 "\020aBcdef5555544444"
233 "\020aCcdef5555544444"
234 "\020aDcdef5555544444"
235 "\020aEcdef5555544444" /* 238 up to here */
236 "\007aabbccd" /* 246 up to here */
237 "\007example\000" /* 255 to here */
239 sldns_buffer_flip(buff
);
240 unit_assert( pkt_dname_len(buff
) == 255 );
241 unit_assert( sldns_buffer_position(buff
) == 255);
244 sldns_buffer_clear(buff
);
245 sldns_buffer_write(buff
,
246 "\020a1cdef5555544444"
247 "\020a2cdef5555544444"
248 "\020a3cdef5555544444"
249 "\020a4cdef5555544444"
250 "\020a5cdef5555544444"
251 "\020a6cdef5555544444"
252 "\020a7cdef5555544444"
253 "\020a8cdef5555544444"
254 "\020a9cdef5555544444"
255 "\020aAcdef5555544444"
256 "\020aBcdef5555544444"
257 "\020aCcdef5555544444"
258 "\020aXcdef5555544444"
259 "\020aXcdef5555544444"
260 "\020aXcdef5555544444"
261 "\020aDcdef5555544444"
262 "\020aEcdef5555544444" /* 238 up to here */
263 "\007aabbccd" /* 246 up to here */
264 "\007example\000" /* 255 to here */
266 sldns_buffer_flip(buff
);
267 unit_assert( pkt_dname_len(buff
) == 0 );
270 /** test dname_lab_cmp */
272 dname_test_dname_lab_cmp(void)
274 int ml
= 0; /* number of labels that matched exactly */
275 unit_show_func("util/data/dname.c", "dname_lab_cmp");
277 /* test for equality succeeds */
278 unit_assert(dname_lab_cmp((uint8_t*)"", 1, (uint8_t*)"", 1, &ml
) == 0);
279 unit_assert(ml
== 1);
280 unit_assert(dname_lab_cmp(
281 (uint8_t*)"\003net", 2,
282 (uint8_t*)"\003net", 2,
284 unit_assert(ml
== 2);
285 unit_assert(dname_lab_cmp(
286 (uint8_t*)"\007example\003net", 3,
287 (uint8_t*)"\007example\003net", 3,
289 unit_assert(ml
== 3);
290 unit_assert(dname_lab_cmp(
291 (uint8_t*)"\004test\007example\003net", 4,
292 (uint8_t*)"\004test\007example\003net", 4,
294 unit_assert(ml
== 4);
296 /* root is smaller than anything else */
297 unit_assert(dname_lab_cmp(
299 (uint8_t*)"\003net", 2,
301 unit_assert(ml
== 1);
302 unit_assert(dname_lab_cmp(
303 (uint8_t*)"\003net", 2,
306 unit_assert(ml
== 1);
307 unit_assert(dname_lab_cmp(
309 (uint8_t*)"\007example\003net", 3,
311 unit_assert(ml
== 1);
312 unit_assert(dname_lab_cmp(
313 (uint8_t*)"\007example\003net", 3,
316 unit_assert(ml
== 1);
318 /* label length makes a difference */
319 unit_assert(dname_lab_cmp(
320 (uint8_t*)"\004neta", 2,
321 (uint8_t*)"\003net", 2,
323 unit_assert(ml
== 1);
324 unit_assert(dname_lab_cmp(
325 (uint8_t*)"\002ne", 2,
326 (uint8_t*)"\004neta", 2,
328 unit_assert(ml
== 1);
330 /* contents follow the zone apex */
331 unit_assert(dname_lab_cmp(
332 (uint8_t*)"\003bla\007example\003net", 4,
333 (uint8_t*)"\007example\003net", 3,
335 unit_assert(ml
== 3);
336 unit_assert(dname_lab_cmp(
337 (uint8_t*)"\007example\003net", 3,
338 (uint8_t*)"\003bla\007example\003net", 4,
340 unit_assert(ml
== 3);
342 /* label content makes a difference */
343 unit_assert(dname_lab_cmp(
344 (uint8_t*)"\003aag\007example\003net", 4,
345 (uint8_t*)"\003bla\007example\003net", 4,
347 unit_assert(ml
== 3);
348 unit_assert(dname_lab_cmp(
349 (uint8_t*)"\003aag\007example\003net", 4,
350 (uint8_t*)"\003bla\007example\003net", 4,
352 unit_assert(ml
== 3);
353 unit_assert(dname_lab_cmp(
354 (uint8_t*)"\003bla\003aag\007example\003net", 5,
355 (uint8_t*)"\003aag\003bla\007example\003net", 5,
357 unit_assert(ml
== 3);
358 unit_assert(dname_lab_cmp(
359 (uint8_t*)"\02sn\003opt\003aag\007example\003net", 6,
360 (uint8_t*)"\02sn\003opt\003bla\007example\003net", 6,
362 unit_assert(ml
== 3);
364 /* but lowercase/uppercase does not make a difference. */
365 unit_assert(dname_lab_cmp(
366 (uint8_t*)"\003bLa\007examPLe\003net", 4,
367 (uint8_t*)"\003bla\007eXAmple\003nET", 4,
369 unit_assert(ml
== 4);
372 /** test dname_subdomain_c */
374 dname_test_subdomain(void)
376 unit_show_func("util/data/dname.c", "dname_subdomain");
377 unit_assert(dname_subdomain_c(
380 unit_assert(dname_subdomain_c(
383 unit_assert(!dname_subdomain_c(
385 (uint8_t*)"\003com"));
386 unit_assert(dname_subdomain_c(
387 (uint8_t*)"\007example\003com",
388 (uint8_t*)"\003com"));
389 unit_assert(!dname_subdomain_c(
391 (uint8_t*)"\007example\003com"));
392 unit_assert(dname_subdomain_c(
393 (uint8_t*)"\007example\003com",
395 unit_assert(!dname_subdomain_c(
397 (uint8_t*)"\003com"));
398 unit_assert(!dname_subdomain_c(
400 (uint8_t*)"\003org"));
401 unit_assert(!dname_subdomain_c(
402 (uint8_t*)"\007example\003net",
403 (uint8_t*)"\003org"));
404 unit_assert(!dname_subdomain_c(
406 (uint8_t*)"\007example\003org"));
409 /** test dname_strict_subdomain */
411 dname_test_strict_subdomain(void)
413 unit_show_func("util/data/dname.c", "dname_strict_subdomain");
414 unit_assert(!dname_strict_subdomain(
417 unit_assert(dname_strict_subdomain(
418 (uint8_t*)"\003com", 2,
420 unit_assert(!dname_strict_subdomain(
422 (uint8_t*)"\003com", 2));
423 unit_assert(dname_strict_subdomain(
424 (uint8_t*)"\007example\003com", 3,
425 (uint8_t*)"\003com", 2));
426 unit_assert(!dname_strict_subdomain(
427 (uint8_t*)"\003com", 2,
428 (uint8_t*)"\007example\003com", 3));
429 unit_assert(dname_strict_subdomain(
430 (uint8_t*)"\007example\003com", 3,
432 unit_assert(!dname_strict_subdomain(
433 (uint8_t*)"\003net", 2,
434 (uint8_t*)"\003com", 2));
435 unit_assert(!dname_strict_subdomain(
436 (uint8_t*)"\003net", 2,
437 (uint8_t*)"\003org", 2));
438 unit_assert(!dname_strict_subdomain(
439 (uint8_t*)"\007example\003net", 3,
440 (uint8_t*)"\003org", 2));
441 unit_assert(!dname_strict_subdomain(
442 (uint8_t*)"\003net", 2,
443 (uint8_t*)"\007example\003org", 3));
446 /** test dname_is_root */
448 dname_test_isroot(void)
450 unit_show_func("util/data/dname.c", "dname_isroot");
451 unit_assert(dname_is_root((uint8_t*)"\000"));
452 unit_assert(!dname_is_root((uint8_t*)"\001a\000"));
453 unit_assert(!dname_is_root((uint8_t*)"\005abvcd\003com\000"));
454 /* malformed dname in this test, but should work */
455 unit_assert(!dname_is_root((uint8_t*)"\077a\000"));
456 unit_assert(dname_is_root((uint8_t*)"\000"));
459 /** test dname_remove_label */
461 dname_test_removelabel(void)
463 uint8_t* orig
= (uint8_t*)"\007example\003com\000";
466 unit_show_func("util/data/dname.c", "dname_remove_label");
467 dname_remove_label(&n
, &l
);
468 unit_assert( n
== orig
+8 );
469 unit_assert( l
== 5 );
470 dname_remove_label(&n
, &l
);
471 unit_assert( n
== orig
+12 );
472 unit_assert( l
== 1 );
473 dname_remove_label(&n
, &l
);
474 unit_assert( n
== orig
+12 );
475 unit_assert( l
== 1 );
478 /** test dname_signame_label_count */
480 dname_test_sigcount(void)
482 unit_show_func("util/data/dname.c", "dname_signame_label_count");
483 unit_assert(dname_signame_label_count((uint8_t*)"\000") == 0);
484 unit_assert(dname_signame_label_count((uint8_t*)"\001*\000") == 0);
485 unit_assert(dname_signame_label_count((uint8_t*)"\003xom\000") == 1);
486 unit_assert(dname_signame_label_count(
487 (uint8_t*)"\001*\003xom\000") == 1);
488 unit_assert(dname_signame_label_count(
489 (uint8_t*)"\007example\003xom\000") == 2);
490 unit_assert(dname_signame_label_count(
491 (uint8_t*)"\001*\007example\003xom\000") == 2);
492 unit_assert(dname_signame_label_count(
493 (uint8_t*)"\003www\007example\003xom\000") == 3);
494 unit_assert(dname_signame_label_count(
495 (uint8_t*)"\001*\003www\007example\003xom\000") == 3);
498 /** test dname_is_wild routine */
500 dname_test_iswild(void)
502 unit_show_func("util/data/dname.c", "dname_iswild");
503 unit_assert( !dname_is_wild((uint8_t*)"\000") );
504 unit_assert( dname_is_wild((uint8_t*)"\001*\000") );
505 unit_assert( !dname_is_wild((uint8_t*)"\003net\000") );
506 unit_assert( dname_is_wild((uint8_t*)"\001*\003net\000") );
509 /** test dname_canonical_compare */
511 dname_test_canoncmp(void)
513 unit_show_func("util/data/dname.c", "dname_canonical_compare");
515 unit_assert( dname_canonical_compare(
519 unit_assert( dname_canonical_compare(
520 (uint8_t*)"\003net\000",
521 (uint8_t*)"\003net\000"
523 unit_assert( dname_canonical_compare(
524 (uint8_t*)"\007example\003net\000",
525 (uint8_t*)"\007example\003net\000"
527 unit_assert( dname_canonical_compare(
528 (uint8_t*)"\004test\007example\003net\000",
529 (uint8_t*)"\004test\007example\003net\000"
533 unit_assert( dname_canonical_compare(
537 unit_assert( dname_canonical_compare(
541 unit_assert( dname_canonical_compare(
542 (uint8_t*)"\007example\003com",
545 unit_assert( dname_canonical_compare(
547 (uint8_t*)"\007example\003com"
549 unit_assert( dname_canonical_compare(
550 (uint8_t*)"\007example\003com",
553 unit_assert( dname_canonical_compare(
555 (uint8_t*)"\007example\003com"
558 /* compare rightmost label */
559 unit_assert( dname_canonical_compare(
563 unit_assert( dname_canonical_compare(
567 unit_assert( dname_canonical_compare(
571 unit_assert( dname_canonical_compare(
572 (uint8_t*)"\007example\003net",
575 unit_assert( dname_canonical_compare(
577 (uint8_t*)"\007example\003net"
580 /* label length makes a difference; but only if rest is equal */
581 unit_assert( dname_canonical_compare(
582 (uint8_t*)"\004neta",
585 unit_assert( dname_canonical_compare(
591 unit_assert( dname_canonical_compare(
592 (uint8_t*)"\003aag\007example\003net",
593 (uint8_t*)"\003bla\007example\003net"
595 unit_assert( dname_canonical_compare(
596 (uint8_t*)"\003bla\007example\003net",
597 (uint8_t*)"\003aag\007example\003net"
599 unit_assert( dname_canonical_compare(
600 (uint8_t*)"\003bla\003aag\007example\003net",
601 (uint8_t*)"\003aag\003bla\007example\003net"
603 unit_assert( dname_canonical_compare(
604 (uint8_t*)"\02sn\003opt\003aag\007example\003net",
605 (uint8_t*)"\02sn\003opt\003bla\007example\003net"
608 /* lowercase during compare */
609 unit_assert( dname_canonical_compare(
610 (uint8_t*)"\003bLa\007examPLe\003net",
611 (uint8_t*)"\003bla\007eXAmple\003nET"
614 /* example from 4034 */
615 /* example a.example yljkjljk.a.example Z.a.example zABC.a.EXAMPLE
616 z.example \001.z.example *.z.example \200.z.example */
617 unit_assert( dname_canonical_compare(
619 (uint8_t*)"\007example"
621 unit_assert( dname_canonical_compare(
622 (uint8_t*)"\007example",
623 (uint8_t*)"\001a\007example"
625 unit_assert( dname_canonical_compare(
626 (uint8_t*)"\001a\007example",
627 (uint8_t*)"\010yljkjljk\001a\007example"
629 unit_assert( dname_canonical_compare(
630 (uint8_t*)"\010yljkjljk\001a\007example",
631 (uint8_t*)"\001Z\001a\007example"
633 unit_assert( dname_canonical_compare(
634 (uint8_t*)"\001Z\001a\007example",
635 (uint8_t*)"\004zABC\001a\007EXAMPLE"
637 unit_assert( dname_canonical_compare(
638 (uint8_t*)"\004zABC\001a\007EXAMPLE",
639 (uint8_t*)"\001z\007example"
641 unit_assert( dname_canonical_compare(
642 (uint8_t*)"\001z\007example",
643 (uint8_t*)"\001\001\001z\007example"
645 unit_assert( dname_canonical_compare(
646 (uint8_t*)"\001\001\001z\007example",
647 (uint8_t*)"\001*\001z\007example"
649 unit_assert( dname_canonical_compare(
650 (uint8_t*)"\001*\001z\007example",
651 (uint8_t*)"\001\200\001z\007example"
653 /* same example in reverse */
654 unit_assert( dname_canonical_compare(
655 (uint8_t*)"\007example",
658 unit_assert( dname_canonical_compare(
659 (uint8_t*)"\001a\007example",
660 (uint8_t*)"\007example"
662 unit_assert( dname_canonical_compare(
663 (uint8_t*)"\010yljkjljk\001a\007example",
664 (uint8_t*)"\001a\007example"
666 unit_assert( dname_canonical_compare(
667 (uint8_t*)"\001Z\001a\007example",
668 (uint8_t*)"\010yljkjljk\001a\007example"
670 unit_assert( dname_canonical_compare(
671 (uint8_t*)"\004zABC\001a\007EXAMPLE",
672 (uint8_t*)"\001Z\001a\007example"
674 unit_assert( dname_canonical_compare(
675 (uint8_t*)"\001z\007example",
676 (uint8_t*)"\004zABC\001a\007EXAMPLE"
678 unit_assert( dname_canonical_compare(
679 (uint8_t*)"\001\001\001z\007example",
680 (uint8_t*)"\001z\007example"
682 unit_assert( dname_canonical_compare(
683 (uint8_t*)"\001*\001z\007example",
684 (uint8_t*)"\001\001\001z\007example"
686 unit_assert( dname_canonical_compare(
687 (uint8_t*)"\001\200\001z\007example",
688 (uint8_t*)"\001*\001z\007example"
690 /* same example for equality */
691 unit_assert( dname_canonical_compare(
692 (uint8_t*)"\007example",
693 (uint8_t*)"\007example"
695 unit_assert( dname_canonical_compare(
696 (uint8_t*)"\001a\007example",
697 (uint8_t*)"\001a\007example"
699 unit_assert( dname_canonical_compare(
700 (uint8_t*)"\010yljkjljk\001a\007example",
701 (uint8_t*)"\010yljkjljk\001a\007example"
703 unit_assert( dname_canonical_compare(
704 (uint8_t*)"\001Z\001a\007example",
705 (uint8_t*)"\001Z\001a\007example"
707 unit_assert( dname_canonical_compare(
708 (uint8_t*)"\004zABC\001a\007EXAMPLE",
709 (uint8_t*)"\004zABC\001a\007EXAMPLE"
711 unit_assert( dname_canonical_compare(
712 (uint8_t*)"\001z\007example",
713 (uint8_t*)"\001z\007example"
715 unit_assert( dname_canonical_compare(
716 (uint8_t*)"\001\001\001z\007example",
717 (uint8_t*)"\001\001\001z\007example"
719 unit_assert( dname_canonical_compare(
720 (uint8_t*)"\001*\001z\007example",
721 (uint8_t*)"\001*\001z\007example"
723 unit_assert( dname_canonical_compare(
724 (uint8_t*)"\001\200\001z\007example",
725 (uint8_t*)"\001\200\001z\007example"
729 /** Test dname_get_shared_topdomain */
731 dname_test_topdomain(void)
733 unit_show_func("util/data/dname.c", "dname_get_shared_topdomain");
734 unit_assert( query_dname_compare(
735 dname_get_shared_topdomain(
739 unit_assert( query_dname_compare(
740 dname_get_shared_topdomain(
741 (uint8_t*)"\003www\007example\003com",
742 (uint8_t*)"\003www\007example\003com"),
743 (uint8_t*)"\003www\007example\003com") == 0);
744 unit_assert( query_dname_compare(
745 dname_get_shared_topdomain(
746 (uint8_t*)"\003www\007example\003com",
747 (uint8_t*)"\003bla\007example\003com"),
748 (uint8_t*)"\007example\003com") == 0);
751 /** Test dname_valid */
753 dname_test_valid(void)
755 unit_show_func("util/data/dname.c", "dname_valid");
756 unit_assert( dname_valid(
757 (uint8_t*)"\003www\007example\003com", 255) == 17);
758 unit_assert( dname_valid((uint8_t*)"", 255) == 1);
759 unit_assert( dname_valid( (uint8_t*)
760 "\020a1cdef5555544444"
761 "\020a2cdef5555544444"
762 "\020a3cdef5555544444"
763 "\020a4cdef5555544444"
764 "\020a5cdef5555544444"
765 "\020a6cdef5555544444"
766 "\020a7cdef5555544444"
767 "\020a8cdef5555544444"
768 "\020a9cdef5555544444"
769 "\020aAcdef5555544444"
770 "\020aBcdef5555544444"
771 "\020aCcdef5555544444"
772 "\020aDcdef5555544444"
773 "\020aEcdef5555544444" /* 238 up to here */
774 "\007aabbccd" /* 246 up to here */
775 "\007example\000" /* 255 to here */
777 unit_assert( dname_valid( (uint8_t*)
778 "\020a1cdef5555544444"
779 "\020a2cdef5555544444"
780 "\020a3cdef5555544444"
781 "\020a4cdef5555544444"
782 "\020a5cdef5555544444"
783 "\020a6cdef5555544444"
784 "\020a7cdef5555544444"
785 "\020a8cdef5555544444"
786 "\020a9cdef5555544444"
787 "\020aAcdef5555544444"
788 "\020aBcdef5555544444"
789 "\020aCcdef5555544444"
790 "\020aDcdef5555544444"
791 "\020aEcdef5555544444" /* 238 up to here */
792 "\007aabbccd" /* 246 up to here */
793 "\010exampleX\000" /* 256 to here */
797 /** test pkt_dname_tolower */
799 dname_test_pdtl(sldns_buffer
* loopbuf
, sldns_buffer
* boundbuf
)
801 unit_show_func("util/data/dname.c", "pkt_dname_tolower");
802 pkt_dname_tolower(loopbuf
, sldns_buffer_at(loopbuf
, 12));
803 pkt_dname_tolower(boundbuf
, sldns_buffer_at(boundbuf
, 12));
806 /** setup looped dname and out-of-bounds dname ptr */
808 dname_setup_bufs(sldns_buffer
* loopbuf
, sldns_buffer
* boundbuf
)
810 sldns_buffer_write_u16(loopbuf
, 0xd54d); /* id */
811 sldns_buffer_write_u16(loopbuf
, 0x12); /* flags */
812 sldns_buffer_write_u16(loopbuf
, 1); /* qdcount */
813 sldns_buffer_write_u16(loopbuf
, 0); /* ancount */
814 sldns_buffer_write_u16(loopbuf
, 0); /* nscount */
815 sldns_buffer_write_u16(loopbuf
, 0); /* arcount */
816 sldns_buffer_write_u8(loopbuf
, 0xc0); /* PTR back at itself */
817 sldns_buffer_write_u8(loopbuf
, 0x0c);
818 sldns_buffer_flip(loopbuf
);
820 sldns_buffer_write_u16(boundbuf
, 0xd54d); /* id */
821 sldns_buffer_write_u16(boundbuf
, 0x12); /* flags */
822 sldns_buffer_write_u16(boundbuf
, 1); /* qdcount */
823 sldns_buffer_write_u16(boundbuf
, 0); /* ancount */
824 sldns_buffer_write_u16(boundbuf
, 0); /* nscount */
825 sldns_buffer_write_u16(boundbuf
, 0); /* arcount */
826 sldns_buffer_write_u8(boundbuf
, 0x01); /* len=1 */
827 sldns_buffer_write_u8(boundbuf
, (uint8_t)'A'); /* A. label */
828 sldns_buffer_write_u8(boundbuf
, 0xc0); /* PTR out of bounds */
829 sldns_buffer_write_u8(boundbuf
, 0xcc);
830 sldns_buffer_flip(boundbuf
);
833 void dname_test(void)
835 sldns_buffer
* loopbuf
= sldns_buffer_new(14);
836 sldns_buffer
* boundbuf
= sldns_buffer_new(16);
837 sldns_buffer
* buff
= sldns_buffer_new(65800);
838 unit_assert(loopbuf
&& boundbuf
&& buff
);
839 sldns_buffer_flip(buff
);
840 dname_setup_bufs(loopbuf
, boundbuf
);
841 dname_test_qdl(buff
);
842 dname_test_qdtl(buff
);
843 dname_test_pdtl(loopbuf
, boundbuf
);
844 dname_test_query_dname_compare();
845 dname_test_count_labels();
846 dname_test_count_size_labels();
847 dname_test_dname_lab_cmp();
848 dname_test_pkt_dname_len(buff
);
849 dname_test_strict_subdomain();
850 dname_test_subdomain();
852 dname_test_removelabel();
853 dname_test_sigcount();
855 dname_test_canoncmp();
856 dname_test_topdomain();
858 sldns_buffer_free(buff
);
859 sldns_buffer_free(loopbuf
);
860 sldns_buffer_free(boundbuf
);