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 memset(pass_argv, 0, sizeof(pass_argv)); 348 349 log_init(NULL, 0, NULL); 350 /* determine commandline options for the daemon */ 351 pass_argc = 1; 352 pass_argv[0] = "unbound"; 353 add_opts("-d", &pass_argc, pass_argv); 354 while( (c=getopt(argc, argv, "12egciho:p:s")) != -1) { 355 switch(c) { 356 case 's': 357 free(pass_argv[1]); 358 testbound_selftest(); 359 checklock_stop(); 360 if(log_get_lock()) { 361 lock_quick_destroy((lock_quick_type*)log_get_lock()); 362 } 363 exit(0); 364 case '1': 365 #ifdef USE_SHA1 366 printf("SHA1 supported\n"); 367 exit(0); 368 #else 369 printf("SHA1 not supported\n"); 370 exit(1); 371 #endif 372 break; 373 case '2': 374 #if (defined(HAVE_EVP_SHA256) || defined(HAVE_NSS) || defined(HAVE_NETTLE)) && defined(USE_SHA2) 375 printf("SHA256 supported\n"); 376 exit(0); 377 #else 378 printf("SHA256 not supported\n"); 379 exit(1); 380 #endif 381 break; 382 case 'e': 383 #if defined(USE_ECDSA) 384 printf("ECDSA supported\n"); 385 exit(0); 386 #else 387 printf("ECDSA not supported\n"); 388 exit(1); 389 #endif 390 break; 391 case 'g': 392 #ifdef USE_GOST 393 if(sldns_key_EVP_load_gost_id()) { 394 printf("GOST supported\n"); 395 exit(0); 396 } else { 397 printf("GOST not supported\n"); 398 exit(1); 399 } 400 #else 401 printf("GOST not supported\n"); 402 exit(1); 403 #endif 404 break; 405 case 'c': 406 #ifdef CLIENT_SUBNET 407 printf("CLIENT_SUBNET supported\n"); 408 exit(0); 409 #else 410 printf("CLIENT_SUBNET not supported\n"); 411 exit(1); 412 #endif 413 break; 414 case 'i': 415 #ifdef USE_IPSECMOD 416 printf("IPSECMOD supported\n"); 417 exit(0); 418 #else 419 printf("IPSECMOD not supported\n"); 420 exit(1); 421 #endif 422 break; 423 case 'p': 424 playback_file = optarg; 425 break; 426 case 'o': 427 add_opts(optarg, &pass_argc, pass_argv); 428 break; 429 case '?': 430 case 'h': 431 default: 432 testbound_usage(); 433 exit(1); 434 } 435 } 436 argc -= optind; 437 /* argv += optind; not using further arguments */ 438 if(argc != 0) { 439 testbound_usage(); 440 exit(1); 441 } 442 log_info("Start of %s testbound program.", PACKAGE_STRING); 443 if(atexit(&remove_configfile) != 0) 444 fatal_exit("atexit() failed: %s", strerror(errno)); 445 446 /* setup test environment */ 447 scen = setup_playback(playback_file, &pass_argc, pass_argv); 448 /* init fake event backend */ 449 fake_event_init(scen); 450 451 pass_argv[pass_argc] = NULL; 452 echo_cmdline(pass_argc, pass_argv); 453 454 /* reset getopt processing */ 455 optind = init_optind; 456 optarg = init_optarg; 457 458 /* run the normal daemon */ 459 res = daemon_main(pass_argc, pass_argv); 460 461 fake_event_cleanup(); 462 for(c=1; c<pass_argc; c++) 463 free(pass_argv[c]); 464 if(res == 0) { 465 log_info("Testbound Exit Success\n"); 466 if(log_get_lock()) { 467 lock_quick_destroy((lock_quick_type*)log_get_lock()); 468 } 469 #ifdef HAVE_PTHREAD 470 /* dlopen frees its thread state (dlopen of gost engine) */ 471 pthread_exit(NULL); 472 #endif 473 } 474 return res; 475 } 476 477 /* fake remote control */ 478 struct listen_port* daemon_remote_open_ports(struct config_file* 479 ATTR_UNUSED(cfg)) 480 { 481 return NULL; 482 } 483 484 struct daemon_remote* daemon_remote_create(struct config_file* ATTR_UNUSED(cfg)) 485 { 486 return (struct daemon_remote*)calloc(1,1); 487 } 488 489 void daemon_remote_delete(struct daemon_remote* rc) 490 { 491 free(rc); 492 } 493 494 void daemon_remote_clear(struct daemon_remote* ATTR_UNUSED(rc)) 495 { 496 /* nothing */ 497 } 498 499 int daemon_remote_open_accept(struct daemon_remote* ATTR_UNUSED(rc), 500 struct listen_port* ATTR_UNUSED(ports), 501 struct worker* ATTR_UNUSED(worker)) 502 { 503 return 1; 504 } 505 506 int remote_accept_callback(struct comm_point* ATTR_UNUSED(c), 507 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error), 508 struct comm_reply* ATTR_UNUSED(repinfo)) 509 { 510 log_assert(0); 511 return 0; 512 } 513 514 int remote_control_callback(struct comm_point* ATTR_UNUSED(c), 515 void* ATTR_UNUSED(arg), int ATTR_UNUSED(error), 516 struct comm_reply* ATTR_UNUSED(repinfo)) 517 { 518 log_assert(0); 519 return 0; 520 } 521 522 void remote_get_opt_ssl(char* ATTR_UNUSED(str), void* ATTR_UNUSED(arg)) 523 { 524 log_assert(0); 525 } 526 527 void wsvc_command_option(const char* ATTR_UNUSED(wopt), 528 const char* ATTR_UNUSED(cfgfile), int ATTR_UNUSED(v), 529 int ATTR_UNUSED(c)) 530 { 531 log_assert(0); 532 } 533 534 void wsvc_setup_worker(struct worker* ATTR_UNUSED(worker)) 535 { 536 /* do nothing */ 537 } 538 539 void wsvc_desetup_worker(struct worker* ATTR_UNUSED(worker)) 540 { 541 /* do nothing */ 542 } 543 544 #ifdef UB_ON_WINDOWS 545 void worker_win_stop_cb(int ATTR_UNUSED(fd), short ATTR_UNUSED(ev), 546 void* ATTR_UNUSED(arg)) 547 { 548 log_assert(0); 549 } 550 551 void wsvc_cron_cb(void* ATTR_UNUSED(arg)) 552 { 553 log_assert(0); 554 } 555 #endif /* UB_ON_WINDOWS */ 556 557