1 /* $NetBSD: name_test.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2001, 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: name_test.c,v 1.43 2009/09/02 23:48:01 tbox Exp */
26 #include <isc/commandline.h>
27 #include <isc/string.h>
30 #include <dns/fixedname.h>
31 #include <dns/result.h>
34 print_wirename(isc_region_t
*name
) {
35 unsigned char *ccurr
, *cend
;
37 if (name
->length
== 0) {
38 printf("<empty wire name>\n");
42 cend
= ccurr
+ name
->length
;
44 printf("%02x ", *ccurr
++);
49 print_name(dns_name_t
*name
) {
55 isc_buffer_init(&source
, s
, sizeof(s
));
56 if (dns_name_countlabels(name
) > 0)
57 result
= dns_name_totext(name
, ISC_FALSE
, &source
);
59 result
= ISC_R_SUCCESS
;
60 if (result
== ISC_R_SUCCESS
) {
61 isc_buffer_usedregion(&source
, &r
);
63 printf("%.*s\n", (int)r
.length
, r
.base
);
65 printf("<empty text name>\n");
67 printf("error: %s\n", dns_result_totext(result
));
71 main(int argc
, char *argv
[]) {
74 dns_fixedname_t wname
, wname2
, oname
, compname
, downname
;
77 dns_name_t
*name
, *origin
, *comp
, *down
;
78 unsigned int downcase
= 0;
80 isc_boolean_t quiet
= ISC_FALSE
;
81 isc_boolean_t concatenate
= ISC_FALSE
;
82 isc_boolean_t got_name
= ISC_FALSE
;
83 isc_boolean_t check_absolute
= ISC_FALSE
;
84 isc_boolean_t check_wildcard
= ISC_FALSE
;
85 isc_boolean_t test_downcase
= ISC_FALSE
;
86 isc_boolean_t inplace
= ISC_FALSE
;
87 isc_boolean_t want_split
= ISC_FALSE
;
88 unsigned int labels
, split_label
= 0;
89 dns_fixedname_t fprefix
, fsuffix
;
90 dns_name_t
*prefix
, *suffix
;
93 while ((ch
= isc_commandline_parse(argc
, argv
, "acdiqs:w")) != -1) {
96 check_absolute
= ISC_TRUE
;
99 concatenate
= ISC_TRUE
;
102 test_downcase
= ISC_TRUE
;
111 want_split
= ISC_TRUE
;
112 split_label
= atoi(isc_commandline_argument
);
115 check_wildcard
= ISC_TRUE
;
120 argc
-= isc_commandline_index
;
121 argv
+= isc_commandline_index
;
124 if (strcasecmp("none", argv
[0]) == 0)
127 len
= strlen(argv
[0]);
128 isc_buffer_init(&source
, argv
[0], len
);
129 isc_buffer_add(&source
, len
);
130 dns_fixedname_init(&oname
);
131 origin
= &oname
.name
;
132 result
= dns_name_fromtext(origin
, &source
,
133 dns_rootname
, 0, NULL
);
136 "dns_name_fromtext() failed: %d\n",
141 } else if (concatenate
)
144 origin
= dns_rootname
;
147 if (strcasecmp("none", argv
[1]) == 0)
150 len
= strlen(argv
[1]);
151 isc_buffer_init(&source
, argv
[1], len
);
152 isc_buffer_add(&source
, len
);
153 dns_fixedname_init(&compname
);
154 comp
= &compname
.name
;
155 result
= dns_name_fromtext(comp
, &source
, origin
,
159 "dns_name_fromtext() failed: %d\n",
167 dns_fixedname_init(&wname
);
168 name
= dns_fixedname_name(&wname
);
169 dns_fixedname_init(&wname2
);
170 while (fgets(s
, sizeof(s
), stdin
) != NULL
) {
172 if (len
> 0U && s
[len
- 1] == '\n') {
176 isc_buffer_init(&source
, s
, len
);
177 isc_buffer_add(&source
, len
);
180 result
= dns_name_fromtext(name
, &source
, origin
,
183 if (name
== dns_fixedname_name(&wname
))
184 dns_fixedname_init(&wname
);
186 dns_fixedname_init(&wname2
);
187 result
= ISC_R_SUCCESS
;
190 if (result
!= ISC_R_SUCCESS
) {
191 printf("%s\n", dns_result_totext(result
));
192 if (name
== dns_fixedname_name(&wname
))
193 dns_fixedname_init(&wname
);
195 dns_fixedname_init(&wname2
);
199 if (check_absolute
&& dns_name_countlabels(name
) > 0) {
200 if (dns_name_isabsolute(name
))
201 printf("absolute\n");
203 printf("relative\n");
205 if (check_wildcard
&& dns_name_countlabels(name
) > 0) {
206 if (dns_name_iswildcard(name
))
207 printf("wildcard\n");
209 printf("not wildcard\n");
211 dns_name_toregion(name
, &r
);
214 printf("%u labels, %u bytes.\n",
215 dns_name_countlabels(name
), r
.length
);
220 printf("Concatenating.\n");
221 result
= dns_name_concatenate(&wname
.name
,
226 if (result
== ISC_R_SUCCESS
) {
227 if (check_absolute
&&
228 dns_name_countlabels(name
) > 0) {
229 if (dns_name_isabsolute(name
))
230 printf("absolute\n");
232 printf("relative\n");
234 if (check_wildcard
&&
235 dns_name_countlabels(name
) > 0) {
236 if (dns_name_iswildcard(name
))
237 printf("wildcard\n");
242 dns_name_toregion(name
, &r
);
247 dns_name_countlabels(name
),
252 dns_result_totext(result
));
253 got_name
= ISC_FALSE
;
257 isc_buffer_init(&source
, s
, sizeof(s
));
258 if (dns_name_countlabels(name
) > 0)
259 result
= dns_name_totext(name
, ISC_FALSE
, &source
);
261 result
= ISC_R_SUCCESS
;
262 if (result
== ISC_R_SUCCESS
) {
263 isc_buffer_usedregion(&source
, &r
);
265 printf("%.*s\n", (int)r
.length
, r
.base
);
267 printf("<empty text name>\n");
269 printf("%u bytes.\n", source
.used
);
272 printf("%s\n", dns_result_totext(result
));
278 dns_fixedname_init(&downname
);
279 down
= dns_fixedname_name(&downname
);
281 result
= dns_name_downcase(name
, down
, NULL
);
282 INSIST(result
== ISC_R_SUCCESS
);
284 dns_name_toregion(down
, &r
);
286 printf("%u labels, %u bytes.\n",
287 dns_name_countlabels(down
),
290 isc_buffer_init(&source
, s
, sizeof(s
));
294 if (comp
!= NULL
&& dns_name_countlabels(name
) > 0) {
296 unsigned int nlabels
;
297 dns_namereln_t namereln
;
299 namereln
= dns_name_fullcompare(name
, comp
, &order
,
309 case dns_namereln_contains
:
310 printf(", contains");
312 case dns_namereln_subdomain
:
313 printf(", subdomain");
315 case dns_namereln_commonancestor
:
316 printf(", common ancestor");
321 if (namereln
!= dns_namereln_none
&&
322 namereln
!= dns_namereln_equal
)
323 printf(", nlabels = %u", nlabels
);
326 printf("dns_name_equal() returns %s\n",
327 dns_name_equal(name
, comp
) ? "TRUE" : "FALSE");
330 labels
= dns_name_countlabels(name
);
331 if (want_split
&& split_label
< labels
) {
332 dns_fixedname_init(&fprefix
);
333 prefix
= dns_fixedname_name(&fprefix
);
334 dns_fixedname_init(&fsuffix
);
335 suffix
= dns_fixedname_name(&fsuffix
);
336 printf("splitting at label %u: ", split_label
);
337 dns_name_split(name
, split_label
, prefix
, suffix
);
338 printf("\n prefix = ");
340 printf(" suffix = ");