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 temp file name */ 139 static void 140 spool_temp_file_name(int* lineno, FILE* cfg, char* id) 141 { 142 char line[MAX_LINE_LEN]; 143 /* find filename for new file */ 144 while(isspace((unsigned char)*id)) 145 id++; 146 if(*id == '\0') 147 fatal_exit("TEMPFILE_NAME must have id, line %d", *lineno); 148 id[strlen(id)-1]=0; /* remove newline */ 149 fake_temp_file("_temp_", id, line, sizeof(line)); 150 fprintf(cfg, "\"%s\"\n", line); 151 } 152 153 /** spool temp file */ 154 static void 155 spool_temp_file(FILE* in, int* lineno, char* id) 156 { 157 char line[MAX_LINE_LEN]; 158 char* parse; 159 FILE* spool; 160 /* find filename for new file */ 161 while(isspace((unsigned char)*id)) 162 id++; 163 if(*id == '\0') 164 fatal_exit("TEMPFILE_CONTENTS must have id, line %d", *lineno); 165 id[strlen(id)-1]=0; /* remove newline */ 166 fake_temp_file("_temp_", id, line, sizeof(line)); 167 /* open file and spool to it */ 168 spool = fopen(line, "w"); 169 if(!spool) fatal_exit("could not open %s: %s", line, strerror(errno)); 170 fprintf(stderr, "testbound is spooling temp file: %s\n", line); 171 if(!cfg_strlist_insert(&cfgfiles, strdup(line))) 172 fatal_exit("out of memory"); 173 line[sizeof(line)-1] = 0; 174 while(fgets(line, MAX_LINE_LEN-1, in)) { 175 parse = line; 176 (*lineno)++; 177 while(isspace((unsigned char)*parse)) 178 parse++; 179 if(strncmp(parse, "$INCLUDE_TEMPFILE", 17) == 0) { 180 char l2[MAX_LINE_LEN-30]; /* -30 makes it fit with 181 a preceding $INCLUDE in the buf line[] */ 182 char* tid = parse+17; 183 while(isspace((unsigned char)*tid)) 184 tid++; 185 tid[strlen(tid)-1]=0; /* remove newline */ 186 fake_temp_file("_temp_", tid, l2, sizeof(l2)); 187 snprintf(line, sizeof(line), "$INCLUDE %s\n", l2); 188 } 189 if(strncmp(parse, "TEMPFILE_END", 12) == 0) { 190 fclose(spool); 191 return; 192 } 193 fputs(line, spool); 194 } 195 fatal_exit("no TEMPFILE_END in input file"); 196 } 197 198 /** spool autotrust file */ 199 static void 200 spool_auto_file(FILE* in, int* lineno, FILE* cfg, char* id) 201 { 202 char line[MAX_LINE_LEN]; 203 char* parse; 204 FILE* spool; 205 /* find filename for new file */ 206 while(isspace((unsigned char)*id)) 207 id++; 208 if(*id == '\0') 209 fatal_exit("AUTROTRUST_FILE must have id, line %d", *lineno); 210 id[strlen(id)-1]=0; /* remove newline */ 211 fake_temp_file("_auto_", id, line, sizeof(line)); 212 /* add option for the file */ 213 fprintf(cfg, "server: auto-trust-anchor-file: \"%s\"\n", line); 214 /* open file and spool to it */ 215 spool = fopen(line, "w"); 216 if(!spool) fatal_exit("could not open %s: %s", line, strerror(errno)); 217 fprintf(stderr, "testbound is spooling key file: %s\n", line); 218 if(!cfg_strlist_insert(&cfgfiles, strdup(line))) 219 fatal_exit("out of memory"); 220 line[sizeof(line)-1] = 0; 221 while(fgets(line, MAX_LINE_LEN-1, in)) { 222 parse = line; 223 (*lineno)++; 224 while(isspace((unsigned char)*parse)) 225 parse++; 226 if(strncmp(parse, "AUTOTRUST_END", 13) == 0) { 227 fclose(spool); 228 return; 229 } 230 fputs(line, spool); 231 } 232 fatal_exit("no AUTOTRUST_END in input file"); 233 } 234 235 /** process config elements */ 236 static void 237 setup_config(FILE* in, int* lineno, int* pass_argc, char* pass_argv[]) 238 { 239 char configfile[MAX_LINE_LEN]; 240 char line[MAX_LINE_LEN]; 241 char* parse; 242 FILE* cfg; 243 fake_temp_file("_cfg", "", configfile, sizeof(configfile)); 244 add_opts("-c", pass_argc, pass_argv); 245 add_opts(configfile, pass_argc, pass_argv); 246 cfg = fopen(configfile, "w"); 247 if(!cfg) fatal_exit("could not open %s: %s", 248 configfile, strerror(errno)); 249 if(!cfg_strlist_insert(&cfgfiles, strdup(configfile))) 250 fatal_exit("out of memory"); 251 line[sizeof(line)-1] = 0; 252 /* some basic settings to not pollute the host system */ 253 fprintf(cfg, "server: use-syslog: no\n"); 254 fprintf(cfg, " directory: \"\"\n"); 255 fprintf(cfg, " chroot: \"\"\n"); 256 fprintf(cfg, " username: \"\"\n"); 257 fprintf(cfg, " pidfile: \"\"\n"); 258 fprintf(cfg, " val-log-level: 2\n"); 259 fprintf(cfg, "remote-control: control-enable: no\n"); 260 while(fgets(line, MAX_LINE_LEN-1, in)) { 261 parse = line; 262 (*lineno)++; 263 while(isspace((unsigned char)*parse)) 264 parse++; 265 if(!*parse || parse[0] == ';') 266 continue; 267 if(strncmp(parse, "COMMANDLINE", 11) == 0) { 268 parse[strlen(parse)-1] = 0; /* strip off \n */ 269 add_opts(parse+11, pass_argc, pass_argv); 270 continue; 271 } 272 if(strncmp(parse, "AUTOTRUST_FILE", 14) == 0) { 273 spool_auto_file(in, lineno, cfg, parse+14); 274 continue; 275 } 276 if(strncmp(parse, "TEMPFILE_NAME", 13) == 0) { 277 spool_temp_file_name(lineno, cfg, parse+13); 278 continue; 279 } 280 if(strncmp(parse, "TEMPFILE_CONTENTS", 17) == 0) { 281 spool_temp_file(in, lineno, parse+17); 282 continue; 283 } 284 if(strncmp(parse, "CONFIG_END", 10) == 0) { 285 fclose(cfg); 286 return; 287 } 288 fputs(line, cfg); 289 } 290 fatal_exit("No CONFIG_END in input file"); 291 292 } 293 294 /** read playback file */ 295 static struct replay_scenario* 296 setup_playback(const char* filename, int* pass_argc, char* pass_argv[]) 297 { 298 struct replay_scenario* scen = NULL; 299 int lineno = 0; 300 301 if(filename) { 302 FILE *in = fopen(filename, "rb"); 303 if(!in) { 304 perror(filename); 305 exit(1); 306 } 307 setup_config(in, &lineno, pass_argc, pass_argv); 308 scen = replay_scenario_read(in, filename, &lineno); 309 fclose(in); 310 if(!scen) 311 fatal_exit("Could not read: %s", filename); 312 } 313 else fatal_exit("need a playback file (-p)"); 314 log_info("Scenario: %s", scen->title); 315 return scen; 316 } 317 318 /** remove config file at exit */ 319 void remove_configfile(void) 320 { 321 struct config_strlist* p; 322 for(p=cfgfiles; p; p=p->next) 323 unlink(p->str); 324 config_delstrlist(cfgfiles); 325 cfgfiles = NULL; 326 } 327 328 /** 329 * Main fake event test program. Setup, teardown and report errors. 330 * @param argc: arg count. 331 * @param argv: array of commandline arguments. 332 * @return program failure if test fails. 333 */ 334 int 335 main(int argc, char* argv[]) 336 { 337 int c, res; 338 int pass_argc = 0; 339 char* pass_argv[MAXARG]; 340 char* playback_file = NULL; 341 int init_optind = optind; 342 char* init_optarg = optarg; 343 struct replay_scenario* scen = NULL; 344 345 /* we do not want the test to depend on the timezone */ 346 (void)putenv("TZ=UTC"); 347 348 log_init(NULL, 0, NULL); 349 /* determine commandline options for the daemon */ 350 pass_argc = 1; 351 pass_argv[0] = "unbound"; 352 add_opts("-d", &pass_argc, pass_argv); 353 while( (c=getopt(argc, argv, "12egciho:p:s")) != -1) { 354 switch(c) { 355 case 's': 356 free(pass_argv[1]); 357 testbound_selftest(); 358 checklock_stop(); 359 if(log_get_lock()) { 360 lock_quick_destroy((lock_quick_type*)log_get_lock()); 361 } 362 exit(0); 363 case '1': 364 #ifdef USE_SHA1 365 printf("SHA1 supported\n"); 366 exit(0); 367 #else 368 printf("SHA1 not supported\n"); 369 exit(1); 370 #endif 371 break; 372 case '2': 373 #if (defined(HAVE_EVP_SHA256) || defined(HAVE_NSS) || defined(HAVE_NETTLE)) && defined(USE_SHA2) 374 printf("SHA256 supported\n"); 375 exit(0); 376 #else 377 printf("SHA256 not supported\n"); 378 exit(1); 379 #endif 380 break; 381 case 'e': 382 #if defined(USE_ECDSA) 383 printf("ECDSA supported\n"); 384 exit(0); 385 #else 386 printf("ECDSA not supported\n"); 387 exit(1); 388 #endif 389 break; 390 case 'g': 391 #ifdef USE_GOST 392 if(sldns_key_EVP_load_gost_id()) { 393 printf("GOST supported\n"); 394 exit(0); 395 } else { 396 printf("GOST not supported\n"); 397 exit(1); 398 } 399 #else 400 printf("GOST not supported\n"); 401 exit(1); 402 #endif 403 break; 404 case 'c': 405 #ifdef CLIENT_SUBNET 406 printf("CLIENT_SUBNET supported\n"); 407 exit(0); 408 #else 409 printf("CLIENT_SUBNET not supported\n"); 410 exit(1); 411 #endif 412 break; 413 case 'i': 414 #ifdef USE_IPSECMOD 415 printf("IPSECMOD supported\n"); 416 exit(0); 417 #else 418 printf("IPSECMOD not supported\n"); 419 exit(1); 420 #endif 421 break; 422 case 'p': 423 playback_file = optarg; 424 break; 425 case 'o': 426 add_opts(optarg, &pass_argc, pass_argv); 427 break; 428 case '?': 429 case 'h': 430 default: 431 testbound_usage(); 432 return 1; 433 } 434 } 435 argc -= optind; 436 argv += optind; 437 if(argc != 0) { 438 testbound_usage(); 439 return 1; 440 } 441 log_info("Start of %s testbound program.", PACKAGE_STRING); 442 if(atexit(&remove_configfile) != 0) 443 fatal_exit("atexit() failed: %s", strerror(errno)); 444 445 /* setup test environment */ 446 scen = setup_playback(playback_file, &pass_argc, pass_argv); 447 /* init fake event backend */ 448 fake_event_init(scen); 449 450 pass_argv[pass_argc] = NULL; 451 echo_cmdline(pass_argc, pass_argv); 452 453 /* reset getopt processing */ 454 optind = init_optind; 455 optarg = init_optarg; 456 457 /* run the normal daemon */ 458 res = daemon_main(pass_argc, pass_argv); 459 460 fake_event_cleanup(); 461 for(c=1; c<pass_argc; c++) 462 free(pass_argv[c]); 463 if(res == 0) { 464 log_info("Testbound Exit Success\n"); 465 if(log_get_lock()) { 466 lock_quick_destroy((lock_quick_type*)log_get_lock()); 467 } 468 #ifdef HAVE_PTHREAD 469 /* dlopen frees its thread state (dlopen of gost engine) */ 470 pthread_exit(NULL); 471 #endif 472 } 473 return res; 474 } 475 476 /* fake remote control */ 477 struct listen_port* daemon_remote_open_ports(struct config_file* 478 ATTR_UNUSED(cfg)) 479 { 480 return NULL; 481 } 482 483 struct daemon_remote* daemon_remote_create(struct config_file* ATTR_UNUSED(cfg)) 484 { 485 return (struct daemon_remote*)calloc(1,1); 486 } 487 488 void daemon_remote_delete(struct daemon_remote* rc) 489 { 490 free(rc); 491 } 492 493 void daemon_remote_clear(struct daemon_remote* ATTR_UNUSED(rc)) 494 { 495 /* nothing */ 496 } 497 498 int daemon_remote_open_accept(struct daemon_remote* ATTR_UNUSED(rc), 499 struct listen_port* ATTR_UNUSED(ports), 500 struct worker* ATTR_UNUSED(worker)) 501 { 502 return 1; 503 } 504 505 int remote_accept_callback(struct comm_point* ATTR_UNUSED(c), 506 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error), 507 struct comm_reply* ATTR_UNUSED(repinfo)) 508 { 509 log_assert(0); 510 return 0; 511 } 512 513 int remote_control_callback(struct comm_point* ATTR_UNUSED(c), 514 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error), 515 struct comm_reply* ATTR_UNUSED(repinfo)) 516 { 517 log_assert(0); 518 return 0; 519 } 520 521 void remote_get_opt_ssl(char* ATTR_UNUSED(str), void* ATTR_UNUSED(arg)) 522 { 523 log_assert(0); 524 } 525 526 void wsvc_command_option(const char* ATTR_UNUSED(wopt), 527 const char* ATTR_UNUSED(cfgfile), int ATTR_UNUSED(v), 528 int ATTR_UNUSED(c)) 529 { 530 log_assert(0); 531 } 532 533 void wsvc_setup_worker(struct worker* ATTR_UNUSED(worker)) 534 { 535 /* do nothing */ 536 } 537 538 void wsvc_desetup_worker(struct worker* ATTR_UNUSED(worker)) 539 { 540 /* do nothing */ 541 } 542 543 #ifdef UB_ON_WINDOWS 544 void worker_win_stop_cb(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev), 545 void* ATTR_UNUSED(arg)) 546 { 547 log_assert(0); 548 } 549 550 void wsvc_cron_cb(void* ATTR_UNUSED(arg)) 551 { 552 log_assert(0); 553 } 554 #endif /* UB_ON_WINDOWS */ 555 556