2 * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: t_master.c,v 1.39 2009/09/01 00:22:25 jinmei Exp $ */
25 #include <isc/buffer.h>
27 #include <isc/string.h> /* Required for HP/UX (and others?) */
30 #include <dns/callbacks.h>
31 #include <dns/master.h>
33 #include <dns/rdataclass.h>
34 #include <dns/rdataset.h>
35 #include <dns/result.h>
37 #include <tests/t_api.h>
40 #define BIGBUFLEN (64 * 1024)
43 t1_add_callback(void *arg, dns_name_t *owner, dns_rdataset_t *dataset);
46 char *Tokens[T_MAXTOKS + 1];
49 t1_add_callback(void *arg, dns_name_t *owner, dns_rdataset_t *dataset) {
56 isc_buffer_init(&target, buf, BIGBUFLEN);
57 result = dns_rdataset_totext(dataset, owner, ISC_FALSE, ISC_FALSE,
59 if (result != ISC_R_SUCCESS)
60 t_info("dns_rdataset_totext: %s\n", dns_result_totext(result));
66 test_master(char *testfile, char *origin, char *class, isc_result_t exp_result)
70 isc_result_t isc_result;
71 isc_result_t dns_result;
72 dns_name_t dns_origin;
75 unsigned char name_buf[BUFLEN];
76 dns_rdatacallbacks_t callbacks;
77 dns_rdataclass_t rdataclass;
78 isc_textregion_t textregion;
80 result = T_UNRESOLVED;
82 isc_result = isc_mem_create(0, 0, &T1_mctx);
84 isc_result = ISC_R_SUCCESS;
85 if (isc_result != ISC_R_SUCCESS) {
86 t_info("isc_mem_create failed %d\n", isc_result);
91 isc_buffer_init(&source, origin, len);
92 isc_buffer_add(&source, len);
93 isc_buffer_setactive(&source, len);
94 isc_buffer_init(&target, name_buf, BUFLEN);
95 dns_name_init(&dns_origin, NULL);
96 dns_result = dns_name_fromtext(&dns_origin, &source, dns_rootname,
98 if (dns_result != ISC_R_SUCCESS) {
99 t_info("dns_name_fromtext failed %s\n",
100 dns_result_totext(dns_result));
101 return(T_UNRESOLVED);
104 dns_rdatacallbacks_init_stdio(&callbacks);
105 callbacks.add = t1_add_callback;
107 textregion.base = class;
108 textregion.length = strlen(class);
110 dns_result = dns_rdataclass_fromtext(&rdataclass, &textregion);
111 if (dns_result != ISC_R_SUCCESS) {
112 t_info("dns_rdataclass_fromtext failed %s\n",
113 dns_result_totext(dns_result));
114 return(T_UNRESOLVED);
117 dns_result = dns_master_loadfile( testfile,
125 if (dns_result == exp_result)
128 t_info("dns_master_loadfile: got %s, expected %s\n",
129 dns_result_totext(dns_result),
130 dns_result_totext(exp_result));
137 test_master_x(const char *filename) {
144 result = T_UNRESOLVED;
146 fp = fopen(filename, "r");
149 while ((p = t_fgetbs(fp)) != NULL) {
154 * Skip comment lines.
156 if ((isspace(*p & 0xff)) || (*p == '#')) {
162 * Name of data file, origin, zclass, expected result.
164 cnt = t_bustline(p, Tokens);
166 result = test_master(Tokens[0], Tokens[1],
168 t_dns_result_fromtext(Tokens[3]));
170 t_info("bad format in %s at line %d\n",
178 t_info("Missing datafile %s\n", filename);
183 static const char *a1 = "dns_master_loadfile loads a valid master file and "
184 "returns ISC_R_SUCCESS";
188 t_assert("dns_master_loadfile", 1, T_REQUIRED, "%s", a1);
189 result = test_master_x("dns_master_load_1_data");
193 static const char *a2 =
194 "dns_master_loadfile returns ISC_R_UNEXPECTEDEND when the "
195 "masterfile input ends unexpectedly";
200 t_assert("dns_master_loadfile", 2, T_REQUIRED, "%s", a2);
201 result = test_master_x("dns_master_load_2_data");
205 static const char *a3 = "dns_master_loadfile returns DNS_R_NOOWNER when the "
206 "an ownername is not specified";
211 t_assert("dns_master_loadfile", 3, T_REQUIRED, "%s", a3);
212 result = test_master_x("dns_master_load_3_data");
216 static const char *a4 = "dns_master_loadfile accepts broken zone files "
217 "where the first record has an undefined TTL, "
218 "as long as it is a SOA";
223 t_assert("dns_master_loadfile", 4, T_REQUIRED, "%s", a4);
224 result = test_master_x("dns_master_load_4_data");
228 static const char *a5 = "dns_master_loadfile returns DNS_R_BADCLASS when the "
229 "the record class did not match the zone class";
235 t_assert("dns_master_loadfile", 5, T_REQUIRED, "%s", a5);
236 result = test_master_x("dns_master_load_5_data");
241 static const char *a6 =
242 "dns_master_loadfile understands DNSKEY RR specifications "
243 "containing key material";
249 t_assert("dns_master_loadfile", 6, T_REQUIRED, "%s", a6);
250 result = test_master_x("dns_master_load_6_data");
255 static const char *a7 =
256 "dns_master_loadfile understands DNSKEY RR specifications "
257 "containing no key material";
263 t_assert("dns_master_loadfile", 7, T_REQUIRED, "%s", a7);
264 result = test_master_x("dns_master_load_7_data");
269 static const char *a8 =
270 "dns_master_loadfile understands $INCLUDE";
276 t_assert("dns_master_loadfile", 8, T_REQUIRED, "%s", a8);
277 result = test_master_x("dns_master_load_8_data");
282 static const char *a9 =
283 "dns_master_loadfile understands $INCLUDE with failure";
289 t_assert("dns_master_loadfile", 9, T_REQUIRED, "%s", a9);
290 result = test_master_x("dns_master_load_9_data");
295 static const char *a10 =
296 "dns_master_loadfile non-empty blank lines";
302 t_assert("dns_master_loadfile", 10, T_REQUIRED, "%s", a10);
303 result = test_master_x("dns_master_load_10_data");
308 static const char *a11 =
309 "dns_master_loadfile allow leading zeros in SOA";
315 t_assert("dns_master_loadfile", 11, T_REQUIRED, "%s", a11);
316 result = test_master_x("dns_master_load_11_data");
322 testspec_t T_testlist[] = {
323 { t1, "ISC_R_SUCCESS" },
324 { t2, "ISC_R_UNEXPECTEDEND" },
325 { t3, "DNS_NOOWNER" },
327 { t5, "DNS_BADCLASS" },
328 { t6, "DNSKEY RR 1" },
329 { t7, "DNSKEY RR 2" },
331 { t9, "$INCLUDE w/ DNS_BADCLASS" },
332 { t10, "non empty blank lines" },
333 { t11, "leading zeros in serial" },