1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 /*! \file */
13 
14 #include <inttypes.h>
15 #include <stdbool.h>
16 
17 #include <isc/buffer.h>
18 #include <isc/hash.h>
19 #include <isc/log.h>
20 #include <isc/mem.h>
21 #include <isc/result.h>
22 #include <isc/string.h>
23 #include <isc/task.h>
24 #include <isc/timer.h>
25 #include <isc/util.h>
26 
27 #include <dns/diff.h>
28 #include <dns/zone.h>
29 
30 typedef struct {
31 	dns_diffop_t op;
32 	const char *owner;
33 	dns_ttl_t ttl;
34 	const char *type;
35 	const char *rdata;
36 } zonechange_t;
37 
38 #define ZONECHANGE_SENTINEL            \
39 	{                              \
40 		0, NULL, 0, NULL, NULL \
41 	}
42 
43 extern isc_mem_t *dt_mctx;
44 extern isc_log_t *lctx;
45 extern isc_taskmgr_t *taskmgr;
46 extern isc_task_t *maintask;
47 extern isc_timermgr_t *timermgr;
48 extern isc_nm_t *netmgr;
49 extern dns_zonemgr_t *zonemgr;
50 extern bool app_running;
51 extern int ncpus;
52 extern bool debug_mem_record;
53 
54 isc_result_t
55 dns_test_begin(FILE *logfile, bool create_managers);
56 
57 void
58 dns_test_end(void);
59 
60 isc_result_t
61 dns_test_makeview(const char *name, dns_view_t **viewp);
62 
63 /*%
64  * Create a zone with origin 'name', return a pointer to the zone object in
65  * 'zonep'.
66  *
67  * If 'view' is set, the returned zone will be assigned to the passed view.
68  * 'createview' must be set to false when 'view' is non-NULL.
69  *
70  * If 'view' is not set and 'createview' is true, a new view is also created
71  * and the returned zone is assigned to it.  This imposes two requirements on
72  * the caller: 1) the returned zone has to be subsequently assigned to a zone
73  * manager, otherwise its cleanup will fail, 2) the created view has to be
74  * cleaned up by the caller.
75  *
76  * If 'view' is not set and 'createview' is false, the returned zone will not
77  * be assigned to any view.
78  */
79 isc_result_t
80 dns_test_makezone(const char *name, dns_zone_t **zonep, dns_view_t *view,
81 		  bool createview);
82 
83 isc_result_t
84 dns_test_setupzonemgr(void);
85 
86 isc_result_t
87 dns_test_managezone(dns_zone_t *zone);
88 
89 void
90 dns_test_releasezone(dns_zone_t *zone);
91 
92 void
93 dns_test_closezonemgr(void);
94 
95 void
96 dns_test_nap(uint32_t usec);
97 
98 isc_result_t
99 dns_test_loaddb(dns_db_t **db, dns_dbtype_t dbtype, const char *origin,
100 		const char *testfile);
101 
102 isc_result_t
103 dns_test_getdata(const char *file, unsigned char *buf, size_t bufsiz,
104 		 size_t *sizep);
105 
106 char *
107 dns_test_tohex(const unsigned char *data, size_t len, char *buf, size_t buflen);
108 
109 /*%
110  * Try parsing text form RDATA in "src" (of class "rdclass" and type "rdtype")
111  * into a structure representing that RDATA at "rdata", storing the
112  * uncompressed wire form of that RDATA at "dst", which is "dstlen" bytes long.
113  * Set 'warnings' to true to print logged warnings from dns_rdata_fromtext().
114  */
115 isc_result_t
116 dns_test_rdatafromstring(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
117 			 dns_rdatatype_t rdtype, unsigned char *dst,
118 			 size_t dstlen, const char *src, bool warnings);
119 
120 void
121 dns_test_namefromstring(const char *namestr, dns_fixedname_t *fname);
122 
123 /*%
124  * Given a pointer to an uninitialized dns_diff_t structure in 'diff', make it
125  * contain diff tuples representing zone database changes listed in 'changes'.
126  * Set 'warnings' to true to print logged warnings from dns_rdata_fromtext().
127  */
128 isc_result_t
129 dns_test_difffromchanges(dns_diff_t *diff, const zonechange_t *changes,
130 			 bool warnings);
131