1 /* 2 * Automated Testing Framework (atf) 3 * 4 * Copyright (c) 2007 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND 17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY 21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/types.h> 31 #include <sys/wait.h> 32 #include <fcntl.h> 33 #include <unistd.h> 34 35 #include <signal.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 40 #include <atf-c.h> 41 42 #include "atf-c/error.h" 43 44 #include "atf-c/detail/env.h" 45 #include "atf-c/detail/fs.h" 46 #include "atf-c/detail/test_helpers.h" 47 #include "atf-c/detail/text.h" 48 49 /* --------------------------------------------------------------------- 50 * Auxiliary functions. 51 * --------------------------------------------------------------------- */ 52 53 static 54 void 55 safe_remove(const char* path) 56 { 57 if (unlink(path) == -1) 58 atf_tc_fail("unlink(2) of %s failed", path); 59 } 60 61 static 62 void 63 touch(const char *path) 64 { 65 int fd; 66 fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644); 67 if (fd == -1) 68 atf_tc_fail("Could not create file %s", path); 69 close(fd); 70 } 71 72 /* --------------------------------------------------------------------- 73 * Helper tests for "t_cleanup". 74 * --------------------------------------------------------------------- */ 75 76 ATF_TC_WITH_CLEANUP(cleanup_pass); 77 ATF_TC_HEAD(cleanup_pass, tc) 78 { 79 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_cleanup test " 80 "program"); 81 } 82 ATF_TC_BODY(cleanup_pass, tc) 83 { 84 touch(atf_tc_get_config_var(tc, "tmpfile")); 85 } 86 ATF_TC_CLEANUP(cleanup_pass, tc) 87 { 88 if (atf_tc_get_config_var_as_bool(tc, "cleanup")) 89 safe_remove(atf_tc_get_config_var(tc, "tmpfile")); 90 } 91 92 ATF_TC_WITH_CLEANUP(cleanup_fail); 93 ATF_TC_HEAD(cleanup_fail, tc) 94 { 95 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_cleanup test " 96 "program"); 97 } 98 ATF_TC_BODY(cleanup_fail, tc) 99 { 100 touch(atf_tc_get_config_var(tc, "tmpfile")); 101 atf_tc_fail("On purpose"); 102 } 103 ATF_TC_CLEANUP(cleanup_fail, tc) 104 { 105 if (atf_tc_get_config_var_as_bool(tc, "cleanup")) 106 safe_remove(atf_tc_get_config_var(tc, "tmpfile")); 107 } 108 109 ATF_TC_WITH_CLEANUP(cleanup_skip); 110 ATF_TC_HEAD(cleanup_skip, tc) 111 { 112 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_cleanup test " 113 "program"); 114 } 115 ATF_TC_BODY(cleanup_skip, tc) 116 { 117 touch(atf_tc_get_config_var(tc, "tmpfile")); 118 atf_tc_skip("On purpose"); 119 } 120 ATF_TC_CLEANUP(cleanup_skip, tc) 121 { 122 if (atf_tc_get_config_var_as_bool(tc, "cleanup")) 123 safe_remove(atf_tc_get_config_var(tc, "tmpfile")); 124 } 125 126 ATF_TC_WITH_CLEANUP(cleanup_curdir); 127 ATF_TC_HEAD(cleanup_curdir, tc) 128 { 129 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_cleanup test " 130 "program"); 131 } 132 ATF_TC_BODY(cleanup_curdir, tc) 133 { 134 FILE *f; 135 136 f = fopen("oldvalue", "w"); 137 if (f == NULL) 138 atf_tc_fail("Failed to create oldvalue file"); 139 fprintf(f, "1234"); 140 fclose(f); 141 } 142 ATF_TC_CLEANUP(cleanup_curdir, tc) 143 { 144 FILE *f; 145 146 f = fopen("oldvalue", "r"); 147 if (f != NULL) { 148 int i; 149 if (fscanf(f, "%d", &i) != 1) 150 fprintf(stderr, "Failed to read old value\n"); 151 else 152 printf("Old value: %d", i); 153 fclose(f); 154 } 155 } 156 157 ATF_TC_WITH_CLEANUP(cleanup_sigterm); 158 ATF_TC_HEAD(cleanup_sigterm, tc) 159 { 160 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_cleanup test " 161 "program"); 162 } 163 ATF_TC_BODY(cleanup_sigterm, tc) 164 { 165 char *nofile; 166 167 touch(atf_tc_get_config_var(tc, "tmpfile")); 168 kill(getpid(), SIGTERM); 169 170 RE(atf_text_format(&nofile, "%s.no", 171 atf_tc_get_config_var(tc, "tmpfile"))); 172 touch(nofile); 173 free(nofile); 174 } 175 ATF_TC_CLEANUP(cleanup_sigterm, tc) 176 { 177 safe_remove(atf_tc_get_config_var(tc, "tmpfile")); 178 } 179 180 ATF_TC_WITH_CLEANUP(cleanup_fork); 181 ATF_TC_HEAD(cleanup_fork, tc) 182 { 183 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_cleanup test " 184 "program"); 185 } 186 ATF_TC_BODY(cleanup_fork, tc) 187 { 188 } 189 ATF_TC_CLEANUP(cleanup_fork, tc) 190 { 191 close(STDOUT_FILENO); 192 close(STDERR_FILENO); 193 close(3); 194 } 195 196 /* --------------------------------------------------------------------- 197 * Helper tests for "t_config". 198 * --------------------------------------------------------------------- */ 199 200 ATF_TC(config_unset); 201 ATF_TC_HEAD(config_unset, tc) 202 { 203 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_config test " 204 "program"); 205 } 206 ATF_TC_BODY(config_unset, tc) 207 { 208 ATF_REQUIRE(!atf_tc_has_config_var(tc, "test")); 209 } 210 211 ATF_TC(config_empty); 212 ATF_TC_HEAD(config_empty, tc) 213 { 214 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_config test " 215 "program"); 216 } 217 ATF_TC_BODY(config_empty, tc) 218 { 219 ATF_REQUIRE(atf_tc_has_config_var(tc, "test")); 220 ATF_REQUIRE(strlen(atf_tc_get_config_var(tc, "test")) == 0); 221 } 222 223 ATF_TC(config_value); 224 ATF_TC_HEAD(config_value, tc) 225 { 226 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_config test " 227 "program"); 228 } 229 ATF_TC_BODY(config_value, tc) 230 { 231 ATF_REQUIRE(atf_tc_has_config_var(tc, "test")); 232 ATF_REQUIRE(strcmp(atf_tc_get_config_var(tc, "test"), "foo") == 0); 233 } 234 235 ATF_TC(config_multi_value); 236 ATF_TC_HEAD(config_multi_value, tc) 237 { 238 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_config test " 239 "program"); 240 } 241 ATF_TC_BODY(config_multi_value, tc) 242 { 243 ATF_REQUIRE(atf_tc_has_config_var(tc, "test")); 244 ATF_REQUIRE(strcmp(atf_tc_get_config_var(tc, "test"), "foo bar") == 0); 245 } 246 247 /* --------------------------------------------------------------------- 248 * Helper tests for "t_expect". 249 * --------------------------------------------------------------------- */ 250 251 ATF_TC_WITHOUT_HEAD(expect_pass_and_pass); 252 ATF_TC_BODY(expect_pass_and_pass, tc) 253 { 254 atf_tc_expect_pass(); 255 256 } 257 258 ATF_TC_WITHOUT_HEAD(expect_pass_but_fail_requirement); 259 ATF_TC_BODY(expect_pass_but_fail_requirement, tc) 260 { 261 atf_tc_expect_pass(); 262 atf_tc_fail("Some reason"); 263 } 264 265 ATF_TC_WITHOUT_HEAD(expect_pass_but_fail_check); 266 ATF_TC_BODY(expect_pass_but_fail_check, tc) 267 { 268 atf_tc_expect_pass(); 269 atf_tc_fail_nonfatal("Some reason"); 270 } 271 272 ATF_TC_WITHOUT_HEAD(expect_fail_and_fail_requirement); 273 ATF_TC_BODY(expect_fail_and_fail_requirement, tc) 274 { 275 atf_tc_expect_fail("Fail %s", "reason"); 276 atf_tc_fail("The failure"); 277 atf_tc_expect_pass(); 278 } 279 280 ATF_TC_WITHOUT_HEAD(expect_fail_and_fail_check); 281 ATF_TC_BODY(expect_fail_and_fail_check, tc) 282 { 283 atf_tc_expect_fail("Fail first"); 284 atf_tc_fail_nonfatal("abc"); 285 atf_tc_expect_pass(); 286 287 atf_tc_expect_fail("And fail again"); 288 atf_tc_fail_nonfatal("def"); 289 atf_tc_expect_pass(); 290 } 291 292 ATF_TC_WITHOUT_HEAD(expect_fail_but_pass); 293 ATF_TC_BODY(expect_fail_but_pass, tc) 294 { 295 atf_tc_expect_fail("Fail first"); 296 atf_tc_fail_nonfatal("abc"); 297 atf_tc_expect_pass(); 298 299 atf_tc_expect_fail("Will not fail"); 300 atf_tc_expect_pass(); 301 302 atf_tc_expect_fail("And fail again"); 303 atf_tc_fail_nonfatal("def"); 304 atf_tc_expect_pass(); 305 } 306 307 ATF_TC_WITHOUT_HEAD(expect_exit_any_and_exit); 308 ATF_TC_BODY(expect_exit_any_and_exit, tc) 309 { 310 atf_tc_expect_exit(-1, "Call will exit"); 311 exit(EXIT_SUCCESS); 312 } 313 314 ATF_TC_WITHOUT_HEAD(expect_exit_code_and_exit); 315 ATF_TC_BODY(expect_exit_code_and_exit, tc) 316 { 317 atf_tc_expect_exit(123, "Call will exit"); 318 exit(123); 319 } 320 321 ATF_TC_WITHOUT_HEAD(expect_exit_but_pass); 322 ATF_TC_BODY(expect_exit_but_pass, tc) 323 { 324 atf_tc_expect_exit(-1, "Call won't exit"); 325 } 326 327 ATF_TC_WITHOUT_HEAD(expect_signal_any_and_signal); 328 ATF_TC_BODY(expect_signal_any_and_signal, tc) 329 { 330 atf_tc_expect_signal(-1, "Call will signal"); 331 kill(getpid(), SIGKILL); 332 } 333 334 ATF_TC_WITHOUT_HEAD(expect_signal_no_and_signal); 335 ATF_TC_BODY(expect_signal_no_and_signal, tc) 336 { 337 atf_tc_expect_signal(SIGHUP, "Call will signal"); 338 kill(getpid(), SIGHUP); 339 } 340 341 ATF_TC_WITHOUT_HEAD(expect_signal_but_pass); 342 ATF_TC_BODY(expect_signal_but_pass, tc) 343 { 344 atf_tc_expect_signal(-1, "Call won't signal"); 345 } 346 347 ATF_TC_WITHOUT_HEAD(expect_death_and_exit); 348 ATF_TC_BODY(expect_death_and_exit, tc) 349 { 350 atf_tc_expect_death("Exit case"); 351 exit(123); 352 } 353 354 ATF_TC_WITHOUT_HEAD(expect_death_and_signal); 355 ATF_TC_BODY(expect_death_and_signal, tc) 356 { 357 atf_tc_expect_death("Signal case"); 358 kill(getpid(), SIGKILL); 359 } 360 361 ATF_TC_WITHOUT_HEAD(expect_death_but_pass); 362 ATF_TC_BODY(expect_death_but_pass, tc) 363 { 364 atf_tc_expect_death("Call won't die"); 365 } 366 367 ATF_TC(expect_timeout_and_hang); 368 ATF_TC_HEAD(expect_timeout_and_hang, tc) 369 { 370 atf_tc_set_md_var(tc, "timeout", "1"); 371 } 372 ATF_TC_BODY(expect_timeout_and_hang, tc) 373 { 374 atf_tc_expect_timeout("Will overrun"); 375 sleep(5); 376 } 377 378 ATF_TC(expect_timeout_but_pass); 379 ATF_TC_HEAD(expect_timeout_but_pass, tc) 380 { 381 atf_tc_set_md_var(tc, "timeout", "1"); 382 } 383 ATF_TC_BODY(expect_timeout_but_pass, tc) 384 { 385 atf_tc_expect_timeout("Will just exit"); 386 } 387 388 /* --------------------------------------------------------------------- 389 * Helper tests for "t_fork". 390 * --------------------------------------------------------------------- */ 391 392 ATF_TC(fork_stop); 393 ATF_TC_HEAD(fork_stop, tc) 394 { 395 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_fork test " 396 "program"); 397 } 398 ATF_TC_BODY(fork_stop, tc) 399 { 400 FILE *f; 401 const char *dfstr, *pfstr; 402 403 dfstr = atf_tc_get_config_var(tc, "donefile"); 404 pfstr = atf_tc_get_config_var(tc, "pidfile"); 405 406 f = fopen(pfstr, "w"); 407 if (f == NULL) 408 atf_tc_fail("Failed to create pidfile %s", pfstr); 409 fprintf(f, "%d", (int)getpid()); 410 fclose(f); 411 printf("Wrote pid file\n"); 412 413 printf("Waiting for done file\n"); 414 while (access(dfstr, F_OK) != 0) 415 usleep(10000); 416 printf("Exiting\n"); 417 } 418 419 /* --------------------------------------------------------------------- 420 * Helper tests for "t_meta_data". 421 * --------------------------------------------------------------------- */ 422 423 ATF_TC_WITHOUT_HEAD(metadata_no_descr); 424 ATF_TC_BODY(metadata_no_descr, tc) 425 { 426 } 427 428 ATF_TC_WITHOUT_HEAD(metadata_no_head); 429 ATF_TC_BODY(metadata_no_head, tc) 430 { 431 } 432 433 /* --------------------------------------------------------------------- 434 * Helper tests for "t_srcdir". 435 * --------------------------------------------------------------------- */ 436 437 ATF_TC(srcdir_exists); 438 ATF_TC_HEAD(srcdir_exists, tc) 439 { 440 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_srcdir test " 441 "program"); 442 } 443 ATF_TC_BODY(srcdir_exists, tc) 444 { 445 atf_fs_path_t p; 446 bool b; 447 448 RE(atf_fs_path_init_fmt(&p, "%s/datafile", 449 atf_tc_get_config_var(tc, "srcdir"))); 450 RE(atf_fs_exists(&p, &b)); 451 atf_fs_path_fini(&p); 452 if (!b) 453 atf_tc_fail("Cannot find datafile"); 454 } 455 456 /* --------------------------------------------------------------------- 457 * Helper tests for "t_result". 458 * --------------------------------------------------------------------- */ 459 460 ATF_TC_WITHOUT_HEAD(result_pass); 461 ATF_TC_BODY(result_pass, tc) 462 { 463 printf("msg\n"); 464 } 465 466 ATF_TC_WITHOUT_HEAD(result_fail); 467 ATF_TC_BODY(result_fail, tc) 468 { 469 printf("msg\n"); 470 atf_tc_fail("Failure reason"); 471 } 472 473 ATF_TC_WITHOUT_HEAD(result_skip); 474 ATF_TC_BODY(result_skip, tc) 475 { 476 printf("msg\n"); 477 atf_tc_skip("Skipped reason"); 478 } 479 480 ATF_TC(result_newlines_fail); 481 ATF_TC_HEAD(result_newlines_fail, tc) 482 { 483 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_result test " 484 "program"); 485 } 486 ATF_TC_BODY(result_newlines_fail, tc) 487 { 488 atf_tc_fail("First line\nSecond line"); 489 } 490 491 ATF_TC(result_newlines_skip); 492 ATF_TC_HEAD(result_newlines_skip, tc) 493 { 494 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_result test " 495 "program"); 496 } 497 ATF_TC_BODY(result_newlines_skip, tc) 498 { 499 atf_tc_skip("First line\nSecond line"); 500 } 501 502 /* --------------------------------------------------------------------- 503 * Main. 504 * --------------------------------------------------------------------- */ 505 506 ATF_TP_ADD_TCS(tp) 507 { 508 /* Add helper tests for t_cleanup. */ 509 ATF_TP_ADD_TC(tp, cleanup_pass); 510 ATF_TP_ADD_TC(tp, cleanup_fail); 511 ATF_TP_ADD_TC(tp, cleanup_skip); 512 ATF_TP_ADD_TC(tp, cleanup_curdir); 513 ATF_TP_ADD_TC(tp, cleanup_sigterm); 514 ATF_TP_ADD_TC(tp, cleanup_fork); 515 516 /* Add helper tests for t_config. */ 517 ATF_TP_ADD_TC(tp, config_unset); 518 ATF_TP_ADD_TC(tp, config_empty); 519 ATF_TP_ADD_TC(tp, config_value); 520 ATF_TP_ADD_TC(tp, config_multi_value); 521 522 /* Add helper tests for t_expect. */ 523 ATF_TP_ADD_TC(tp, expect_pass_and_pass); 524 ATF_TP_ADD_TC(tp, expect_pass_but_fail_requirement); 525 ATF_TP_ADD_TC(tp, expect_pass_but_fail_check); 526 ATF_TP_ADD_TC(tp, expect_fail_and_fail_requirement); 527 ATF_TP_ADD_TC(tp, expect_fail_and_fail_check); 528 ATF_TP_ADD_TC(tp, expect_fail_but_pass); 529 ATF_TP_ADD_TC(tp, expect_exit_any_and_exit); 530 ATF_TP_ADD_TC(tp, expect_exit_code_and_exit); 531 ATF_TP_ADD_TC(tp, expect_exit_but_pass); 532 ATF_TP_ADD_TC(tp, expect_signal_any_and_signal); 533 ATF_TP_ADD_TC(tp, expect_signal_no_and_signal); 534 ATF_TP_ADD_TC(tp, expect_signal_but_pass); 535 ATF_TP_ADD_TC(tp, expect_death_and_exit); 536 ATF_TP_ADD_TC(tp, expect_death_and_signal); 537 ATF_TP_ADD_TC(tp, expect_death_but_pass); 538 ATF_TP_ADD_TC(tp, expect_timeout_and_hang); 539 ATF_TP_ADD_TC(tp, expect_timeout_but_pass); 540 541 /* Add helper tests for t_fork. */ 542 ATF_TP_ADD_TC(tp, fork_stop); 543 544 /* Add helper tests for t_meta_data. */ 545 ATF_TP_ADD_TC(tp, metadata_no_descr); 546 ATF_TP_ADD_TC(tp, metadata_no_head); 547 548 /* Add helper tests for t_srcdir. */ 549 ATF_TP_ADD_TC(tp, srcdir_exists); 550 551 /* Add helper tests for t_result. */ 552 ATF_TP_ADD_TC(tp, result_pass); 553 ATF_TP_ADD_TC(tp, result_fail); 554 ATF_TP_ADD_TC(tp, result_skip); 555 ATF_TP_ADD_TC(tp, result_newlines_fail); 556 ATF_TP_ADD_TC(tp, result_newlines_skip); 557 558 return atf_no_error(); 559 } 560