Debian Codegraph @

tests/dns/name_test.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <inttypes.h>
15 #include <sched.h> /* IWYU pragma: keep */
16 #include <setjmp.h>
17 #include <stdarg.h>
18 #include <stdbool.h>
19 #include <stddef.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #define UNIT_TESTING
25 #include <cmocka.h>
26
27 #include <isc/buffer.h>
28 #include <isc/commandline.h>
29 #include <isc/mem.h>
30 #include <isc/os.h>
31 #include <isc/thread.h>
32 #include <isc/util.h>
33
34 #include <dns/compress.h>
35 #include <dns/fixedname.h>
36 #include <dns/name.h>
37
38 #include <tests/dns.h>
39
40 /* Set to true (or use -v option) for verbose output */
41 static bool verbose = false;
42
43 /* dns_name_fullcompare test */
44 ISC_RUN_TEST_IMPL(fullcompare) {
45 dns_fixedname_t fixed1;
46 dns_fixedname_t fixed2;
47 dns_name_t *name1;
48 dns_name_t *name2;
49 dns_namereln_t relation;
50 int i;
51 isc_result_t result;
52 struct {
53 const char *name1;
54 const char *name2;
55 dns_namereln_t relation;
56 int order;
57 unsigned int nlabels;
58 } data[] = {
59 /* relative */
60 { "", "", dns_namereln_equal, 0, 0 },
61 { "foo", "", dns_namereln_subdomain, 1, 0 },
62 { "", "foo", dns_namereln_contains, -1, 0 },
63 { "foo", "bar", dns_namereln_none, 1, 0 },
64 { "bar", "foo", dns_namereln_none, -1, 0 },
65 { "bar.foo", "foo", dns_namereln_subdomain, 1, 1 },
66 { "foo", "bar.foo", dns_namereln_contains, -1, 1 },
67 { "baz.bar.foo", "bar.foo", dns_namereln_subdomain, 1, 2 },
68 { "bar.foo", "baz.bar.foo", dns_namereln_contains, -1, 2 },
69 { "foo.example", "bar.example", dns_namereln_commonancestor, 1,
70 1 },
71
72 /* absolute */
73 { ".", ".", dns_namereln_equal, 0, 1 },
74 { "foo.", "bar.", dns_namereln_commonancestor, 1, 1 },
75 { "bar.", "foo.", dns_namereln_commonancestor, -1, 1 },
76 { "foo.example.", "bar.example.", dns_namereln_commonancestor,
77 1, 2 },
78 { "bar.foo.", "foo.", dns_namereln_subdomain, 1, 2 },
79 { "foo.", "bar.foo.", dns_namereln_contains, -1, 2 },
80 { "baz.bar.foo.", "bar.foo.", dns_namereln_subdomain, 1, 3 },
81 { "bar.foo.", "baz.bar.foo.", dns_namereln_contains, -1, 3 },
82 { NULL, NULL, dns_namereln_none, 0, 0 }
83 };
84
85 UNUSED(state);
86
87 name1 = dns_fixedname_initname(&fixed1);
88 name2 = dns_fixedname_initname(&fixed2);
89 for (i = 0; data[i].name1 != NULL; i++) {
90 int order = 3000;
91 unsigned int nlabels = 3000;
92
93 if (data[i].name1[0] == 0) {
94 dns_fixedname_init(&fixed1);
95 } else {
96 result = dns_name_fromstring(name1, data[i].name1, NULL,
97 0, NULL);
98 assert_int_equal(result, ISC_R_SUCCESS);
99 }
100 if (data[i].name2[0] == 0) {
101 dns_fixedname_init(&fixed2);
102 } else {
103 result = dns_name_fromstring(name2, data[i].name2, NULL,
104 0, NULL);
105 assert_int_equal(result, ISC_R_SUCCESS);
106 }
107 relation = dns_name_fullcompare(name1, name1, &order, &nlabels);
108 assert_int_equal(relation, dns_namereln_equal);
109 assert_int_equal(order, 0);
110 assert_int_equal(nlabels, name1->labels);
111
112 /* Some random initializer */
113 order = 3001;
114 nlabels = 3001;
115
116 relation = dns_name_fullcompare(name1, name2, &order, &nlabels);
117 assert_int_equal(relation, data[i].relation);
118 assert_int_equal(order, data[i].order);
119 assert_int_equal(nlabels, data[i].nlabels);
120 }
121 }
122
123 static void
124 compress_test(const dns_name_t *name1, const dns_name_t *name2,
125 const dns_name_t *name3, unsigned char *compressed,
126 unsigned int compressed_length, unsigned char *expanded,
127 unsigned int expanded_length, dns_compress_t *cctx,
128 dns_decompress_t dctx, bool rdata) {
129 isc_buffer_t source;
130 isc_buffer_t target;
131 dns_name_t name;
132 unsigned char buf1[1024];
133 unsigned char buf2[1024];
134
135 isc_buffer_init(&source, buf1, sizeof(buf1));
136 isc_buffer_init(&target, buf2, sizeof(buf2));
137
138 /*
139 * compression offsets are not allowed to be zero so our
140 * names need to start after a little fake header
141 */
142 isc_buffer_putuint16(&source, 0xEAD);
143 isc_buffer_putuint16(&target, 0xEAD);
144
145 if (rdata) {
146 /* RDATA compression */
147 assert_int_equal(dns_name_towire(name1, cctx, &source, NULL),
148 ISC_R_SUCCESS);
149 assert_int_equal(dns_name_towire(name2, cctx, &source, NULL),
150 ISC_R_SUCCESS);
151 assert_int_equal(dns_name_towire(name2, cctx, &source, NULL),
152 ISC_R_SUCCESS);
153 assert_int_equal(dns_name_towire(name3, cctx, &source, NULL),
154 ISC_R_SUCCESS);
155 } else {
156 /* Owner name compression */
157 uint16_t offset = 0xffff;
158 assert_int_equal(dns_name_towire(name1, cctx, &source, &offset),
159 ISC_R_SUCCESS);
160
161 offset = 0xffff;
162 assert_int_equal(dns_name_towire(name2, cctx, &source, &offset),
163 ISC_R_SUCCESS);
164 assert_int_equal(dns_name_towire(name2, cctx, &source, &offset),
165 ISC_R_SUCCESS);
166
167 offset = 0xffff;
168 assert_int_equal(dns_name_towire(name3, cctx, &source, &offset),
169 ISC_R_SUCCESS);
170 }
171 assert_int_equal(source.used, compressed_length);
172 assert_true(memcmp(source.base, compressed, source.used) == 0);
173
174 isc_buffer_setactive(&source, source.used);
175
176 dns_name_init(&name, NULL);
177 RUNTIME_CHECK(isc_buffer_getuint16(&source) == 0xEAD);
178 RUNTIME_CHECK(dns_name_fromwire(&name, &source, dctx, &target) ==
179 ISC_R_SUCCESS);
180 RUNTIME_CHECK(dns_name_fromwire(&name, &source, dctx, &target) ==
181 ISC_R_SUCCESS);
182 RUNTIME_CHECK(dns_name_fromwire(&name, &source, dctx, &target) ==
183 ISC_R_SUCCESS);
184 RUNTIME_CHECK(dns_name_fromwire(&name, &source, dctx, &target) ==
185 ISC_R_SUCCESS);
186
187 assert_int_equal(target.used, expanded_length);
188 assert_true(memcmp(target.base, expanded, target.used) == 0);
189 }
190
191 /* name compression test */
192 ISC_RUN_TEST_IMPL(compression) {
193 bool permitted;
194 dns_compress_t cctx;
195 dns_decompress_t dctx;
196 dns_name_t name1;
197 dns_name_t name2;
198 dns_name_t name3;
199 dns_name_t name4;
200 isc_region_t r;
201 unsigned char plain1[] = "\003yyy\003foo";
202 unsigned char plain2[] = "\003bar\003yyy\003foo";
203 unsigned char plain3[] = "\003xxx\003bar\003foo";
204 unsigned char plain4[] = "\003xxx\003bar\003zzz";
205
206 unsigned char plain[] = "\x0E\xAD"
207 "\003yyy\003foo\0"
208 "\003bar\003yyy\003foo\0"
209 "\003bar\003yyy\003foo\0"
210 "\003xxx\003bar\003foo";
211
212 unsigned char compressed[29] = "\x0E\xAD"
213 "\003yyy\003foo\0"
214 "\003bar\xc0\x02"
215 "\xc0\x0B"
216 "\003xxx\003bar\xc0\x06";
217 /*
218 * Only the second owner name is compressed.
219 */
220 unsigned char disabled_owner[] = "\x0E\xAD"
221 "\003yyy\003foo\0"
222 "\003bar\003yyy\003foo\0"
223 "\xc0\x0B"
224 "\003xxx\003bar\003foo";
225
226 unsigned char root_plain[] = "\x0E\xAD"
227 "\003yyy\003foo\0"
228 "\0\0"
229 "\003xxx\003bar\003zzz";
230
231 UNUSED(state);
232
233 dns_name_init(&name1, NULL);
234 r.base = plain1;
235 r.length = sizeof(plain1);
236 dns_name_fromregion(&name1, &r);
237
238 dns_name_init(&name2, NULL);
239 r.base = plain2;
240 r.length = sizeof(plain2);
241 dns_name_fromregion(&name2, &r);
242
243 dns_name_init(&name3, NULL);
244 r.base = plain3;
245 r.length = sizeof(plain3);
246 dns_name_fromregion(&name3, &r);
247
248 dns_name_init(&name4, NULL);
249 r.base = plain4;
250 r.length = sizeof(plain3);
251 dns_name_fromregion(&name4, &r);
252
253 /* Test 1: off, rdata */
254 permitted = false;
255 dns_compress_init(&cctx, mctx, 0);
256 dns_compress_setpermitted(&cctx, permitted);
257 dctx = dns_decompress_setpermitted(DNS_DECOMPRESS_DEFAULT, permitted);
258
259 compress_test(&name1, &name2, &name3, plain, sizeof(plain), plain,
260 sizeof(plain), &cctx, dctx, true);
261
262 dns_compress_rollback(&cctx, 0);
263 dns_compress_invalidate(&cctx);
264
265 /* Test2: on, rdata */
266 permitted = true;
267 dns_compress_init(&cctx, mctx, 0);
268 dns_compress_setpermitted(&cctx, permitted);
269 dctx = dns_decompress_setpermitted(DNS_DECOMPRESS_DEFAULT, permitted);
270
271 compress_test(&name1, &name2, &name3, compressed, sizeof(compressed),
272 plain, sizeof(plain), &cctx, dctx, true);
273
274 dns_compress_rollback(&cctx, 0);
275 dns_compress_invalidate(&cctx);
276
277 /* Test3: off, disabled, rdata */
278 permitted = false;
279 dns_compress_init(&cctx, mctx, DNS_COMPRESS_DISABLED);
280 dns_compress_setpermitted(&cctx, permitted);
281 dctx = dns_decompress_setpermitted(DNS_DECOMPRESS_DEFAULT, permitted);
282
283 compress_test(&name1, &name2, &name3, plain, sizeof(plain), plain,
284 sizeof(plain), &cctx, dctx, true);
285
286 dns_compress_rollback(&cctx, 0);
287 dns_compress_invalidate(&cctx);
288
289 /* Test4: on, disabled, rdata */
290 permitted = true;
291 dns_compress_init(&cctx, mctx, DNS_COMPRESS_DISABLED);
292 dns_compress_setpermitted(&cctx, permitted);
293 dctx = dns_decompress_setpermitted(DNS_DECOMPRESS_DEFAULT, permitted);
294
295 compress_test(&name1, &name2, &name3, plain, sizeof(plain), plain,
296 sizeof(plain), &cctx, dctx, true);
297
298 dns_compress_rollback(&cctx, 0);
299 dns_compress_invalidate(&cctx);
300
301 /* Test5: on, rdata */
302 permitted = true;
303 dns_compress_init(&cctx, mctx, 0);
304 dns_compress_setpermitted(&cctx, permitted);
305 dctx = dns_decompress_setpermitted(DNS_DECOMPRESS_DEFAULT, permitted);
306
307 compress_test(&name1, dns_rootname, &name4, root_plain,
308 sizeof(root_plain), root_plain, sizeof(root_plain), &cctx,
309 dctx, true);
310
311 dns_compress_rollback(&cctx, 0);
312 dns_compress_invalidate(&cctx);
313
314 /* Test 6: off, owner */
315 permitted = false;
316 dns_compress_init(&cctx, mctx, 0);
317 dns_compress_setpermitted(&cctx, permitted);
318 dctx = dns_decompress_setpermitted(DNS_DECOMPRESS_DEFAULT, permitted);
319
320 compress_test(&name1, &name2, &name3, plain, sizeof(plain), plain,
321 sizeof(plain), &cctx, dctx, false);
322
323 dns_compress_rollback(&cctx, 0);
324 dns_compress_invalidate(&cctx);
325
326 /* Test7: on, owner */
327 permitted = true;
328 dns_compress_init(&cctx, mctx, 0);
329 dns_compress_setpermitted(&cctx, permitted);
330 dctx = dns_decompress_setpermitted(DNS_DECOMPRESS_DEFAULT, permitted);
331
332 compress_test(&name1, &name2, &name3, compressed, sizeof(compressed),
333 plain, sizeof(plain), &cctx, dctx, false);
334
335 dns_compress_rollback(&cctx, 0);
336 dns_compress_invalidate(&cctx);
337
338 /* Test8: off, disabled, owner */
339 permitted = false;
340 dns_compress_init(&cctx, mctx, DNS_COMPRESS_DISABLED);
341 dns_compress_setpermitted(&cctx, permitted);
342 dctx = dns_decompress_setpermitted(DNS_DECOMPRESS_DEFAULT, permitted);
343
344 compress_test(&name1, &name2, &name3, plain, sizeof(plain), plain,
345 sizeof(plain), &cctx, dctx, false);
346
347 dns_compress_rollback(&cctx, 0);
348 dns_compress_invalidate(&cctx);
349
350 /* Test9: on, disabled, owner */
351 permitted = true;
352 dns_compress_init(&cctx, mctx, DNS_COMPRESS_DISABLED);
353 dns_compress_setpermitted(&cctx, permitted);
354 dctx = dns_decompress_setpermitted(DNS_DECOMPRESS_DEFAULT, permitted);
355
356 compress_test(&name1, &name2, &name3, disabled_owner,
357 sizeof(disabled_owner), plain, sizeof(plain), &cctx, dctx,
358 false);
359
360 dns_compress_rollback(&cctx, 0);
361 dns_compress_invalidate(&cctx);
362
363 /* Test10: on, owner */
364 permitted = true;
365 dns_compress_init(&cctx, mctx, 0);
366 dns_compress_setpermitted(&cctx, permitted);
367 dctx = dns_decompress_setpermitted(DNS_DECOMPRESS_DEFAULT, permitted);
368
369 compress_test(&name1, dns_rootname, &name4, root_plain,
370 sizeof(root_plain), root_plain, sizeof(root_plain), &cctx,
371 dctx, false);
372
373 dns_compress_rollback(&cctx, 0);
374 dns_compress_invalidate(&cctx);
375 }
376
377 #define NAME_LO 25
378 #define NAME_HI 250000
379
380 /*
381 * test compression context hash set collisions and rollbacks
382 */
383 ISC_RUN_TEST_IMPL(collision) {
384 isc_result_t result;
385 isc_region_t r;
386 dns_compress_t cctx;
387 isc_buffer_t message;
388 uint8_t msgbuf[65536];
389 dns_name_t name;
390 char namebuf[256];
391 uint8_t offsets[128];
392
393 dns_compress_init(&cctx, mctx, DNS_COMPRESS_LARGE);
394 isc_buffer_init(&message, msgbuf, sizeof(msgbuf));
395 dns_name_init(&name, offsets);
396
397 /*
398 * compression offsets are not allowed to be zero so our
399 * names need to start after a little fake header
400 */
401 isc_buffer_putuint16(&message, 0xEAD);
402
403 static const char zone[] = "test";
404 const int zonelen = sizeof(zone) - 1;
405 unsigned int zone_coff = 0;
406
407 for (int i = NAME_LO; i < NAME_HI; i++) {
408 unsigned int prefix_len, suffix_coff;
409 unsigned int coff = isc_buffer_usedlength(&message);
410
411 int len = snprintf(namebuf, sizeof(namebuf), ".%d%c%s", i,
412 zonelen, zone);
413 namebuf[0] = len - zonelen - 2;
414 r = (isc_region_t){ .base = (uint8_t *)namebuf,
415 .length = len + 1 };
416 dns_name_fromregion(&name, &r);
417
418 /* the name we are about to add must partially match */
419 prefix_len = name.length;
420 suffix_coff = 0;
421 dns_compress_name(&cctx, &message, &name, &prefix_len,
422 &suffix_coff);
423 if (i == NAME_LO) {
424 assert_int_equal(prefix_len, name.length);
425 assert_int_equal(suffix_coff, 0);
426 zone_coff = 2 + len - zonelen - 1;
427 } else {
428 assert_int_equal(prefix_len, len - zonelen - 1);
429 assert_int_equal(suffix_coff, zone_coff);
430 }
431 dns_compress_rollback(&cctx, coff);
432
433 result = dns_name_towire(&name, &cctx, &message, NULL);
434 assert_int_equal(result, ISC_R_SUCCESS);
435
436 /* we must be able to find the name we just added */
437 prefix_len = name.length;
438 suffix_coff = 0;
439 dns_compress_name(&cctx, &message, &name, &prefix_len,
440 &suffix_coff);
441 assert_int_equal(prefix_len, 0);
442 assert_int_equal(suffix_coff, coff);
443
444 /* don't let the hash set get too full */
445 if (cctx.count > cctx.mask * 3 / 5) {
446 dns_compress_rollback(&cctx, zone_coff + zonelen + 2);
447 isc_buffer_clear(&message);
448 isc_buffer_add(&message, zone_coff + zonelen + 2);
449 }
450 }
451
452 dns_compress_invalidate(&cctx);
453 }
454
455 ISC_RUN_TEST_IMPL(fromregion) {
456 dns_name_t name;
457 isc_buffer_t b;
458 isc_region_t r;
459 /*
460 * target and source need to be bigger than DNS_NAME_MAXWIRE to
461 * exercise 'len > DNS_NAME_MAXWIRE' test in dns_name_fromwire
462 */
463 unsigned char target[DNS_NAME_MAXWIRE + 10];
464 unsigned char source[DNS_NAME_MAXWIRE + 10] = { '\007', 'e', 'x', 'a',
465 'm', 'p', 'l', 'e' };
466 /*
467 * Extract the fully qualified name at the beginning of 'source'
468 * into 'name' where 'name.ndata' points to the buffer 'target'.
469 */
470 isc_buffer_init(&b, target, sizeof(target));
471 dns_name_init(&name, NULL);
472 dns_name_setbuffer(&name, &b);
473 r.base = source;
474 r.length = sizeof(source);
475 dns_name_fromregion(&name, &r);
476 assert_int_equal(9, name.length);
477 assert_ptr_equal(target, name.ndata);
478 assert_true(dns_name_isabsolute(&name));
479
480 /*
481 * Extract the fully qualified name at the beginning of 'source'
482 * into 'name' where 'name.ndata' points to the source.
483 */
484 isc_buffer_init(&b, target, sizeof(target));
485 dns_name_init(&name, NULL);
486 r.base = source;
487 r.length = sizeof(source);
488 dns_name_fromregion(&name, &r);
489 assert_int_equal(9, name.length);
490 assert_ptr_equal(source, name.ndata);
491 assert_true(dns_name_isabsolute(&name));
492
493 /*
494 * Extract the partially qualified name in 'source' into 'name'
495 * where 'name.ndata' points to the source.
496 */
497 isc_buffer_init(&b, target, sizeof(target));
498 dns_name_init(&name, NULL);
499 r.base = source;
500 r.length = 8;
501 dns_name_fromregion(&name, &r);
502 assert_int_equal(8, name.length);
503 assert_ptr_equal(source, name.ndata);
504 assert_false(dns_name_isabsolute(&name));
505
506 /*
507 * Extract empty name in 'source' into 'name'.
508 */
509 isc_buffer_init(&b, target, sizeof(target));
510 dns_name_init(&name, NULL);
511 r.base = source;
512 r.length = 0;
513 dns_name_fromregion(&name, &r);
514 assert_int_equal(0, name.length);
515 assert_ptr_equal(source, name.ndata);
516 assert_false(dns_name_isabsolute(&name));
517 }
518
519 /* is trust-anchor-telemetry test */
520 ISC_RUN_TEST_IMPL(istat) {
521 dns_fixedname_t fixed;
522 dns_name_t *name;
523 isc_result_t result;
524 size_t i;
525 struct {
526 const char *name;
527 bool istat;
528 } data[] = { { ".", false },
529 { "_ta-", false },
530 { "_ta-1234", true },
531 { "_TA-1234", true },
532 { "+TA-1234", false },
533 { "_fa-1234", false },
534 { "_td-1234", false },
535 { "_ta_1234", false },
536 { "_ta-g234", false },
537 { "_ta-1h34", false },
538 { "_ta-12i4", false },
539 { "_ta-123j", false },
540 { "_ta-1234-abcf", true },
541 { "_ta-1234-abcf-ED89", true },
542 { "_ta-12345-abcf-ED89", false },
543 { "_ta-.example", false },
544 { "_ta-1234.example", true },
545 { "_ta-1234-abcf.example", true },
546 { "_ta-1234-abcf-ED89.example", true },
547 { "_ta-12345-abcf-ED89.example", false },
548 { "_ta-1234-abcfe-ED89.example", false },
549 { "_ta-1234-abcf-EcD89.example", false } };
550
551 UNUSED(state);
552
553 name = dns_fixedname_initname(&fixed);
554
555 for (i = 0; i < (sizeof(data) / sizeof(data[0])); i++) {
556 result = dns_name_fromstring(name, data[i].name, dns_rootname,
557 0, NULL);
558 assert_int_equal(result, ISC_R_SUCCESS);
559 assert_int_equal(dns_name_istat(name), data[i].istat);
560 }
561 }
562
563 static bool
564 name_attr_zero(struct dns_name_attrs attributes) {
565 return !(attributes.absolute | attributes.readonly |
566 attributes.dynamic | attributes.dynoffsets |
567 attributes.nocompress | attributes.cache | attributes.answer |
568 attributes.ncache | attributes.chaining | attributes.chase |
569 attributes.wildcard | attributes.prerequisite |
570 attributes.update | attributes.hasupdaterec);
571 }
572
573 /* dns_name_init */
574 ISC_RUN_TEST_IMPL(init) {
575 dns_name_t name;
576 unsigned char offsets[1];
577
578 UNUSED(state);
579
580 dns_name_init(&name, offsets);
581
582 assert_null(name.ndata);
583 assert_int_equal(name.length, 0);
584 assert_int_equal(name.labels, 0);
585 assert_ptr_equal(name.offsets, offsets);
586 assert_null(name.buffer);
587 assert_true(name_attr_zero(name.attributes));
588 }
589
590 /* dns_name_invalidate */
591 ISC_RUN_TEST_IMPL(invalidate) {
592 dns_name_t name;
593 unsigned char offsets[1];
594
595 UNUSED(state);
596
597 dns_name_init(&name, offsets);
598 dns_name_invalidate(&name);
599
600 assert_null(name.ndata);
601 assert_int_equal(name.length, 0);
602 assert_int_equal(name.labels, 0);
603 assert_null(name.offsets);
604 assert_null(name.buffer);
605 assert_true(name_attr_zero(name.attributes));
606 }
607
608 /* dns_name_setbuffer/hasbuffer */
609 ISC_RUN_TEST_IMPL(buffer) {
610 dns_name_t name;
611 unsigned char buf[BUFSIZ];
612 isc_buffer_t b;
613
614 UNUSED(state);
615
616 isc_buffer_init(&b, buf, BUFSIZ);
617 dns_name_init(&name, NULL);
618 dns_name_setbuffer(&name, &b);
619 assert_ptr_equal(name.buffer, &b);
620 assert_true(dns_name_hasbuffer(&name));
621 }
622
623 /* dns_name_isabsolute */
624 ISC_RUN_TEST_IMPL(isabsolute) {
625 struct {
626 const char *namestr;
627 bool expect;
628 } testcases[] = { { "x", false },
629 { "a.b.c.d.", true },
630 { "x.z", false } };
631 unsigned int i;
632
633 UNUSED(state);
634
635 for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) {
636 isc_result_t result;
637 dns_name_t name;
638 unsigned char data[BUFSIZ];
639 isc_buffer_t b, nb;
640 size_t len;
641
642 len = strlen(testcases[i].namestr);
643 isc_buffer_constinit(&b, testcases[i].namestr, len);
644 isc_buffer_add(&b, len);
645
646 dns_name_init(&name, NULL);
647 isc_buffer_init(&nb, data, BUFSIZ);
648 dns_name_setbuffer(&name, &nb);
649 result = dns_name_fromtext(&name, &b, NULL, 0, NULL);
650 assert_int_equal(result, ISC_R_SUCCESS);
651
652 assert_int_equal(dns_name_isabsolute(&name),
653 testcases[i].expect);
654 }
655 }
656
657 /* dns_name_hash */
658 ISC_RUN_TEST_IMPL(hash) {
659 struct {
660 const char *name1;
661 const char *name2;
662 bool expect;
663 bool expecti;
664 } testcases[] = {
665 { "a.b.c.d", "A.B.C.D", true, false },
666 { "a.b.c.d.", "A.B.C.D.", true, false },
667 { "a.b.c.d", "a.b.c.d", true, true },
668 { "A.B.C.D.", "A.B.C.D.", true, false },
669 { "x.y.z.w", "a.b.c.d", false, false },
670 { "x.y.z.w.", "a.b.c.d.", false, false },
671 };
672 unsigned int i;
673
674 UNUSED(state);
675
676 for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) {
677 isc_result_t result;
678 dns_fixedname_t f1, f2;
679 dns_name_t *n1, *n2;
680 unsigned int h1, h2;
681
682 n1 = dns_fixedname_initname(&f1);
683 n2 = dns_fixedname_initname(&f2);
684
685 result = dns_name_fromstring(n1, testcases[i].name1, NULL, 0,
686 NULL);
687 assert_int_equal(result, ISC_R_SUCCESS);
688 result = dns_name_fromstring(n2, testcases[i].name2, NULL, 0,
689 NULL);
690 assert_int_equal(result, ISC_R_SUCCESS);
691
692 /* Check case-insensitive hashing first */
693 h1 = dns_name_hash(n1);
694 h2 = dns_name_hash(n2);
695
696 if (verbose) {
697 print_message("# %s hashes to %u, "
698 "%s to %u, case insensitive\n",
699 testcases[i].name1, h1,
700 testcases[i].name2, h2);
701 }
702
703 assert_int_equal((h1 == h2), testcases[i].expect);
704
705 /* Now case-sensitive */
706 h1 = dns_name_hash(n1);
707 h2 = dns_name_hash(n2);
708
709 if (verbose) {
710 print_message("# %s hashes to %u, "
711 "%s to %u, case sensitive\n",
712 testcases[i].name1, h1,
713 testcases[i].name2, h2);
714 }
715
716 assert_int_equal((h1 == h2), testcases[i].expect);
717 }
718 }
719
720 /* dns_name_issubdomain */
721 ISC_RUN_TEST_IMPL(issubdomain) {
722 struct {
723 const char *name1;
724 const char *name2;
725 bool expect;
726 } testcases[] = {
727 { "c.d", "a.b.c.d", false }, { "c.d.", "a.b.c.d.", false },
728 { "b.c.d", "c.d", true }, { "a.b.c.d.", "c.d.", true },
729 { "a.b.c", "a.b.c", true }, { "a.b.c.", "a.b.c.", true },
730 { "x.y.z", "a.b.c", false }
731 };
732 unsigned int i;
733
734 UNUSED(state);
735
736 for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) {
737 isc_result_t result;
738 dns_fixedname_t f1, f2;
739 dns_name_t *n1, *n2;
740
741 n1 = dns_fixedname_initname(&f1);
742 n2 = dns_fixedname_initname(&f2);
743
744 result = dns_name_fromstring(n1, testcases[i].name1, NULL, 0,
745 NULL);
746 assert_int_equal(result, ISC_R_SUCCESS);
747 result = dns_name_fromstring(n2, testcases[i].name2, NULL, 0,
748 NULL);
749 assert_int_equal(result, ISC_R_SUCCESS);
750
751 if (verbose) {
752 print_message("# check: %s %s a subdomain of %s\n",
753 testcases[i].name1,
754 testcases[i].expect ? "is" : "is not",
755 testcases[i].name2);
756 }
757
758 assert_int_equal(dns_name_issubdomain(n1, n2),
759 testcases[i].expect);
760 }
761 }
762
763 /* dns_name_countlabels */
764 ISC_RUN_TEST_IMPL(countlabels) {
765 struct {
766 const char *namestr;
767 unsigned int expect;
768 } testcases[] = {
769 { "c.d", 2 },
770 { "c.d.", 3 },
771 { "a.b.c.d.", 5 },
772 { "a.b.c.d", 4 },
773 { "a.b.c", 3 },
774 { ".", 1 },
775 { "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y."
776 "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y."
777 "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y."
778 "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y."
779 "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y."
780 "a.b.",
781 128 },
782 };
783 unsigned int i;
784
785 UNUSED(state);
786
787 for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) {
788 isc_result_t result;
789 dns_fixedname_t fname;
790 dns_name_t *name;
791
792 name = dns_fixedname_initname(&fname);
793
794 result = dns_name_fromstring(name, testcases[i].namestr, NULL,
795 0, NULL);
796 assert_int_equal(result, ISC_R_SUCCESS);
797
798 if (verbose) {
799 print_message("# %s: expect %u labels\n",
800 testcases[i].namestr,
801 testcases[i].expect);
802 }
803
804 assert_int_equal(dns_name_countlabels(name),
805 testcases[i].expect);
806 }
807 }
808
809 /* dns_name_getlabel */
810 ISC_RUN_TEST_IMPL(getlabel) {
811 struct {
812 const char *name1;
813 unsigned int pos1;
814 const char *name2;
815 unsigned int pos2;
816 } testcases[] = {
817 { "c.d", 1, "a.b.c.d", 3 },
818 { "a.b.c.d", 3, "c.d", 1 },
819 { "a.b.c.", 3, "A.B.C.", 3 },
820 };
821 unsigned int i;
822
823 UNUSED(state);
824
825 for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) {
826 isc_result_t result;
827 dns_fixedname_t f1, f2;
828 dns_name_t *n1, *n2;
829 dns_label_t l1, l2;
830 unsigned int j;
831
832 n1 = dns_fixedname_initname(&f1);
833 n2 = dns_fixedname_initname(&f2);
834
835 result = dns_name_fromstring(n1, testcases[i].name1, NULL, 0,
836 NULL);
837 assert_int_equal(result, ISC_R_SUCCESS);
838 result = dns_name_fromstring(n2, testcases[i].name2, NULL, 0,
839 NULL);
840 assert_int_equal(result, ISC_R_SUCCESS);
841
842 dns_name_getlabel(n1, testcases[i].pos1, &l1);
843 dns_name_getlabel(n2, testcases[i].pos2, &l2);
844 assert_int_equal(l1.length, l2.length);
845
846 for (j = 0; j < l1.length; j++) {
847 assert_int_equal(l1.base[j], l2.base[j]);
848 }
849 }
850 }
851
852 /* dns_name_getlabelsequence */
853 ISC_RUN_TEST_IMPL(getlabelsequence) {
854 struct {
855 const char *name1;
856 unsigned int pos1;
857 const char *name2;
858 unsigned int pos2;
859 unsigned int range;
860 } testcases[] = {
861 { "c.d", 1, "a.b.c.d", 3, 1 },
862 { "a.b.c.d.e", 2, "c.d", 0, 2 },
863 { "a.b.c", 0, "a.b.c", 0, 3 },
864 };
865 unsigned int i;
866
867 UNUSED(state);
868
869 for (i = 0; i < (sizeof(testcases) / sizeof(testcases[0])); i++) {
870 isc_result_t result;
871 dns_name_t t1, t2;
872 dns_fixedname_t f1, f2;
873 dns_name_t *n1, *n2;
874
875 /* target names */
876 dns_name_init(&t1, NULL);
877 dns_name_init(&t2, NULL);
878
879 /* source names */
880 n1 = dns_fixedname_initname(&f1);
881 n2 = dns_fixedname_initname(&f2);
882
883 result = dns_name_fromstring(n1, testcases[i].name1, NULL, 0,
884 NULL);
885 assert_int_equal(result, ISC_R_SUCCESS);
886 result = dns_name_fromstring(n2, testcases[i].name2, NULL, 0,
887 NULL);
888 assert_int_equal(result, ISC_R_SUCCESS);
889
890 dns_name_getlabelsequence(n1, testcases[i].pos1,
891 testcases[i].range, &t1);
892 dns_name_getlabelsequence(n2, testcases[i].pos2,
893 testcases[i].range, &t2);
894
895 assert_true(dns_name_equal(&t1, &t2));
896 }
897 }
898
899 ISC_RUN_TEST_IMPL(maxlabels) {
900 isc_result_t result;
901 dns_fixedname_t fixed;
902 dns_name_t *name = NULL;
903
904 const char one_too_many[] =
905 "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y."
906 "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y."
907 "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y."
908 "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y."
909 "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y."
910 "a.b.c.";
911
912 name = dns_fixedname_initname(&fixed);
913 result = dns_name_fromstring(name, one_too_many, dns_rootname, 0, NULL);
914 assert_int_equal(result, ISC_R_NOSPACE);
915
916 name = dns_fixedname_initname(&fixed);
917 result = dns_name_fromstring(name, one_too_many + 2, dns_rootname, 0,
918 NULL);
919 assert_int_equal(result, ISC_R_SUCCESS);
920 assert_true(dns_name_isvalid(name));
921 assert_int_equal(dns_name_countlabels(name), DNS_NAME_MAXLABELS);
922 }
923
924 #ifdef DNS_BENCHMARK_TESTS
925
926 /*
927 * XXXMUKS: Don't delete this code. It is useful in benchmarking the
928 * name parser, but we don't require it as part of the unit test runs.
929 */
930
931 /* Benchmark dns_name_fromwire() implementation */
932
933 ISC_RUN_TEST_IMPL(fromwire_thread(void *arg) {
934 unsigned int maxval = 32000000;
935 uint8_t data[] = { 3, 'w', 'w', 'w', 7, 'e', 'x',
936 'a', 'm', 'p', 'l', 'e', 7, 'i',
937 'n', 'v', 'a', 'l', 'i', 'd', 0 };
938 unsigned char output_data[DNS_NAME_MAXWIRE];
939 isc_buffer_t source, target;
940 unsigned int i;
941 dns_decompress_t dctx;
942
943 UNUSED(arg);
944
945 dns_decompress_init(&dctx, DNS_DECOMPRESS_STRICT);
946 dns_decompress_setmethods(&dctx, DNS_COMPRESS_NONE);
947
948 isc_buffer_init(&source, data, sizeof(data));
949 isc_buffer_add(&source, sizeof(data));
950 isc_buffer_init(&target, output_data, sizeof(output_data));
951
952 /* Parse 32 million names in each thread */
953 for (i = 0; i < maxval; i++) {
954 dns_name_t name;
955
956 isc_buffer_clear(&source);
957 isc_buffer_clear(&target);
958 isc_buffer_add(&source, sizeof(data));
959 isc_buffer_setactive(&source, sizeof(data));
960
961 dns_name_init(&name, NULL);
962 (void)dns_name_fromwire(&name, &source, &dctx, &target);
963 }
964
965 return NULL;
966 }
967
968 ISC_RUN_TEST_IMPL(benchmark) {
969 isc_result_t result;
970 unsigned int i;
971 isc_time_t ts1, ts2;
972 double t;
973 unsigned int nthreads;
974 isc_thread_t threads[32];
975
976 UNUSED(state);
977
978 debug_mem_record = false;
979
980 result = isc_time_now(&ts1);
981 assert_int_equal(result, ISC_R_SUCCESS);
982
983 nthreads = ISC_MIN(isc_os_ncpus(), 32);
984 nthreads = ISC_MAX(nthreads, 1);
985 for (i = 0; i < nthreads; i++) {
986 isc_thread_create(fromwire_thread, NULL, &threads[i]);
987 }
988
989 for (i = 0; i < nthreads; i++) {
990 isc_thread_join(threads[i], NULL);
991 }
992
993 result = isc_time_now(&ts2);
994 assert_int_equal(result, ISC_R_SUCCESS);
995
996 t = isc_time_microdiff(&ts2, &ts1);
997
998 printf("%u dns_name_fromwire() calls, %f seconds, %f calls/second\n",
999 nthreads * 32000000, t / 1000000.0,
1000 (nthreads * 32000000) / (t / 1000000.0));
1001 }
1002
1003 #endif /* DNS_BENCHMARK_TESTS */
1004
1005 ISC_TEST_LIST_START
1006 ISC_TEST_ENTRY(fullcompare)
1007 ISC_TEST_ENTRY(compression)
1008 ISC_TEST_ENTRY(collision)
1009 ISC_TEST_ENTRY(fromregion)
1010 ISC_TEST_ENTRY(istat)
1011 ISC_TEST_ENTRY(init)
1012 ISC_TEST_ENTRY(invalidate)
1013 ISC_TEST_ENTRY(buffer)
1014 ISC_TEST_ENTRY(isabsolute)
1015 ISC_TEST_ENTRY(hash)
1016 ISC_TEST_ENTRY(issubdomain)
1017 ISC_TEST_ENTRY(countlabels)
1018 ISC_TEST_ENTRY(getlabel)
1019 ISC_TEST_ENTRY(getlabelsequence)
1020 ISC_TEST_ENTRY(maxlabels)
1021 #ifdef DNS_BENCHMARK_TESTS
1022 ISC_TEST_ENTRY(benchmark)
1023 #endif /* DNS_BENCHMARK_TESTS */
1024 ISC_TEST_LIST_END
1025
1026 ISC_TEST_MAIN