1 /* $NetBSD: t_master.c,v 1.6 2014/12/10 04:37:53 christos Exp $ */
4 * Copyright (C) 2004, 2005, 2007, 2009, 2011, 2013 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.41 2011/03/12 04:59:46 tbox 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
;
83 isc_result
= isc_mem_create(0, 0, &T1_mctx
);
85 isc_result
= ISC_R_SUCCESS
;
86 if (isc_result
!= ISC_R_SUCCESS
) {
87 t_info("isc_mem_create failed %d\n", isc_result
);
92 isc_buffer_init(&source
, origin
, len
);
93 isc_buffer_add(&source
, len
);
94 isc_buffer_setactive(&source
, len
);
95 isc_buffer_init(&target
, name_buf
, BUFLEN
);
96 dns_name_init(&dns_origin
, NULL
);
97 dns_result
= dns_name_fromtext(&dns_origin
, &source
, dns_rootname
,
99 if (dns_result
!= ISC_R_SUCCESS
) {
100 t_info("dns_name_fromtext failed %s\n",
101 dns_result_totext(dns_result
));
102 return(T_UNRESOLVED
);
105 dns_rdatacallbacks_init_stdio(&callbacks
);
106 callbacks
.add
= t1_add_callback
;
108 textregion
.base
= class;
109 textregion
.length
= strlen(class);
111 dns_result
= dns_rdataclass_fromtext(&rdataclass
, &textregion
);
112 if (dns_result
!= ISC_R_SUCCESS
) {
113 t_info("dns_rdataclass_fromtext failed %s\n",
114 dns_result_totext(dns_result
));
115 return(T_UNRESOLVED
);
118 dns_result
= dns_master_loadfile( testfile
,
126 if (dns_result
== exp_result
)
129 t_info("dns_master_loadfile: got %s, expected %s\n",
130 dns_result_totext(dns_result
),
131 dns_result_totext(exp_result
));
138 test_master_x(const char *filename
) {
145 result
= T_UNRESOLVED
;
147 fp
= fopen(filename
, "r");
150 while ((p
= t_fgetbs(fp
)) != NULL
) {
155 * Skip comment lines.
157 if ((isspace(*p
& 0xff)) || (*p
== '#')) {
163 * Name of data file, origin, zclass, expected result.
165 cnt
= t_bustline(p
, Tokens
);
167 result
= test_master(Tokens
[0], Tokens
[1],
169 t_dns_result_fromtext(Tokens
[3]));
171 t_info("bad format in %s at line %d\n",
179 t_info("Missing datafile %s\n", filename
);
184 static const char *a1
= "dns_master_loadfile loads a valid master file and "
185 "returns ISC_R_SUCCESS";
189 t_assert("dns_master_loadfile", 1, T_REQUIRED
, "%s", a1
);
190 result
= test_master_x("dns_master_load_1_data");
194 static const char *a2
=
195 "dns_master_loadfile returns ISC_R_UNEXPECTEDEND when the "
196 "masterfile input ends unexpectedly";
201 t_assert("dns_master_loadfile", 2, T_REQUIRED
, "%s", a2
);
202 result
= test_master_x("dns_master_load_2_data");
206 static const char *a3
= "dns_master_loadfile returns DNS_R_NOOWNER when the "
207 "an ownername is not specified";
212 t_assert("dns_master_loadfile", 3, T_REQUIRED
, "%s", a3
);
213 result
= test_master_x("dns_master_load_3_data");
217 static const char *a4
= "dns_master_loadfile accepts broken zone files "
218 "where the first record has an undefined TTL, "
219 "as long as it is a SOA";
224 t_assert("dns_master_loadfile", 4, T_REQUIRED
, "%s", a4
);
225 result
= test_master_x("dns_master_load_4_data");
229 static const char *a5
= "dns_master_loadfile returns DNS_R_BADCLASS when the "
230 "the record class did not match the zone class";
236 t_assert("dns_master_loadfile", 5, T_REQUIRED
, "%s", a5
);
237 result
= test_master_x("dns_master_load_5_data");
242 static const char *a6
=
243 "dns_master_loadfile understands DNSKEY RR specifications "
244 "containing key material";
250 t_assert("dns_master_loadfile", 6, T_REQUIRED
, "%s", a6
);
251 result
= test_master_x("dns_master_load_6_data");
256 static const char *a7
=
257 "dns_master_loadfile understands DNSKEY RR specifications "
258 "containing no key material";
264 t_assert("dns_master_loadfile", 7, T_REQUIRED
, "%s", a7
);
265 result
= test_master_x("dns_master_load_7_data");
270 static const char *a8
=
271 "dns_master_loadfile understands $INCLUDE";
277 t_assert("dns_master_loadfile", 8, T_REQUIRED
, "%s", a8
);
278 result
= test_master_x("dns_master_load_8_data");
283 static const char *a9
=
284 "dns_master_loadfile understands $INCLUDE with failure";
290 t_assert("dns_master_loadfile", 9, T_REQUIRED
, "%s", a9
);
291 result
= test_master_x("dns_master_load_9_data");
296 static const char *a10
=
297 "dns_master_loadfile non-empty blank lines";
303 t_assert("dns_master_loadfile", 10, T_REQUIRED
, "%s", a10
);
304 result
= test_master_x("dns_master_load_10_data");
309 static const char *a11
=
310 "dns_master_loadfile allow leading zeros in SOA";
316 t_assert("dns_master_loadfile", 11, T_REQUIRED
, "%s", a11
);
317 result
= test_master_x("dns_master_load_11_data");
323 testspec_t T_testlist
[] = {
324 { (PFV
) t1
, "ISC_R_SUCCESS" },
325 { (PFV
) t2
, "ISC_R_UNEXPECTEDEND" },
326 { (PFV
) t3
, "DNS_NOOWNER" },
327 { (PFV
) t4
, "DNS_NOTTL" },
328 { (PFV
) t5
, "DNS_BADCLASS" },
329 { (PFV
) t6
, "DNSKEY RR 1" },
330 { (PFV
) t7
, "DNSKEY RR 2" },
331 { (PFV
) t8
, "$INCLUDE" },
332 { (PFV
) t9
, "$INCLUDE w/ DNS_BADCLASS" },
333 { (PFV
) t10
, "non empty blank lines" },
334 { (PFV
) t11
, "leading zeros in serial" },
340 main(int argc
, char **argv
) {
341 t_settests(T_testlist
);
342 return (t_main(argc
, argv
));