xref: /netbsd-src/external/bsd/unbound/dist/testcode/testbound.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*
2  * testcode/testbound.c - test program for unbound.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  */
36 /**
37  * \file
38  * Exits with code 1 on a failure. 0 if all unit tests are successful.
39  */
40 
41 #include "config.h"
42 #ifdef HAVE_TIME_H
43 #  include <time.h>
44 #endif
45 #include "testcode/testpkts.h"
46 #include "testcode/replay.h"
47 #include "testcode/fake_event.h"
48 #include "daemon/remote.h"
49 #include "util/config_file.h"
50 #include "sldns/keyraw.h"
51 #include <ctype.h>
52 
53 /** signal that this is a testbound compile */
54 #define unbound_testbound 1
55 /**
56  * include the main program from the unbound daemon.
57  * rename main to daemon_main to call it
58  */
59 #define main daemon_main
60 #include "daemon/unbound.c"
61 #undef main
62 
63 /** maximum line length for lines in the replay file. */
64 #define MAX_LINE_LEN 1024
65 /** config files (removed at exit) */
66 static struct config_strlist* cfgfiles = NULL;
67 
68 /** give commandline usage for testbound. */
69 static void
70 testbound_usage(void)
71 {
72 	printf("usage: testbound [options]\n");
73 	printf("\ttest the unbound daemon.\n");
74 	printf("-h      this help\n");
75 	printf("-p file	playback text file\n");
76 	printf("-1 	detect SHA1 support (exit code 0 or 1)\n");
77 	printf("-2 	detect SHA256 support (exit code 0 or 1)\n");
78 	printf("-g 	detect GOST support (exit code 0 or 1)\n");
79 	printf("-e 	detect ECDSA support (exit code 0 or 1)\n");
80 	printf("-c 	detect CLIENT_SUBNET support (exit code 0 or 1)\n");
81 	printf("-i 	detect IPSECMOD support (exit code 0 or 1)\n");
82 	printf("-s 	testbound self-test - unit test of testbound parts.\n");
83 	printf("-o str  unbound commandline options separated by spaces.\n");
84 	printf("Version %s\n", PACKAGE_VERSION);
85 	printf("BSD licensed, see LICENSE file in source package.\n");
86 	printf("Report bugs to %s.\n", PACKAGE_BUGREPORT);
87 }
88 
89 /** Max number of arguments to pass to unbound. */
90 #define MAXARG 100
91 
92 /**
93  * Add options from string to passed argc. splits on whitespace.
94  * @param args: the option argument, "-v -p 12345" or so.
95  * @param pass_argc: ptr to the argc for unbound. Modified.
96  * @param pass_argv: the argv to pass to unbound. Modified.
97  */
98 static void
99 add_opts(const char* args, int* pass_argc, char* pass_argv[])
100 {
101 	const char *p = args, *np;
102 	size_t len;
103 	while(p && isspace((unsigned char)*p))
104 		p++;
105 	while(p && *p) {
106 		/* find location of next string and length of this one */
107 		if((np = strchr(p, ' ')))
108 			len = (size_t)(np-p);
109 		else	len = strlen(p);
110 		/* allocate and copy option */
111 		if(*pass_argc >= MAXARG-1)
112 			fatal_exit("too many arguments: '%s'", p);
113 		pass_argv[*pass_argc] = (char*)malloc(len+1);
114 		if(!pass_argv[*pass_argc])
115 			fatal_exit("add_opts: out of memory");
116 		memcpy(pass_argv[*pass_argc], p, len);
117 		pass_argv[*pass_argc][len] = 0;
118 		(*pass_argc)++;
119 		/* go to next option */
120 	        p = np;
121 		while(p && isspace((unsigned char)*p))
122 			p++;
123 	}
124 }
125 
126 /** pretty print commandline for unbound in this test */
127 static void
128 echo_cmdline(int argc, char* argv[])
129 {
130 	int i;
131 	fprintf(stderr, "testbound is starting:");
132 	for(i=0; i<argc; i++) {
133 		fprintf(stderr, " [%s]", argv[i]);
134 	}
135 	fprintf(stderr, "\n");
136 }
137 
138 /** spool autotrust file */
139 static void
140 spool_auto_file(FILE* in, int* lineno, FILE* cfg, char* id)
141 {
142 	char line[MAX_LINE_LEN];
143 	char* parse;
144 	FILE* spool;
145 	/* find filename for new file */
146 	while(isspace((unsigned char)*id))
147 		id++;
148 	if(*id == '\0')
149 		fatal_exit("AUTROTRUST_FILE must have id, line %d", *lineno);
150 	id[strlen(id)-1]=0; /* remove newline */
151 	fake_temp_file("_auto_", id, line, sizeof(line));
152 	/* add option for the file */
153 	fprintf(cfg, "server:	auto-trust-anchor-file: \"%s\"\n", line);
154 	/* open file and spool to it */
155 	spool = fopen(line, "w");
156 	if(!spool) fatal_exit("could not open %s: %s", line, strerror(errno));
157 	fprintf(stderr, "testbound is spooling key file: %s\n", line);
158 	if(!cfg_strlist_insert(&cfgfiles, strdup(line)))
159 		fatal_exit("out of memory");
160 	line[sizeof(line)-1] = 0;
161 	while(fgets(line, MAX_LINE_LEN-1, in)) {
162 		parse = line;
163 		(*lineno)++;
164 		while(isspace((unsigned char)*parse))
165 			parse++;
166 		if(strncmp(parse, "AUTOTRUST_END", 13) == 0) {
167 			fclose(spool);
168 			return;
169 		}
170 		fputs(line, spool);
171 	}
172 	fatal_exit("no AUTOTRUST_END in input file");
173 }
174 
175 /** process config elements */
176 static void
177 setup_config(FILE* in, int* lineno, int* pass_argc, char* pass_argv[])
178 {
179 	char configfile[MAX_LINE_LEN];
180 	char line[MAX_LINE_LEN];
181 	char* parse;
182 	FILE* cfg;
183 	fake_temp_file("_cfg", "", configfile, sizeof(configfile));
184 	add_opts("-c", pass_argc, pass_argv);
185 	add_opts(configfile, pass_argc, pass_argv);
186 	cfg = fopen(configfile, "w");
187 	if(!cfg) fatal_exit("could not open %s: %s",
188 			configfile, strerror(errno));
189 	if(!cfg_strlist_insert(&cfgfiles, strdup(configfile)))
190 		fatal_exit("out of memory");
191 	line[sizeof(line)-1] = 0;
192 	/* some basic settings to not pollute the host system */
193 	fprintf(cfg, "server:	use-syslog: no\n");
194 	fprintf(cfg, "		directory: \"\"\n");
195 	fprintf(cfg, "		chroot: \"\"\n");
196 	fprintf(cfg, "		username: \"\"\n");
197 	fprintf(cfg, "		pidfile: \"\"\n");
198 	fprintf(cfg, "		val-log-level: 2\n");
199 	fprintf(cfg, "remote-control:	control-enable: no\n");
200 	while(fgets(line, MAX_LINE_LEN-1, in)) {
201 		parse = line;
202 		(*lineno)++;
203 		while(isspace((unsigned char)*parse))
204 			parse++;
205 		if(!*parse || parse[0] == ';')
206 			continue;
207 		if(strncmp(parse, "COMMANDLINE", 11) == 0) {
208 			parse[strlen(parse)-1] = 0; /* strip off \n */
209 			add_opts(parse+11, pass_argc, pass_argv);
210 			continue;
211 		}
212 		if(strncmp(parse, "AUTOTRUST_FILE", 14) == 0) {
213 			spool_auto_file(in, lineno, cfg, parse+14);
214 			continue;
215 		}
216 		if(strncmp(parse, "CONFIG_END", 10) == 0) {
217 			fclose(cfg);
218 			return;
219 		}
220 		fputs(line, cfg);
221 	}
222 	fatal_exit("No CONFIG_END in input file");
223 
224 }
225 
226 /** read playback file */
227 static struct replay_scenario*
228 setup_playback(const char* filename, int* pass_argc, char* pass_argv[])
229 {
230 	struct replay_scenario* scen = NULL;
231 	int lineno = 0;
232 
233 	if(filename) {
234 		FILE *in = fopen(filename, "rb");
235 		if(!in) {
236 			perror(filename);
237 			exit(1);
238 		}
239 		setup_config(in, &lineno, pass_argc, pass_argv);
240 		scen = replay_scenario_read(in, filename, &lineno);
241 		fclose(in);
242 		if(!scen)
243 			fatal_exit("Could not read: %s", filename);
244 	}
245 	else fatal_exit("need a playback file (-p)");
246 	log_info("Scenario: %s", scen->title);
247 	return scen;
248 }
249 
250 /** remove config file at exit */
251 void remove_configfile(void)
252 {
253 	struct config_strlist* p;
254 	for(p=cfgfiles; p; p=p->next)
255 		unlink(p->str);
256 	config_delstrlist(cfgfiles);
257 	cfgfiles = NULL;
258 }
259 
260 /**
261  * Main fake event test program. Setup, teardown and report errors.
262  * @param argc: arg count.
263  * @param argv: array of commandline arguments.
264  * @return program failure if test fails.
265  */
266 int
267 main(int argc, char* argv[])
268 {
269 	int c, res;
270 	int pass_argc = 0;
271 	char* pass_argv[MAXARG];
272 	char* playback_file = NULL;
273 	int init_optind = optind;
274 	char* init_optarg = optarg;
275 	struct replay_scenario* scen = NULL;
276 
277 	/* we do not want the test to depend on the timezone */
278 	(void)putenv("TZ=UTC");
279 
280 	log_init(NULL, 0, NULL);
281 	/* determine commandline options for the daemon */
282 	pass_argc = 1;
283 	pass_argv[0] = "unbound";
284 	add_opts("-d", &pass_argc, pass_argv);
285 	while( (c=getopt(argc, argv, "12egciho:p:s")) != -1) {
286 		switch(c) {
287 		case 's':
288 			free(pass_argv[1]);
289 			testbound_selftest();
290 			exit(0);
291 		case '1':
292 #ifdef USE_SHA1
293 			printf("SHA1 supported\n");
294 			exit(0);
295 #else
296 			printf("SHA1 not supported\n");
297 			exit(1);
298 #endif
299 			break;
300 		case '2':
301 #if (defined(HAVE_EVP_SHA256) || defined(HAVE_NSS) || defined(HAVE_NETTLE)) && defined(USE_SHA2)
302 			printf("SHA256 supported\n");
303 			exit(0);
304 #else
305 			printf("SHA256 not supported\n");
306 			exit(1);
307 #endif
308 			break;
309 		case 'e':
310 #if defined(USE_ECDSA)
311 			printf("ECDSA supported\n");
312 			exit(0);
313 #else
314 			printf("ECDSA not supported\n");
315 			exit(1);
316 #endif
317 			break;
318 		case 'g':
319 #ifdef USE_GOST
320 			if(sldns_key_EVP_load_gost_id()) {
321 				printf("GOST supported\n");
322 				exit(0);
323 			} else {
324 				printf("GOST not supported\n");
325 				exit(1);
326 			}
327 #else
328 			printf("GOST not supported\n");
329 			exit(1);
330 #endif
331 			break;
332 		case 'c':
333 #ifdef CLIENT_SUBNET
334 			printf("CLIENT_SUBNET supported\n");
335 			exit(0);
336 #else
337 			printf("CLIENT_SUBNET not supported\n");
338 			exit(1);
339 #endif
340 			break;
341 		case 'i':
342 #ifdef USE_IPSECMOD
343 			printf("IPSECMOD supported\n");
344 			exit(0);
345 #else
346 			printf("IPSECMOD not supported\n");
347 			exit(1);
348 #endif
349 			break;
350 		case 'p':
351 			playback_file = optarg;
352 			break;
353 		case 'o':
354 			add_opts(optarg, &pass_argc, pass_argv);
355 			break;
356 		case '?':
357 		case 'h':
358 		default:
359 			testbound_usage();
360 			return 1;
361 		}
362 	}
363 	argc -= optind;
364 	argv += optind;
365 	if(argc != 0) {
366 		testbound_usage();
367 		return 1;
368 	}
369 	log_info("Start of %s testbound program.", PACKAGE_STRING);
370 	if(atexit(&remove_configfile) != 0)
371 		fatal_exit("atexit() failed: %s", strerror(errno));
372 
373 	/* setup test environment */
374 	scen = setup_playback(playback_file, &pass_argc, pass_argv);
375 	/* init fake event backend */
376 	fake_event_init(scen);
377 
378 	pass_argv[pass_argc] = NULL;
379 	echo_cmdline(pass_argc, pass_argv);
380 
381 	/* reset getopt processing */
382 	optind = init_optind;
383 	optarg = init_optarg;
384 
385 	/* run the normal daemon */
386 	res = daemon_main(pass_argc, pass_argv);
387 
388 	fake_event_cleanup();
389 	for(c=1; c<pass_argc; c++)
390 		free(pass_argv[c]);
391 	if(res == 0) {
392 		log_info("Testbound Exit Success");
393 #ifdef HAVE_PTHREAD
394 		/* dlopen frees its thread state (dlopen of gost engine) */
395 		pthread_exit(NULL);
396 #endif
397 	}
398 	return res;
399 }
400 
401 /* fake remote control */
402 struct listen_port* daemon_remote_open_ports(struct config_file*
403 	ATTR_UNUSED(cfg))
404 {
405 	return NULL;
406 }
407 
408 struct daemon_remote* daemon_remote_create(struct config_file* ATTR_UNUSED(cfg))
409 {
410 	return (struct daemon_remote*)calloc(1,1);
411 }
412 
413 void daemon_remote_delete(struct daemon_remote* rc)
414 {
415 	free(rc);
416 }
417 
418 void daemon_remote_clear(struct daemon_remote* ATTR_UNUSED(rc))
419 {
420 	/* nothing */
421 }
422 
423 int daemon_remote_open_accept(struct daemon_remote* ATTR_UNUSED(rc),
424         struct listen_port* ATTR_UNUSED(ports),
425 	struct worker* ATTR_UNUSED(worker))
426 {
427 	return 1;
428 }
429 
430 int remote_accept_callback(struct comm_point* ATTR_UNUSED(c),
431 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
432         struct comm_reply* ATTR_UNUSED(repinfo))
433 {
434 	log_assert(0);
435 	return 0;
436 }
437 
438 int remote_control_callback(struct comm_point* ATTR_UNUSED(c),
439 	void* ATTR_UNUSED(arg), int ATTR_UNUSED(error),
440         struct comm_reply* ATTR_UNUSED(repinfo))
441 {
442 	log_assert(0);
443 	return 0;
444 }
445 
446 void remote_get_opt_ssl(char* ATTR_UNUSED(str), void* ATTR_UNUSED(arg))
447 {
448         log_assert(0);
449 }
450 
451 void wsvc_command_option(const char* ATTR_UNUSED(wopt),
452 	const char* ATTR_UNUSED(cfgfile), int ATTR_UNUSED(v),
453 	int ATTR_UNUSED(c))
454 {
455 	log_assert(0);
456 }
457 
458 void wsvc_setup_worker(struct worker* ATTR_UNUSED(worker))
459 {
460 	/* do nothing */
461 }
462 
463 void wsvc_desetup_worker(struct worker* ATTR_UNUSED(worker))
464 {
465 	/* do nothing */
466 }
467 
468 #ifdef UB_ON_WINDOWS
469 void worker_win_stop_cb(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev),
470 	void* ATTR_UNUSED(arg))
471 {
472 	log_assert(0);
473 }
474 
475 void wsvc_cron_cb(void* ATTR_UNUSED(arg))
476 {
477 	log_assert(0);
478 }
479 #endif /* UB_ON_WINDOWS */
480 
481