xref: /netbsd-src/external/mpl/bind/dist/tests/isccfg/parser_test.c (revision bcda20f65a8566e103791ec395f7f499ef322704)
1 /*	$NetBSD: parser_test.c,v 1.3 2025/01/26 16:25:51 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 #include <inttypes.h>
17 #include <sched.h> /* IWYU pragma: keep */
18 #include <setjmp.h>
19 #include <stdarg.h>
20 #include <stddef.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 
26 #define UNIT_TESTING
27 #include <cmocka.h>
28 
29 #include <isc/buffer.h>
30 #include <isc/lex.h>
31 #include <isc/log.h>
32 #include <isc/mem.h>
33 #include <isc/string.h>
34 #include <isc/types.h>
35 #include <isc/util.h>
36 
37 #include <isccfg/cfg.h>
38 #include <isccfg/grammar.h>
39 #include <isccfg/namedconf.h>
40 
41 #include <tests/isc.h>
42 
43 static isc_logcategory_t categories[] = { { "", 0 },
44 					  { "client", 0 },
45 					  { "network", 0 },
46 					  { "update", 0 },
47 					  { "queries", 0 },
48 					  { "unmatched", 0 },
49 					  { "update-security", 0 },
50 					  { "query-errors", 0 },
51 					  { NULL, 0 } };
52 
53 ISC_SETUP_TEST_IMPL(group) {
54 	isc_result_t result;
55 	isc_logdestination_t destination;
56 	isc_logconfig_t *logconfig = NULL;
57 
58 	isc_log_create(mctx, &lctx, &logconfig);
59 	isc_log_registercategories(lctx, categories);
60 	isc_log_setcontext(lctx);
61 
62 	destination.file.stream = stderr;
63 	destination.file.name = NULL;
64 	destination.file.versions = ISC_LOG_ROLLNEVER;
65 	destination.file.maximum_size = 0;
66 	isc_log_createchannel(logconfig, "stderr", ISC_LOG_TOFILEDESC,
67 			      ISC_LOG_DYNAMIC, &destination, 0);
68 	result = isc_log_usechannel(logconfig, "stderr", NULL, NULL);
69 
70 	if (result != ISC_R_SUCCESS) {
71 		return -1;
72 	}
73 
74 	return 0;
75 }
76 
77 ISC_TEARDOWN_TEST_IMPL(group) {
78 	if (lctx == NULL) {
79 		return -1;
80 	}
81 
82 	isc_log_setcontext(NULL);
83 	isc_log_destroy(&lctx);
84 
85 	return 0;
86 }
87 
88 /* mimic calling nzf_append() */
89 static void
90 append(void *arg, const char *str, int len) {
91 	char *buf = arg;
92 	size_t l = strlen(buf);
93 	snprintf(buf + l, 1024 - l, "%.*s", len, str);
94 }
95 
96 ISC_RUN_TEST_IMPL(addzoneconf) {
97 	isc_result_t result;
98 	isc_buffer_t b;
99 	cfg_parser_t *p = NULL;
100 	const char *tests[] = {
101 		"zone \"test4.baz\" { type primary; file \"e.db\"; };",
102 		"zone \"test/.baz\" { type primary; file \"e.db\"; };",
103 		"zone \"test\\\".baz\" { type primary; file \"e.db\"; };",
104 		"zone \"test\\.baz\" { type primary; file \"e.db\"; };",
105 		"zone \"test\\\\.baz\" { type primary; file \"e.db\"; };",
106 		"zone \"test\\032.baz\" { type primary; file \"e.db\"; };",
107 		"zone \"test\\010.baz\" { type primary; file \"e.db\"; };"
108 	};
109 	char buf[1024];
110 
111 	/* Parse with default line numbering */
112 	result = cfg_parser_create(mctx, lctx, &p);
113 	assert_int_equal(result, ISC_R_SUCCESS);
114 
115 	for (size_t i = 0; i < ARRAY_SIZE(tests); i++) {
116 		cfg_obj_t *conf = NULL;
117 		const cfg_obj_t *obj = NULL, *zlist = NULL;
118 
119 		isc_buffer_constinit(&b, tests[i], strlen(tests[i]));
120 		isc_buffer_add(&b, strlen(tests[i]));
121 
122 		result = cfg_parse_buffer(p, &b, "text1", 0,
123 					  &cfg_type_namedconf, 0, &conf);
124 		assert_int_equal(result, ISC_R_SUCCESS);
125 
126 		/*
127 		 * Mimic calling nzf_append() from bin/named/server.c
128 		 * and check that the output matches the input.
129 		 */
130 		result = cfg_map_get(conf, "zone", &zlist);
131 		assert_int_equal(result, ISC_R_SUCCESS);
132 
133 		obj = cfg_listelt_value(cfg_list_first(zlist));
134 		assert_ptr_not_equal(obj, NULL);
135 
136 		strlcpy(buf, "zone ", sizeof(buf));
137 		cfg_printx(obj, CFG_PRINTER_ONELINE, append, buf);
138 		strlcat(buf, ";", sizeof(buf));
139 		assert_string_equal(tests[i], buf);
140 
141 		cfg_obj_destroy(p, &conf);
142 		cfg_parser_reset(p);
143 	}
144 
145 	cfg_parser_destroy(&p);
146 }
147 
148 /* test cfg_parse_buffer() */
149 ISC_RUN_TEST_IMPL(parse_buffer) {
150 	isc_result_t result;
151 	unsigned char text[] = "options\n{\nrecursion yes;\n};\n";
152 	isc_buffer_t buf1, buf2;
153 	cfg_parser_t *p1 = NULL, *p2 = NULL;
154 	cfg_obj_t *c1 = NULL, *c2 = NULL;
155 
156 	isc_buffer_init(&buf1, &text[0], sizeof(text) - 1);
157 	isc_buffer_add(&buf1, sizeof(text) - 1);
158 
159 	/* Parse with default line numbering */
160 	result = cfg_parser_create(mctx, lctx, &p1);
161 	assert_int_equal(result, ISC_R_SUCCESS);
162 
163 	result = cfg_parse_buffer(p1, &buf1, "text1", 0, &cfg_type_namedconf, 0,
164 				  &c1);
165 	assert_int_equal(result, ISC_R_SUCCESS);
166 	assert_int_equal(p1->line, 5);
167 
168 	isc_buffer_init(&buf2, &text[0], sizeof(text) - 1);
169 	isc_buffer_add(&buf2, sizeof(text) - 1);
170 
171 	/* Parse with changed line number */
172 	result = cfg_parser_create(mctx, lctx, &p2);
173 	assert_int_equal(result, ISC_R_SUCCESS);
174 
175 	result = cfg_parse_buffer(p2, &buf2, "text2", 100, &cfg_type_namedconf,
176 				  0, &c2);
177 	assert_int_equal(result, ISC_R_SUCCESS);
178 	assert_int_equal(p2->line, 104);
179 
180 	cfg_obj_destroy(p1, &c1);
181 	cfg_obj_destroy(p2, &c2);
182 
183 	cfg_parser_destroy(&p1);
184 	cfg_parser_destroy(&p2);
185 }
186 
187 /* test cfg_map_firstclause() */
188 ISC_RUN_TEST_IMPL(cfg_map_firstclause) {
189 	const char *name = NULL;
190 	const void *clauses = NULL;
191 	unsigned int idx;
192 
193 	name = cfg_map_firstclause(&cfg_type_zoneopts, &clauses, &idx);
194 	assert_non_null(name);
195 	assert_non_null(clauses);
196 	assert_int_equal(idx, 0);
197 }
198 
199 /* test cfg_map_nextclause() */
200 ISC_RUN_TEST_IMPL(cfg_map_nextclause) {
201 	const char *name = NULL;
202 	const void *clauses = NULL;
203 	unsigned int idx;
204 
205 	name = cfg_map_firstclause(&cfg_type_zoneopts, &clauses, &idx);
206 	assert_non_null(name);
207 	assert_non_null(clauses);
208 	assert_int_equal(idx, ISC_R_SUCCESS);
209 
210 	do {
211 		name = cfg_map_nextclause(&cfg_type_zoneopts, &clauses, &idx);
212 		if (name != NULL) {
213 			assert_non_null(clauses);
214 		} else {
215 			assert_null(clauses);
216 			assert_int_equal(idx, 0);
217 		}
218 	} while (name != NULL);
219 }
220 
221 ISC_TEST_LIST_START
222 
223 ISC_TEST_ENTRY(addzoneconf)
224 ISC_TEST_ENTRY(parse_buffer)
225 ISC_TEST_ENTRY(cfg_map_firstclause)
226 ISC_TEST_ENTRY(cfg_map_nextclause)
227 
228 ISC_TEST_LIST_END
229 
230 ISC_TEST_MAIN_CUSTOM(setup_test_group, teardown_test_group)
231