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 <ctype.h> 46 #include "testcode/testpkts.h" 47 #include "testcode/replay.h" 48 #include "testcode/fake_event.h" 49 #include "daemon/remote.h" 50 #include "libunbound/worker.h" 51 #include "daemon/worker.h" 52 #include "util/config_file.h" 53 #include "sldns/keyraw.h" 54 #ifdef UB_ON_WINDOWS 55 #include "winrc/win_svc.h" 56 #endif 57 58 /** signal that this is a testbound compile */ 59 #define unbound_testbound 1 60 /** renamed main routine */ 61 int daemon_main(int argc, char* argv[]); 62 /** 63 * include the main program from the unbound daemon. 64 * rename main to daemon_main to call it 65 */ 66 #define main daemon_main 67 #include "daemon/unbound.c" 68 #undef main 69 70 /** maximum line length for lines in the replay file. */ 71 #define MAX_LINE_LEN 1024 72 /** config files (removed at exit) */ 73 static struct config_strlist* cfgfiles = NULL; 74 75 /** give commandline usage for testbound. */ 76 static void 77 testbound_usage(void) 78 { 79 printf("usage: testbound [options]\n"); 80 printf("\ttest the unbound daemon.\n"); 81 printf("-h this help\n"); 82 printf("-p file playback text file\n"); 83 printf("-1 detect SHA1 support (exit code 0 or 1)\n"); 84 printf("-2 detect SHA256 support (exit code 0 or 1)\n"); 85 printf("-g detect GOST support (exit code 0 or 1)\n"); 86 printf("-e detect ECDSA support (exit code 0 or 1)\n"); 87 printf("-c detect CLIENT_SUBNET support (exit code 0 or 1)\n"); 88 printf("-i detect IPSECMOD support (exit code 0 or 1)\n"); 89 printf("-s testbound self-test - unit test of testbound parts.\n"); 90 printf("-o str unbound commandline options separated by spaces.\n"); 91 printf("Version %s\n", PACKAGE_VERSION); 92 printf("BSD licensed, see LICENSE file in source package.\n"); 93 printf("Report bugs to %s.\n", PACKAGE_BUGREPORT); 94 } 95 96 /** Max number of arguments to pass to unbound. */ 97 #define MAXARG 100 98 99 /** 100 * Add options from string to passed argc. splits on whitespace. 101 * @param args: the option argument, "-v -p 12345" or so. 102 * @param pass_argc: ptr to the argc for unbound. Modified. 103 * @param pass_argv: the argv to pass to unbound. Modified. 104 */ 105 static void 106 add_opts(const char* args, int* pass_argc, char* pass_argv[]) 107 { 108 const char *p = args, *np; 109 size_t len; 110 while(p && isspace((unsigned char)*p)) 111 p++; 112 while(p && *p) { 113 /* find location of next string and length of this one */ 114 if((np = strchr(p, ' '))) 115 len = (size_t)(np-p); 116 else len = strlen(p); 117 /* allocate and copy option */ 118 if(*pass_argc >= MAXARG-1) 119 fatal_exit("too many arguments: '%s'", p); 120 pass_argv[*pass_argc] = (char*)malloc(len+1); 121 if(!pass_argv[*pass_argc]) 122 fatal_exit("add_opts: out of memory"); 123 memcpy(pass_argv[*pass_argc], p, len); 124 pass_argv[*pass_argc][len] = 0; 125 (*pass_argc)++; 126 /* go to next option */ 127 p = np; 128 while(p && isspace((unsigned char)*p)) 129 p++; 130 } 131 } 132 133 /** pretty print commandline for unbound in this test */ 134 static void 135 echo_cmdline(int argc, char* argv[]) 136 { 137 int i; 138 fprintf(stderr, "testbound is starting:"); 139 for(i=0; i<argc; i++) { 140 fprintf(stderr, " [%s]", argv[i]); 141 } 142 fprintf(stderr, "\n"); 143 } 144 145 /** spool temp file name */ 146 static void 147 spool_temp_file_name(int* lineno, FILE* cfg, char* id) 148 { 149 char line[MAX_LINE_LEN]; 150 /* find filename for new file */ 151 while(isspace((unsigned char)*id)) 152 id++; 153 if(*id == '\0') 154 fatal_exit("TEMPFILE_NAME must have id, line %d", *lineno); 155 strip_end_white(id); 156 fake_temp_file("_temp_", id, line, sizeof(line)); 157 fprintf(cfg, "\"%s\"\n", line); 158 } 159 160 /** spool temp file */ 161 static void 162 spool_temp_file(FILE* in, int* lineno, char* id) 163 { 164 char line[MAX_LINE_LEN]; 165 char* parse; 166 FILE* spool; 167 /* find filename for new file */ 168 while(isspace((unsigned char)*id)) 169 id++; 170 if(*id == '\0') 171 fatal_exit("TEMPFILE_CONTENTS must have id, line %d", *lineno); 172 strip_end_white(id); 173 fake_temp_file("_temp_", id, line, sizeof(line)); 174 /* open file and spool to it */ 175 spool = fopen(line, "w"); 176 if(!spool) fatal_exit("could not open %s: %s", line, strerror(errno)); 177 fprintf(stderr, "testbound is spooling temp file: %s\n", line); 178 if(!cfg_strlist_insert(&cfgfiles, strdup(line))) 179 fatal_exit("out of memory"); 180 line[sizeof(line)-1] = 0; 181 while(fgets(line, MAX_LINE_LEN-1, in)) { 182 parse = line; 183 (*lineno)++; 184 while(isspace((unsigned char)*parse)) 185 parse++; 186 if(strncmp(parse, "$INCLUDE_TEMPFILE", 17) == 0) { 187 char l2[MAX_LINE_LEN-30]; /* -30 makes it fit with 188 a preceding $INCLUDE in the buf line[] */ 189 char* tid = parse+17; 190 while(isspace((unsigned char)*tid)) 191 tid++; 192 strip_end_white(tid); 193 fake_temp_file("_temp_", tid, l2, sizeof(l2)); 194 snprintf(line, sizeof(line), "$INCLUDE %s\n", l2); 195 } 196 if(strncmp(parse, "TEMPFILE_END", 12) == 0) { 197 fclose(spool); 198 return; 199 } 200 fputs(line, spool); 201 } 202 fatal_exit("no TEMPFILE_END in input file"); 203 } 204 205 /** spool autotrust file */ 206 static void 207 spool_auto_file(FILE* in, int* lineno, FILE* cfg, char* id) 208 { 209 char line[MAX_LINE_LEN]; 210 char* parse; 211 FILE* spool; 212 /* find filename for new file */ 213 while(isspace((unsigned char)*id)) 214 id++; 215 if(*id == '\0') 216 fatal_exit("AUTROTRUST_FILE must have id, line %d", *lineno); 217 strip_end_white(id); 218 fake_temp_file("_auto_", id, line, sizeof(line)); 219 /* add option for the file */ 220 fprintf(cfg, "server: auto-trust-anchor-file: \"%s\"\n", line); 221 /* open file and spool to it */ 222 spool = fopen(line, "w"); 223 if(!spool) fatal_exit("could not open %s: %s", line, strerror(errno)); 224 fprintf(stderr, "testbound is spooling key file: %s\n", line); 225 if(!cfg_strlist_insert(&cfgfiles, strdup(line))) 226 fatal_exit("out of memory"); 227 line[sizeof(line)-1] = 0; 228 while(fgets(line, MAX_LINE_LEN-1, in)) { 229 parse = line; 230 (*lineno)++; 231 while(isspace((unsigned char)*parse)) 232 parse++; 233 if(strncmp(parse, "AUTOTRUST_END", 13) == 0) { 234 fclose(spool); 235 return; 236 } 237 fputs(line, spool); 238 } 239 fatal_exit("no AUTOTRUST_END in input file"); 240 } 241 242 /** process config elements */ 243 static void 244 setup_config(FILE* in, int* lineno, int* pass_argc, char* pass_argv[]) 245 { 246 char configfile[MAX_LINE_LEN]; 247 char line[MAX_LINE_LEN]; 248 char* parse; 249 FILE* cfg; 250 fake_temp_file("_cfg", "", configfile, sizeof(configfile)); 251 add_opts("-c", pass_argc, pass_argv); 252 add_opts(configfile, pass_argc, pass_argv); 253 cfg = fopen(configfile, "w"); 254 if(!cfg) fatal_exit("could not open %s: %s", 255 configfile, strerror(errno)); 256 if(!cfg_strlist_insert(&cfgfiles, strdup(configfile))) 257 fatal_exit("out of memory"); 258 line[sizeof(line)-1] = 0; 259 /* some basic settings to not pollute the host system */ 260 fprintf(cfg, "server: use-syslog: no\n"); 261 fprintf(cfg, " directory: \"\"\n"); 262 fprintf(cfg, " chroot: \"\"\n"); 263 fprintf(cfg, " username: \"\"\n"); 264 fprintf(cfg, " pidfile: \"\"\n"); 265 fprintf(cfg, " val-log-level: 2\n"); 266 fprintf(cfg, " log-servfail: yes\n"); 267 fprintf(cfg, "remote-control: control-enable: no\n"); 268 while(fgets(line, MAX_LINE_LEN-1, in)) { 269 parse = line; 270 (*lineno)++; 271 while(isspace((unsigned char)*parse)) 272 parse++; 273 if(!*parse || parse[0] == ';') 274 continue; 275 if(strncmp(parse, "COMMANDLINE", 11) == 0) { 276 parse[strlen(parse)-1] = 0; /* strip off \n */ 277 add_opts(parse+11, pass_argc, pass_argv); 278 continue; 279 } 280 if(strncmp(parse, "AUTOTRUST_FILE", 14) == 0) { 281 spool_auto_file(in, lineno, cfg, parse+14); 282 continue; 283 } 284 if(strncmp(parse, "TEMPFILE_NAME", 13) == 0) { 285 spool_temp_file_name(lineno, cfg, parse+13); 286 continue; 287 } 288 if(strncmp(parse, "TEMPFILE_CONTENTS", 17) == 0) { 289 spool_temp_file(in, lineno, parse+17); 290 continue; 291 } 292 if(strncmp(parse, "CONFIG_END", 10) == 0) { 293 fclose(cfg); 294 return; 295 } 296 fputs(line, cfg); 297 } 298 fatal_exit("No CONFIG_END in input file"); 299 300 } 301 302 /** read playback file */ 303 static struct replay_scenario* 304 setup_playback(const char* filename, int* pass_argc, char* pass_argv[]) 305 { 306 struct replay_scenario* scen = NULL; 307 int lineno = 0; 308 309 if(filename) { 310 FILE *in = fopen(filename, "rb"); 311 if(!in) { 312 perror(filename); 313 exit(1); 314 } 315 setup_config(in, &lineno, pass_argc, pass_argv); 316 scen = replay_scenario_read(in, filename, &lineno); 317 fclose(in); 318 if(!scen) 319 fatal_exit("Could not read: %s", filename); 320 } 321 else fatal_exit("need a playback file (-p)"); 322 log_info("Scenario: %s", scen->title); 323 return scen; 324 } 325 326 /** remove config file at exit */ 327 static void remove_configfile(void) 328 { 329 struct config_strlist* p; 330 for(p=cfgfiles; p; p=p->next) 331 unlink(p->str); 332 config_delstrlist(cfgfiles); 333 cfgfiles = NULL; 334 } 335 336 /** 337 * Main fake event test program. Setup, teardown and report errors. 338 * @param argc: arg count. 339 * @param argv: array of commandline arguments. 340 * @return program failure if test fails. 341 */ 342 int 343 main(int argc, char* argv[]) 344 { 345 int c, res; 346 int pass_argc = 0; 347 char* pass_argv[MAXARG]; 348 char* playback_file = NULL; 349 int init_optind = optind; 350 char* init_optarg = optarg; 351 struct replay_scenario* scen = NULL; 352 353 /* we do not want the test to depend on the timezone */ 354 (void)putenv("TZ=UTC"); 355 memset(pass_argv, 0, sizeof(pass_argv)); 356 #ifdef HAVE_SYSTEMD 357 /* we do not want the test to use systemd daemon startup notification*/ 358 (void)unsetenv("NOTIFY_SOCKET"); 359 #endif /* HAVE_SYSTEMD */ 360 361 checklock_start(); 362 log_init(NULL, 0, NULL); 363 /* determine commandline options for the daemon */ 364 pass_argc = 1; 365 pass_argv[0] = "unbound"; 366 add_opts("-d", &pass_argc, pass_argv); 367 while( (c=getopt(argc, argv, "12egciho:p:s")) != -1) { 368 switch(c) { 369 case 's': 370 free(pass_argv[1]); 371 testbound_selftest(); 372 checklock_stop(); 373 if(log_get_lock()) { 374 lock_basic_destroy((lock_basic_type*)log_get_lock()); 375 } 376 exit(0); 377 case '1': 378 #ifdef USE_SHA1 379 printf("SHA1 supported\n"); 380 exit(0); 381 #else 382 printf("SHA1 not supported\n"); 383 exit(1); 384 #endif 385 break; 386 case '2': 387 #if (defined(HAVE_EVP_SHA256) || defined(HAVE_NSS) || defined(HAVE_NETTLE)) && defined(USE_SHA2) 388 printf("SHA256 supported\n"); 389 exit(0); 390 #else 391 printf("SHA256 not supported\n"); 392 exit(1); 393 #endif 394 break; 395 case 'e': 396 #if defined(USE_ECDSA) 397 printf("ECDSA supported\n"); 398 exit(0); 399 #else 400 printf("ECDSA not supported\n"); 401 exit(1); 402 #endif 403 break; 404 case 'g': 405 #ifdef USE_GOST 406 if(sldns_key_EVP_load_gost_id()) { 407 printf("GOST supported\n"); 408 exit(0); 409 } else { 410 printf("GOST not supported\n"); 411 exit(1); 412 } 413 #else 414 printf("GOST not supported\n"); 415 exit(1); 416 #endif 417 break; 418 case 'c': 419 #ifdef CLIENT_SUBNET 420 printf("CLIENT_SUBNET supported\n"); 421 exit(0); 422 #else 423 printf("CLIENT_SUBNET not supported\n"); 424 exit(1); 425 #endif 426 break; 427 case 'i': 428 #ifdef USE_IPSECMOD 429 printf("IPSECMOD supported\n"); 430 exit(0); 431 #else 432 printf("IPSECMOD not supported\n"); 433 exit(1); 434 #endif 435 break; 436 case 'p': 437 playback_file = optarg; 438 break; 439 case 'o': 440 add_opts(optarg, &pass_argc, pass_argv); 441 break; 442 case '?': 443 case 'h': 444 default: 445 testbound_usage(); 446 exit(1); 447 } 448 } 449 argc -= optind; 450 /* argv += optind; not using further arguments */ 451 if(argc != 0) { 452 testbound_usage(); 453 exit(1); 454 } 455 log_info("Start of %s testbound program.", PACKAGE_STRING); 456 if(atexit(&remove_configfile) != 0) 457 fatal_exit("atexit() failed: %s", strerror(errno)); 458 459 /* setup test environment */ 460 scen = setup_playback(playback_file, &pass_argc, pass_argv); 461 /* init fake event backend */ 462 fake_event_init(scen); 463 464 pass_argv[pass_argc] = NULL; 465 echo_cmdline(pass_argc, pass_argv); 466 467 /* reset getopt processing */ 468 optind = init_optind; 469 optarg = init_optarg; 470 471 /* run the normal daemon */ 472 res = daemon_main(pass_argc, pass_argv); 473 474 fake_event_cleanup(); 475 for(c=1; c<pass_argc; c++) 476 free(pass_argv[c]); 477 if(res == 0) { 478 log_info("Testbound Exit Success\n"); 479 /* remove configfile from here, the atexit() is for when 480 * there is a crash to remove the tmpdir file. 481 * This one removes the file while alloc and log locks are 482 * still valid, and can be logged (for memory calculation), 483 * it leaves the ptr NULL so the atexit does nothing. */ 484 remove_configfile(); 485 if(log_get_lock()) { 486 lock_basic_destroy((lock_basic_type*)log_get_lock()); 487 } 488 #ifdef HAVE_PTHREAD 489 /* dlopen frees its thread state (dlopen of gost engine) */ 490 pthread_exit(NULL); 491 #endif 492 } 493 return res; 494 } 495 496 /* fake remote control */ 497 struct listen_port* daemon_remote_open_ports(struct config_file* 498 ATTR_UNUSED(cfg)) 499 { 500 return NULL; 501 } 502 503 struct daemon_remote* daemon_remote_create(struct config_file* ATTR_UNUSED(cfg)) 504 { 505 return (struct daemon_remote*)calloc(1,1); 506 } 507 508 void daemon_remote_delete(struct daemon_remote* rc) 509 { 510 free(rc); 511 } 512 513 void daemon_remote_clear(struct daemon_remote* ATTR_UNUSED(rc)) 514 { 515 /* nothing */ 516 } 517 518 int daemon_remote_open_accept(struct daemon_remote* ATTR_UNUSED(rc), 519 struct listen_port* ATTR_UNUSED(ports), struct worker* worker) 520 { 521 struct replay_runtime* runtime = (struct replay_runtime*)worker->base; 522 runtime->daemon = worker->daemon; 523 return 1; 524 } 525 526 int remote_accept_callback(struct comm_point* ATTR_UNUSED(c), 527 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error), 528 struct comm_reply* ATTR_UNUSED(repinfo)) 529 { 530 log_assert(0); 531 return 0; 532 } 533 534 int remote_control_callback(struct comm_point* ATTR_UNUSED(c), 535 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error), 536 struct comm_reply* ATTR_UNUSED(repinfo)) 537 { 538 log_assert(0); 539 return 0; 540 } 541 542 void remote_get_opt_ssl(char* ATTR_UNUSED(str), void* ATTR_UNUSED(arg)) 543 { 544 log_assert(0); 545 } 546 547 #ifdef UB_ON_WINDOWS 548 void wsvc_command_option(const char* ATTR_UNUSED(wopt), 549 const char* ATTR_UNUSED(cfgfile), int ATTR_UNUSED(v), 550 int ATTR_UNUSED(c)) 551 { 552 log_assert(0); 553 } 554 #endif 555 556 #ifdef UB_ON_WINDOWS 557 void wsvc_setup_worker(struct worker* ATTR_UNUSED(worker)) 558 { 559 /* do nothing */ 560 } 561 #endif 562 563 #ifdef UB_ON_WINDOWS 564 void wsvc_desetup_worker(struct worker* ATTR_UNUSED(worker)) 565 { 566 /* do nothing */ 567 } 568 #endif 569 570 #ifdef UB_ON_WINDOWS 571 void worker_win_stop_cb(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev), 572 void* ATTR_UNUSED(arg)) 573 { 574 log_assert(0); 575 } 576 577 void wsvc_cron_cb(void* ATTR_UNUSED(arg)) 578 { 579 log_assert(0); 580 } 581 #endif /* UB_ON_WINDOWS */ 582 583 int tcp_connect_errno_needs_log(struct sockaddr* ATTR_UNUSED(addr), 584 socklen_t ATTR_UNUSED(addrlen)) 585 { 586 return 1; 587 } 588 589 int squelch_err_ssl_handshake(unsigned long ATTR_UNUSED(err)) 590 { 591 return 0; 592 } 593 594 void listen_setup_locks(void) 595 { 596 /* nothing */ 597 } 598 599 void listen_desetup_locks(void) 600 { 601 /* nothing */ 602 } 603