No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / tests / names / t_names.c
blob8232fd928e857941dc3b74ffc6f75c4bfd69a1af
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: t_names.c,v 1.50 2009/09/01 23:47:44 tbox Exp */
22 #include <config.h>
24 #include <ctype.h>
25 #include <stdlib.h>
27 #include <isc/buffer.h>
28 #include <isc/mem.h>
29 #include <isc/string.h>
31 #include <dns/compress.h>
32 #include <dns/name.h>
33 #include <dns/result.h>
35 #include <tests/t_api.h>
37 #define MAXTOKS 16
38 #define BUFLEN 256
39 #define BIGBUFLEN 4096
41 static char *Tokens[MAXTOKS + 1];
44 #ifdef NEED_PBUF
46 /*%
47 * get a hex formatted dns message from a data
48 * file into an isc_buffer_t
49 * caller supplies data storage and the isc_buffer
50 * we read the file, convert, setup the buffer
51 * and return the data length
54 static char *
55 ctoh(unsigned char c) {
56 int val;
57 static char buf[3];
59 val = (c >> 4) & 0x0f;
60 if ((0 <= val) && (val <= 9))
61 buf[0] = '0' + val;
62 else if ((10 <= val) && (val <= 16))
63 buf[0] = 'a' + val - 10;
64 val = c & 0x0f;
65 if ((0 <= val) && (val <= 9))
66 buf[1] = '0' + val;
67 else if ((10 <= val) && (val <= 16))
68 buf[1] = 'a' + val - 10;
69 buf[2] = '\0';
70 return (buf);
73 static void
74 pbuf(isc_buffer_t *pbuf) {
75 size_t len;
76 unsigned char *p;
78 len = 0;
79 p = pbuf->base;
80 while (len < pbuf->length) {
81 printf("%s", ctoh(*p));
82 ++p;
83 ++len;
84 if ((len % 40) == 0)
85 printf("\n");
89 #endif /* NEED_PBUF */
91 /*%
92 * Compare data at buf with data in hex representation at exp_data,
93 * of length exp_data_len, for equality.
94 * Return 0 if equal, else non-zero.
97 static int
98 chkdata(unsigned char *buf, size_t buflen, char *exp_data,
99 size_t exp_data_len)
101 int result;
102 unsigned char *p;
103 unsigned char *v;
104 char *q;
105 unsigned char *data;
106 size_t cnt;
108 if (buflen == exp_data_len) {
109 data = (unsigned char *)malloc(exp_data_len *
110 sizeof(unsigned char));
111 if (data == NULL) {
112 t_info("malloc failed unexpectedly\n");
113 return (-1);
117 * First convert exp_data from hex format.
119 p = data;
120 q = exp_data;
121 cnt = 0;
122 while (cnt < exp_data_len) {
124 if (('0' <= *q) && (*q <= '9'))
125 *p = *q - '0';
126 else if (('a' <= *q) && (*q <= 'z'))
127 *p = *q - 'a' + 10;
128 else if (('A' <= *q) && (*q <= 'Z'))
129 *p = *q - 'A' + 10;
130 ++q;
132 *p <<= 4;
134 if (('0' <= *q) && (*q <= '9'))
135 *p |= ((*q - '0') & 0x0f);
136 else if (('a' <= *q) && (*q <= 'z'))
137 *p |= ((*q - 'a' + 10) & 0x0f);
138 else if (('A' <= *q) && (*q <= 'Z'))
139 *p |= ((*q - 'A' + 10) & 0x0f);
140 ++p;
141 ++q;
142 ++cnt;
146 * Now compare data.
148 p = buf;
149 v = data;
150 for (cnt = 0; cnt < exp_data_len; ++cnt) {
151 if (*p != *v)
152 break;
153 ++p;
154 ++v;
156 if (cnt == exp_data_len)
157 result = 0;
158 else {
159 t_info("bad data at position %lu, "
160 "got 0x%.2x, expected 0x%.2x\n",
161 (unsigned long)cnt, *p, *q);
162 result = cnt + 1;
164 (void)free(data);
165 } else {
166 t_info("data length error, expected %lu, got %lu\n",
167 (unsigned long)exp_data_len, (unsigned long)buflen);
168 result = exp_data_len - buflen;
170 return (result);
174 * Get a hex formatted dns message from a data file into an isc_buffer_t.
175 * Caller supplies data storage and the isc_buffer. We read the file, convert,
176 * setup the buffer and return the data length.
178 static int
179 getmsg(char *datafile_name, unsigned char *buf, int buflen, isc_buffer_t *pbuf)
181 int c;
182 int len;
183 int cnt;
184 unsigned int val;
185 unsigned char *p;
186 FILE *fp;
188 fp = fopen(datafile_name, "r");
189 if (fp == NULL) {
190 t_info("No such file %s\n", datafile_name);
191 return (0);
194 p = buf;
195 cnt = 0;
196 len = 0;
197 val = 0;
198 while ((c = getc(fp)) != EOF) {
199 if ( (c == ' ') || (c == '\t') ||
200 (c == '\r') || (c == '\n'))
201 continue;
202 if (c == '#') {
203 while ((c = getc(fp)) != '\n')
205 continue;
207 if (('0' <= c) && (c <= '9'))
208 val = c - '0';
209 else if (('a' <= c) && (c <= 'z'))
210 val = c - 'a' + 10;
211 else if (('A' <= c) && (c <= 'Z'))
212 val = c - 'A'+ 10;
213 else {
214 (void)fclose(fp);
215 t_info("Bad format in datafile\n");
216 return (0);
218 if ((len % 2) == 0) {
219 *p = (val << 4);
220 } else {
221 *p += val;
222 ++p;
223 ++cnt;
224 if (cnt >= buflen) {
226 * Buffer too small.
228 (void)fclose(fp);
229 t_info("Buffer overflow error\n");
230 return (0);
233 ++len;
235 (void)fclose(fp);
237 if (len % 2) {
238 t_info("Bad format in %s\n", datafile_name);
239 return (0);
242 *p = '\0';
243 isc_buffer_init(pbuf, buf, cnt);
244 isc_buffer_add(pbuf, cnt);
245 return (cnt);
248 static int
249 bustline(char *line, char **toks) {
250 int cnt;
251 char *p;
253 cnt = 0;
254 if (line && *line) {
255 while ((p = strtok(line, "\t")) && (cnt < MAXTOKS)) {
256 *toks++ = p;
257 line = NULL;
258 ++cnt;
261 return (cnt);
265 #ifdef NEED_HNAME_TO_TNAME
268 * convert a name from hex representation to text form
269 * format of hex notation is:
270 * %xXXXXXXXX
273 static int
274 hname_to_tname(char *src, char *target, size_t len) {
275 int i;
276 int c;
277 unsigned int val;
278 size_t srclen;
279 char *p;
280 char *q;
282 p = src;
283 srclen = strlen(p);
284 if ((srclen >= 2) && ((*p != '%') || (*(p+1) != 'x'))) {
286 * No conversion needed.
288 if (srclen >= len)
289 return (1);
290 memcpy(target, src, srclen + 1);
291 return (0);
294 i = 0;
295 p += 2;
296 q = target;
297 while (*p) {
298 c = *p;
299 if (('0' < c) && (c <= '9'))
300 val = c - '0';
301 else if (('a' <= c) && (c <= 'z'))
302 val = c + 10 - 'a';
303 else if (('A' <= c) && (c <= 'Z'))
304 val = c + 10 - 'A';
305 else {
306 return (1);
308 if (i % 2) {
309 *q |= val;
310 ++q;
311 } else
312 *q = (val << 4);
313 ++i;
314 ++p;
316 if (i % 2) {
317 return (1);
318 } else {
319 *q = '\0';
320 return (0);
324 #endif /* NEED_HNAME_TO_TNAME */
327 * initialize a dns_name_t from a text name, hiding all
328 * buffer and other object initialization from the caller
332 static isc_result_t
333 dname_from_tname(char *name, dns_name_t *dns_name) {
334 int len;
335 isc_buffer_t txtbuf;
336 isc_buffer_t *binbuf;
337 unsigned char *junk;
338 isc_result_t result;
340 len = strlen(name);
341 isc_buffer_init(&txtbuf, name, len);
342 isc_buffer_add(&txtbuf, len);
343 junk = (unsigned char *)malloc(sizeof(unsigned char) * BUFLEN);
344 binbuf = (isc_buffer_t *)malloc(sizeof(isc_buffer_t));
345 if ((junk != NULL) && (binbuf != NULL)) {
346 isc_buffer_init(binbuf, junk, BUFLEN);
347 dns_name_init(dns_name, NULL);
348 dns_name_setbuffer(dns_name, binbuf);
349 result = dns_name_fromtext(dns_name, &txtbuf, NULL, 0, NULL);
350 } else {
351 result = ISC_R_NOSPACE;
352 if (junk != NULL)
353 (void)free(junk);
354 if (binbuf != NULL)
355 (void)free(binbuf);
357 return (result);
360 static const char *a3 = "dns_name_init initializes 'name' to the empty name";
362 static void
363 t_dns_name_init(void) {
364 int rval;
365 int result;
366 dns_name_t name;
367 unsigned char offsets[1];
369 rval = 0;
370 t_assert("dns_name_init", 1, T_REQUIRED, "%s", a3);
372 dns_name_init(&name, offsets);
373 /* magic is hidden in name.c ...
374 if (name.magic != NAME_MAGIC) {
375 t_info("name.magic is not set to NAME_MAGIC\n");
376 ++rval;
379 if (name.ndata != NULL) {
380 t_info("name.ndata is not NULL\n");
381 ++rval;
383 if (name.length != 0) {
384 t_info("name.length is not 0\n");
385 ++rval;
387 if (name.labels != 0) {
388 t_info("name.labels is not 0\n");
389 ++rval;
391 if (name.attributes != 0) {
392 t_info("name.attributes is not 0\n");
393 ++rval;
395 if (name.offsets != offsets) {
396 t_info("name.offsets is incorrect\n");
397 ++rval;
399 if (name.buffer != NULL) {
400 t_info("name.buffer is not NULL\n");
401 ++rval;
404 if (rval == 0)
405 result = T_PASS;
406 else
407 result = T_FAIL;
408 t_result(result);
411 static const char *a4 = "dns_name_invalidate invalidates 'name'";
413 static void
414 t_dns_name_invalidate(void) {
415 int rval;
416 int result;
417 dns_name_t name;
418 unsigned char offsets[1];
420 t_assert("dns_name_invalidate", 1, T_REQUIRED, "%s", a4);
422 rval = 0;
423 dns_name_init(&name, offsets);
424 dns_name_invalidate(&name);
426 /* magic is hidden in name.c ...
427 if (name.magic != 0) {
428 t_info("name.magic is not set to NAME_MAGIC\n");
429 ++rval;
432 if (name.ndata != NULL) {
433 t_info("name.ndata is not NULL\n");
434 ++rval;
436 if (name.length != 0) {
437 t_info("name.length is not 0\n");
438 ++rval;
440 if (name.labels != 0) {
441 t_info("name.labels is not 0\n");
442 ++rval;
444 if (name.attributes != 0) {
445 t_info("name.attributes is not 0\n");
446 ++rval;
448 if (name.offsets != NULL) {
449 t_info("name.offsets is not NULL\n");
450 ++rval;
452 if (name.buffer != NULL) {
453 t_info("name.buffer is not NULL\n");
454 ++rval;
457 if (rval == 0)
458 result = T_PASS;
459 else
460 result = T_FAIL;
461 t_result(result);
464 static const char *a5 = "dns_name_setbuffer dedicates a buffer for use "
465 "with 'name'";
467 static void
468 t_dns_name_setbuffer(void) {
469 int result;
470 unsigned char junk[BUFLEN];
471 dns_name_t name;
472 isc_buffer_t buffer;
474 t_assert("dns_name_setbuffer", 1, T_REQUIRED, "%s", a5);
476 isc_buffer_init(&buffer, junk, BUFLEN);
477 dns_name_init(&name, NULL);
478 dns_name_setbuffer(&name, &buffer);
479 if (name.buffer == &buffer)
480 result = T_PASS;
481 else
482 result = T_FAIL;
484 t_result(result);
487 static const char *a6 = "dns_name_hasbuffer returns ISC_TRUE if 'name' has a "
488 "dedicated buffer, otherwise it returns ISC_FALSE";
490 static void
491 t_dns_name_hasbuffer(void) {
492 int result;
493 int rval;
494 unsigned char junk[BUFLEN];
495 dns_name_t name;
496 isc_buffer_t buffer;
498 t_assert("dns_name_hasbuffer", 1, T_REQUIRED, "%s", a6);
500 rval = 0;
501 isc_buffer_init(&buffer, junk, BUFLEN);
502 dns_name_init(&name, NULL);
503 if (dns_name_hasbuffer(&name) != ISC_FALSE)
504 ++rval;
505 dns_name_setbuffer(&name, &buffer);
506 if (dns_name_hasbuffer(&name) != ISC_TRUE)
507 ++rval;
508 if (rval == 0)
509 result = T_PASS;
510 else
511 result = T_FAIL;
513 t_result(result);
516 static const char *a7 = "dns_name_isabsolute returns ISC_TRUE if 'name' ends "
517 "in the root label";
519 static int
520 test_dns_name_isabsolute(char *test_name, isc_boolean_t expected) {
521 dns_name_t name;
522 isc_buffer_t buf;
523 isc_buffer_t binbuf;
524 unsigned char junk[BUFLEN];
525 int len;
526 int rval;
527 isc_boolean_t isabs_p;
528 isc_result_t result;
530 rval = T_UNRESOLVED;
532 t_info("testing name %s\n", test_name);
533 len = strlen(test_name);
534 isc_buffer_init(&buf, test_name, len);
535 isc_buffer_add(&buf, len);
536 isc_buffer_init(&binbuf, &junk[0], BUFLEN);
537 dns_name_init(&name, NULL);
538 dns_name_setbuffer(&name, &binbuf);
539 result = dns_name_fromtext(&name, &buf, NULL, 0, NULL);
540 if (result == ISC_R_SUCCESS) {
541 isabs_p = dns_name_isabsolute(&name);
542 if (isabs_p == expected)
543 rval = T_PASS;
544 else
545 rval = T_FAIL;
546 } else {
547 t_info("dns_name_fromtext %s failed, result = %s\n",
548 test_name, dns_result_totext(result));
550 return (rval);
553 static void
554 t_dns_name_isabsolute(void) {
555 int line;
556 int cnt;
557 int result;
558 char *p;
559 FILE *fp;
561 t_assert("dns_name_isabsolute", 1, T_REQUIRED, "%s", a7);
563 result = T_UNRESOLVED;
564 fp = fopen("dns_name_isabsolute_data", "r");
565 if (fp != NULL) {
566 line = 0;
567 while ((p = t_fgetbs(fp)) != NULL) {
569 ++line;
572 * Skip comment lines.
574 if ((isspace((unsigned char)*p)) || (*p == '#')) {
575 (void)free(p);
576 continue;
579 cnt = bustline(p, Tokens);
580 if (cnt == 2) {
582 * label, bitpos, expected value.
584 result = test_dns_name_isabsolute(Tokens[0],
585 atoi(Tokens[1])
586 == 0 ?
587 ISC_FALSE :
588 ISC_TRUE);
589 } else {
590 t_info("bad datafile format at line %d\n",
591 line);
594 (void)free(p);
595 t_result(result);
597 (void)fclose(fp);
598 } else {
599 t_info("Missing datafile dns_name_isabsolute_data\n");
600 t_result(result);
604 static const char *a8 = "dns_name_hash(name, case_sensitive) returns "
605 "a hash of 'name' which is case_sensitive if case_sensitive "
606 "is true";
609 * a9 merged with a8.
612 static int
613 test_dns_name_hash(char *test_name1, char *test_name2,
614 isc_boolean_t csh_match, isc_boolean_t cish_match) {
615 int rval;
616 int failures;
617 isc_boolean_t match;
618 unsigned int hash1;
619 unsigned int hash2;
620 dns_name_t dns_name1;
621 dns_name_t dns_name2;
622 isc_result_t result;
624 rval = T_UNRESOLVED;
625 failures = 0;
627 t_info("testing names %s and %s\n", test_name1, test_name2);
629 result = dname_from_tname(test_name1, &dns_name1);
630 if (result == ISC_R_SUCCESS) {
631 result = dname_from_tname(test_name2, &dns_name2);
632 if (result == ISC_R_SUCCESS) {
633 hash1 = dns_name_hash(&dns_name1, ISC_TRUE);
634 hash2 = dns_name_hash(&dns_name2, ISC_TRUE);
635 match = ISC_FALSE;
636 if (hash1 == hash2)
637 match = ISC_TRUE;
638 if (match != csh_match) {
639 ++failures;
640 t_info("hash mismatch when ISC_TRUE\n");
642 hash1 = dns_name_hash(&dns_name1, ISC_FALSE);
643 hash2 = dns_name_hash(&dns_name2, ISC_FALSE);
644 match = ISC_FALSE;
645 if (hash1 == hash2)
646 match = ISC_TRUE;
647 if (match != cish_match) {
648 ++failures;
649 t_info("hash mismatch when ISC_FALSE\n");
651 if (failures == 0)
652 rval = T_PASS;
653 else
654 rval = T_FAIL;
655 } else {
656 t_info("dns_fromtext %s failed, result = %s\n",
657 test_name2, dns_result_totext(result));
659 } else {
660 t_info("dns_fromtext %s failed, result = %s\n",
661 test_name1, dns_result_totext(result));
663 return (rval);
666 static void
667 t_dns_name_hash(void) {
668 int line;
669 int cnt;
670 int result;
671 char *p;
672 FILE *fp;
674 t_assert("dns_name_hash", 1, T_REQUIRED, "%s", a8);
676 result = T_UNRESOLVED;
677 fp = fopen("dns_name_hash_data", "r");
678 if (fp != NULL) {
679 line = 0;
680 while ((p = t_fgetbs(fp)) != NULL) {
682 ++line;
685 * Skip comment lines.
687 if ((isspace((unsigned char)*p)) || (*p == '#')) {
688 (void)free(p);
689 continue;
692 cnt = bustline(p, Tokens);
693 if (cnt == 4) {
695 * name1, name2, exp match value if
696 * case_sensitive true,
697 * exp match value of case_sensitive false
699 result = test_dns_name_hash(
700 Tokens[0],
701 Tokens[1],
702 atoi(Tokens[2]) == 0 ?
703 ISC_FALSE : ISC_TRUE,
704 atoi(Tokens[3]) == 0 ?
705 ISC_FALSE : ISC_TRUE);
706 } else {
707 t_info("bad datafile format at line %d\n",
708 line);
711 (void)free(p);
712 t_result(result);
714 (void)fclose(fp);
715 } else {
716 t_info("Missing datafile dns_name_hash_data\n");
717 t_result(result);
721 static const char *a10 =
722 "dns_name_fullcompare(name1, name2, orderp, nlabelsp) "
723 "returns the DNSSEC ordering relationship between name1 and "
724 "name2, sets orderp to -1 if name1 < name2, to 0 if "
725 "name1 == name2, or to 1 if name1 > name2, sets nlabelsp "
726 "to the number of labels name1 and name2 have in common, "
727 "and sets nbitsp to the number of bits name1 and name2 "
728 "have in common";
731 * a11 thru a22 merged into a10.
733 static const char *
734 dns_namereln_to_text(dns_namereln_t reln) {
735 const char *p;
737 if (reln == dns_namereln_contains)
738 p = "contains";
739 else if (reln == dns_namereln_subdomain)
740 p = "subdomain";
741 else if (reln == dns_namereln_equal)
742 p = "equal";
743 else if (reln == dns_namereln_none)
744 p = "none";
745 else if (reln == dns_namereln_commonancestor)
746 p = "commonancestor";
747 else
748 p = "unknown";
750 return (p);
753 static int
754 test_dns_name_fullcompare(char *name1, char *name2,
755 dns_namereln_t exp_dns_reln,
756 int exp_order, int exp_nlabels)
758 int result;
759 int nfails;
760 int order;
761 unsigned int nlabels;
762 dns_name_t dns_name1;
763 dns_name_t dns_name2;
764 isc_result_t dns_result;
765 dns_namereln_t dns_reln;
767 nfails = 0;
768 result = T_UNRESOLVED;
771 t_info("testing names %s and %s for relation %s\n", name1, name2,
772 dns_namereln_to_text(exp_dns_reln));
774 dns_result = dname_from_tname(name1, &dns_name1);
775 if (dns_result == ISC_R_SUCCESS) {
776 dns_result = dname_from_tname(name2, &dns_name2);
777 if (dns_result == ISC_R_SUCCESS) {
778 dns_reln = dns_name_fullcompare(&dns_name1, &dns_name2,
779 &order, &nlabels);
781 if (dns_reln != exp_dns_reln) {
782 ++nfails;
783 t_info("expected relationship of %s, got %s\n",
784 dns_namereln_to_text(exp_dns_reln),
785 dns_namereln_to_text(dns_reln));
788 * Normalize order.
790 if (order < 0)
791 order = -1;
792 else if (order > 0)
793 order = 1;
794 if (order != exp_order) {
795 ++nfails;
796 t_info("expected ordering %d, got %d\n",
797 exp_order, order);
799 if ((exp_nlabels >= 0) &&
800 (nlabels != (unsigned int)exp_nlabels)) {
801 ++nfails;
802 t_info("expecting %d labels, got %d\n",
803 exp_nlabels, nlabels);
805 if (nfails == 0)
806 result = T_PASS;
807 else
808 result = T_FAIL;
809 } else {
810 t_info("dname_from_tname failed, result == %s\n",
811 dns_result_totext(result));
813 } else {
814 t_info("dname_from_tname failed, result == %s\n",
815 dns_result_totext(result));
818 return (result);
821 static void
822 t_dns_name_fullcompare(void) {
823 int line;
824 int cnt;
825 int result;
826 char *p;
827 FILE *fp;
828 dns_namereln_t reln;
830 t_assert("dns_name_fullcompare", 1, T_REQUIRED, "%s", a10);
832 result = T_UNRESOLVED;
833 fp = fopen("dns_name_fullcompare_data", "r");
834 if (fp != NULL) {
835 line = 0;
836 while ((p = t_fgetbs(fp)) != NULL) {
838 ++line;
841 * Skip comment lines.
843 if ((isspace((unsigned char)*p)) || (*p == '#')) {
844 (void)free(p);
845 continue;
848 cnt = bustline(p, Tokens);
849 if (cnt == 6) {
851 * name1, name2, exp_reln, exp_order,
852 * exp_nlabels
854 if (!strcmp(Tokens[2], "none"))
855 reln = dns_namereln_none;
856 else if (!strcmp(Tokens[2], "contains"))
857 reln = dns_namereln_contains;
858 else if (!strcmp(Tokens[2], "subdomain"))
859 reln = dns_namereln_subdomain;
860 else if (!strcmp(Tokens[2], "equal"))
861 reln = dns_namereln_equal;
862 else if (!strcmp(Tokens[2], "commonancestor"))
863 reln = dns_namereln_commonancestor;
864 else {
865 t_info("bad format at line %d\n",
866 line);
867 continue;
869 result = test_dns_name_fullcompare(
870 Tokens[0],
871 Tokens[1],
872 reln,
873 atoi(Tokens[3]),
874 atoi(Tokens[4]));
875 } else {
876 t_info("bad format at line %d\n", line);
879 (void)free(p);
880 t_result(result);
882 (void)fclose(fp);
883 } else {
884 t_info("Missing datafile dns_name_fullcompare_data\n");
885 t_result(result);
889 static const char *a23 =
890 "dns_name_compare(name1, name2) returns information about "
891 "the relative ordering under the DNSSEC ordering relationship "
892 "of name1 and name2";
895 * a24 thru a29 merged into a23.
898 static int
899 test_dns_name_compare(char *name1, char *name2, int exp_order) {
900 int result;
901 int order;
902 isc_result_t dns_result;
903 dns_name_t dns_name1;
904 dns_name_t dns_name2;
906 result = T_UNRESOLVED;
908 t_info("testing %s %s %s\n", name1,
909 exp_order == 0 ? "==": (exp_order == -1 ? "<" : ">"),
910 name2);
912 dns_result = dname_from_tname(name1, &dns_name1);
913 if (dns_result == ISC_R_SUCCESS) {
914 dns_result = dname_from_tname(name2, &dns_name2);
915 if (dns_result == ISC_R_SUCCESS) {
916 order = dns_name_compare(&dns_name1, &dns_name2);
918 * Normalize order.
920 if (order < 0)
921 order = -1;
922 else if (order > 0)
923 order = 1;
924 if (order != exp_order) {
925 t_info("expected order of %d, got %d\n",
926 exp_order, order);
927 result = T_FAIL;
928 } else
929 result = T_PASS;
930 } else {
931 t_info("dname_from_tname failed, result == %s\n",
932 dns_result_totext(result));
934 } else {
935 t_info("dname_from_tname failed, result == %s\n",
936 dns_result_totext(result));
939 return (result);
942 static void
943 t_dns_name_compare(void) {
944 int line;
945 int cnt;
946 int result;
947 char *p;
948 FILE *fp;
950 t_assert("dns_name_compare", 1, T_REQUIRED, "%s", a23);
952 result = T_UNRESOLVED;
953 fp = fopen("dns_name_compare_data", "r");
954 if (fp != NULL) {
955 line = 0;
956 while ((p = t_fgetbs(fp)) != NULL) {
958 ++line;
961 * Skip comment lines.
963 if ((isspace((unsigned char)*p)) || (*p == '#')) {
964 (void)free(p);
965 continue;
968 cnt = bustline(p, Tokens);
969 if (cnt == 3) {
971 * name1, name2, order.
973 result = test_dns_name_compare(
974 Tokens[0],
975 Tokens[1],
976 atoi(Tokens[2]));
977 } else {
978 t_info("bad datafile format at line %d\n",
979 line);
982 (void)free(p);
983 t_result(result);
985 (void)fclose(fp);
986 } else {
987 t_info("Missing datafile dns_name_compare_data\n");
988 t_result(result);
992 static const char *a30 =
993 "dns_name_rdatacompare(name1, name2) returns information "
994 "about the relative ordering of name1 and name2 as if they "
995 "are part of rdata in DNSSEC canonical form";
998 * a31, a32 merged into a30.
1001 static int
1002 test_dns_name_rdatacompare(char *name1, char *name2, int exp_order) {
1003 int result;
1004 int order;
1005 isc_result_t dns_result;
1006 dns_name_t dns_name1;
1007 dns_name_t dns_name2;
1009 result = T_UNRESOLVED;
1011 t_info("testing %s %s %s\n", name1,
1012 exp_order == 0 ? "==": (exp_order == -1 ? "<" : ">"), name2);
1014 dns_result = dname_from_tname(name1, &dns_name1);
1015 if (dns_result == ISC_R_SUCCESS) {
1016 dns_result = dname_from_tname(name2, &dns_name2);
1017 if (dns_result == ISC_R_SUCCESS) {
1018 order = dns_name_rdatacompare(&dns_name1, &dns_name2);
1020 * Normalize order.
1022 if (order < 0)
1023 order = -1;
1024 else if (order > 0)
1025 order = 1;
1026 if (order != exp_order) {
1027 t_info("expected order of %d, got %d\n",
1028 exp_order, order);
1029 result = T_FAIL;
1030 } else
1031 result = T_PASS;
1032 } else {
1033 t_info("dname_from_tname failed, result == %s\n",
1034 dns_result_totext(result));
1036 } else {
1037 t_info("dname_from_tname failed, result == %s\n",
1038 dns_result_totext(result));
1041 return (result);
1044 static void
1045 t_dns_name_rdatacompare(void) {
1046 int line;
1047 int cnt;
1048 int result;
1049 char *p;
1050 FILE *fp;
1052 t_assert("dns_name_rdatacompare", 1, T_REQUIRED, "%s", a30);
1054 result = T_UNRESOLVED;
1055 fp = fopen("dns_name_rdatacompare_data", "r");
1056 if (fp != NULL) {
1057 line = 0;
1058 while ((p = t_fgetbs(fp)) != NULL) {
1060 ++line;
1063 * Skip comment lines.
1065 if ((isspace((unsigned char)*p)) || (*p == '#')) {
1066 (void)free(p);
1067 continue;
1070 cnt = bustline(p, Tokens);
1071 if (cnt == 3) {
1073 * name1, name2, order.
1075 result = test_dns_name_rdatacompare(
1076 Tokens[0],
1077 Tokens[1],
1078 atoi(Tokens[2]));
1079 } else {
1080 t_info("bad datafile format at line %d\n",
1081 line);
1084 (void)free(p);
1085 t_result(result);
1087 (void)fclose(fp);
1088 } else {
1089 t_info("Missing datafile dns_name_rdatacompare_data\n");
1090 t_result(result);
1095 static const char *a33 =
1096 "when name1 is a subdomain of name2, "
1097 "dns_name_issubdomain(name1, name2) returns true, "
1098 "otherwise it returns false.";
1101 * a34 merged into a33.
1104 static int
1105 test_dns_name_issubdomain(char *name1, char *name2, isc_boolean_t exp_rval) {
1106 int result;
1107 isc_boolean_t rval;
1108 isc_result_t dns_result;
1109 dns_name_t dns_name1;
1110 dns_name_t dns_name2;
1112 result = T_UNRESOLVED;
1114 t_info("testing %s %s a subdomain of %s\n", name1,
1115 exp_rval == 0 ? "is not" : "is", name2);
1117 dns_result = dname_from_tname(name1, &dns_name1);
1118 if (dns_result == ISC_R_SUCCESS) {
1119 dns_result = dname_from_tname(name2, &dns_name2);
1120 if (dns_result == ISC_R_SUCCESS) {
1121 rval = dns_name_issubdomain(&dns_name1, &dns_name2);
1123 if (rval != exp_rval) {
1124 t_info("expected return value of %s, got %s\n",
1125 exp_rval == ISC_TRUE ? "true" : "false",
1126 rval == ISC_TRUE ? "true" : "false");
1127 result = T_FAIL;
1128 } else
1129 result = T_PASS;
1130 } else {
1131 t_info("dname_from_tname failed, result == %s\n",
1132 dns_result_totext(result));
1134 } else {
1135 t_info("dname_from_tname failed, result == %s\n",
1136 dns_result_totext(result));
1139 return (result);
1142 static void
1143 t_dns_name_issubdomain(void) {
1144 int line;
1145 int cnt;
1146 int result;
1147 char *p;
1148 FILE *fp;
1150 t_assert("dns_name_issubdomain", 1, T_REQUIRED, "%s", a33);
1152 result = T_UNRESOLVED;
1153 fp = fopen("dns_name_issubdomain_data", "r");
1154 if (fp != NULL) {
1155 line = 0;
1156 while ((p = t_fgetbs(fp)) != NULL) {
1158 ++line;
1161 * Skip comment lines.
1163 if ((isspace((unsigned char)*p)) || (*p == '#')) {
1164 (void)free(p);
1165 continue;
1168 cnt = bustline(p, Tokens);
1169 if (cnt == 3) {
1171 * name1, name2, issubdomain_p.
1173 result = test_dns_name_issubdomain(
1174 Tokens[0],
1175 Tokens[1],
1176 atoi(Tokens[2]) == 0 ?
1177 ISC_FALSE : ISC_TRUE);
1178 } else {
1179 t_info("bad datafile format at line %d\n",
1180 line);
1183 (void)free(p);
1184 t_result(result);
1186 (void)fclose(fp);
1187 } else {
1188 t_info("Missing datafile dns_name_issubdomain_data\n");
1189 t_result(result);
1193 static const char *a35 =
1194 "dns_name_countlabels(name) returns the number "
1195 "of labels in name";
1197 static int
1198 test_dns_name_countlabels(char *test_name, unsigned int exp_nlabels) {
1199 int result;
1200 unsigned int nlabels;
1201 isc_result_t dns_result;
1202 dns_name_t dns_name;
1204 result = T_UNRESOLVED;
1206 t_info("testing %s\n", test_name);
1208 dns_result = dname_from_tname(test_name, &dns_name);
1209 if (dns_result == ISC_R_SUCCESS) {
1210 nlabels = dns_name_countlabels(&dns_name);
1212 if (nlabels != exp_nlabels) {
1213 t_info("expected %d, got %d\n", exp_nlabels, nlabels);
1214 result = T_FAIL;
1215 } else
1216 result = T_PASS;
1217 } else {
1218 t_info("dname_from_tname failed, result == %s\n",
1219 dns_result_totext(dns_result));
1222 return (result);
1225 static void
1226 t_dns_name_countlabels(void) {
1227 int line;
1228 int cnt;
1229 int result;
1230 char *p;
1231 FILE *fp;
1233 t_assert("dns_name_countlabels", 1, T_REQUIRED, "%s", a35);
1235 result = T_UNRESOLVED;
1236 fp = fopen("dns_name_countlabels_data", "r");
1237 if (fp != NULL) {
1238 line = 0;
1239 while ((p = t_fgetbs(fp)) != NULL) {
1241 ++line;
1244 * Skip comment lines.
1246 if ((isspace((unsigned char)*p)) || (*p == '#')) {
1247 (void)free(p);
1248 continue;
1251 cnt = bustline(p, Tokens);
1252 if (cnt == 2) {
1254 * name, nlabels.
1256 result = test_dns_name_countlabels(Tokens[0],
1257 atoi(Tokens[1]));
1258 } else {
1259 t_info("bad format at line %d\n", line);
1262 (void)free(p);
1263 t_result(result);
1265 (void)fclose(fp);
1266 } else {
1267 t_info("Missing datafile dns_name_countlabels_data\n");
1268 t_result(result);
1272 static const char *a36 =
1273 "when n is less than the number of labels in name, "
1274 "dns_name_getlabel(name, n, labelp) initializes labelp "
1275 "to point to the nth label in name";
1278 * The strategy here is two take two dns names with a shared label in
1279 * different positions, get the two labels and compare them for equality.
1280 * If they don't match, dns_name_getlabel failed.
1283 static int
1284 test_dns_name_getlabel(char *test_name1, int label1_pos, char *test_name2,
1285 int label2_pos)
1287 int result;
1288 int nfails;
1289 unsigned int cnt;
1290 unsigned char *p;
1291 unsigned char *q;
1292 dns_name_t dns_name1;
1293 dns_name_t dns_name2;
1294 dns_label_t dns_label1;
1295 dns_label_t dns_label2;
1296 isc_result_t dns_result;
1298 nfails = 0;
1299 result = T_UNRESOLVED;
1301 t_info("testing with %s and %s\n", test_name1, test_name2);
1303 dns_result = dname_from_tname(test_name1, &dns_name1);
1304 if (dns_result == ISC_R_SUCCESS) {
1305 dns_result = dname_from_tname(test_name2, &dns_name2);
1306 if (dns_result == ISC_R_SUCCESS) {
1307 dns_name_getlabel(&dns_name1, label1_pos, &dns_label1);
1308 dns_name_getlabel(&dns_name2, label2_pos, &dns_label2);
1309 if (dns_label1.length != dns_label2.length) {
1310 t_info("label lengths differ\n");
1311 ++nfails;
1313 p = dns_label1.base;
1314 q = dns_label2.base;
1315 for (cnt = 0; cnt < dns_label1.length; ++cnt) {
1316 if (*p++ != *q++) {
1317 t_info("labels differ at position %d",
1318 cnt);
1319 ++nfails;
1322 if (nfails == 0)
1323 result = T_PASS;
1324 else
1325 result = T_FAIL;
1326 } else {
1327 t_info("dname_from_tname failed, result == %s",
1328 dns_result_totext(result));
1330 } else {
1331 t_info("dname_from_tname failed, result == %s",
1332 dns_result_totext(result));
1334 return (result);
1337 static void
1338 t_dns_name_getlabel(void) {
1339 int line;
1340 int cnt;
1341 int result;
1342 char *p;
1343 FILE *fp;
1345 t_assert("dns_name_getlabel", 1, T_REQUIRED, "%s", a36);
1347 result = T_UNRESOLVED;
1348 fp = fopen("dns_name_getlabel_data", "r");
1349 if (fp != NULL) {
1350 line = 0;
1351 while ((p = t_fgetbs(fp)) != NULL) {
1353 ++line;
1356 * Skip comment lines.
1358 if ((isspace((unsigned char)*p)) || (*p == '#')) {
1359 (void)free(p);
1360 continue;
1363 cnt = bustline(p, Tokens);
1364 if (cnt == 4) {
1366 * name1, name2, nlabels.
1368 result = test_dns_name_getlabel(Tokens[0],
1369 atoi(Tokens[1]),
1370 Tokens[2],
1371 atoi(Tokens[3]));
1372 } else {
1373 t_info("bad format at line %d\n", line);
1376 (void)free(p);
1377 t_result(result);
1379 (void)fclose(fp);
1380 } else {
1381 t_info("Missing datafile dns_name_getlabel_data\n");
1382 t_result(result);
1386 static const char *a37 =
1387 "when source contains at least first + n labels, "
1388 "dns_name_getlabelsequence(source, first, n, target) "
1389 "initializes target to point to the n label sequence of "
1390 "labels in source starting with first";
1393 * We adopt a similiar strategy to that used by the dns_name_getlabel test.
1396 static int
1397 test_dns_name_getlabelsequence(char *test_name1, int label1_start,
1398 char *test_name2, int label2_start, int range)
1400 int result;
1401 int nfails;
1402 unsigned int cnt;
1403 unsigned char *p;
1404 unsigned char *q;
1405 dns_name_t dns_name1;
1406 dns_name_t dns_name2;
1407 dns_name_t dns_targetname1;
1408 dns_name_t dns_targetname2;
1409 isc_result_t dns_result;
1410 isc_buffer_t buffer1;
1411 isc_buffer_t buffer2;
1412 unsigned char junk1[BUFLEN];
1413 unsigned char junk2[BUFLEN];
1415 nfails = 0;
1416 result = T_UNRESOLVED;
1417 dns_result = dname_from_tname(test_name1, &dns_name1);
1418 if (dns_result == ISC_R_SUCCESS) {
1419 dns_result = dname_from_tname(test_name2, &dns_name2);
1420 if (dns_result == ISC_R_SUCCESS) {
1421 t_info("testing %s %s\n", test_name1, test_name2);
1422 dns_name_init(&dns_targetname1, NULL);
1423 dns_name_init(&dns_targetname2, NULL);
1424 dns_name_getlabelsequence(&dns_name1, label1_start,
1425 range, &dns_targetname1);
1426 dns_name_getlabelsequence(&dns_name2, label2_start,
1427 range, &dns_targetname2);
1430 * Now convert both targets to text for comparison.
1432 isc_buffer_init(&buffer1, junk1, BUFLEN);
1433 isc_buffer_init(&buffer2, junk2, BUFLEN);
1434 dns_name_totext(&dns_targetname1, ISC_TRUE, &buffer1);
1435 dns_name_totext(&dns_targetname2, ISC_TRUE, &buffer2);
1436 if (buffer1.used == buffer2.used) {
1437 p = buffer1.base;
1438 q = buffer2.base;
1439 for (cnt = 0; cnt < buffer1.used; ++cnt) {
1440 if (*p != *q) {
1441 ++nfails;
1442 t_info("names differ at %d\n",
1443 cnt);
1444 break;
1446 ++p; ++q;
1448 } else {
1449 ++nfails;
1450 t_info("lengths differ\n");
1452 if (nfails == 0)
1453 result = T_PASS;
1454 else
1455 result = T_FAIL;
1456 } else {
1457 t_info("dname_from_tname failed, result == %s",
1458 dns_result_totext(dns_result));
1460 } else {
1461 t_info("dname_from_tname failed, result == %s",
1462 dns_result_totext(dns_result));
1464 return (result);
1467 static void
1468 t_dns_name_getlabelsequence(void) {
1469 int line;
1470 int cnt;
1471 int result;
1472 char *p;
1473 FILE *fp;
1475 t_assert("dns_name_getlabelsequence", 1, T_REQUIRED, "%s", a37);
1477 result = T_UNRESOLVED;
1478 fp = fopen("dns_name_getlabelsequence_data", "r");
1479 if (fp != NULL) {
1480 line = 0;
1481 while ((p = t_fgetbs(fp)) != NULL) {
1483 ++line;
1486 * Skip comment lines.
1488 if ((isspace((unsigned char)*p)) || (*p == '#')) {
1489 (void)free(p);
1490 continue;
1493 cnt = bustline(p, Tokens);
1494 if (cnt == 5) {
1496 * name1, name2, nlabels.
1498 result = test_dns_name_getlabelsequence(
1499 Tokens[0],
1500 atoi(Tokens[1]),
1501 Tokens[2],
1502 atoi(Tokens[3]),
1503 atoi(Tokens[4]));
1504 } else {
1505 t_info("bad format at line %d\n", line);
1508 (void)free(p);
1509 t_result(result);
1511 (void)fclose(fp);
1512 } else {
1513 t_info("Missing datafile dns_name_getlabelsequence_data\n");
1514 t_result(result);
1518 static const char *a38 =
1519 "dns_name_fromregion(name, region) converts a DNS name "
1520 "from a region representation to a name representation";
1522 static int
1523 test_dns_name_fromregion(char *test_name) {
1524 int result;
1525 int order;
1526 unsigned int nlabels;
1527 isc_result_t dns_result;
1528 dns_name_t dns_name1;
1529 dns_name_t dns_name2;
1530 dns_namereln_t dns_namereln;
1531 isc_region_t region;
1533 result = T_UNRESOLVED;
1535 t_info("testing %s\n", test_name);
1537 dns_result = dname_from_tname(test_name, &dns_name1);
1538 if (dns_result == ISC_R_SUCCESS) {
1540 dns_name_toregion(&dns_name1, &region);
1542 dns_name_init(&dns_name2, NULL);
1543 dns_name_fromregion(&dns_name2, &region);
1544 dns_namereln = dns_name_fullcompare(&dns_name1, &dns_name2,
1545 &order, &nlabels);
1546 if (dns_namereln == dns_namereln_equal)
1547 result = T_PASS;
1548 else
1549 result = T_FAIL;
1550 } else {
1551 t_info("dname_from_tname failed, result == %s\n",
1552 dns_result_totext(result));
1554 return (result);
1557 static void
1558 t_dns_name_fromregion(void) {
1559 int line;
1560 int cnt;
1561 int result;
1562 char *p;
1563 FILE *fp;
1565 t_assert("dns_name_fromregion", 1, T_REQUIRED, "%s", a38);
1567 result = T_UNRESOLVED;
1568 fp = fopen("dns_name_fromregion_data", "r");
1569 if (fp != NULL) {
1570 line = 0;
1571 while ((p = t_fgetbs(fp)) != NULL) {
1573 ++line;
1576 * Skip comment lines.
1578 if ((isspace((unsigned char)*p)) || (*p == '#')) {
1579 (void)free(p);
1580 continue;
1583 cnt = bustline(p, Tokens);
1584 if (cnt == 1) {
1586 * test_name.
1588 result = test_dns_name_fromregion(Tokens[0]);
1589 } else {
1590 t_info("bad format at line %d\n", line);
1593 (void)free(p);
1594 t_result(result);
1596 (void)fclose(fp);
1597 } else {
1598 t_info("Missing datafile dns_name_fromregion_data\n");
1599 t_result(result);
1603 static const char *a39 =
1604 "dns_name_toregion(name, region) converts a DNS name "
1605 "from a region representation to a name representation";
1607 static void
1608 t_dns_name_toregion(void) {
1609 int line;
1610 int cnt;
1611 int result;
1612 char *p;
1613 FILE *fp;
1615 t_assert("dns_name_toregion", 1, T_REQUIRED, "%s", a39);
1617 result = T_UNRESOLVED;
1618 fp = fopen("dns_name_toregion_data", "r");
1619 if (fp != NULL) {
1620 line = 0;
1621 while ((p = t_fgetbs(fp)) != NULL) {
1623 ++line;
1626 * Skip comment lines.
1628 if ((isspace((unsigned char)*p)) || (*p == '#')) {
1629 (void)free(p);
1630 continue;
1633 cnt = bustline(p, Tokens);
1634 if (cnt == 1) {
1636 * test_name.
1638 result = test_dns_name_fromregion(Tokens[0]);
1639 } else {
1640 t_info("bad format at line %d\n", line);
1643 (void)free(p);
1644 t_result(result);
1646 (void)fclose(fp);
1647 } else {
1648 t_info("Missing datafile dns_name_toregion_data\n");
1649 t_result(result);
1653 static const char *a40 =
1654 "dns_name_fromtext(name, source, origin, downcase, target) "
1655 "converts the textual representation of a DNS name at source "
1656 "into uncompressed wire form at target, appending origin to "
1657 "the converted name if origin is non-NULL and converting "
1658 "upper case to lower case during conversion "
1659 "if downcase is true.";
1661 static int
1662 test_dns_name_fromtext(char *test_name1, char *test_name2, char *test_origin,
1663 unsigned int downcase)
1665 int result;
1666 int order;
1667 unsigned int nlabels;
1668 unsigned char junk1[BUFLEN];
1669 unsigned char junk2[BUFLEN];
1670 unsigned char junk3[BUFLEN];
1671 isc_buffer_t binbuf1;
1672 isc_buffer_t binbuf2;
1673 isc_buffer_t binbuf3;
1674 isc_buffer_t txtbuf1;
1675 isc_buffer_t txtbuf2;
1676 isc_buffer_t txtbuf3;
1677 dns_name_t dns_name1;
1678 dns_name_t dns_name2;
1679 dns_name_t dns_name3;
1680 isc_result_t dns_result;
1681 dns_namereln_t dns_namereln;
1683 result = T_UNRESOLVED;
1685 t_info("testing %s %s %s\n", test_name1, test_name2, test_origin);
1687 isc_buffer_init(&binbuf1, junk1, BUFLEN);
1688 isc_buffer_init(&binbuf2, junk2, BUFLEN);
1689 isc_buffer_init(&binbuf3, junk3, BUFLEN);
1691 isc_buffer_init(&txtbuf1, test_name1, strlen(test_name1));
1692 isc_buffer_add(&txtbuf1, strlen(test_name1));
1694 isc_buffer_init(&txtbuf2, test_name2, strlen(test_name2));
1695 isc_buffer_add(&txtbuf2, strlen(test_name2));
1697 isc_buffer_init(&txtbuf3, test_origin, strlen(test_origin));
1698 isc_buffer_add(&txtbuf3, strlen(test_origin));
1699 dns_name_init(&dns_name1, NULL);
1700 dns_name_init(&dns_name2, NULL);
1701 dns_name_init(&dns_name3, NULL);
1702 dns_name_setbuffer(&dns_name1, &binbuf1);
1703 dns_name_setbuffer(&dns_name2, &binbuf2);
1704 dns_name_setbuffer(&dns_name3, &binbuf3);
1706 dns_result = dns_name_fromtext(&dns_name3, &txtbuf3, NULL, 0,
1707 &binbuf3);
1708 if (dns_result != ISC_R_SUCCESS) {
1709 t_info("dns_name_fromtext(dns_name3) failed, result == %s\n",
1710 dns_result_totext(dns_result));
1711 return (T_FAIL);
1714 dns_result = dns_name_fromtext(&dns_name1, &txtbuf1, &dns_name3,
1715 downcase, &binbuf1);
1716 if (dns_result != ISC_R_SUCCESS) {
1717 t_info("dns_name_fromtext(dns_name1) failed, result == %s\n",
1718 dns_result_totext(dns_result));
1719 return (T_FAIL);
1722 dns_result = dns_name_fromtext(&dns_name2, &txtbuf2, NULL, 0,
1723 &binbuf2);
1724 if (dns_result != ISC_R_SUCCESS) {
1725 t_info("dns_name_fromtext(dns_name2) failed, result == %s\n",
1726 dns_result_totext(dns_result));
1727 return (T_FAIL);
1730 dns_namereln = dns_name_fullcompare(&dns_name1, &dns_name2, &order,
1731 &nlabels);
1733 if (dns_namereln == dns_namereln_equal)
1734 result = T_PASS;
1735 else {
1736 t_info("dns_name_fullcompare returned %s\n",
1737 dns_namereln_to_text(dns_namereln));
1738 result = T_FAIL;
1741 return (result);
1744 static void
1745 t_dns_name_fromtext(void) {
1746 int line;
1747 int cnt;
1748 int result;
1749 char *p;
1750 FILE *fp;
1752 t_assert("dns_name_fromtext", 1, T_REQUIRED, "%s", a40);
1754 result = T_UNRESOLVED;
1755 fp = fopen("dns_name_fromtext_data", "r");
1756 if (fp != NULL) {
1757 line = 0;
1758 while ((p = t_fgetbs(fp)) != NULL) {
1760 ++line;
1763 * Skip comment lines.
1765 if ((isspace((unsigned char)*p)) || (*p == '#')) {
1766 (void)free(p);
1767 continue;
1770 cnt = bustline(p, Tokens);
1771 if (cnt == 4) {
1773 * test_name1, test_name2, test_origin,
1774 * downcase.
1776 result = test_dns_name_fromtext(Tokens[0],
1777 Tokens[1],
1778 Tokens[2],
1779 atoi(Tokens[3])
1780 == 0 ?
1782 DNS_NAME_DOWNCASE);
1783 } else {
1784 t_info("bad format at line %d\n", line);
1787 (void)free(p);
1788 t_result(result);
1790 (void)fclose(fp);
1791 } else {
1792 t_info("Missing datafile dns_name_fromtext\n");
1793 t_result(result);
1797 static const char *a41 =
1798 "dns_name_totext(name, omit_final_dot, target) converts "
1799 "the DNS name 'name' in wire format to textual format "
1800 "at target, and adds a final '.' to the name if "
1801 "omit_final_dot is false";
1803 static int
1804 test_dns_name_totext(char *test_name, isc_boolean_t omit_final) {
1805 int result;
1806 int len;
1807 int order;
1808 unsigned int nlabels;
1809 unsigned char junk1[BUFLEN];
1810 unsigned char junk2[BUFLEN];
1811 unsigned char junk3[BUFLEN];
1812 isc_buffer_t buf1;
1813 isc_buffer_t buf2;
1814 isc_buffer_t buf3;
1815 dns_name_t dns_name1;
1816 dns_name_t dns_name2;
1817 isc_result_t dns_result;
1818 dns_namereln_t dns_namereln;
1820 result = T_UNRESOLVED;
1822 t_info("testing %s\n", test_name);
1824 len = strlen(test_name);
1825 isc_buffer_init(&buf1, test_name, len);
1826 isc_buffer_add(&buf1, len);
1828 dns_name_init(&dns_name1, NULL);
1829 isc_buffer_init(&buf2, junk2, BUFLEN);
1832 * Out of the data file to dns_name1.
1834 dns_result = dns_name_fromtext(&dns_name1, &buf1, NULL, 0, &buf2);
1835 if (dns_result != ISC_R_SUCCESS) {
1836 t_info("dns_name_fromtext failed, result == %s\n",
1837 dns_result_totext(dns_result));
1838 return (T_UNRESOLVED);
1842 * From dns_name1 into a text buffer.
1844 isc_buffer_invalidate(&buf1);
1845 isc_buffer_init(&buf1, junk1, BUFLEN);
1846 dns_result = dns_name_totext(&dns_name1, omit_final, &buf1);
1847 if (dns_result != ISC_R_SUCCESS) {
1848 t_info("dns_name_totext failed, result == %s\n",
1849 dns_result_totext(dns_result));
1850 return (T_FAIL);
1854 * From the text buffer into dns_name2.
1856 dns_name_init(&dns_name2, NULL);
1857 isc_buffer_init(&buf3, junk3, BUFLEN);
1858 dns_result = dns_name_fromtext(&dns_name2, &buf1, NULL, 0, &buf3);
1859 if (dns_result != ISC_R_SUCCESS) {
1860 t_info("dns_name_fromtext failed, result == %s\n",
1861 dns_result_totext(dns_result));
1862 return (T_UNRESOLVED);
1865 dns_namereln = dns_name_fullcompare(&dns_name1, &dns_name2,
1866 &order, &nlabels);
1867 if (dns_namereln == dns_namereln_equal)
1868 result = T_PASS;
1869 else {
1870 t_info("dns_name_fullcompare returned %s\n",
1871 dns_namereln_to_text(dns_namereln));
1872 result = T_FAIL;
1875 return (result);
1878 static void
1879 t_dns_name_totext(void) {
1880 int line;
1881 int cnt;
1882 int result;
1883 char *p;
1884 FILE *fp;
1886 t_assert("dns_name_totext", 1, T_REQUIRED, "%s", a41);
1888 result = T_UNRESOLVED;
1889 fp = fopen("dns_name_totext_data", "r");
1890 if (fp != NULL) {
1891 line = 0;
1892 while ((p = t_fgetbs(fp)) != NULL) {
1894 ++line;
1897 * Skip comment lines.
1899 if ((isspace((unsigned char)*p)) || (*p == '#')) {
1900 (void)free(p);
1901 continue;
1904 cnt = bustline(p, Tokens);
1905 if (cnt == 2) {
1907 * test_name, omit_final.
1909 result = test_dns_name_totext(Tokens[0],
1910 atoi(Tokens[1]) == 0 ?
1911 ISC_FALSE :
1912 ISC_TRUE);
1913 } else {
1914 t_info("bad format at line %d\n", line);
1917 (void)free(p);
1918 t_result(result);
1920 (void)fclose(fp);
1921 } else {
1922 t_info("Missing datafile dns_name_totext\n");
1923 t_result(result);
1927 static const char *a42 =
1928 "dns_name_fromwire(name, source, dctx, downcase, target) "
1929 "converts the possibly compressed DNS name 'name' in wire "
1930 "format to canonicalized form at target, performing upper to "
1931 "lower case conversion if downcase is true, and returns "
1932 "ISC_R_SUCCESS";
1934 #if 0
1936 * XXXRTH these tests appear to be broken, so I have
1937 * disabled them.
1939 static const char *a43 =
1940 "when a label length is invalid, dns_name_fromwire() "
1941 "returns DNS_R_FORMERR";
1943 static const char *a44 =
1944 "when a label type is invalid, dns_name_fromwire() "
1945 "returns DNS_R_BADLABELTYPE";
1946 #endif
1948 static const char *a45 =
1949 "when a name length is invalid, dns_name_fromwire() "
1950 "returns DNS_R_FORMERR";
1952 static const char *a46 =
1953 "when a compression type is invalid, dns_name_fromwire() "
1954 "returns DNS_R_DISALLOWED";
1956 static const char *a47 =
1957 "when a bad compression pointer is encountered, "
1958 "dns_name_fromwire() returns DNS_R_BADPOINTER";
1960 static const char *a48 =
1961 "when input ends unexpected, dns_name_fromwire() "
1962 "returns ISC_R_UNEXPECTEDEND";
1964 static const char *a49 =
1965 "when there is not enough space in target, "
1966 "dns_name_fromwire(name, source, dcts, downcase, target) "
1967 "returns ISC_R_NOSPACE";
1969 static int
1970 test_dns_name_fromwire(char *datafile_name, int testname_offset, int downcase,
1971 unsigned int dc_method, char *exp_name,
1972 isc_result_t exp_result, size_t buflen)
1974 int result;
1975 int order;
1976 unsigned int nlabels;
1977 int len;
1978 unsigned char buf1[BIGBUFLEN];
1979 char buf2[BUFLEN];
1980 isc_buffer_t iscbuf1;
1981 isc_buffer_t iscbuf2;
1982 dns_name_t dns_name1;
1983 dns_name_t dns_name2;
1984 isc_result_t dns_result;
1985 dns_namereln_t dns_namereln;
1986 dns_decompress_t dctx;
1988 result = T_UNRESOLVED;
1990 t_info("testing using %s\n", datafile_name);
1991 len = getmsg(datafile_name, buf1, BIGBUFLEN, &iscbuf1);
1993 isc_buffer_setactive(&iscbuf1, len);
1994 iscbuf1.current = testname_offset;
1996 isc_buffer_init(&iscbuf2, buf2, buflen);
1997 dns_name_init(&dns_name1, NULL);
1998 dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT);
1999 dns_decompress_setmethods(&dctx, dc_method);
2000 dns_result = dns_name_fromwire(&dns_name1, &iscbuf1,
2001 &dctx, downcase ? ISC_TRUE : ISC_FALSE,
2002 &iscbuf2);
2004 if ((dns_result == exp_result) && (exp_result == ISC_R_SUCCESS)) {
2006 dns_result = dname_from_tname(exp_name, &dns_name2);
2007 if (dns_result == ISC_R_SUCCESS) {
2008 dns_namereln = dns_name_fullcompare(&dns_name1,
2009 &dns_name2,
2010 &order, &nlabels);
2011 if (dns_namereln != dns_namereln_equal) {
2012 t_info("dns_name_fullcompare returned %s\n",
2013 dns_namereln_to_text(dns_namereln));
2014 result = T_FAIL;
2015 } else {
2016 result = T_PASS;
2018 } else {
2019 t_info("dns_name_fromtext %s failed, result = %s\n",
2020 exp_name, dns_result_totext(dns_result));
2021 result = T_UNRESOLVED;
2023 } else if (dns_result == exp_result) {
2024 result = T_PASS;
2025 } else {
2026 t_info("dns_name_fromwire returned %s\n",
2027 dns_result_totext(dns_result));
2028 result = T_FAIL;
2031 return (result);
2034 static void
2035 t_dns_name_fromwire_x(const char *testfile, size_t buflen) {
2036 int line;
2037 int cnt;
2038 int result;
2039 unsigned int dc_method;
2040 isc_result_t exp_result;
2041 char *p;
2042 char *tok;
2043 FILE *fp;
2045 result = T_UNRESOLVED;
2046 fp = fopen(testfile, "r");
2047 if (fp != NULL) {
2048 line = 0;
2049 while ((p = t_fgetbs(fp)) != NULL) {
2051 ++line;
2054 * Skip comment lines.
2056 if ((isspace((unsigned char)*p)) || (*p == '#')) {
2057 (void)free(p);
2058 continue;
2061 cnt = bustline(p, Tokens);
2062 if (cnt == 6) {
2064 * datafile_name, testname_offset,
2065 * downcase, dc_method,
2066 * exp_name, exp_result.
2069 tok = Tokens[5];
2070 exp_result = ISC_R_SUCCESS;
2071 if (! strcmp(tok, "ISC_R_SUCCESS"))
2072 exp_result = ISC_R_SUCCESS;
2073 else if (! strcmp(tok, "ISC_R_NOSPACE"))
2074 exp_result = ISC_R_NOSPACE;
2075 else if (! strcmp(tok, "DNS_R_BADLABELTYPE"))
2076 exp_result = DNS_R_BADLABELTYPE;
2077 else if (! strcmp(tok, "DNS_R_FORMERR"))
2078 exp_result = DNS_R_FORMERR;
2079 else if (! strcmp(tok, "DNS_R_BADPOINTER"))
2080 exp_result = DNS_R_BADPOINTER;
2081 else if (! strcmp(tok, "ISC_R_UNEXPECTEDEND"))
2082 exp_result = ISC_R_UNEXPECTEDEND;
2083 else if (! strcmp(tok, "DNS_R_TOOMANYHOPS"))
2084 exp_result = DNS_R_TOOMANYHOPS;
2085 else if (! strcmp(tok, "DNS_R_DISALLOWED"))
2086 exp_result = DNS_R_DISALLOWED;
2087 else if (! strcmp(tok, "DNS_R_NAMETOOLONG"))
2088 exp_result = DNS_R_NAMETOOLONG;
2090 tok = Tokens[3];
2091 dc_method = DNS_COMPRESS_NONE;
2092 if (! strcmp(tok, "DNS_COMPRESS_GLOBAL14"))
2093 dc_method = DNS_COMPRESS_GLOBAL14;
2094 else if (! strcmp(tok, "DNS_COMPRESS_ALL"))
2095 dc_method = DNS_COMPRESS_ALL;
2097 result = test_dns_name_fromwire(Tokens[0],
2098 atoi(Tokens[1]),
2099 atoi(Tokens[2]),
2100 dc_method,
2101 Tokens[4],
2102 exp_result,
2103 buflen);
2104 } else {
2105 t_info("bad format at line %d\n", line);
2108 (void)free(p);
2109 t_result(result);
2111 (void)fclose(fp);
2112 } else {
2113 t_info("Missing datafile %s\n", testfile);
2114 t_result(result);
2118 static void
2119 t_dns_name_fromwire(void) {
2120 t_assert("dns_name_fromwire", 1, T_REQUIRED, "%s", a42);
2121 t_dns_name_fromwire_x("dns_name_fromwire_1_data", BUFLEN);
2123 #if 0
2125 * XXXRTH these tests appear to be broken, so I have
2126 * disabled them.
2128 t_assert("dns_name_fromwire", 2, T_REQUIRED, "%s", a43);
2129 t_dns_name_fromwire_x("dns_name_fromwire_2_data", BUFLEN);
2131 t_assert("dns_name_fromwire", 3, T_REQUIRED, "%s", a44);
2132 t_dns_name_fromwire_x("dns_name_fromwire_3_data", BUFLEN);
2133 #endif
2135 t_assert("dns_name_fromwire", 4, T_REQUIRED, "%s", a45);
2136 t_dns_name_fromwire_x("dns_name_fromwire_4_data", BUFLEN);
2138 t_assert("dns_name_fromwire", 5, T_REQUIRED, "%s", a46);
2139 t_dns_name_fromwire_x("dns_name_fromwire_5_data", BUFLEN);
2141 t_assert("dns_name_fromwire", 6, T_REQUIRED, "%s", a47);
2142 t_dns_name_fromwire_x("dns_name_fromwire_6_data", BUFLEN);
2144 t_assert("dns_name_fromwire", 7, T_REQUIRED, "%s", a48);
2145 t_dns_name_fromwire_x("dns_name_fromwire_7_data", BUFLEN);
2147 t_assert("dns_name_fromwire", 9, T_REQUIRED, "%s", a49);
2148 t_dns_name_fromwire_x("dns_name_fromwire_8_data", 2);
2152 static const char *a51 =
2153 "dns_name_towire(name, cctx, target) converts the DNS name "
2154 "'name' into wire format, compresses it as specified "
2155 "by the compression context cctx, stores the result in "
2156 "target and returns DNS_SUCCESS";
2158 static const char *a52 =
2159 "when not enough space exists in target, "
2160 "dns_name_towire(name, cctx, target) returns ISC_R_NOSPACE";
2162 static int
2163 test_dns_name_towire(char *testname, unsigned int dc_method, char *exp_data,
2164 int exp_data_len, isc_result_t exp_result, size_t buflen)
2166 int result;
2167 int val;
2168 int len;
2169 unsigned char buf2[BUFLEN];
2170 unsigned char buf3[BUFLEN];
2171 isc_buffer_t iscbuf1;
2172 isc_buffer_t iscbuf2;
2173 isc_buffer_t iscbuf3;
2174 dns_name_t dns_name;
2175 isc_result_t dns_result;
2176 isc_result_t isc_result;
2177 dns_compress_t cctx;
2178 isc_mem_t *mctx;
2180 t_info("testing using %s\n", testname);
2182 result = T_UNRESOLVED;
2183 mctx = NULL;
2185 isc_result = isc_mem_create(0, 0, &mctx);
2186 if (isc_result != ISC_R_SUCCESS) {
2187 t_info("isc_mem_create failed\n");
2188 return (result);
2190 dns_compress_init(&cctx, -1, mctx);
2191 dns_compress_setmethods(&cctx, dc_method);
2192 dns_name_init(&dns_name, NULL);
2193 len = strlen(testname);
2194 isc_buffer_init(&iscbuf1, testname, len);
2195 isc_buffer_add(&iscbuf1, len);
2196 isc_buffer_init(&iscbuf2, buf2, BUFLEN);
2197 dns_result = dns_name_fromtext(&dns_name, &iscbuf1, NULL, 0, &iscbuf2);
2198 if (dns_result == ISC_R_SUCCESS) {
2199 isc_buffer_init(&iscbuf3, buf3, buflen);
2200 dns_result = dns_name_towire(&dns_name, &cctx, &iscbuf3);
2201 if (dns_result == exp_result) {
2202 if (exp_result == ISC_R_SUCCESS) {
2204 * Compare results with expected data.
2206 val = chkdata(buf3, iscbuf3.used, exp_data,
2207 exp_data_len);
2208 if (val == 0)
2209 result = T_PASS;
2210 else
2211 result = T_FAIL;
2212 } else
2213 result = T_PASS;
2214 } else {
2215 t_info("dns_name_towire unexpectedly returned %s\n",
2216 dns_result_totext(dns_result));
2217 result = T_FAIL;
2219 } else {
2220 t_info("dns_name_fromtext %s failed, result = %s\n",
2221 testname, dns_result_totext(dns_result));
2223 return (result);
2226 static void
2227 t_dns_name_towire_x(const char *testfile, size_t buflen) {
2228 int line;
2229 int cnt;
2230 int result;
2231 unsigned int dc_method;
2232 isc_result_t exp_result;
2233 char *p;
2234 FILE *fp;
2236 result = T_UNRESOLVED;
2237 fp = fopen(testfile, "r");
2238 if (fp != NULL) {
2239 line = 0;
2240 while ((p = t_fgetbs(fp)) != NULL) {
2242 ++line;
2245 * Skip comment lines.
2247 if ((isspace((unsigned char)*p)) || (*p == '#')) {
2248 (void)free(p);
2249 continue;
2252 cnt = bustline(p, Tokens);
2253 if (cnt == 5) {
2255 * testname, dc_method,
2256 * exp_data, exp_data_len,
2257 * exp_result.
2260 dc_method = t_dc_method_fromtext(Tokens[3]);
2261 exp_result = t_dns_result_fromtext(Tokens[4]);
2263 result = test_dns_name_towire(Tokens[0],
2264 dc_method,
2265 Tokens[2],
2266 atoi(Tokens[3]),
2267 exp_result,
2268 buflen);
2269 } else {
2270 t_info("bad format at line %d\n", line);
2273 (void)free(p);
2274 t_result(result);
2276 (void)fclose(fp);
2277 } else {
2278 t_info("Missing datafile %s\n", testfile);
2279 t_result(result);
2283 static void
2284 t_dns_name_towire_1(void) {
2285 t_assert("dns_name_towire", 1, T_REQUIRED, "%s", a51);
2286 t_dns_name_towire_x("dns_name_towire_1_data", BUFLEN);
2289 static void
2290 t_dns_name_towire_2(void) {
2291 t_assert("dns_name_towire", 2, T_REQUIRED, "%s", a52);
2292 t_dns_name_towire_x("dns_name_towire_2_data", 2);
2295 static void
2296 t_dns_name_towire(void) {
2297 t_dns_name_towire_1();
2298 t_dns_name_towire_2();
2301 #if 0 /* This is silly. A test should either exist, or not, but not
2302 * one that just returns "UNTESTED."
2304 static const char *a53 =
2305 "dns_name_concatenate(prefix, suffix, name, target) "
2306 "concatenates prefix and suffix, stores the result "
2307 "in target, canonicalizes any bitstring labels "
2308 "and returns ISC_R_SUCCESS";
2310 static void
2311 t_dns_name_concatenate(void) {
2312 t_assert("dns_name_concatenate", 1, T_REQUIRED, "%s", a53);
2313 t_result(T_UNTESTED);
2315 #endif
2317 testspec_t T_testlist[] = {
2318 { t_dns_name_init, "dns_name_init" },
2319 { t_dns_name_invalidate, "dns_name_invalidate" },
2320 { t_dns_name_setbuffer, "dns_name_setbuffer" },
2321 { t_dns_name_hasbuffer, "dns_name_hasbuffer" },
2322 { t_dns_name_isabsolute, "dns_name_isabsolute" },
2323 { t_dns_name_hash, "dns_name_hash" },
2324 { t_dns_name_fullcompare, "dns_name_fullcompare" },
2325 { t_dns_name_compare, "dns_name_compare" },
2326 { t_dns_name_rdatacompare, "dns_name_rdatacompare" },
2327 { t_dns_name_issubdomain, "dns_name_issubdomain" },
2328 { t_dns_name_countlabels, "dns_name_countlabels" },
2329 { t_dns_name_getlabel, "dns_name_getlabel" },
2330 { t_dns_name_getlabelsequence, "dns_name_getlabelsequence" },
2331 { t_dns_name_fromregion, "dns_name_fromregion" },
2332 { t_dns_name_toregion, "dns_name_toregion" },
2333 { t_dns_name_fromwire, "dns_name_fromwire" },
2334 { t_dns_name_towire, "dns_name_towire" },
2335 { t_dns_name_fromtext, "dns_name_fromtext" },
2336 { t_dns_name_totext, "dns_name_totext" },
2337 #if 0
2338 { t_dns_name_concatenate, "dns_name_concatenate" },
2339 #endif
2340 { NULL, NULL }