1 /*	$NetBSD: t_names.c,v 1.9 2014/12/10 04:37:53 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2004-2009, 2011-2014  Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (C) 1998-2003  Internet Software Consortium.
6  *
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.
10  *
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.
18  */
19 
20 /* Id: t_names.c,v 1.52 2011/03/12 04:59:46 tbox Exp  */
21 
22 #include <config.h>
23 
24 #include <ctype.h>
25 #include <stdlib.h>
26 
27 #include <isc/buffer.h>
28 #include <isc/mem.h>
29 #include <isc/string.h>
30 
31 #include <dns/compress.h>
32 #include <dns/fixedname.h>
33 #include <dns/name.h>
34 #include <dns/result.h>
35 
36 #include <tests/t_api.h>
37 
38 #define	MAXTOKS		16
39 #define	BUFLEN		256
40 #define	BIGBUFLEN	4096
41 
42 static char	*Tokens[MAXTOKS + 1];
43 
44 
45 #ifdef	NEED_PBUF
46 
47 /*%
48  * get a hex formatted dns message from a data
49  * file into an isc_buffer_t
50  * caller supplies data storage and the isc_buffer
51  * we read the file, convert, setup the buffer
52  * and return the data length
53  */
54 
55 static char *
ctoh(unsigned char c)56 ctoh(unsigned char c) {
57 	int		val;
58 	static char	buf[3];
59 
60 	val = (c >> 4) & 0x0f;
61 	if ((0 <= val) && (val <= 9))
62 		buf[0] = '0' + val;
63 	else if ((10 <= val) && (val <= 16))
64 		buf[0] = 'a' + val - 10;
65 	val = c & 0x0f;
66 	if ((0 <= val) && (val <= 9))
67 		buf[1] = '0' + val;
68 	else if ((10 <= val) && (val <= 16))
69 		buf[1] = 'a' + val - 10;
70 	buf[2] = '\0';
71 	return (buf);
72 }
73 
74 static void
pbuf(isc_buffer_t * pbuf)75 pbuf(isc_buffer_t *pbuf) {
76 	size_t		len;
77 	unsigned char	*p;
78 
79 	len = 0;
80 	p = pbuf->base;
81 	while (len < pbuf->length) {
82 		printf("%s", ctoh(*p));
83 		++p;
84 		++len;
85 		if ((len % 40) == 0)
86 			printf("\n");
87 	}
88 }
89 
90 #endif	/* NEED_PBUF */
91 
92 /*%
93  * Compare data at buf with data in hex representation at exp_data,
94  * of length exp_data_len, for equality.
95  * Return 0 if equal, else non-zero.
96  */
97 
98 static int
chkdata(unsigned char * buf,size_t buflen,char * exp_data,size_t exp_data_len)99 chkdata(unsigned char *buf, size_t buflen, char *exp_data,
100 	size_t exp_data_len)
101 {
102 	int		result;
103 	unsigned char	*p;
104 	unsigned char	*v;
105 	char		*q;
106 	unsigned char	*data;
107 	size_t		cnt;
108 
109 	if (buflen == exp_data_len) {
110 		data = (unsigned char *)malloc(exp_data_len *
111 					       sizeof(unsigned char));
112 		if (data == NULL) {
113 			t_info("malloc failed unexpectedly\n");
114 			return (-1);
115 		}
116 
117 		/*
118 		 * First convert exp_data from hex format.
119 		 */
120 		p = data;
121 		q = exp_data;
122 		cnt = 0;
123 		while (cnt < exp_data_len) {
124 
125 			if (('0' <= *q) && (*q <= '9'))
126 				*p = *q - '0';
127 			else if (('a' <= *q) && (*q <= 'f'))
128 				*p = *q - 'a' + 10;
129 			else if (('A' <= *q) && (*q <= 'F'))
130 				*p = *q - 'A' + 10;
131 			else {
132 				t_info("malformed comparison data\n");
133 				free(data);
134 				return (-1);
135 			}
136 			++q;
137 
138 			*p <<= 4;
139 
140 			if (('0' <= *q) && (*q <= '9'))
141 				*p |= ((*q - '0') & 0x0f);
142 			else if (('a' <= *q) && (*q <= 'f'))
143 				*p |= ((*q - 'a' + 10) & 0x0f);
144 			else if (('A' <= *q) && (*q <= 'F'))
145 				*p |= ((*q - 'A' + 10) & 0x0f);
146 			else {
147 				t_info("malformed comparison data\n");
148 				free(data);
149 				return (-1);
150 			}
151 			++p;
152 			++q;
153 			++cnt;
154 		}
155 
156 		/*
157 		 * Now compare data.
158 		 */
159 		p = buf;
160 		v = data;
161 		for (cnt = 0; cnt < exp_data_len; ++cnt) {
162 			if (*p != *v)
163 				break;
164 			++p;
165 			++v;
166 		}
167 		if (cnt == exp_data_len)
168 			result = 0;
169 		else {
170 			t_info("bad data at position %lu, "
171 			       "got 0x%.2x, expected 0x%.2x\n",
172 			       (unsigned long)cnt, *p, *v);
173 			result = (int)cnt + 1;
174 		}
175 		(void)free(data);
176 	} else {
177 		t_info("data length error, expected %lu, got %lu\n",
178 			(unsigned long)exp_data_len, (unsigned long)buflen);
179 		result = (int)(exp_data_len - buflen);
180 	}
181 	return (result);
182 }
183 
184 /*%
185  * Get a hex formatted dns message from a data file into an isc_buffer_t.
186  * Caller supplies data storage and the isc_buffer.  We read the file, convert,
187  * setup the buffer and return the data length.
188  */
189 static int
getmsg(char * datafile_name,isc_buffer_t * pbuf)190 getmsg(char *datafile_name, isc_buffer_t *pbuf)
191 {
192 	int			c;
193 	unsigned int		len;
194 	unsigned int		cnt;
195 	unsigned char		*p;
196 	FILE			*fp;
197 	unsigned int		buflen;
198 
199 	fp = fopen(datafile_name, "r");
200 	if (fp == NULL) {
201 		t_info("No such file %s\n", datafile_name);
202 		return (0);
203 	}
204 
205 	p = isc_buffer_used(pbuf);
206 	buflen = isc_buffer_availablelength(pbuf);
207 	cnt = 0;
208 	len = 0;
209 	while ((c = getc(fp)) != EOF) {
210 		unsigned int		val;
211 		if (	(c == ' ') || (c == '\t') ||
212 			(c == '\r') || (c == '\n'))
213 				continue;
214 		if (c == '#') {
215 			while ((c = getc(fp)) != '\n')
216 				;
217 			continue;
218 		}
219 		if (('0' <= c) && (c <= '9'))
220 			val = c - '0';
221 		else if (('a' <= c) && (c <= 'f'))
222 			val = c - 'a' + 10;
223 		else if (('A' <= c) && (c <= 'F'))
224 			val = c - 'A'+ 10;
225 		else {
226 			(void)fclose(fp);
227 			t_info("Bad format in datafile\n");
228 			return (0);
229 		}
230 		if ((len % 2) == 0) {
231 			*p = (val << 4);
232 		} else {
233 			*p += val;
234 			++p;
235 			++cnt;
236 			if (cnt >= buflen) {
237 				/*
238 				 * Buffer too small.
239 				 */
240 				(void)fclose(fp);
241 				t_info("Buffer overflow error\n");
242 				return (0);
243 			}
244 		}
245 		++len;
246 	}
247 	(void)fclose(fp);
248 
249 	if (len % 2) {
250 		t_info("Bad format in %s\n", datafile_name);
251 		return (0);
252 	}
253 
254 	*p = '\0';
255 	isc_buffer_add(pbuf, cnt);
256 	return (cnt);
257 }
258 
259 static int
bustline(char * line,char ** toks)260 bustline(char *line, char **toks) {
261 	int	cnt;
262 	char	*p;
263 
264 	cnt = 0;
265 	if (line && *line) {
266 		while ((p = strtok(line, "\t")) && (cnt < MAXTOKS)) {
267 			*toks++ = p;
268 			line = NULL;
269 			++cnt;
270 		}
271 	}
272 	return (cnt);
273 }
274 
275 
276 #ifdef	NEED_HNAME_TO_TNAME
277 
278 /*%
279  * convert a name from hex representation to text form
280  * format of hex notation is:
281  * %xXXXXXXXX
282  */
283 
284 static int
hname_to_tname(char * src,char * target,size_t len)285 hname_to_tname(char *src, char *target, size_t len) {
286 	int		i;
287 	int		c;
288 	unsigned int	val;
289 	size_t		srclen;
290 	char		*p;
291 	char		*q;
292 
293 	p = src;
294 	srclen = strlen(p);
295 	if ((srclen >= 2) && ((*p != '%') || (*(p+1) != 'x'))) {
296 		/*
297 		 * No conversion needed.
298 		 */
299 		if (srclen >= len)
300 			return (1);
301 		memmove(target, src, srclen + 1);
302 		return (0);
303 	}
304 
305 	i = 0;
306 	p += 2;
307 	q = target;
308 	while (*p) {
309 		c = *p;
310 		if (('0' < c) && (c <= '9'))
311 			val = c - '0';
312 		else if (('a' <= c) && (c <= 'z'))
313 			val = c + 10 - 'a';
314 		else if (('A' <= c) && (c <= 'Z'))
315 			val = c + 10 - 'A';
316 		else {
317 			return (1);
318 		}
319 		if (i % 2) {
320 			*q |= val;
321 			++q;
322 		} else
323 			*q = (val << 4);
324 		++i;
325 		++p;
326 	}
327 	if (i % 2) {
328 		return (1);
329 	} else {
330 		*q = '\0';
331 		return (0);
332 	}
333 }
334 
335 #endif	/* NEED_HNAME_TO_TNAME */
336 
337 static const char *a3 =	"dns_name_init initializes 'name' to the empty name";
338 
339 static void
t_dns_name_init(void)340 t_dns_name_init(void) {
341 	int		rval;
342 	int		result;
343 	dns_name_t	name;
344 	unsigned char	offsets[1];
345 
346 	rval = 0;
347 	t_assert("dns_name_init", 1, T_REQUIRED, "%s", a3);
348 
349 	dns_name_init(&name, offsets);
350 	/* magic is hidden in name.c ...
351 	if (name.magic != NAME_MAGIC) {
352 		t_info("name.magic is not set to NAME_MAGIC\n");
353 		++rval;
354 	}
355 	*/
356 	if (name.ndata != NULL) {
357 		t_info("name.ndata is not NULL\n");
358 		++rval;
359 	}
360 	if (name.length != 0) {
361 		t_info("name.length is not 0\n");
362 		++rval;
363 	}
364 	if (name.labels != 0) {
365 		t_info("name.labels is not 0\n");
366 		++rval;
367 	}
368 	if (name.attributes != 0) {
369 		t_info("name.attributes is not 0\n");
370 		++rval;
371 	}
372 	if (name.offsets != offsets) {
373 		t_info("name.offsets is incorrect\n");
374 		++rval;
375 	}
376 	if (name.buffer != NULL) {
377 		t_info("name.buffer is not NULL\n");
378 		++rval;
379 	}
380 
381 	if (rval == 0)
382 		result = T_PASS;
383 	else
384 		result = T_FAIL;
385 	t_result(result);
386 }
387 
388 static const char *a4 =	"dns_name_invalidate invalidates 'name'";
389 
390 static void
t_dns_name_invalidate(void)391 t_dns_name_invalidate(void) {
392 	int		rval;
393 	int		result;
394 	dns_name_t	name;
395 	unsigned char	offsets[1];
396 
397 	t_assert("dns_name_invalidate", 1, T_REQUIRED, "%s", a4);
398 
399 	rval = 0;
400 	dns_name_init(&name, offsets);
401 	dns_name_invalidate(&name);
402 
403 	/* magic is hidden in name.c ...
404 	if (name.magic != 0) {
405 		t_info("name.magic is not set to NAME_MAGIC\n");
406 		++rval;
407 	}
408 	*/
409 	if (name.ndata != NULL) {
410 		t_info("name.ndata is not NULL\n");
411 		++rval;
412 	}
413 	if (name.length != 0) {
414 		t_info("name.length is not 0\n");
415 		++rval;
416 	}
417 	if (name.labels != 0) {
418 		t_info("name.labels is not 0\n");
419 		++rval;
420 	}
421 	if (name.attributes != 0) {
422 		t_info("name.attributes is not 0\n");
423 		++rval;
424 	}
425 	if (name.offsets != NULL) {
426 		t_info("name.offsets is not NULL\n");
427 		++rval;
428 	}
429 	if (name.buffer != NULL) {
430 		t_info("name.buffer is not NULL\n");
431 		++rval;
432 	}
433 
434 	if (rval == 0)
435 		result = T_PASS;
436 	else
437 		result = T_FAIL;
438 	t_result(result);
439 }
440 
441 static const char *a5 =	"dns_name_setbuffer dedicates a buffer for use "
442 			"with 'name'";
443 
444 static void
t_dns_name_setbuffer(void)445 t_dns_name_setbuffer(void) {
446 	int		result;
447 	unsigned char	junk[BUFLEN];
448 	dns_name_t	name;
449 	isc_buffer_t	buffer;
450 
451 	t_assert("dns_name_setbuffer", 1, T_REQUIRED, "%s", a5);
452 
453 	isc_buffer_init(&buffer, junk, BUFLEN);
454 	dns_name_init(&name, NULL);
455 	dns_name_setbuffer(&name, &buffer);
456 	if (name.buffer == &buffer)
457 		result = T_PASS;
458 	else
459 		result = T_FAIL;
460 
461 	t_result(result);
462 }
463 
464 static const char *a6 =	"dns_name_hasbuffer returns ISC_TRUE if 'name' has a "
465 			"dedicated buffer, otherwise it returns ISC_FALSE";
466 
467 static void
t_dns_name_hasbuffer(void)468 t_dns_name_hasbuffer(void) {
469 	int		result;
470 	int		rval;
471 	unsigned char	junk[BUFLEN];
472 	dns_name_t	name;
473 	isc_buffer_t	buffer;
474 
475 	t_assert("dns_name_hasbuffer", 1, T_REQUIRED, "%s", a6);
476 
477 	rval = 0;
478 	isc_buffer_init(&buffer, junk, BUFLEN);
479 	dns_name_init(&name, NULL);
480 	if (dns_name_hasbuffer(&name) != ISC_FALSE)
481 		++rval;
482 	dns_name_setbuffer(&name, &buffer);
483 	if (dns_name_hasbuffer(&name) != ISC_TRUE)
484 		++rval;
485 	if (rval == 0)
486 		result = T_PASS;
487 	else
488 		result = T_FAIL;
489 
490 	t_result(result);
491 }
492 
493 static const char *a7 =	"dns_name_isabsolute returns ISC_TRUE if 'name' ends "
494 			"in the root label";
495 
496 static int
test_dns_name_isabsolute(char * test_name,isc_boolean_t expected)497 test_dns_name_isabsolute(char *test_name, isc_boolean_t expected) {
498 	dns_name_t	name;
499 	isc_buffer_t	buf;
500 	isc_buffer_t	binbuf;
501 	unsigned char	junk[BUFLEN];
502 	int		len;
503 	int		rval;
504 	isc_boolean_t	isabs_p;
505 	isc_result_t	result;
506 
507 	rval = T_UNRESOLVED;
508 
509 	t_info("testing name %s\n", test_name);
510 	len = strlen(test_name);
511 	isc_buffer_init(&buf, test_name, len);
512 	isc_buffer_add(&buf, len);
513 	isc_buffer_init(&binbuf, &junk[0], BUFLEN);
514 	dns_name_init(&name, NULL);
515 	dns_name_setbuffer(&name, &binbuf);
516 	result = dns_name_fromtext(&name,  &buf, NULL, 0, NULL);
517 	if (result == ISC_R_SUCCESS) {
518 		isabs_p = dns_name_isabsolute(&name);
519 		if (isabs_p == expected)
520 			rval = T_PASS;
521 		else
522 			rval = T_FAIL;
523 	} else {
524 		t_info("dns_name_fromtext %s failed, result = %s\n",
525 				test_name, dns_result_totext(result));
526 	}
527 	return (rval);
528 }
529 
530 static void
t_dns_name_isabsolute(void)531 t_dns_name_isabsolute(void) {
532 	int	line;
533 	int	cnt;
534 	int	result;
535 	char	*p;
536 	FILE	*fp;
537 
538 	t_assert("dns_name_isabsolute", 1, T_REQUIRED, "%s", a7);
539 
540 	result = T_UNRESOLVED;
541 	fp = fopen("dns_name_isabsolute_data", "r");
542 	if (fp != NULL) {
543 		line = 0;
544 		while ((p = t_fgetbs(fp)) != NULL) {
545 
546 			++line;
547 
548 			/*
549 			 * Skip comment lines.
550 			 */
551 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
552 				(void)free(p);
553 				continue;
554 			}
555 
556 			cnt = bustline(p, Tokens);
557 			if (cnt == 2) {
558 				/*
559 				 * label, bitpos, expected value.
560 				 */
561 				result = test_dns_name_isabsolute(Tokens[0],
562 								atoi(Tokens[1])
563 								  == 0 ?
564 								  ISC_FALSE :
565 								  ISC_TRUE);
566 			} else {
567 				t_info("bad datafile format at line %d\n",
568 				       line);
569 			}
570 
571 			(void)free(p);
572 			t_result(result);
573 		}
574 		(void)fclose(fp);
575 	} else {
576 		t_info("Missing datafile dns_name_isabsolute_data\n");
577 		t_result(result);
578 	}
579 }
580 
581 static const char *a8 =	"dns_name_hash(name, case_sensitive) returns "
582 		"a hash of 'name' which is case_sensitive if case_sensitive "
583 		"is true";
584 
585 /*%
586  * a9 merged with a8.
587  */
588 
589 static int
test_dns_name_hash(char * test_name1,char * test_name2,isc_boolean_t csh_match,isc_boolean_t cish_match)590 test_dns_name_hash(char *test_name1, char *test_name2,
591 		isc_boolean_t csh_match, isc_boolean_t cish_match) {
592 	int		rval;
593 	int		failures;
594 	isc_boolean_t	match;
595 	unsigned int	hash1;
596 	unsigned int	hash2;
597 	dns_fixedname_t	fixed1;
598 	dns_fixedname_t	fixed2;
599 	dns_name_t	*dns_name1;
600 	dns_name_t	*dns_name2;
601 	isc_result_t	result;
602 
603 	rval = T_UNRESOLVED;
604 	failures = 0;
605 
606 	t_info("testing names %s and %s\n", test_name1, test_name2);
607 
608 	dns_fixedname_init(&fixed1);
609 	dns_fixedname_init(&fixed2);
610 	dns_name1 = dns_fixedname_name(&fixed1);
611 	dns_name2 = dns_fixedname_name(&fixed2);
612 	result = dns_name_fromstring2(dns_name1, test_name1, NULL, 0, NULL);
613 	if (result == ISC_R_SUCCESS) {
614 		result = dns_name_fromstring2(dns_name2, test_name2, NULL,
615 					      0, NULL);
616 		if (result == ISC_R_SUCCESS) {
617 			hash1 = dns_name_hash(dns_name1, ISC_TRUE);
618 			hash2 = dns_name_hash(dns_name2, ISC_TRUE);
619 			match = ISC_FALSE;
620 			if (hash1 == hash2)
621 				match = ISC_TRUE;
622 			if (match != csh_match) {
623 				++failures;
624 				t_info("hash mismatch when ISC_TRUE\n");
625 			}
626 			hash1 = dns_name_hash(dns_name1, ISC_FALSE);
627 			hash2 = dns_name_hash(dns_name2, ISC_FALSE);
628 			match = ISC_FALSE;
629 			if (hash1 == hash2)
630 				match = ISC_TRUE;
631 			if (match != cish_match) {
632 				++failures;
633 				t_info("hash mismatch when ISC_FALSE\n");
634 			}
635 			if (failures == 0)
636 				rval = T_PASS;
637 			else
638 				rval = T_FAIL;
639 		} else {
640 			t_info("dns_name_fromstring2 %s failed, result = %s\n",
641 				test_name2, dns_result_totext(result));
642 		}
643 	} else {
644 		t_info("dns_name_fromstring2 %s failed, result = %s\n",
645 				test_name1, dns_result_totext(result));
646 	}
647 	return (rval);
648 }
649 
650 static void
t_dns_name_hash(void)651 t_dns_name_hash(void) {
652 	int	line;
653 	int	cnt;
654 	int	result;
655 	char	*p;
656 	FILE	*fp;
657 
658 	t_assert("dns_name_hash", 1, T_REQUIRED, "%s", a8);
659 
660 	result = T_UNRESOLVED;
661 	fp = fopen("dns_name_hash_data", "r");
662 	if (fp != NULL) {
663 		line = 0;
664 		while ((p = t_fgetbs(fp)) != NULL) {
665 
666 			++line;
667 
668 			/*
669 			 * Skip comment lines.
670 			 */
671 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
672 				(void)free(p);
673 				continue;
674 			}
675 
676 			cnt = bustline(p, Tokens);
677 			if (cnt == 4) {
678 				/*
679 				 * name1, name2, exp match value if
680 				 * case_sensitive true,
681 				 * exp match value of case_sensitive false
682 				 */
683 				result = test_dns_name_hash(
684 						Tokens[0],
685 						Tokens[1],
686 						atoi(Tokens[2]) == 0 ?
687 							ISC_FALSE : ISC_TRUE,
688 						atoi(Tokens[3]) == 0 ?
689 							ISC_FALSE : ISC_TRUE);
690 			} else {
691 				t_info("bad datafile format at line %d\n",
692 						line);
693 			}
694 
695 			(void)free(p);
696 			t_result(result);
697 		}
698 		(void)fclose(fp);
699 	} else {
700 		t_info("Missing datafile dns_name_hash_data\n");
701 		t_result(result);
702 	}
703 }
704 
705 static const char *a10 =
706 		"dns_name_fullcompare(name1, name2, orderp, nlabelsp) "
707 		"returns the DNSSEC ordering relationship between name1 and "
708 		"name2, sets orderp to -1 if name1 < name2, to 0 if "
709 		"name1 == name2, or to 1 if name1 > name2, sets nlabelsp "
710 		"to the number of labels name1 and name2 have in common, "
711 		"and sets nbitsp to the number of bits name1 and name2 "
712 		"have in common";
713 
714 /*%
715  * a11 thru a22 merged into a10.
716  */
717 static const char *
dns_namereln_to_text(dns_namereln_t reln)718 dns_namereln_to_text(dns_namereln_t reln) {
719 	const char *p;
720 
721 	if (reln == dns_namereln_contains)
722 		p = "contains";
723 	else if (reln == dns_namereln_subdomain)
724 		p = "subdomain";
725 	else if (reln == dns_namereln_equal)
726 		p = "equal";
727 	else if (reln == dns_namereln_none)
728 		p = "none";
729 	else if (reln == dns_namereln_commonancestor)
730 		p = "commonancestor";
731 	else
732 		p = "unknown";
733 
734 	return (p);
735 }
736 
737 static int
test_dns_name_fullcompare(char * name1,char * name2,dns_namereln_t exp_dns_reln,int exp_order,int exp_nlabels)738 test_dns_name_fullcompare(char *name1, char *name2,
739 			  dns_namereln_t exp_dns_reln,
740 			  int exp_order, int exp_nlabels)
741 {
742 	int		result;
743 	int		nfails;
744 	int		order;
745 	unsigned int	nlabels;
746 	dns_fixedname_t fixed1;
747 	dns_fixedname_t fixed2;
748 	dns_name_t	*dns_name1;
749 	dns_name_t	*dns_name2;
750 	isc_result_t	dns_result;
751 	dns_namereln_t	dns_reln;
752 
753 	nfails = 0;
754 	result = T_UNRESOLVED;
755 
756 
757 	t_info("testing names %s and %s for relation %s\n", name1, name2,
758 	       dns_namereln_to_text(exp_dns_reln));
759 
760 	dns_fixedname_init(&fixed1);
761 	dns_fixedname_init(&fixed2);
762 	dns_name1 = dns_fixedname_name(&fixed1);
763 	dns_name2 = dns_fixedname_name(&fixed2);
764 	dns_result = dns_name_fromstring2(dns_name1, name1, NULL, 0, NULL);
765 	if (dns_result == ISC_R_SUCCESS) {
766 		dns_result = dns_name_fromstring2(dns_name2, name2, NULL,
767 						  0, NULL);
768 		if (dns_result == ISC_R_SUCCESS) {
769 			dns_reln = dns_name_fullcompare(dns_name1, dns_name2,
770 					&order, &nlabels);
771 
772 			if (dns_reln != exp_dns_reln) {
773 				++nfails;
774 				t_info("expected relationship of %s, got %s\n",
775 					dns_namereln_to_text(exp_dns_reln),
776 					dns_namereln_to_text(dns_reln));
777 			}
778 			/*
779 			 * Normalize order.
780 			 */
781 			if (order < 0)
782 				order = -1;
783 			else if (order > 0)
784 				order = 1;
785 			if (order != exp_order) {
786 				++nfails;
787 				t_info("expected ordering %d, got %d\n",
788 						exp_order, order);
789 			}
790 			if ((exp_nlabels >= 0) &&
791 			    (nlabels != (unsigned int)exp_nlabels)) {
792 				++nfails;
793 				t_info("expecting %d labels, got %d\n",
794 				       exp_nlabels, nlabels);
795 			}
796 			if (nfails == 0)
797 				result = T_PASS;
798 			else
799 				result = T_FAIL;
800 		} else {
801 			t_info("dns_name_fromstring2 failed, result == %s\n",
802 			       dns_result_totext(result));
803 		}
804 	} else {
805 		t_info("dns_name_fromstring2 failed, result == %s\n",
806 		       dns_result_totext(result));
807 	}
808 
809 	return (result);
810 }
811 
812 static void
t_dns_name_fullcompare(void)813 t_dns_name_fullcompare(void) {
814 	int		line;
815 	int		cnt;
816 	int		result;
817 	char		*p;
818 	FILE		*fp;
819 	dns_namereln_t	reln;
820 
821 	t_assert("dns_name_fullcompare", 1, T_REQUIRED, "%s", a10);
822 
823 	result = T_UNRESOLVED;
824 	fp = fopen("dns_name_fullcompare_data", "r");
825 	if (fp != NULL) {
826 		line = 0;
827 		while ((p = t_fgetbs(fp)) != NULL) {
828 			++line;
829 
830 			/*
831 			 * Skip comment lines.
832 			 */
833 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
834 				(void)free(p);
835 				continue;
836 			}
837 
838 			cnt = bustline(p, Tokens);
839 			if (cnt == 6) {
840 				/*
841 				 * name1, name2, exp_reln, exp_order,
842 				 * exp_nlabels
843 				 */
844 				if (!strcmp(Tokens[2], "none"))
845 					reln = dns_namereln_none;
846 				else if (!strcmp(Tokens[2], "contains"))
847 					reln = dns_namereln_contains;
848 				else if (!strcmp(Tokens[2], "subdomain"))
849 					reln = dns_namereln_subdomain;
850 				else if (!strcmp(Tokens[2], "equal"))
851 					reln = dns_namereln_equal;
852 				else if (!strcmp(Tokens[2], "commonancestor"))
853 					reln = dns_namereln_commonancestor;
854 				else {
855 					t_info("bad format at line %d\n", line);
856 					(void)free(p);
857 					continue;
858 				}
859 				result = test_dns_name_fullcompare(
860 						Tokens[0],
861 						Tokens[1],
862 						reln,
863 						atoi(Tokens[3]),
864 						atoi(Tokens[4]));
865 			} else {
866 				t_info("bad format at line %d\n", line);
867 			}
868 
869 			(void)free(p);
870 			t_result(result);
871 		}
872 		(void)fclose(fp);
873 	} else {
874 		t_info("Missing datafile dns_name_fullcompare_data\n");
875 		t_result(result);
876 	}
877 }
878 
879 static const char *a23 =
880 		"dns_name_compare(name1, name2) returns information about "
881 		"the relative ordering under the DNSSEC ordering relationship "
882 		"of name1 and name2";
883 
884 /*%
885  * a24 thru a29 merged into a23.
886  */
887 
888 static int
test_dns_name_compare(char * name1,char * name2,int exp_order)889 test_dns_name_compare(char *name1, char *name2, int exp_order) {
890 	int		result;
891 	int		order;
892 	isc_result_t	dns_result;
893 	dns_fixedname_t fixed1;
894 	dns_fixedname_t fixed2;
895 	dns_name_t	*dns_name1;
896 	dns_name_t	*dns_name2;
897 
898 	result = T_UNRESOLVED;
899 
900 	t_info("testing %s %s %s\n", name1,
901 	       exp_order == 0 ? "==": (exp_order == -1 ? "<" : ">"),
902 	       name2);
903 
904 	dns_fixedname_init(&fixed1);
905 	dns_fixedname_init(&fixed2);
906 	dns_name1 = dns_fixedname_name(&fixed1);
907 	dns_name2 = dns_fixedname_name(&fixed2);
908 	dns_result = dns_name_fromstring2(dns_name1, name1, NULL, 0, NULL);
909 	if (dns_result == ISC_R_SUCCESS) {
910 		dns_result = dns_name_fromstring2(dns_name2, name2, NULL,
911 						  0, NULL);
912 		if (dns_result == ISC_R_SUCCESS) {
913 			order = dns_name_compare(dns_name1, dns_name2);
914 			/*
915 			 * Normalize order.
916 			 */
917 			if (order < 0)
918 				order = -1;
919 			else if (order > 0)
920 				order = 1;
921 			if (order != exp_order) {
922 				t_info("expected order of %d, got %d\n",
923 				       exp_order, order);
924 				result = T_FAIL;
925 			} else
926 				result = T_PASS;
927 		} else {
928 			t_info("dns_name_fromstring2 failed, result == %s\n",
929 					dns_result_totext(result));
930 		}
931 	} else {
932 		t_info("dns_name_fromstring2 failed, result == %s\n",
933 				dns_result_totext(result));
934 	}
935 
936 	return (result);
937 }
938 
939 static void
t_dns_name_compare(void)940 t_dns_name_compare(void) {
941 	int		line;
942 	int		cnt;
943 	int		result;
944 	char		*p;
945 	FILE		*fp;
946 
947 	t_assert("dns_name_compare", 1, T_REQUIRED, "%s", a23);
948 
949 	result = T_UNRESOLVED;
950 	fp = fopen("dns_name_compare_data", "r");
951 	if (fp != NULL) {
952 		line = 0;
953 		while ((p = t_fgetbs(fp)) != NULL) {
954 
955 			++line;
956 
957 			/*
958 			 * Skip comment lines.
959 			 */
960 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
961 				(void)free(p);
962 				continue;
963 			}
964 
965 			cnt = bustline(p, Tokens);
966 			if (cnt == 3) {
967 				/*
968 				 * name1, name2, order.
969 				 */
970 				result = test_dns_name_compare(
971 						Tokens[0],
972 						Tokens[1],
973 						atoi(Tokens[2]));
974 			} else {
975 				t_info("bad datafile format at line %d\n",
976 				       line);
977 			}
978 
979 			(void)free(p);
980 			t_result(result);
981 		}
982 		(void)fclose(fp);
983 	} else {
984 		t_info("Missing datafile dns_name_compare_data\n");
985 		t_result(result);
986 	}
987 }
988 
989 static const char *a30 =
990 		"dns_name_rdatacompare(name1, name2) returns information "
991 		"about the relative ordering of name1 and name2 as if they "
992 		"are part of rdata in DNSSEC canonical form";
993 
994 /*%
995  * a31, a32 merged into a30.
996  */
997 
998 static int
test_dns_name_rdatacompare(char * name1,char * name2,int exp_order)999 test_dns_name_rdatacompare(char *name1, char *name2, int exp_order) {
1000 	int		result;
1001 	int		order;
1002 	isc_result_t	dns_result;
1003 	dns_fixedname_t fixed1;
1004 	dns_fixedname_t fixed2;
1005 	dns_name_t	*dns_name1;
1006 	dns_name_t	*dns_name2;
1007 
1008 	result = T_UNRESOLVED;
1009 
1010 	t_info("testing %s %s %s\n", name1,
1011 	       exp_order == 0 ? "==": (exp_order == -1 ? "<" : ">"), name2);
1012 
1013 	dns_fixedname_init(&fixed1);
1014 	dns_fixedname_init(&fixed2);
1015 	dns_name1 = dns_fixedname_name(&fixed1);
1016 	dns_name2 = dns_fixedname_name(&fixed2);
1017 	dns_result = dns_name_fromstring2(dns_name1, name1, NULL, 0, NULL);
1018 	if (dns_result == ISC_R_SUCCESS) {
1019 		dns_result = dns_name_fromstring2(dns_name2, name2, NULL,
1020 						  0, NULL);
1021 		if (dns_result == ISC_R_SUCCESS) {
1022 			order = dns_name_rdatacompare(dns_name1, dns_name2);
1023 			/*
1024 			 * Normalize order.
1025 			 */
1026 			if (order < 0)
1027 				order = -1;
1028 			else if (order > 0)
1029 				order = 1;
1030 			if (order != exp_order) {
1031 				t_info("expected order of %d, got %d\n",
1032 				       exp_order, order);
1033 				result = T_FAIL;
1034 			} else
1035 				result = T_PASS;
1036 		} else {
1037 			t_info("dns_name_fromstring2 failed, result == %s\n",
1038 			       dns_result_totext(result));
1039 		}
1040 	} else {
1041 		t_info("dns_name_fromstring2 failed, result == %s\n",
1042 		       dns_result_totext(result));
1043 	}
1044 
1045 	return (result);
1046 }
1047 
1048 static void
t_dns_name_rdatacompare(void)1049 t_dns_name_rdatacompare(void) {
1050 	int		line;
1051 	int		cnt;
1052 	int		result;
1053 	char		*p;
1054 	FILE		*fp;
1055 
1056 	t_assert("dns_name_rdatacompare", 1, T_REQUIRED, "%s", a30);
1057 
1058 	result = T_UNRESOLVED;
1059 	fp = fopen("dns_name_rdatacompare_data", "r");
1060 	if (fp != NULL) {
1061 		line = 0;
1062 		while ((p = t_fgetbs(fp)) != NULL) {
1063 
1064 			++line;
1065 
1066 			/*
1067 			 * Skip comment lines.
1068 			 */
1069 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
1070 				(void)free(p);
1071 				continue;
1072 			}
1073 
1074 			cnt = bustline(p, Tokens);
1075 			if (cnt == 3) {
1076 				/*
1077 				 * name1, name2, order.
1078 				 */
1079 				result = test_dns_name_rdatacompare(
1080 						Tokens[0],
1081 						Tokens[1],
1082 						atoi(Tokens[2]));
1083 			} else {
1084 				t_info("bad datafile format at line %d\n",
1085 				       line);
1086 			}
1087 
1088 			(void)free(p);
1089 			t_result(result);
1090 		}
1091 		(void)fclose(fp);
1092 	} else {
1093 		t_info("Missing datafile dns_name_rdatacompare_data\n");
1094 		t_result(result);
1095 	}
1096 }
1097 
1098 
1099 static const char *a33 =
1100 		"when name1 is a subdomain of name2, "
1101 		"dns_name_issubdomain(name1, name2) returns true, "
1102 		"otherwise it returns false.";
1103 
1104 /*%
1105  * a34 merged into a33.
1106  */
1107 
1108 static int
test_dns_name_issubdomain(char * name1,char * name2,isc_boolean_t exp_rval)1109 test_dns_name_issubdomain(char *name1, char *name2, isc_boolean_t exp_rval) {
1110 	int		result;
1111 	isc_boolean_t	rval;
1112 	isc_result_t	dns_result;
1113 	dns_fixedname_t fixed1;
1114 	dns_fixedname_t fixed2;
1115 	dns_name_t	*dns_name1;
1116 	dns_name_t	*dns_name2;
1117 
1118 	result = T_UNRESOLVED;
1119 
1120 	t_info("testing %s %s a subdomain of %s\n", name1,
1121 	       exp_rval == 0 ? "is not" : "is", name2);
1122 
1123 	dns_fixedname_init(&fixed1);
1124 	dns_fixedname_init(&fixed2);
1125 	dns_name1 = dns_fixedname_name(&fixed1);
1126 	dns_name2 = dns_fixedname_name(&fixed2);
1127 	dns_result = dns_name_fromstring2(dns_name1, name1, NULL, 0, NULL);
1128 	if (dns_result == ISC_R_SUCCESS) {
1129 		dns_result = dns_name_fromstring2(dns_name2, name2, NULL,
1130 						  0, NULL);
1131 		if (dns_result == ISC_R_SUCCESS) {
1132 			rval = dns_name_issubdomain(dns_name1, dns_name2);
1133 
1134 			if (rval != exp_rval) {
1135 				t_info("expected return value of %s, got %s\n",
1136 				       exp_rval == ISC_TRUE ? "true" : "false",
1137 				       rval == ISC_TRUE ? "true" : "false");
1138 				result = T_FAIL;
1139 			} else
1140 				result = T_PASS;
1141 		} else {
1142 			t_info("dns_name_fromstring2 failed, result == %s\n",
1143 			       dns_result_totext(result));
1144 		}
1145 	} else {
1146 		t_info("dns_name_fromstring2 failed, result == %s\n",
1147 		       dns_result_totext(result));
1148 	}
1149 
1150 	return (result);
1151 }
1152 
1153 static void
t_dns_name_issubdomain(void)1154 t_dns_name_issubdomain(void) {
1155 	int		line;
1156 	int		cnt;
1157 	int		result;
1158 	char		*p;
1159 	FILE		*fp;
1160 
1161 	t_assert("dns_name_issubdomain", 1, T_REQUIRED, "%s", a33);
1162 
1163 	result = T_UNRESOLVED;
1164 	fp = fopen("dns_name_issubdomain_data", "r");
1165 	if (fp != NULL) {
1166 		line = 0;
1167 		while ((p = t_fgetbs(fp)) != NULL) {
1168 
1169 			++line;
1170 
1171 			/*
1172 			 * Skip comment lines.
1173 			 */
1174 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
1175 				(void)free(p);
1176 				continue;
1177 			}
1178 
1179 			cnt = bustline(p, Tokens);
1180 			if (cnt == 3) {
1181 				/*
1182 				 * name1, name2, issubdomain_p.
1183 				 */
1184 				result = test_dns_name_issubdomain(
1185 						Tokens[0],
1186 						Tokens[1],
1187 						atoi(Tokens[2]) == 0 ?
1188 						ISC_FALSE : ISC_TRUE);
1189 			} else {
1190 				t_info("bad datafile format at line %d\n",
1191 				       line);
1192 			}
1193 
1194 			(void)free(p);
1195 			t_result(result);
1196 		}
1197 		(void)fclose(fp);
1198 	} else {
1199 		t_info("Missing datafile dns_name_issubdomain_data\n");
1200 		t_result(result);
1201 	}
1202 }
1203 
1204 static const char *a35 =
1205 		"dns_name_countlabels(name) returns the number "
1206 		"of labels in name";
1207 
1208 static int
test_dns_name_countlabels(char * test_name,unsigned int exp_nlabels)1209 test_dns_name_countlabels(char *test_name, unsigned int exp_nlabels) {
1210 	int		result;
1211 	unsigned int	nlabels;
1212 	isc_result_t	dns_result;
1213 	dns_fixedname_t	fixed;
1214 	dns_name_t	*dns_name;
1215 
1216 	result = T_UNRESOLVED;
1217 
1218 	t_info("testing %s\n", test_name);
1219 
1220 	dns_fixedname_init(&fixed);
1221 	dns_name = dns_fixedname_name(&fixed);
1222 	dns_result = dns_name_fromstring2(dns_name, test_name, NULL, 0, NULL);
1223 	if (dns_result == ISC_R_SUCCESS) {
1224 		nlabels = dns_name_countlabels(dns_name);
1225 
1226 		if (nlabels != exp_nlabels) {
1227 			t_info("expected %d, got %d\n", exp_nlabels, nlabels);
1228 			result = T_FAIL;
1229 		} else
1230 			result = T_PASS;
1231 	} else {
1232 		t_info("dns_name_fromstring2 failed, result == %s\n",
1233 		       dns_result_totext(dns_result));
1234 	}
1235 
1236 	return (result);
1237 }
1238 
1239 static void
t_dns_name_countlabels(void)1240 t_dns_name_countlabels(void) {
1241 	int		line;
1242 	int		cnt;
1243 	int		result;
1244 	char		*p;
1245 	FILE		*fp;
1246 
1247 	t_assert("dns_name_countlabels", 1, T_REQUIRED, "%s", a35);
1248 
1249 	result = T_UNRESOLVED;
1250 	fp = fopen("dns_name_countlabels_data", "r");
1251 	if (fp != NULL) {
1252 		line = 0;
1253 		while ((p = t_fgetbs(fp)) != NULL) {
1254 
1255 			++line;
1256 
1257 			/*
1258 			 * Skip comment lines.
1259 			 */
1260 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
1261 				(void)free(p);
1262 				continue;
1263 			}
1264 
1265 			cnt = bustline(p, Tokens);
1266 			if (cnt == 2) {
1267 				/*
1268 				 * name, nlabels.
1269 				 */
1270 				result = test_dns_name_countlabels(Tokens[0],
1271 							      atoi(Tokens[1]));
1272 			} else {
1273 				t_info("bad format at line %d\n", line);
1274 			}
1275 
1276 			(void)free(p);
1277 			t_result(result);
1278 		}
1279 		(void)fclose(fp);
1280 	} else {
1281 		t_info("Missing datafile dns_name_countlabels_data\n");
1282 		t_result(result);
1283 	}
1284 }
1285 
1286 static const char *a36 =
1287 		"when n is less than the number of labels in name, "
1288 		"dns_name_getlabel(name, n, labelp) initializes labelp "
1289 		"to point to the nth label in name";
1290 
1291 /*%
1292  * The strategy here is two take two dns names with a shared label in
1293  * different positions, get the two labels and compare them for equality.
1294  * If they don't match, dns_name_getlabel failed.
1295  */
1296 
1297 static int
test_dns_name_getlabel(char * test_name1,int label1_pos,char * test_name2,int label2_pos)1298 test_dns_name_getlabel(char *test_name1, int label1_pos, char *test_name2,
1299 		       int label2_pos)
1300 {
1301 	int		result;
1302 	int		nfails;
1303 	unsigned int	cnt;
1304 	unsigned char	*p;
1305 	unsigned char	*q;
1306 	dns_fixedname_t fixed1;
1307 	dns_fixedname_t fixed2;
1308 	dns_name_t	*dns_name1;
1309 	dns_name_t	*dns_name2;
1310 	dns_label_t	dns_label1;
1311 	dns_label_t	dns_label2;
1312 	isc_result_t	dns_result;
1313 
1314 	nfails = 0;
1315 	result = T_UNRESOLVED;
1316 
1317 	t_info("testing with %s and %s\n", test_name1, test_name2);
1318 
1319 	dns_fixedname_init(&fixed1);
1320 	dns_fixedname_init(&fixed2);
1321 	dns_name1 = dns_fixedname_name(&fixed1);
1322 	dns_name2 = dns_fixedname_name(&fixed2);
1323 	dns_result = dns_name_fromstring2(dns_name1, test_name1, NULL,
1324 					  0, NULL);
1325 	if (dns_result == ISC_R_SUCCESS) {
1326 		dns_result = dns_name_fromstring2(dns_name2, test_name2, NULL,
1327 						  0, NULL);
1328 		if (dns_result == ISC_R_SUCCESS) {
1329 			dns_name_getlabel(dns_name1, label1_pos, &dns_label1);
1330 			dns_name_getlabel(dns_name2, label2_pos, &dns_label2);
1331 			if (dns_label1.length != dns_label2.length) {
1332 				t_info("label lengths differ\n");
1333 				++nfails;
1334 			}
1335 			p = dns_label1.base;
1336 			q = dns_label2.base;
1337 			for (cnt = 0; cnt < dns_label1.length; ++cnt) {
1338 				if (*p++ != *q++) {
1339 					t_info("labels differ at position %d",
1340 					       cnt);
1341 					++nfails;
1342 				}
1343 			}
1344 			if (nfails == 0)
1345 				result = T_PASS;
1346 			else
1347 				result = T_FAIL;
1348 		} else {
1349 			t_info("dns_name_fromstring2 failed, result == %s",
1350 			       dns_result_totext(result));
1351 		}
1352 	} else {
1353 		t_info("dns_name_fromstring2 failed, result == %s",
1354 		       dns_result_totext(result));
1355 	}
1356 	return (result);
1357 }
1358 
1359 static void
t_dns_name_getlabel(void)1360 t_dns_name_getlabel(void) {
1361 	int		line;
1362 	int		cnt;
1363 	int		result;
1364 	char		*p;
1365 	FILE		*fp;
1366 
1367 	t_assert("dns_name_getlabel", 1, T_REQUIRED, "%s", a36);
1368 
1369 	result = T_UNRESOLVED;
1370 	fp = fopen("dns_name_getlabel_data", "r");
1371 	if (fp != NULL) {
1372 		line = 0;
1373 		while ((p = t_fgetbs(fp)) != NULL) {
1374 
1375 			++line;
1376 
1377 			/*
1378 			 * Skip comment lines.
1379 			 */
1380 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
1381 				(void)free(p);
1382 				continue;
1383 			}
1384 
1385 			cnt = bustline(p, Tokens);
1386 			if (cnt == 4) {
1387 				/*
1388 				 * name1, name2, nlabels.
1389 				 */
1390 				result = test_dns_name_getlabel(Tokens[0],
1391 							      atoi(Tokens[1]),
1392 								   Tokens[2],
1393 							      atoi(Tokens[3]));
1394 			} else {
1395 				t_info("bad format at line %d\n", line);
1396 			}
1397 
1398 			(void)free(p);
1399 			t_result(result);
1400 		}
1401 		(void)fclose(fp);
1402 	} else {
1403 		t_info("Missing datafile dns_name_getlabel_data\n");
1404 		t_result(result);
1405 	}
1406 }
1407 
1408 static const char *a37 =
1409 		"when source contains at least first + n labels, "
1410 		"dns_name_getlabelsequence(source, first, n, target) "
1411 		"initializes target to point to the n label sequence of "
1412 		"labels in source starting with first";
1413 
1414 /*%
1415  * We adopt a similiar strategy to that used by the dns_name_getlabel test.
1416  */
1417 
1418 static int
test_dns_name_getlabelsequence(char * test_name1,int label1_start,char * test_name2,int label2_start,int range)1419 test_dns_name_getlabelsequence(char *test_name1, int label1_start,
1420 			       char *test_name2, int label2_start, int range)
1421 {
1422 	int		result;
1423 	int		nfails;
1424 	unsigned int	cnt;
1425 	unsigned char	*p;
1426 	unsigned char	*q;
1427 	dns_fixedname_t fixed1;
1428 	dns_fixedname_t fixed2;
1429 	dns_name_t	*dns_name1;
1430 	dns_name_t	*dns_name2;
1431 	dns_name_t	dns_targetname1;
1432 	dns_name_t	dns_targetname2;
1433 	isc_result_t	dns_result;
1434 	isc_buffer_t	buffer1;
1435 	isc_buffer_t	buffer2;
1436 	unsigned char	junk1[BUFLEN];
1437 	unsigned char	junk2[BUFLEN];
1438 
1439 	nfails = 0;
1440 	result = T_UNRESOLVED;
1441 	dns_fixedname_init(&fixed1);
1442 	dns_fixedname_init(&fixed2);
1443 	dns_name1 = dns_fixedname_name(&fixed1);
1444 	dns_name2 = dns_fixedname_name(&fixed2);
1445 	dns_result = dns_name_fromstring2(dns_name1, test_name1, NULL,
1446 					  0, NULL);
1447 	if (dns_result == ISC_R_SUCCESS) {
1448 		dns_result = dns_name_fromstring2(dns_name2, test_name2, NULL,
1449 						  0, NULL);
1450 		if (dns_result == ISC_R_SUCCESS) {
1451 			t_info("testing %s %s\n", test_name1, test_name2);
1452 			dns_name_init(&dns_targetname1, NULL);
1453 			dns_name_init(&dns_targetname2, NULL);
1454 			dns_name_getlabelsequence(dns_name1, label1_start,
1455 						  range, &dns_targetname1);
1456 			dns_name_getlabelsequence(dns_name2, label2_start,
1457 						  range, &dns_targetname2);
1458 
1459 			/*
1460 			 * Now convert both targets to text for comparison.
1461 			 */
1462 			isc_buffer_init(&buffer1, junk1, BUFLEN);
1463 			isc_buffer_init(&buffer2, junk2, BUFLEN);
1464 			dns_name_totext(&dns_targetname1, ISC_TRUE, &buffer1);
1465 			dns_name_totext(&dns_targetname2, ISC_TRUE, &buffer2);
1466 			if (buffer1.used == buffer2.used) {
1467 				p = buffer1.base;
1468 				q = buffer2.base;
1469 				for (cnt = 0; cnt < buffer1.used; ++cnt) {
1470 					if (*p != *q) {
1471 						++nfails;
1472 						t_info("names differ at %d\n",
1473 						       cnt);
1474 						break;
1475 					}
1476 					++p; ++q;
1477 				}
1478 			} else {
1479 				++nfails;
1480 				t_info("lengths differ\n");
1481 			}
1482 			if (nfails == 0)
1483 				result = T_PASS;
1484 			else
1485 				result = T_FAIL;
1486 		} else {
1487 			t_info("dns_name_fromstring2 failed, result == %s",
1488 			       dns_result_totext(dns_result));
1489 		}
1490 	} else {
1491 		t_info("dns_name_fromstring2 failed, result == %s",
1492 		       dns_result_totext(dns_result));
1493 	}
1494 	return (result);
1495 }
1496 
1497 static void
t_dns_name_getlabelsequence(void)1498 t_dns_name_getlabelsequence(void) {
1499 	int		line;
1500 	int		cnt;
1501 	int		result;
1502 	char		*p;
1503 	FILE		*fp;
1504 
1505 	t_assert("dns_name_getlabelsequence", 1, T_REQUIRED, "%s", a37);
1506 
1507 	result = T_UNRESOLVED;
1508 	fp = fopen("dns_name_getlabelsequence_data", "r");
1509 	if (fp != NULL) {
1510 		line = 0;
1511 		while ((p = t_fgetbs(fp)) != NULL) {
1512 
1513 			++line;
1514 
1515 			/*
1516 			 * Skip comment lines.
1517 			 */
1518 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
1519 				(void)free(p);
1520 				continue;
1521 			}
1522 
1523 			cnt = bustline(p, Tokens);
1524 			if (cnt == 5) {
1525 				/*
1526 				 * name1, name2, nlabels.
1527 				 */
1528 				result = test_dns_name_getlabelsequence(
1529 								   Tokens[0],
1530 							      atoi(Tokens[1]),
1531 								   Tokens[2],
1532 							      atoi(Tokens[3]),
1533 							      atoi(Tokens[4]));
1534 			} else {
1535 				t_info("bad format at line %d\n", line);
1536 			}
1537 
1538 			(void)free(p);
1539 			t_result(result);
1540 		}
1541 		(void)fclose(fp);
1542 	} else {
1543 		t_info("Missing datafile dns_name_getlabelsequence_data\n");
1544 		t_result(result);
1545 	}
1546 }
1547 
1548 static const char *a38 =
1549 		"dns_name_fromregion(name, region) converts a DNS name "
1550 		"from a region representation to a name representation";
1551 
1552 static int
test_dns_name_fromregion(char * test_name)1553 test_dns_name_fromregion(char *test_name) {
1554 	int		result;
1555 	int		order;
1556 	unsigned int	nlabels;
1557 	isc_result_t	dns_result;
1558 	dns_fixedname_t fixed1;
1559 	dns_name_t	*dns_name1;
1560 	dns_name_t	dns_name2;
1561 	dns_namereln_t	dns_namereln;
1562 	isc_region_t	region;
1563 
1564 	result = T_UNRESOLVED;
1565 
1566 	t_info("testing %s\n", test_name);
1567 
1568 	dns_fixedname_init(&fixed1);
1569 	dns_name1 = dns_fixedname_name(&fixed1);
1570 	dns_result = dns_name_fromstring2(dns_name1, test_name, NULL, 0, NULL);
1571 	if (dns_result == ISC_R_SUCCESS) {
1572 
1573 		dns_name_toregion(dns_name1, &region);
1574 
1575 		dns_name_init(&dns_name2, NULL);
1576 		dns_name_fromregion(&dns_name2, &region);
1577 		dns_namereln = dns_name_fullcompare(dns_name1, &dns_name2,
1578 						    &order, &nlabels);
1579 		if (dns_namereln == dns_namereln_equal)
1580 			result = T_PASS;
1581 		else
1582 			result = T_FAIL;
1583 	} else {
1584 		t_info("dns_name_fromstring2 failed, result == %s\n",
1585 		       dns_result_totext(result));
1586 	}
1587 	return (result);
1588 }
1589 
1590 static void
t_dns_name_fromregion(void)1591 t_dns_name_fromregion(void) {
1592 	int		line;
1593 	int		cnt;
1594 	int		result;
1595 	char		*p;
1596 	FILE		*fp;
1597 
1598 	t_assert("dns_name_fromregion", 1, T_REQUIRED, "%s", a38);
1599 
1600 	result = T_UNRESOLVED;
1601 	fp = fopen("dns_name_fromregion_data", "r");
1602 	if (fp != NULL) {
1603 		line = 0;
1604 		while ((p = t_fgetbs(fp)) != NULL) {
1605 
1606 			++line;
1607 
1608 			/*
1609 			 * Skip comment lines.
1610 			 */
1611 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
1612 				(void)free(p);
1613 				continue;
1614 			}
1615 
1616 			cnt = bustline(p, Tokens);
1617 			if (cnt == 1) {
1618 				/*
1619 				 * test_name.
1620 				 */
1621 				result = test_dns_name_fromregion(Tokens[0]);
1622 			} else {
1623 				t_info("bad format at line %d\n", line);
1624 			}
1625 
1626 			(void)free(p);
1627 			t_result(result);
1628 		}
1629 		(void)fclose(fp);
1630 	} else {
1631 		t_info("Missing datafile dns_name_fromregion_data\n");
1632 		t_result(result);
1633 	}
1634 }
1635 
1636 static const char *a39 =
1637 		"dns_name_toregion(name, region) converts a DNS name "
1638 		"from a region representation to a name representation";
1639 
1640 static void
t_dns_name_toregion(void)1641 t_dns_name_toregion(void) {
1642 	int		line;
1643 	int		cnt;
1644 	int		result;
1645 	char		*p;
1646 	FILE		*fp;
1647 
1648 	t_assert("dns_name_toregion", 1, T_REQUIRED, "%s", a39);
1649 
1650 	result = T_UNRESOLVED;
1651 	fp = fopen("dns_name_toregion_data", "r");
1652 	if (fp != NULL) {
1653 		line = 0;
1654 		while ((p = t_fgetbs(fp)) != NULL) {
1655 
1656 			++line;
1657 
1658 			/*
1659 			 * Skip comment lines.
1660 			 */
1661 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
1662 				(void)free(p);
1663 				continue;
1664 			}
1665 
1666 			cnt = bustline(p, Tokens);
1667 			if (cnt == 1) {
1668 				/*
1669 				 * test_name.
1670 				 */
1671 				result = test_dns_name_fromregion(Tokens[0]);
1672 			} else {
1673 				t_info("bad format at line %d\n", line);
1674 			}
1675 
1676 			(void)free(p);
1677 			t_result(result);
1678 		}
1679 		(void)fclose(fp);
1680 	} else {
1681 		t_info("Missing datafile dns_name_toregion_data\n");
1682 		t_result(result);
1683 	}
1684 }
1685 
1686 static const char *a40 =
1687 		"dns_name_fromtext(name, source, origin, downcase, target) "
1688 		"converts the textual representation of a DNS name at source "
1689 		"into uncompressed wire form at target, appending origin to "
1690 		"the converted name if origin is non-NULL and converting "
1691 		"upper case to lower case during conversion "
1692 		"if downcase is true.";
1693 
1694 static int
test_dns_name_fromtext(char * test_name1,char * test_name2,char * test_origin,unsigned int downcase)1695 test_dns_name_fromtext(char *test_name1, char *test_name2, char *test_origin,
1696 		       unsigned int downcase)
1697 {
1698 	int		result;
1699 	int		order;
1700 	unsigned int	nlabels;
1701 	unsigned char	junk1[BUFLEN];
1702 	unsigned char	junk2[BUFLEN];
1703 	unsigned char	junk3[BUFLEN];
1704 	isc_buffer_t	binbuf1;
1705 	isc_buffer_t	binbuf2;
1706 	isc_buffer_t	binbuf3;
1707 	isc_buffer_t	txtbuf1;
1708 	isc_buffer_t	txtbuf2;
1709 	isc_buffer_t	txtbuf3;
1710 	dns_name_t	dns_name1;
1711 	dns_name_t	dns_name2;
1712 	dns_name_t	dns_name3;
1713 	isc_result_t	dns_result;
1714 	dns_namereln_t	dns_namereln;
1715 
1716 	t_info("testing %s %s %s\n", test_name1, test_name2, test_origin);
1717 
1718 	isc_buffer_init(&binbuf1, junk1, BUFLEN);
1719 	isc_buffer_init(&binbuf2, junk2, BUFLEN);
1720 	isc_buffer_init(&binbuf3, junk3, BUFLEN);
1721 
1722 	isc_buffer_init(&txtbuf1, test_name1, strlen(test_name1));
1723 	isc_buffer_add(&txtbuf1, strlen(test_name1));
1724 
1725 	isc_buffer_init(&txtbuf2, test_name2, strlen(test_name2));
1726 	isc_buffer_add(&txtbuf2, strlen(test_name2));
1727 
1728 	isc_buffer_init(&txtbuf3, test_origin, strlen(test_origin));
1729 	isc_buffer_add(&txtbuf3, strlen(test_origin));
1730 	dns_name_init(&dns_name1, NULL);
1731 	dns_name_init(&dns_name2, NULL);
1732 	dns_name_init(&dns_name3, NULL);
1733 	dns_name_setbuffer(&dns_name1, &binbuf1);
1734 	dns_name_setbuffer(&dns_name2, &binbuf2);
1735 	dns_name_setbuffer(&dns_name3, &binbuf3);
1736 
1737 	dns_result = dns_name_fromtext(&dns_name3,  &txtbuf3, NULL, 0,
1738 				       &binbuf3);
1739 	if (dns_result != ISC_R_SUCCESS) {
1740 		t_info("dns_name_fromtext(dns_name3) failed, result == %s\n",
1741 			dns_result_totext(dns_result));
1742 		return (T_FAIL);
1743 	}
1744 
1745 	dns_result = dns_name_fromtext(&dns_name1, &txtbuf1, &dns_name3,
1746 				       downcase, &binbuf1);
1747 	if (dns_result != ISC_R_SUCCESS) {
1748 		t_info("dns_name_fromtext(dns_name1) failed, result == %s\n",
1749 		       dns_result_totext(dns_result));
1750 		return (T_FAIL);
1751 	}
1752 
1753 	dns_result = dns_name_fromtext(&dns_name2,  &txtbuf2, NULL, 0,
1754 				       &binbuf2);
1755 	if (dns_result != ISC_R_SUCCESS) {
1756 		t_info("dns_name_fromtext(dns_name2) failed, result == %s\n",
1757 		       dns_result_totext(dns_result));
1758 		return (T_FAIL);
1759 	}
1760 
1761 	dns_namereln = dns_name_fullcompare(&dns_name1, &dns_name2, &order,
1762 					    &nlabels);
1763 
1764 	if (dns_namereln == dns_namereln_equal)
1765 		result = T_PASS;
1766 	else {
1767 		t_info("dns_name_fullcompare returned %s\n",
1768 		       dns_namereln_to_text(dns_namereln));
1769 		result = T_FAIL;
1770 	}
1771 
1772 	return (result);
1773 }
1774 
1775 static void
t_dns_name_fromtext(void)1776 t_dns_name_fromtext(void) {
1777 	int		line;
1778 	int		cnt;
1779 	int		result;
1780 	char		*p;
1781 	FILE		*fp;
1782 
1783 	t_assert("dns_name_fromtext", 1, T_REQUIRED, "%s", a40);
1784 
1785 	result = T_UNRESOLVED;
1786 	fp = fopen("dns_name_fromtext_data", "r");
1787 	if (fp != NULL) {
1788 		line = 0;
1789 		while ((p = t_fgetbs(fp)) != NULL) {
1790 
1791 			++line;
1792 
1793 			/*
1794 			 * Skip comment lines.
1795 			 */
1796 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
1797 				(void)free(p);
1798 				continue;
1799 			}
1800 
1801 			cnt = bustline(p, Tokens);
1802 			if (cnt == 4) {
1803 				/*
1804 				 * test_name1, test_name2, test_origin,
1805 				 * downcase.
1806 				 */
1807 				result = test_dns_name_fromtext(Tokens[0],
1808 								Tokens[1],
1809 								Tokens[2],
1810 							   atoi(Tokens[3])
1811 								== 0 ?
1812 								0 :
1813 							     DNS_NAME_DOWNCASE);
1814 			} else {
1815 				t_info("bad format at line %d\n", line);
1816 			}
1817 
1818 			(void)free(p);
1819 			t_result(result);
1820 		}
1821 		(void)fclose(fp);
1822 	} else {
1823 		t_info("Missing datafile dns_name_fromtext\n");
1824 		t_result(result);
1825 	}
1826 }
1827 
1828 static const char *a41 =
1829 		"dns_name_totext(name, omit_final_dot, target) converts "
1830 		"the DNS name 'name' in wire format to textual format "
1831 		"at target, and adds a final '.' to the name if "
1832 		"omit_final_dot is false";
1833 
1834 static int
test_dns_name_totext(char * test_name,isc_boolean_t omit_final)1835 test_dns_name_totext(char *test_name, isc_boolean_t omit_final) {
1836 	int		result;
1837 	int		len;
1838 	int		order;
1839 	unsigned int	nlabels;
1840 	unsigned char	junk1[BUFLEN];
1841 	unsigned char	junk2[BUFLEN];
1842 	unsigned char	junk3[BUFLEN];
1843 	isc_buffer_t	buf1;
1844 	isc_buffer_t	buf2;
1845 	isc_buffer_t	buf3;
1846 	dns_name_t	dns_name1;
1847 	dns_name_t	dns_name2;
1848 	isc_result_t	dns_result;
1849 	dns_namereln_t	dns_namereln;
1850 
1851 	t_info("testing %s\n", test_name);
1852 
1853 	len = strlen(test_name);
1854 	isc_buffer_init(&buf1, test_name, len);
1855 	isc_buffer_add(&buf1, len);
1856 
1857 	dns_name_init(&dns_name1, NULL);
1858 	isc_buffer_init(&buf2, junk2, BUFLEN);
1859 
1860 	/*
1861 	 * Out of the data file to dns_name1.
1862 	 */
1863 	dns_result = dns_name_fromtext(&dns_name1, &buf1, NULL, 0, &buf2);
1864 	if (dns_result != ISC_R_SUCCESS) {
1865 		t_info("dns_name_fromtext failed, result == %s\n",
1866 		       dns_result_totext(dns_result));
1867 		return (T_UNRESOLVED);
1868 	}
1869 
1870 	/*
1871 	 * From dns_name1 into a text buffer.
1872 	 */
1873 	isc_buffer_invalidate(&buf1);
1874 	isc_buffer_init(&buf1, junk1, BUFLEN);
1875 	dns_result = dns_name_totext(&dns_name1, omit_final, &buf1);
1876 	if (dns_result != ISC_R_SUCCESS) {
1877 		t_info("dns_name_totext failed, result == %s\n",
1878 		       dns_result_totext(dns_result));
1879 		return (T_FAIL);
1880 	}
1881 
1882 	/*
1883 	 * From the text buffer into dns_name2.
1884 	 */
1885 	dns_name_init(&dns_name2, NULL);
1886 	isc_buffer_init(&buf3, junk3, BUFLEN);
1887 	dns_result = dns_name_fromtext(&dns_name2, &buf1, NULL, 0, &buf3);
1888 	if (dns_result != ISC_R_SUCCESS) {
1889 		t_info("dns_name_fromtext failed, result == %s\n",
1890 		       dns_result_totext(dns_result));
1891 		return (T_UNRESOLVED);
1892 	}
1893 
1894 	dns_namereln = dns_name_fullcompare(&dns_name1, &dns_name2,
1895 					    &order, &nlabels);
1896 	if (dns_namereln == dns_namereln_equal)
1897 		result = T_PASS;
1898 	else {
1899 		t_info("dns_name_fullcompare returned %s\n",
1900 		       dns_namereln_to_text(dns_namereln));
1901 		result = T_FAIL;
1902 	}
1903 
1904 	return (result);
1905 }
1906 
1907 static void
t_dns_name_totext(void)1908 t_dns_name_totext(void) {
1909 	int		line;
1910 	int		cnt;
1911 	int		result;
1912 	char		*p;
1913 	FILE		*fp;
1914 
1915 	t_assert("dns_name_totext", 1, T_REQUIRED, "%s", a41);
1916 
1917 	result = T_UNRESOLVED;
1918 	fp = fopen("dns_name_totext_data", "r");
1919 	if (fp != NULL) {
1920 		line = 0;
1921 		while ((p = t_fgetbs(fp)) != NULL) {
1922 
1923 			++line;
1924 
1925 			/*
1926 			 * Skip comment lines.
1927 			 */
1928 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
1929 				(void)free(p);
1930 				continue;
1931 			}
1932 
1933 			cnt = bustline(p, Tokens);
1934 			if (cnt == 2) {
1935 				/*
1936 				 * test_name, omit_final.
1937 				 */
1938 				result = test_dns_name_totext(Tokens[0],
1939 							 atoi(Tokens[1]) == 0 ?
1940 							      ISC_FALSE :
1941 							      ISC_TRUE);
1942 			} else {
1943 				t_info("bad format at line %d\n", line);
1944 			}
1945 
1946 			(void)free(p);
1947 			t_result(result);
1948 		}
1949 		(void)fclose(fp);
1950 	} else {
1951 		t_info("Missing datafile dns_name_totext\n");
1952 		t_result(result);
1953 	}
1954 }
1955 
1956 static const char *a42 =
1957 		"dns_name_fromwire(name, source, dctx, downcase, target) "
1958 		"converts the possibly compressed DNS name 'name' in wire "
1959 		"format to canonicalized form at target, performing upper to "
1960 		"lower case conversion if downcase is true, and returns "
1961 		"ISC_R_SUCCESS";
1962 
1963 #if 0
1964 	/*
1965 	 * XXXRTH these tests appear to be broken, so I have
1966 	 * disabled them.
1967 	 */
1968 static const char *a43 =
1969 		"when a label length is invalid, dns_name_fromwire() "
1970 		"returns DNS_R_FORMERR";
1971 
1972 static const char *a44 =
1973 		"when a label type is invalid, dns_name_fromwire() "
1974 		"returns DNS_R_BADLABELTYPE";
1975 #endif
1976 
1977 static const char *a45 =
1978 		"when a name length is invalid, dns_name_fromwire() "
1979 		"returns DNS_R_FORMERR";
1980 
1981 static const char *a46 =
1982 		"when a compression type is invalid, dns_name_fromwire() "
1983 		"returns DNS_R_DISALLOWED";
1984 
1985 static const char *a47 =
1986 		"when a bad compression pointer is encountered, "
1987 		"dns_name_fromwire() returns DNS_R_BADPOINTER";
1988 
1989 static const char *a48 =
1990 		"when input ends unexpected, dns_name_fromwire() "
1991 		"returns ISC_R_UNEXPECTEDEND";
1992 
1993 static const char *a49 =
1994 		"when there is not enough space in target, "
1995 		"dns_name_fromwire(name, source, dcts, downcase, target) "
1996 		"returns ISC_R_NOSPACE";
1997 
1998 static int
test_dns_name_fromwire(char * datafile_name,int testname_offset,int downcase,unsigned int dc_method,char * exp_name,isc_result_t exp_result,size_t buflen)1999 test_dns_name_fromwire(char *datafile_name, int testname_offset, int downcase,
2000 		       unsigned int dc_method, char *exp_name,
2001 		       isc_result_t exp_result, size_t buflen)
2002 {
2003 	int			result;
2004 	int			order;
2005 	unsigned int		nlabels;
2006 	int			len;
2007 	unsigned char		buf1[BIGBUFLEN];
2008 	char			buf2[BUFLEN];
2009 	isc_buffer_t		iscbuf1;
2010 	isc_buffer_t		iscbuf2;
2011 	dns_fixedname_t		fixed2;
2012 	dns_name_t		dns_name1;
2013 	dns_name_t		*dns_name2;
2014 	isc_result_t		dns_result;
2015 	dns_namereln_t		dns_namereln;
2016 	dns_decompress_t	dctx;
2017 
2018 	t_info("testing using %s\n", datafile_name);
2019 	isc_buffer_init(&iscbuf1, buf1, sizeof(buf1));
2020 	len = getmsg(datafile_name, &iscbuf1);
2021 	if (len == 0)
2022 		return (T_FAIL);
2023 
2024 	isc_buffer_setactive(&iscbuf1, len);
2025 	iscbuf1.current = testname_offset;
2026 
2027 	isc_buffer_init(&iscbuf2, buf2, (unsigned int)buflen);
2028 	dns_name_init(&dns_name1, NULL);
2029 	dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_STRICT);
2030 	dns_decompress_setmethods(&dctx, dc_method);
2031 	dns_result = dns_name_fromwire(&dns_name1, &iscbuf1,
2032 				       &dctx, downcase ? ISC_TRUE : ISC_FALSE,
2033 				       &iscbuf2);
2034 
2035 	if ((dns_result == exp_result) && (exp_result == ISC_R_SUCCESS)) {
2036 
2037 		dns_fixedname_init(&fixed2);
2038 		dns_name2 = dns_fixedname_name(&fixed2);
2039 		dns_result = dns_name_fromstring2(dns_name2, exp_name, NULL,
2040 						  0, NULL);
2041 		if (dns_result == ISC_R_SUCCESS) {
2042 			dns_namereln = dns_name_fullcompare(&dns_name1,
2043 							    dns_name2,
2044 							    &order, &nlabels);
2045 			if (dns_namereln != dns_namereln_equal) {
2046 				t_info("dns_name_fullcompare  returned %s\n",
2047 				       dns_namereln_to_text(dns_namereln));
2048 				result = T_FAIL;
2049 			} else {
2050 				result = T_PASS;
2051 			}
2052 		} else {
2053 			t_info("dns_name_fromtext %s failed, result = %s\n",
2054 			       exp_name, dns_result_totext(dns_result));
2055 			result = T_UNRESOLVED;
2056 		}
2057 	} else if (dns_result == exp_result) {
2058 		result = T_PASS;
2059 	} else {
2060 		t_info("dns_name_fromwire returned %s\n",
2061 		       dns_result_totext(dns_result));
2062 		result = T_FAIL;
2063 	}
2064 
2065 	return (result);
2066 }
2067 
2068 static void
t_dns_name_fromwire_x(const char * testfile,size_t buflen)2069 t_dns_name_fromwire_x(const char *testfile, size_t buflen) {
2070 	int		line;
2071 	int		cnt;
2072 	int		result;
2073 	unsigned int	dc_method;
2074 	isc_result_t	exp_result;
2075 	char		*p;
2076 	char		*tok;
2077 	FILE		*fp;
2078 
2079 	result = T_UNRESOLVED;
2080 	fp = fopen(testfile, "r");
2081 	if (fp != NULL) {
2082 		line = 0;
2083 		while ((p = t_fgetbs(fp)) != NULL) {
2084 
2085 			++line;
2086 
2087 			/*
2088 			 * Skip comment lines.
2089 			 */
2090 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
2091 				(void)free(p);
2092 				continue;
2093 			}
2094 
2095 			cnt = bustline(p, Tokens);
2096 			if (cnt == 6) {
2097 				/*
2098 				 *	datafile_name, testname_offset,
2099 				 *	downcase, dc_method,
2100 				 *	exp_name, exp_result.
2101 				 */
2102 
2103 				tok = Tokens[5];
2104 				exp_result = ISC_R_SUCCESS;
2105 				if (! strcmp(tok, "ISC_R_SUCCESS"))
2106 					exp_result = ISC_R_SUCCESS;
2107 				else if (! strcmp(tok, "ISC_R_NOSPACE"))
2108 					exp_result = ISC_R_NOSPACE;
2109 				else if (! strcmp(tok, "DNS_R_BADLABELTYPE"))
2110 					exp_result = DNS_R_BADLABELTYPE;
2111 				else if (! strcmp(tok, "DNS_R_FORMERR"))
2112 					exp_result = DNS_R_FORMERR;
2113 				else if (! strcmp(tok, "DNS_R_BADPOINTER"))
2114 					exp_result = DNS_R_BADPOINTER;
2115 				else if (! strcmp(tok, "ISC_R_UNEXPECTEDEND"))
2116 					exp_result = ISC_R_UNEXPECTEDEND;
2117 				else if (! strcmp(tok, "DNS_R_TOOMANYHOPS"))
2118 					exp_result = DNS_R_TOOMANYHOPS;
2119 				else if (! strcmp(tok, "DNS_R_DISALLOWED"))
2120 					exp_result = DNS_R_DISALLOWED;
2121 				else if (! strcmp(tok, "DNS_R_NAMETOOLONG"))
2122 					exp_result = DNS_R_NAMETOOLONG;
2123 
2124 				tok = Tokens[3];
2125 				dc_method = DNS_COMPRESS_NONE;
2126 				if (! strcmp(tok, "DNS_COMPRESS_GLOBAL14"))
2127 					dc_method = DNS_COMPRESS_GLOBAL14;
2128 				else if (! strcmp(tok, "DNS_COMPRESS_ALL"))
2129 					dc_method = DNS_COMPRESS_ALL;
2130 
2131 				result = test_dns_name_fromwire(Tokens[0],
2132 							   atoi(Tokens[1]),
2133 							   atoi(Tokens[2]),
2134 								dc_method,
2135 								Tokens[4],
2136 								exp_result,
2137 								buflen);
2138 			} else {
2139 				t_info("bad format at line %d\n", line);
2140 			}
2141 
2142 			(void)free(p);
2143 			t_result(result);
2144 		}
2145 		(void)fclose(fp);
2146 	} else {
2147 		t_info("Missing datafile %s\n", testfile);
2148 		t_result(result);
2149 	}
2150 }
2151 
2152 static void
t_dns_name_fromwire(void)2153 t_dns_name_fromwire(void) {
2154 	t_assert("dns_name_fromwire", 1, T_REQUIRED, "%s", a42);
2155 	t_dns_name_fromwire_x("dns_name_fromwire_1_data", BUFLEN);
2156 
2157 #if 0
2158 	/*
2159 	 * XXXRTH these tests appear to be broken, so I have
2160 	 * disabled them.
2161 	 */
2162 	t_assert("dns_name_fromwire", 2, T_REQUIRED, "%s", a43);
2163 	t_dns_name_fromwire_x("dns_name_fromwire_2_data", BUFLEN);
2164 
2165 	t_assert("dns_name_fromwire", 3, T_REQUIRED, "%s", a44);
2166 	t_dns_name_fromwire_x("dns_name_fromwire_3_data", BUFLEN);
2167 #endif
2168 
2169 	t_assert("dns_name_fromwire", 4, T_REQUIRED, "%s", a45);
2170 	t_dns_name_fromwire_x("dns_name_fromwire_4_data", BUFLEN);
2171 
2172 	t_assert("dns_name_fromwire", 5, T_REQUIRED, "%s", a46);
2173 	t_dns_name_fromwire_x("dns_name_fromwire_5_data", BUFLEN);
2174 
2175 	t_assert("dns_name_fromwire", 6, T_REQUIRED, "%s", a47);
2176 	t_dns_name_fromwire_x("dns_name_fromwire_6_data", BUFLEN);
2177 
2178 	t_assert("dns_name_fromwire", 7, T_REQUIRED, "%s", a48);
2179 	t_dns_name_fromwire_x("dns_name_fromwire_7_data", BUFLEN);
2180 
2181 	t_assert("dns_name_fromwire", 9, T_REQUIRED, "%s", a49);
2182 	t_dns_name_fromwire_x("dns_name_fromwire_8_data", 2);
2183 }
2184 
2185 
2186 static const char *a51 =
2187 		"dns_name_towire(name, cctx, target) converts the DNS name "
2188 		"'name' into wire format, compresses it as specified "
2189 		"by the compression context cctx, stores the result in "
2190 		"target and returns DNS_SUCCESS";
2191 
2192 static const char *a52 =
2193 		"when not enough space exists in target, "
2194 		"dns_name_towire(name, cctx, target) returns ISC_R_NOSPACE";
2195 
2196 static int
test_dns_name_towire(char * testname,unsigned int dc_method,char * exp_data,size_t exp_data_len,isc_result_t exp_result,size_t buflen)2197 test_dns_name_towire(char *testname, unsigned int dc_method, char *exp_data,
2198 		     size_t exp_data_len, isc_result_t exp_result,
2199 		     size_t buflen)
2200 {
2201 	int			result;
2202 	int			val;
2203 	int			len;
2204 	unsigned char		buf2[BUFLEN];
2205 	unsigned char		buf3[BUFLEN];
2206 	isc_buffer_t		iscbuf1;
2207 	isc_buffer_t		iscbuf2;
2208 	isc_buffer_t		iscbuf3;
2209 	dns_name_t		dns_name;
2210 	isc_result_t		dns_result;
2211 	isc_result_t		isc_result;
2212 	dns_compress_t		cctx;
2213 	isc_mem_t		*mctx;
2214 
2215 	t_info("testing using %s\n", testname);
2216 
2217 	result = T_UNRESOLVED;
2218 	mctx = NULL;
2219 
2220 	isc_result = isc_mem_create(0, 0, &mctx);
2221 	if (isc_result != ISC_R_SUCCESS) {
2222 		t_info("isc_mem_create failed\n");
2223 		return (result);
2224 	}
2225 	dns_compress_init(&cctx, -1, mctx);
2226 	dns_compress_setmethods(&cctx, dc_method);
2227 	dns_name_init(&dns_name, NULL);
2228 	len = strlen(testname);
2229 	isc_buffer_init(&iscbuf1, testname, len);
2230 	isc_buffer_add(&iscbuf1, len);
2231 	isc_buffer_init(&iscbuf2, buf2, BUFLEN);
2232 	dns_result = dns_name_fromtext(&dns_name, &iscbuf1, NULL, 0, &iscbuf2);
2233 	if (dns_result == ISC_R_SUCCESS) {
2234 	  isc_buffer_init(&iscbuf3, buf3, (unsigned int)buflen);
2235 		dns_result = dns_name_towire(&dns_name, &cctx, &iscbuf3);
2236 		if (dns_result == exp_result) {
2237 			if (exp_result == ISC_R_SUCCESS) {
2238 				/*
2239 				 * Compare results with expected data.
2240 				 */
2241 				val = chkdata(buf3, iscbuf3.used, exp_data,
2242 					      exp_data_len);
2243 				if (val == 0)
2244 					result = T_PASS;
2245 				else
2246 					result = T_FAIL;
2247 			} else
2248 				result = T_PASS;
2249 		} else {
2250 			t_info("dns_name_towire unexpectedly returned %s\n",
2251 			       dns_result_totext(dns_result));
2252 			result = T_FAIL;
2253 		}
2254 	} else {
2255 		t_info("dns_name_fromtext %s failed, result = %s\n",
2256 				testname, dns_result_totext(dns_result));
2257 	}
2258 	return (result);
2259 }
2260 
2261 static void
t_dns_name_towire_x(const char * testfile,size_t buflen)2262 t_dns_name_towire_x(const char *testfile, size_t buflen) {
2263 	int		line;
2264 	int		cnt;
2265 	int		result;
2266 	unsigned int	dc_method;
2267 	isc_result_t	exp_result;
2268 	size_t		exp_data_len;
2269 	char		*p;
2270 	FILE		*fp;
2271 
2272 	result = T_UNRESOLVED;
2273 	fp = fopen(testfile, "r");
2274 	if (fp != NULL) {
2275 		line = 0;
2276 		while ((p = t_fgetbs(fp)) != NULL) {
2277 
2278 			++line;
2279 
2280 			/*
2281 			 * Skip comment lines.
2282 			 */
2283 			if ((isspace((unsigned char)*p)) || (*p == '#')) {
2284 				(void)free(p);
2285 				continue;
2286 			}
2287 
2288 			cnt = bustline(p, Tokens);
2289 			if (cnt == 5) {
2290 				/*
2291 				 *	testname, dc_method,
2292 				 *	exp_data, exp_data_len,
2293 				 *	exp_result.
2294 				 */
2295 
2296 				dc_method = t_dc_method_fromtext(Tokens[3]);
2297 				exp_result = t_dns_result_fromtext(Tokens[4]);
2298 				exp_data_len = strtoul(Tokens[3], NULL, 10);
2299 
2300 				result = test_dns_name_towire(Tokens[0],
2301 							      dc_method,
2302 							      Tokens[2],
2303 							      exp_data_len,
2304 							      exp_result,
2305 							      buflen);
2306 			} else {
2307 				t_info("bad format at line %d\n", line);
2308 			}
2309 
2310 			(void)free(p);
2311 			t_result(result);
2312 		}
2313 		(void)fclose(fp);
2314 	} else {
2315 		t_info("Missing datafile %s\n", testfile);
2316 		t_result(result);
2317 	}
2318 }
2319 
2320 static void
t_dns_name_towire_1(void)2321 t_dns_name_towire_1(void) {
2322 	t_assert("dns_name_towire", 1, T_REQUIRED, "%s", a51);
2323 	t_dns_name_towire_x("dns_name_towire_1_data", BUFLEN);
2324 }
2325 
2326 static void
t_dns_name_towire_2(void)2327 t_dns_name_towire_2(void) {
2328 	t_assert("dns_name_towire", 2, T_REQUIRED, "%s", a52);
2329 	t_dns_name_towire_x("dns_name_towire_2_data", 2);
2330 }
2331 
2332 static void
t_dns_name_towire(void)2333 t_dns_name_towire(void) {
2334 	t_dns_name_towire_1();
2335 	t_dns_name_towire_2();
2336 }
2337 
2338 #if 0 /* This is silly.  A test should either exist, or not, but not
2339        * one that just returns "UNTESTED."
2340        */
2341 static const char *a53 =
2342 		"dns_name_concatenate(prefix, suffix, name, target) "
2343 		"concatenates prefix and suffix, stores the result "
2344 		"in target, canonicalizes any bitstring labels "
2345 		"and returns ISC_R_SUCCESS";
2346 
2347 static void
2348 t_dns_name_concatenate(void) {
2349 	t_assert("dns_name_concatenate", 1, T_REQUIRED, "%s", a53);
2350 	t_result(T_UNTESTED);
2351 }
2352 #endif
2353 
2354 testspec_t T_testlist[] = {
2355 	{	(PFV) t_dns_name_init,		"dns_name_init"		},
2356 	{	(PFV) t_dns_name_invalidate,	"dns_name_invalidate"	},
2357 	{	(PFV) t_dns_name_setbuffer,	"dns_name_setbuffer"	},
2358 	{	(PFV) t_dns_name_hasbuffer,	"dns_name_hasbuffer"	},
2359 	{	(PFV) t_dns_name_isabsolute,	"dns_name_isabsolute"	},
2360 	{	(PFV) t_dns_name_hash,		"dns_name_hash"		},
2361 	{	(PFV) t_dns_name_fullcompare,	"dns_name_fullcompare"	},
2362 	{	(PFV) t_dns_name_compare,	"dns_name_compare"	},
2363 	{	(PFV) t_dns_name_rdatacompare,	"dns_name_rdatacompare"	},
2364 	{	(PFV) t_dns_name_issubdomain,	"dns_name_issubdomain"	},
2365 	{	(PFV) t_dns_name_countlabels,	"dns_name_countlabels"	},
2366 	{	(PFV) t_dns_name_getlabel,	"dns_name_getlabel"	},
2367 	{	(PFV) t_dns_name_getlabelsequence, "dns_name_getlabelsequence" },
2368 	{	(PFV) t_dns_name_fromregion,	"dns_name_fromregion"	},
2369 	{	(PFV) t_dns_name_toregion,	"dns_name_toregion"	},
2370 	{	(PFV) t_dns_name_fromwire,	"dns_name_fromwire"	},
2371 	{	(PFV) t_dns_name_towire,	"dns_name_towire"	},
2372 	{	(PFV) t_dns_name_fromtext,	"dns_name_fromtext"	},
2373 	{	(PFV) t_dns_name_totext,	"dns_name_totext"	},
2374 #if 0
2375 	{	(PFV) t_dns_name_concatenate,	"dns_name_concatenate"	},
2376 #endif
2377 	{	(PFV) 0,			NULL			}
2378 };
2379 
2380 #ifdef WIN32
2381 int
main(int argc,char ** argv)2382 main(int argc, char **argv) {
2383 	t_settests(T_testlist);
2384 	return (t_main(argc, argv));
2385 }
2386 #endif
2387