Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / bin / tests / master / t_master.c
blob2123b2b12121677aeccb18161f5c0d77507faac6
1 /* $NetBSD$ */
3 /*
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 */
22 #include <config.h>
24 #include <ctype.h>
25 #include <stdlib.h>
27 #include <isc/buffer.h>
28 #include <isc/mem.h>
29 #include <isc/string.h> /* Required for HP/UX (and others?) */
30 #include <isc/util.h>
32 #include <dns/callbacks.h>
33 #include <dns/master.h>
34 #include <dns/name.h>
35 #include <dns/rdataclass.h>
36 #include <dns/rdataset.h>
37 #include <dns/result.h>
39 #include <tests/t_api.h>
41 #define BUFLEN 255
42 #define BIGBUFLEN (64 * 1024)
44 static isc_result_t
45 t1_add_callback(void *arg, dns_name_t *owner, dns_rdataset_t *dataset);
47 isc_mem_t *T1_mctx;
48 char *Tokens[T_MAXTOKS + 1];
50 static isc_result_t
51 t1_add_callback(void *arg, dns_name_t *owner, dns_rdataset_t *dataset) {
52 char buf[BIGBUFLEN];
53 isc_buffer_t target;
54 isc_result_t result;
56 UNUSED(arg);
58 isc_buffer_init(&target, buf, BIGBUFLEN);
59 result = dns_rdataset_totext(dataset, owner, ISC_FALSE, ISC_FALSE,
60 &target);
61 if (result != ISC_R_SUCCESS)
62 t_info("dns_rdataset_totext: %s\n", dns_result_totext(result));
64 return(result);
67 static int
68 test_master(char *testfile, char *origin, char *class, isc_result_t exp_result)
70 int result;
71 int len;
72 isc_result_t isc_result;
73 isc_result_t dns_result;
74 dns_name_t dns_origin;
75 isc_buffer_t source;
76 isc_buffer_t target;
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;
83 if (T1_mctx == NULL)
84 isc_result = isc_mem_create(0, 0, &T1_mctx);
85 else
86 isc_result = ISC_R_SUCCESS;
87 if (isc_result != ISC_R_SUCCESS) {
88 t_info("isc_mem_create failed %d\n", isc_result);
89 return(T_UNRESOLVED);
92 len = strlen(origin);
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,
99 0, &target);
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,
120 &dns_origin,
121 &dns_origin,
122 rdataclass,
123 ISC_TRUE,
124 &callbacks,
125 T1_mctx);
127 if (dns_result == exp_result)
128 result = T_PASS;
129 else {
130 t_info("dns_master_loadfile: got %s, expected %s\n",
131 dns_result_totext(dns_result),
132 dns_result_totext(exp_result));
133 result = T_FAIL;
135 return(result);
138 static int
139 test_master_x(const char *filename) {
140 FILE *fp;
141 char *p;
142 int line;
143 int cnt;
144 int result;
146 result = T_UNRESOLVED;
148 fp = fopen(filename, "r");
149 if (fp != NULL) {
150 line = 0;
151 while ((p = t_fgetbs(fp)) != NULL) {
153 ++line;
156 * Skip comment lines.
158 if ((isspace(*p & 0xff)) || (*p == '#')) {
159 (void)free(p);
160 continue;
164 * Name of data file, origin, zclass, expected result.
166 cnt = t_bustline(p, Tokens);
167 if (cnt == 4) {
168 result = test_master(Tokens[0], Tokens[1],
169 Tokens[2],
170 t_dns_result_fromtext(Tokens[3]));
171 } else {
172 t_info("bad format in %s at line %d\n",
173 filename, line);
176 (void)free(p);
178 (void)fclose(fp);
179 } else {
180 t_info("Missing datafile %s\n", filename);
182 return(result);
185 static const char *a1 = "dns_master_loadfile loads a valid master file and "
186 "returns ISC_R_SUCCESS";
187 static void
188 t1(void) {
189 int result;
190 t_assert("dns_master_loadfile", 1, T_REQUIRED, "%s", a1);
191 result = test_master_x("dns_master_load_1_data");
192 t_result(result);
195 static const char *a2 =
196 "dns_master_loadfile returns ISC_R_UNEXPECTEDEND when the "
197 "masterfile input ends unexpectedly";
199 static void
200 t2(void) {
201 int result;
202 t_assert("dns_master_loadfile", 2, T_REQUIRED, "%s", a2);
203 result = test_master_x("dns_master_load_2_data");
204 t_result(result);
207 static const char *a3 = "dns_master_loadfile returns DNS_R_NOOWNER when the "
208 "an ownername is not specified";
210 static void
211 t3() {
212 int result;
213 t_assert("dns_master_loadfile", 3, T_REQUIRED, "%s", a3);
214 result = test_master_x("dns_master_load_3_data");
215 t_result(result);
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";
222 static void
223 t4() {
224 int result;
225 t_assert("dns_master_loadfile", 4, T_REQUIRED, "%s", a4);
226 result = test_master_x("dns_master_load_4_data");
227 t_result(result);
230 static const char *a5 = "dns_master_loadfile returns DNS_R_BADCLASS when the "
231 "the record class did not match the zone class";
233 static void
234 t5() {
235 int result;
237 t_assert("dns_master_loadfile", 5, T_REQUIRED, "%s", a5);
238 result = test_master_x("dns_master_load_5_data");
240 t_result(result);
243 static const char *a6 =
244 "dns_master_loadfile understands DNSKEY RR specifications "
245 "containing key material";
247 static void
248 t6() {
249 int result;
251 t_assert("dns_master_loadfile", 6, T_REQUIRED, "%s", a6);
252 result = test_master_x("dns_master_load_6_data");
254 t_result(result);
257 static const char *a7 =
258 "dns_master_loadfile understands DNSKEY RR specifications "
259 "containing no key material";
261 static void
262 t7() {
263 int result;
265 t_assert("dns_master_loadfile", 7, T_REQUIRED, "%s", a7);
266 result = test_master_x("dns_master_load_7_data");
268 t_result(result);
271 static const char *a8 =
272 "dns_master_loadfile understands $INCLUDE";
274 static void
275 t8() {
276 int result;
278 t_assert("dns_master_loadfile", 8, T_REQUIRED, "%s", a8);
279 result = test_master_x("dns_master_load_8_data");
281 t_result(result);
284 static const char *a9 =
285 "dns_master_loadfile understands $INCLUDE with failure";
287 static void
288 t9() {
289 int result;
291 t_assert("dns_master_loadfile", 9, T_REQUIRED, "%s", a9);
292 result = test_master_x("dns_master_load_9_data");
294 t_result(result);
297 static const char *a10 =
298 "dns_master_loadfile non-empty blank lines";
300 static void
301 t10() {
302 int result;
304 t_assert("dns_master_loadfile", 10, T_REQUIRED, "%s", a10);
305 result = test_master_x("dns_master_load_10_data");
307 t_result(result);
310 static const char *a11 =
311 "dns_master_loadfile allow leading zeros in SOA";
313 static void
314 t11() {
315 int result;
317 t_assert("dns_master_loadfile", 11, T_REQUIRED, "%s", a11);
318 result = test_master_x("dns_master_load_11_data");
320 t_result(result);
324 testspec_t T_testlist[] = {
325 { t1, "ISC_R_SUCCESS" },
326 { t2, "ISC_R_UNEXPECTEDEND" },
327 { t3, "DNS_NOOWNER" },
328 { t4, "DNS_NOTTL" },
329 { t5, "DNS_BADCLASS" },
330 { t6, "DNSKEY RR 1" },
331 { t7, "DNSKEY RR 2" },
332 { t8, "$INCLUDE" },
333 { t9, "$INCLUDE w/ DNS_BADCLASS" },
334 { t10, "non empty blank lines" },
335 { t11, "leading zeros in serial" },
336 { NULL, NULL }