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