update to 9.7.1-P2
[tridge/bind9.git] / bin / tests / master / t_master.c
1 /*
2  * Copyright (C) 2004, 2005, 2007, 2009  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2001, 2003  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: t_master.c,v 1.39 2009/09/01 00:22:25 jinmei Exp $ */
19
20 #include <config.h>
21
22 #include <ctype.h>
23 #include <stdlib.h>
24
25 #include <isc/buffer.h>
26 #include <isc/mem.h>
27 #include <isc/string.h>         /* Required for HP/UX (and others?) */
28 #include <isc/util.h>
29
30 #include <dns/callbacks.h>
31 #include <dns/master.h>
32 #include <dns/name.h>
33 #include <dns/rdataclass.h>
34 #include <dns/rdataset.h>
35 #include <dns/result.h>
36
37 #include <tests/t_api.h>
38
39 #define BUFLEN          255
40 #define BIGBUFLEN       (64 * 1024)
41
42 static isc_result_t
43 t1_add_callback(void *arg, dns_name_t *owner, dns_rdataset_t *dataset);
44
45 isc_mem_t       *T1_mctx;
46 char            *Tokens[T_MAXTOKS + 1];
47
48 static isc_result_t
49 t1_add_callback(void *arg, dns_name_t *owner, dns_rdataset_t *dataset) {
50         char buf[BIGBUFLEN];
51         isc_buffer_t target;
52         isc_result_t result;
53
54         UNUSED(arg);
55
56         isc_buffer_init(&target, buf, BIGBUFLEN);
57         result = dns_rdataset_totext(dataset, owner, ISC_FALSE, ISC_FALSE,
58                                      &target);
59         if (result != ISC_R_SUCCESS)
60                 t_info("dns_rdataset_totext: %s\n", dns_result_totext(result));
61
62         return(result);
63 }
64
65 static int
66 test_master(char *testfile, char *origin, char *class, isc_result_t exp_result)
67 {
68         int                     result;
69         int                     len;
70         isc_result_t            isc_result;
71         isc_result_t            dns_result;
72         dns_name_t              dns_origin;
73         isc_buffer_t            source;
74         isc_buffer_t            target;
75         unsigned char           name_buf[BUFLEN];
76         dns_rdatacallbacks_t    callbacks;
77         dns_rdataclass_t        rdataclass;
78         isc_textregion_t        textregion;
79
80         result = T_UNRESOLVED;
81         if (T1_mctx == NULL)
82                 isc_result = isc_mem_create(0, 0, &T1_mctx);
83         else
84                 isc_result = ISC_R_SUCCESS;
85         if (isc_result != ISC_R_SUCCESS) {
86                 t_info("isc_mem_create failed %d\n", isc_result);
87                 return(T_UNRESOLVED);
88         }
89
90         len = strlen(origin);
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,
97                                        0, &target);
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);
102         }
103
104         dns_rdatacallbacks_init_stdio(&callbacks);
105         callbacks.add = t1_add_callback;
106
107         textregion.base = class;
108         textregion.length = strlen(class);
109
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);
115         }
116
117         dns_result = dns_master_loadfile(       testfile,
118                                                 &dns_origin,
119                                                 &dns_origin,
120                                                 rdataclass,
121                                                 ISC_TRUE,
122                                                 &callbacks,
123                                                 T1_mctx);
124
125         if (dns_result == exp_result)
126                 result = T_PASS;
127         else {
128                 t_info("dns_master_loadfile: got %s, expected %s\n",
129                         dns_result_totext(dns_result),
130                         dns_result_totext(exp_result));
131                 result = T_FAIL;
132         }
133         return(result);
134 }
135
136 static int
137 test_master_x(const char *filename) {
138         FILE            *fp;
139         char            *p;
140         int             line;
141         int             cnt;
142         int             result;
143
144         result = T_UNRESOLVED;
145
146         fp = fopen(filename, "r");
147         if (fp != NULL) {
148                 line = 0;
149                 while ((p = t_fgetbs(fp)) != NULL) {
150
151                         ++line;
152
153                         /*
154                          * Skip comment lines.
155                          */
156                         if ((isspace(*p & 0xff)) || (*p == '#')) {
157                                 (void)free(p);
158                                 continue;
159                         }
160
161                         /*
162                          * Name of data file, origin, zclass, expected result.
163                          */
164                         cnt = t_bustline(p, Tokens);
165                         if (cnt == 4) {
166                                 result = test_master(Tokens[0], Tokens[1],
167                                              Tokens[2],
168                                              t_dns_result_fromtext(Tokens[3]));
169                         } else {
170                                 t_info("bad format in %s at line %d\n",
171                                        filename, line);
172                         }
173
174                         (void)free(p);
175                 }
176                 (void)fclose(fp);
177         } else {
178                 t_info("Missing datafile %s\n", filename);
179         }
180         return(result);
181 }
182
183 static const char *a1 = "dns_master_loadfile loads a valid master file and "
184                         "returns ISC_R_SUCCESS";
185 static void
186 t1(void) {
187         int     result;
188         t_assert("dns_master_loadfile", 1, T_REQUIRED, "%s", a1);
189         result = test_master_x("dns_master_load_1_data");
190         t_result(result);
191 }
192
193 static const char *a2 =
194         "dns_master_loadfile returns ISC_R_UNEXPECTEDEND when the "
195         "masterfile input ends unexpectedly";
196
197 static void
198 t2(void) {
199         int     result;
200         t_assert("dns_master_loadfile", 2, T_REQUIRED, "%s", a2);
201         result = test_master_x("dns_master_load_2_data");
202         t_result(result);
203 }
204
205 static const char *a3 = "dns_master_loadfile returns DNS_R_NOOWNER when the "
206                         "an ownername is not specified";
207
208 static void
209 t3() {
210         int     result;
211         t_assert("dns_master_loadfile", 3, T_REQUIRED, "%s", a3);
212         result = test_master_x("dns_master_load_3_data");
213         t_result(result);
214 }
215
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";
219
220 static void
221 t4() {
222         int     result;
223         t_assert("dns_master_loadfile", 4, T_REQUIRED, "%s", a4);
224         result = test_master_x("dns_master_load_4_data");
225         t_result(result);
226 }
227
228 static const char *a5 = "dns_master_loadfile returns DNS_R_BADCLASS when the "
229                         "the record class did not match the zone class";
230
231 static void
232 t5() {
233         int     result;
234
235         t_assert("dns_master_loadfile", 5, T_REQUIRED, "%s", a5);
236         result = test_master_x("dns_master_load_5_data");
237
238         t_result(result);
239 }
240
241 static const char *a6 =
242         "dns_master_loadfile understands DNSKEY RR specifications "
243         "containing key material";
244
245 static void
246 t6() {
247         int     result;
248
249         t_assert("dns_master_loadfile", 6, T_REQUIRED, "%s", a6);
250         result = test_master_x("dns_master_load_6_data");
251
252         t_result(result);
253 }
254
255 static const char *a7 =
256         "dns_master_loadfile understands DNSKEY RR specifications "
257         "containing no key material";
258
259 static void
260 t7() {
261         int     result;
262
263         t_assert("dns_master_loadfile", 7, T_REQUIRED, "%s", a7);
264         result = test_master_x("dns_master_load_7_data");
265
266         t_result(result);
267 }
268
269 static const char *a8 =
270         "dns_master_loadfile understands $INCLUDE";
271
272 static void
273 t8() {
274         int     result;
275
276         t_assert("dns_master_loadfile", 8, T_REQUIRED, "%s", a8);
277         result = test_master_x("dns_master_load_8_data");
278
279         t_result(result);
280 }
281
282 static const char *a9 =
283         "dns_master_loadfile understands $INCLUDE with failure";
284
285 static void
286 t9() {
287         int     result;
288
289         t_assert("dns_master_loadfile", 9, T_REQUIRED, "%s", a9);
290         result = test_master_x("dns_master_load_9_data");
291
292         t_result(result);
293 }
294
295 static const char *a10 =
296         "dns_master_loadfile non-empty blank lines";
297
298 static void
299 t10() {
300         int     result;
301
302         t_assert("dns_master_loadfile", 10, T_REQUIRED, "%s", a10);
303         result = test_master_x("dns_master_load_10_data");
304
305         t_result(result);
306 }
307
308 static const char *a11 =
309         "dns_master_loadfile allow leading zeros in SOA";
310
311 static void
312 t11() {
313         int     result;
314
315         t_assert("dns_master_loadfile", 11, T_REQUIRED, "%s", a11);
316         result = test_master_x("dns_master_load_11_data");
317
318         t_result(result);
319 }
320
321
322 testspec_t      T_testlist[] = {
323         {       t1,     "ISC_R_SUCCESS"         },
324         {       t2,     "ISC_R_UNEXPECTEDEND"   },
325         {       t3,     "DNS_NOOWNER"           },
326         {       t4,     "DNS_NOTTL"             },
327         {       t5,     "DNS_BADCLASS"          },
328         {       t6,     "DNSKEY RR 1"           },
329         {       t7,     "DNSKEY RR 2"           },
330         {       t8,     "$INCLUDE"              },
331         {       t9,     "$INCLUDE w/ DNS_BADCLASS"      },
332         {       t10,    "non empty blank lines" },
333         {       t11,    "leading zeros in serial"       },
334         {       NULL,   NULL                    }
335 };
336