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 /* --------------------------------------------------------------------- 181 * Helper tests for "t_config". 182 * --------------------------------------------------------------------- */ 183 184 ATF_TC(config_unset); 185 ATF_TC_HEAD(config_unset, tc) 186 { 187 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_config test " 188 "program"); 189 } 190 ATF_TC_BODY(config_unset, tc) 191 { 192 ATF_REQUIRE(!atf_tc_has_config_var(tc, "test")); 193 } 194 195 ATF_TC(config_empty); 196 ATF_TC_HEAD(config_empty, tc) 197 { 198 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_config test " 199 "program"); 200 } 201 ATF_TC_BODY(config_empty, tc) 202 { 203 ATF_REQUIRE(atf_tc_has_config_var(tc, "test")); 204 ATF_REQUIRE(strlen(atf_tc_get_config_var(tc, "test")) == 0); 205 } 206 207 ATF_TC(config_value); 208 ATF_TC_HEAD(config_value, tc) 209 { 210 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_config test " 211 "program"); 212 } 213 ATF_TC_BODY(config_value, tc) 214 { 215 ATF_REQUIRE(atf_tc_has_config_var(tc, "test")); 216 ATF_REQUIRE(strcmp(atf_tc_get_config_var(tc, "test"), "foo") == 0); 217 } 218 219 ATF_TC(config_multi_value); 220 ATF_TC_HEAD(config_multi_value, tc) 221 { 222 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_config test " 223 "program"); 224 } 225 ATF_TC_BODY(config_multi_value, tc) 226 { 227 ATF_REQUIRE(atf_tc_has_config_var(tc, "test")); 228 ATF_REQUIRE(strcmp(atf_tc_get_config_var(tc, "test"), "foo bar") == 0); 229 } 230 231 /* --------------------------------------------------------------------- 232 * Helper tests for "t_expect". 233 * --------------------------------------------------------------------- */ 234 235 ATF_TC_WITHOUT_HEAD(expect_pass_and_pass); 236 ATF_TC_BODY(expect_pass_and_pass, tc) 237 { 238 atf_tc_expect_pass(); 239 240 } 241 242 ATF_TC_WITHOUT_HEAD(expect_pass_but_fail_requirement); 243 ATF_TC_BODY(expect_pass_but_fail_requirement, tc) 244 { 245 atf_tc_expect_pass(); 246 atf_tc_fail("Some reason"); 247 } 248 249 ATF_TC_WITHOUT_HEAD(expect_pass_but_fail_check); 250 ATF_TC_BODY(expect_pass_but_fail_check, tc) 251 { 252 atf_tc_expect_pass(); 253 atf_tc_fail_nonfatal("Some reason"); 254 } 255 256 ATF_TC_WITHOUT_HEAD(expect_fail_and_fail_requirement); 257 ATF_TC_BODY(expect_fail_and_fail_requirement, tc) 258 { 259 atf_tc_expect_fail("Fail %s", "reason"); 260 atf_tc_fail("The failure"); 261 atf_tc_expect_pass(); 262 } 263 264 ATF_TC_WITHOUT_HEAD(expect_fail_and_fail_check); 265 ATF_TC_BODY(expect_fail_and_fail_check, tc) 266 { 267 atf_tc_expect_fail("Fail first"); 268 atf_tc_fail_nonfatal("abc"); 269 atf_tc_expect_pass(); 270 271 atf_tc_expect_fail("And fail again"); 272 atf_tc_fail_nonfatal("def"); 273 atf_tc_expect_pass(); 274 } 275 276 ATF_TC_WITHOUT_HEAD(expect_fail_but_pass); 277 ATF_TC_BODY(expect_fail_but_pass, tc) 278 { 279 atf_tc_expect_fail("Fail first"); 280 atf_tc_fail_nonfatal("abc"); 281 atf_tc_expect_pass(); 282 283 atf_tc_expect_fail("Will not fail"); 284 atf_tc_expect_pass(); 285 286 atf_tc_expect_fail("And fail again"); 287 atf_tc_fail_nonfatal("def"); 288 atf_tc_expect_pass(); 289 } 290 291 ATF_TC_WITHOUT_HEAD(expect_exit_any_and_exit); 292 ATF_TC_BODY(expect_exit_any_and_exit, tc) 293 { 294 atf_tc_expect_exit(-1, "Call will exit"); 295 exit(EXIT_SUCCESS); 296 } 297 298 ATF_TC_WITHOUT_HEAD(expect_exit_code_and_exit); 299 ATF_TC_BODY(expect_exit_code_and_exit, tc) 300 { 301 atf_tc_expect_exit(123, "Call will exit"); 302 exit(123); 303 } 304 305 ATF_TC_WITHOUT_HEAD(expect_exit_but_pass); 306 ATF_TC_BODY(expect_exit_but_pass, tc) 307 { 308 atf_tc_expect_exit(-1, "Call won't exit"); 309 } 310 311 ATF_TC_WITHOUT_HEAD(expect_signal_any_and_signal); 312 ATF_TC_BODY(expect_signal_any_and_signal, tc) 313 { 314 atf_tc_expect_signal(-1, "Call will signal"); 315 kill(getpid(), SIGKILL); 316 } 317 318 ATF_TC_WITHOUT_HEAD(expect_signal_no_and_signal); 319 ATF_TC_BODY(expect_signal_no_and_signal, tc) 320 { 321 atf_tc_expect_signal(SIGHUP, "Call will signal"); 322 kill(getpid(), SIGHUP); 323 } 324 325 ATF_TC_WITHOUT_HEAD(expect_signal_but_pass); 326 ATF_TC_BODY(expect_signal_but_pass, tc) 327 { 328 atf_tc_expect_signal(-1, "Call won't signal"); 329 } 330 331 ATF_TC_WITHOUT_HEAD(expect_death_and_exit); 332 ATF_TC_BODY(expect_death_and_exit, tc) 333 { 334 atf_tc_expect_death("Exit case"); 335 exit(123); 336 } 337 338 ATF_TC_WITHOUT_HEAD(expect_death_and_signal); 339 ATF_TC_BODY(expect_death_and_signal, tc) 340 { 341 atf_tc_expect_death("Signal case"); 342 kill(getpid(), SIGKILL); 343 } 344 345 ATF_TC_WITHOUT_HEAD(expect_death_but_pass); 346 ATF_TC_BODY(expect_death_but_pass, tc) 347 { 348 atf_tc_expect_death("Call won't die"); 349 } 350 351 ATF_TC(expect_timeout_and_hang); 352 ATF_TC_HEAD(expect_timeout_and_hang, tc) 353 { 354 atf_tc_set_md_var(tc, "timeout", "1"); 355 } 356 ATF_TC_BODY(expect_timeout_and_hang, tc) 357 { 358 atf_tc_expect_timeout("Will overrun"); 359 sleep(5); 360 } 361 362 ATF_TC(expect_timeout_but_pass); 363 ATF_TC_HEAD(expect_timeout_but_pass, tc) 364 { 365 atf_tc_set_md_var(tc, "timeout", "1"); 366 } 367 ATF_TC_BODY(expect_timeout_but_pass, tc) 368 { 369 atf_tc_expect_timeout("Will just exit"); 370 } 371 372 /* --------------------------------------------------------------------- 373 * Helper tests for "t_meta_data". 374 * --------------------------------------------------------------------- */ 375 376 ATF_TC_WITHOUT_HEAD(metadata_no_descr); 377 ATF_TC_BODY(metadata_no_descr, tc) 378 { 379 } 380 381 ATF_TC_WITHOUT_HEAD(metadata_no_head); 382 ATF_TC_BODY(metadata_no_head, tc) 383 { 384 } 385 386 /* --------------------------------------------------------------------- 387 * Helper tests for "t_srcdir". 388 * --------------------------------------------------------------------- */ 389 390 ATF_TC(srcdir_exists); 391 ATF_TC_HEAD(srcdir_exists, tc) 392 { 393 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_srcdir test " 394 "program"); 395 } 396 ATF_TC_BODY(srcdir_exists, tc) 397 { 398 atf_fs_path_t p; 399 bool b; 400 401 RE(atf_fs_path_init_fmt(&p, "%s/datafile", 402 atf_tc_get_config_var(tc, "srcdir"))); 403 RE(atf_fs_exists(&p, &b)); 404 atf_fs_path_fini(&p); 405 if (!b) 406 atf_tc_fail("Cannot find datafile"); 407 } 408 409 /* --------------------------------------------------------------------- 410 * Helper tests for "t_result". 411 * --------------------------------------------------------------------- */ 412 413 ATF_TC_WITHOUT_HEAD(result_pass); 414 ATF_TC_BODY(result_pass, tc) 415 { 416 printf("msg\n"); 417 } 418 419 ATF_TC_WITHOUT_HEAD(result_fail); 420 ATF_TC_BODY(result_fail, tc) 421 { 422 printf("msg\n"); 423 atf_tc_fail("Failure reason"); 424 } 425 426 ATF_TC_WITHOUT_HEAD(result_skip); 427 ATF_TC_BODY(result_skip, tc) 428 { 429 printf("msg\n"); 430 atf_tc_skip("Skipped reason"); 431 } 432 433 ATF_TC(result_newlines_fail); 434 ATF_TC_HEAD(result_newlines_fail, tc) 435 { 436 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_result test " 437 "program"); 438 } 439 ATF_TC_BODY(result_newlines_fail, tc) 440 { 441 atf_tc_fail("First line\nSecond line"); 442 } 443 444 ATF_TC(result_newlines_skip); 445 ATF_TC_HEAD(result_newlines_skip, tc) 446 { 447 atf_tc_set_md_var(tc, "descr", "Helper test case for the t_result test " 448 "program"); 449 } 450 ATF_TC_BODY(result_newlines_skip, tc) 451 { 452 atf_tc_skip("First line\nSecond line"); 453 } 454 455 /* --------------------------------------------------------------------- 456 * Main. 457 * --------------------------------------------------------------------- */ 458 459 ATF_TP_ADD_TCS(tp) 460 { 461 /* Add helper tests for t_cleanup. */ 462 ATF_TP_ADD_TC(tp, cleanup_pass); 463 ATF_TP_ADD_TC(tp, cleanup_fail); 464 ATF_TP_ADD_TC(tp, cleanup_skip); 465 ATF_TP_ADD_TC(tp, cleanup_curdir); 466 ATF_TP_ADD_TC(tp, cleanup_sigterm); 467 468 /* Add helper tests for t_config. */ 469 ATF_TP_ADD_TC(tp, config_unset); 470 ATF_TP_ADD_TC(tp, config_empty); 471 ATF_TP_ADD_TC(tp, config_value); 472 ATF_TP_ADD_TC(tp, config_multi_value); 473 474 /* Add helper tests for t_expect. */ 475 ATF_TP_ADD_TC(tp, expect_pass_and_pass); 476 ATF_TP_ADD_TC(tp, expect_pass_but_fail_requirement); 477 ATF_TP_ADD_TC(tp, expect_pass_but_fail_check); 478 ATF_TP_ADD_TC(tp, expect_fail_and_fail_requirement); 479 ATF_TP_ADD_TC(tp, expect_fail_and_fail_check); 480 ATF_TP_ADD_TC(tp, expect_fail_but_pass); 481 ATF_TP_ADD_TC(tp, expect_exit_any_and_exit); 482 ATF_TP_ADD_TC(tp, expect_exit_code_and_exit); 483 ATF_TP_ADD_TC(tp, expect_exit_but_pass); 484 ATF_TP_ADD_TC(tp, expect_signal_any_and_signal); 485 ATF_TP_ADD_TC(tp, expect_signal_no_and_signal); 486 ATF_TP_ADD_TC(tp, expect_signal_but_pass); 487 ATF_TP_ADD_TC(tp, expect_death_and_exit); 488 ATF_TP_ADD_TC(tp, expect_death_and_signal); 489 ATF_TP_ADD_TC(tp, expect_death_but_pass); 490 ATF_TP_ADD_TC(tp, expect_timeout_and_hang); 491 ATF_TP_ADD_TC(tp, expect_timeout_but_pass); 492 493 /* Add helper tests for t_meta_data. */ 494 ATF_TP_ADD_TC(tp, metadata_no_descr); 495 ATF_TP_ADD_TC(tp, metadata_no_head); 496 497 /* Add helper tests for t_srcdir. */ 498 ATF_TP_ADD_TC(tp, srcdir_exists); 499 500 /* Add helper tests for t_result. */ 501 ATF_TP_ADD_TC(tp, result_pass); 502 ATF_TP_ADD_TC(tp, result_fail); 503 ATF_TP_ADD_TC(tp, result_skip); 504 ATF_TP_ADD_TC(tp, result_newlines_fail); 505 ATF_TP_ADD_TC(tp, result_newlines_skip); 506 507 return atf_no_error(); 508 } 509