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: t_master.c,v 1.39 2009/09/01 00:22:25 jinmei Exp */
27 #include <isc/buffer.h>
29 #include <isc/string.h> /* Required for HP/UX (and others?) */
32 #include <dns/callbacks.h>
33 #include <dns/master.h>
35 #include <dns/rdataclass.h>
36 #include <dns/rdataset.h>
37 #include <dns/result.h>
39 #include <tests/t_api.h>
42 #define BIGBUFLEN (64 * 1024)
45 t1_add_callback(void *arg
, dns_name_t
*owner
, dns_rdataset_t
*dataset
);
48 char *Tokens
[T_MAXTOKS
+ 1];
51 t1_add_callback(void *arg
, dns_name_t
*owner
, dns_rdataset_t
*dataset
) {
58 isc_buffer_init(&target
, buf
, BIGBUFLEN
);
59 result
= dns_rdataset_totext(dataset
, owner
, ISC_FALSE
, ISC_FALSE
,
61 if (result
!= ISC_R_SUCCESS
)
62 t_info("dns_rdataset_totext: %s\n", dns_result_totext(result
));
68 test_master(char *testfile
, char *origin
, char *class, isc_result_t exp_result
)
72 isc_result_t isc_result
;
73 isc_result_t dns_result
;
74 dns_name_t dns_origin
;
77 unsigned char name_buf
[BUFLEN
];
78 dns_rdatacallbacks_t callbacks
;
79 dns_rdataclass_t rdataclass
;
80 isc_textregion_t textregion
;
82 result
= T_UNRESOLVED
;
84 isc_result
= isc_mem_create(0, 0, &T1_mctx
);
86 isc_result
= ISC_R_SUCCESS
;
87 if (isc_result
!= ISC_R_SUCCESS
) {
88 t_info("isc_mem_create failed %d\n", isc_result
);
93 isc_buffer_init(&source
, origin
, len
);
94 isc_buffer_add(&source
, len
);
95 isc_buffer_setactive(&source
, len
);
96 isc_buffer_init(&target
, name_buf
, BUFLEN
);
97 dns_name_init(&dns_origin
, NULL
);
98 dns_result
= dns_name_fromtext(&dns_origin
, &source
, dns_rootname
,
100 if (dns_result
!= ISC_R_SUCCESS
) {
101 t_info("dns_name_fromtext failed %s\n",
102 dns_result_totext(dns_result
));
103 return(T_UNRESOLVED
);
106 dns_rdatacallbacks_init_stdio(&callbacks
);
107 callbacks
.add
= t1_add_callback
;
109 textregion
.base
= class;
110 textregion
.length
= strlen(class);
112 dns_result
= dns_rdataclass_fromtext(&rdataclass
, &textregion
);
113 if (dns_result
!= ISC_R_SUCCESS
) {
114 t_info("dns_rdataclass_fromtext failed %s\n",
115 dns_result_totext(dns_result
));
116 return(T_UNRESOLVED
);
119 dns_result
= dns_master_loadfile( testfile
,
127 if (dns_result
== exp_result
)
130 t_info("dns_master_loadfile: got %s, expected %s\n",
131 dns_result_totext(dns_result
),
132 dns_result_totext(exp_result
));
139 test_master_x(const char *filename
) {
146 result
= T_UNRESOLVED
;
148 fp
= fopen(filename
, "r");
151 while ((p
= t_fgetbs(fp
)) != NULL
) {
156 * Skip comment lines.
158 if ((isspace(*p
& 0xff)) || (*p
== '#')) {
164 * Name of data file, origin, zclass, expected result.
166 cnt
= t_bustline(p
, Tokens
);
168 result
= test_master(Tokens
[0], Tokens
[1],
170 t_dns_result_fromtext(Tokens
[3]));
172 t_info("bad format in %s at line %d\n",
180 t_info("Missing datafile %s\n", filename
);
185 static const char *a1
= "dns_master_loadfile loads a valid master file and "
186 "returns ISC_R_SUCCESS";
190 t_assert("dns_master_loadfile", 1, T_REQUIRED
, "%s", a1
);
191 result
= test_master_x("dns_master_load_1_data");
195 static const char *a2
=
196 "dns_master_loadfile returns ISC_R_UNEXPECTEDEND when the "
197 "masterfile input ends unexpectedly";
202 t_assert("dns_master_loadfile", 2, T_REQUIRED
, "%s", a2
);
203 result
= test_master_x("dns_master_load_2_data");
207 static const char *a3
= "dns_master_loadfile returns DNS_R_NOOWNER when the "
208 "an ownername is not specified";
213 t_assert("dns_master_loadfile", 3, T_REQUIRED
, "%s", a3
);
214 result
= test_master_x("dns_master_load_3_data");
218 static const char *a4
= "dns_master_loadfile accepts broken zone files "
219 "where the first record has an undefined TTL, "
220 "as long as it is a SOA";
225 t_assert("dns_master_loadfile", 4, T_REQUIRED
, "%s", a4
);
226 result
= test_master_x("dns_master_load_4_data");
230 static const char *a5
= "dns_master_loadfile returns DNS_R_BADCLASS when the "
231 "the record class did not match the zone class";
237 t_assert("dns_master_loadfile", 5, T_REQUIRED
, "%s", a5
);
238 result
= test_master_x("dns_master_load_5_data");
243 static const char *a6
=
244 "dns_master_loadfile understands DNSKEY RR specifications "
245 "containing key material";
251 t_assert("dns_master_loadfile", 6, T_REQUIRED
, "%s", a6
);
252 result
= test_master_x("dns_master_load_6_data");
257 static const char *a7
=
258 "dns_master_loadfile understands DNSKEY RR specifications "
259 "containing no key material";
265 t_assert("dns_master_loadfile", 7, T_REQUIRED
, "%s", a7
);
266 result
= test_master_x("dns_master_load_7_data");
271 static const char *a8
=
272 "dns_master_loadfile understands $INCLUDE";
278 t_assert("dns_master_loadfile", 8, T_REQUIRED
, "%s", a8
);
279 result
= test_master_x("dns_master_load_8_data");
284 static const char *a9
=
285 "dns_master_loadfile understands $INCLUDE with failure";
291 t_assert("dns_master_loadfile", 9, T_REQUIRED
, "%s", a9
);
292 result
= test_master_x("dns_master_load_9_data");
297 static const char *a10
=
298 "dns_master_loadfile non-empty blank lines";
304 t_assert("dns_master_loadfile", 10, T_REQUIRED
, "%s", a10
);
305 result
= test_master_x("dns_master_load_10_data");
310 static const char *a11
=
311 "dns_master_loadfile allow leading zeros in SOA";
317 t_assert("dns_master_loadfile", 11, T_REQUIRED
, "%s", a11
);
318 result
= test_master_x("dns_master_load_11_data");
324 testspec_t T_testlist
[] = {
325 { t1
, "ISC_R_SUCCESS" },
326 { t2
, "ISC_R_UNEXPECTEDEND" },
327 { t3
, "DNS_NOOWNER" },
329 { t5
, "DNS_BADCLASS" },
330 { t6
, "DNSKEY RR 1" },
331 { t7
, "DNSKEY RR 2" },
333 { t9
, "$INCLUDE w/ DNS_BADCLASS" },
334 { t10
, "non empty blank lines" },
335 { t11
, "leading zeros in serial" },