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 extern "C" { 31 #include <signal.h> 32 #include <unistd.h> 33 } 34 35 #include <cstdlib> 36 #include <fstream> 37 #include <iostream> 38 39 #include "atf-c++/macros.hpp" 40 41 #include "atf-c++/detail/fs.hpp" 42 43 // ------------------------------------------------------------------------ 44 // Helper tests for "t_config". 45 // ------------------------------------------------------------------------ 46 47 ATF_TEST_CASE(config_unset); 48 ATF_TEST_CASE_HEAD(config_unset) 49 { 50 set_md_var("descr", "Helper test case for the t_config test program"); 51 } 52 ATF_TEST_CASE_BODY(config_unset) 53 { 54 ATF_REQUIRE(!has_config_var("test")); 55 } 56 57 ATF_TEST_CASE(config_empty); 58 ATF_TEST_CASE_HEAD(config_empty) 59 { 60 set_md_var("descr", "Helper test case for the t_config test program"); 61 } 62 ATF_TEST_CASE_BODY(config_empty) 63 { 64 ATF_REQUIRE_EQ(get_config_var("test"), ""); 65 } 66 67 ATF_TEST_CASE(config_value); 68 ATF_TEST_CASE_HEAD(config_value) 69 { 70 set_md_var("descr", "Helper test case for the t_config test program"); 71 } 72 ATF_TEST_CASE_BODY(config_value) 73 { 74 ATF_REQUIRE_EQ(get_config_var("test"), "foo"); 75 } 76 77 ATF_TEST_CASE(config_multi_value); 78 ATF_TEST_CASE_HEAD(config_multi_value) 79 { 80 set_md_var("descr", "Helper test case for the t_config test program"); 81 } 82 ATF_TEST_CASE_BODY(config_multi_value) 83 { 84 ATF_REQUIRE_EQ(get_config_var("test"), "foo bar"); 85 } 86 87 // ------------------------------------------------------------------------ 88 // Helper tests for "t_expect". 89 // ------------------------------------------------------------------------ 90 91 ATF_TEST_CASE_WITHOUT_HEAD(expect_pass_and_pass); 92 ATF_TEST_CASE_BODY(expect_pass_and_pass) 93 { 94 expect_pass(); 95 } 96 97 ATF_TEST_CASE_WITHOUT_HEAD(expect_pass_but_fail_requirement); 98 ATF_TEST_CASE_BODY(expect_pass_but_fail_requirement) 99 { 100 expect_pass(); 101 fail("Some reason"); 102 } 103 104 ATF_TEST_CASE_WITHOUT_HEAD(expect_pass_but_fail_check); 105 ATF_TEST_CASE_BODY(expect_pass_but_fail_check) 106 { 107 expect_pass(); 108 fail_nonfatal("Some reason"); 109 } 110 111 ATF_TEST_CASE_WITHOUT_HEAD(expect_fail_and_fail_requirement); 112 ATF_TEST_CASE_BODY(expect_fail_and_fail_requirement) 113 { 114 expect_fail("Fail reason"); 115 fail("The failure"); 116 expect_pass(); 117 } 118 119 ATF_TEST_CASE_WITHOUT_HEAD(expect_fail_and_fail_check); 120 ATF_TEST_CASE_BODY(expect_fail_and_fail_check) 121 { 122 expect_fail("Fail first"); 123 fail_nonfatal("abc"); 124 expect_pass(); 125 126 expect_fail("And fail again"); 127 fail_nonfatal("def"); 128 expect_pass(); 129 } 130 131 ATF_TEST_CASE_WITHOUT_HEAD(expect_fail_but_pass); 132 ATF_TEST_CASE_BODY(expect_fail_but_pass) 133 { 134 expect_fail("Fail first"); 135 fail_nonfatal("abc"); 136 expect_pass(); 137 138 expect_fail("Will not fail"); 139 expect_pass(); 140 141 expect_fail("And fail again"); 142 fail_nonfatal("def"); 143 expect_pass(); 144 } 145 146 ATF_TEST_CASE_WITHOUT_HEAD(expect_exit_any_and_exit); 147 ATF_TEST_CASE_BODY(expect_exit_any_and_exit) 148 { 149 expect_exit(-1, "Call will exit"); 150 std::exit(EXIT_SUCCESS); 151 } 152 153 ATF_TEST_CASE_WITHOUT_HEAD(expect_exit_code_and_exit); 154 ATF_TEST_CASE_BODY(expect_exit_code_and_exit) 155 { 156 expect_exit(123, "Call will exit"); 157 std::exit(123); 158 } 159 160 ATF_TEST_CASE_WITHOUT_HEAD(expect_exit_but_pass); 161 ATF_TEST_CASE_BODY(expect_exit_but_pass) 162 { 163 expect_exit(-1, "Call won't exit"); 164 } 165 166 ATF_TEST_CASE_WITHOUT_HEAD(expect_signal_any_and_signal); 167 ATF_TEST_CASE_BODY(expect_signal_any_and_signal) 168 { 169 expect_signal(-1, "Call will signal"); 170 ::kill(getpid(), SIGKILL); 171 } 172 173 ATF_TEST_CASE_WITHOUT_HEAD(expect_signal_no_and_signal); 174 ATF_TEST_CASE_BODY(expect_signal_no_and_signal) 175 { 176 expect_signal(SIGHUP, "Call will signal"); 177 ::kill(getpid(), SIGHUP); 178 } 179 180 ATF_TEST_CASE_WITHOUT_HEAD(expect_signal_but_pass); 181 ATF_TEST_CASE_BODY(expect_signal_but_pass) 182 { 183 expect_signal(-1, "Call won't signal"); 184 } 185 186 ATF_TEST_CASE_WITHOUT_HEAD(expect_death_and_exit); 187 ATF_TEST_CASE_BODY(expect_death_and_exit) 188 { 189 expect_death("Exit case"); 190 std::exit(123); 191 } 192 193 ATF_TEST_CASE_WITHOUT_HEAD(expect_death_and_signal); 194 ATF_TEST_CASE_BODY(expect_death_and_signal) 195 { 196 expect_death("Signal case"); 197 kill(getpid(), SIGKILL); 198 } 199 200 ATF_TEST_CASE_WITHOUT_HEAD(expect_death_but_pass); 201 ATF_TEST_CASE_BODY(expect_death_but_pass) 202 { 203 expect_death("Call won't die"); 204 } 205 206 ATF_TEST_CASE(expect_timeout_and_hang); 207 ATF_TEST_CASE_HEAD(expect_timeout_and_hang) 208 { 209 set_md_var("timeout", "1"); 210 } 211 ATF_TEST_CASE_BODY(expect_timeout_and_hang) 212 { 213 expect_timeout("Will overrun"); 214 ::sleep(5); 215 } 216 217 ATF_TEST_CASE(expect_timeout_but_pass); 218 ATF_TEST_CASE_HEAD(expect_timeout_but_pass) 219 { 220 set_md_var("timeout", "1"); 221 } 222 ATF_TEST_CASE_BODY(expect_timeout_but_pass) 223 { 224 expect_timeout("Will just exit"); 225 } 226 227 // ------------------------------------------------------------------------ 228 // Helper tests for "t_fork". 229 // ------------------------------------------------------------------------ 230 231 ATF_TEST_CASE(fork_stop); 232 ATF_TEST_CASE_HEAD(fork_stop) 233 { 234 set_md_var("descr", "Helper test case for the t_fork test program"); 235 } 236 ATF_TEST_CASE_BODY(fork_stop) 237 { 238 std::ofstream os(get_config_var("pidfile").c_str()); 239 os << ::getpid() << "\n"; 240 os.close(); 241 std::cout << "Wrote pid file\n"; 242 std::cout << "Waiting for done file\n"; 243 while (::access(get_config_var("donefile").c_str(), F_OK) != 0) 244 ::usleep(10000); 245 std::cout << "Exiting\n"; 246 } 247 248 // ------------------------------------------------------------------------ 249 // Helper tests for "t_meta_data". 250 // ------------------------------------------------------------------------ 251 252 ATF_TEST_CASE(metadata_no_descr); 253 ATF_TEST_CASE_HEAD(metadata_no_descr) 254 { 255 } 256 ATF_TEST_CASE_BODY(metadata_no_descr) 257 { 258 } 259 260 ATF_TEST_CASE_WITHOUT_HEAD(metadata_no_head); 261 ATF_TEST_CASE_BODY(metadata_no_head) 262 { 263 } 264 265 // ------------------------------------------------------------------------ 266 // Helper tests for "t_srcdir". 267 // ------------------------------------------------------------------------ 268 269 ATF_TEST_CASE(srcdir_exists); 270 ATF_TEST_CASE_HEAD(srcdir_exists) 271 { 272 set_md_var("descr", "Helper test case for the t_srcdir test program"); 273 } 274 ATF_TEST_CASE_BODY(srcdir_exists) 275 { 276 if (!atf::fs::exists(atf::fs::path(get_config_var("srcdir")) / 277 "datafile")) 278 ATF_FAIL("Cannot find datafile"); 279 } 280 281 // ------------------------------------------------------------------------ 282 // Helper tests for "t_result". 283 // ------------------------------------------------------------------------ 284 285 ATF_TEST_CASE(result_pass); 286 ATF_TEST_CASE_HEAD(result_pass) { } 287 ATF_TEST_CASE_BODY(result_pass) 288 { 289 std::cout << "msg\n"; 290 } 291 292 ATF_TEST_CASE(result_fail); 293 ATF_TEST_CASE_HEAD(result_fail) { } 294 ATF_TEST_CASE_BODY(result_fail) 295 { 296 std::cout << "msg\n"; 297 ATF_FAIL("Failure reason"); 298 } 299 300 ATF_TEST_CASE(result_skip); 301 ATF_TEST_CASE_HEAD(result_skip) { } 302 ATF_TEST_CASE_BODY(result_skip) 303 { 304 std::cout << "msg\n"; 305 ATF_SKIP("Skipped reason"); 306 } 307 308 ATF_TEST_CASE(result_newlines_fail); 309 ATF_TEST_CASE_HEAD(result_newlines_fail) 310 { 311 set_md_var("descr", "Helper test case for the t_result test program"); 312 } 313 ATF_TEST_CASE_BODY(result_newlines_fail) 314 { 315 ATF_FAIL("First line\nSecond line"); 316 } 317 318 ATF_TEST_CASE(result_newlines_skip); 319 ATF_TEST_CASE_HEAD(result_newlines_skip) 320 { 321 set_md_var("descr", "Helper test case for the t_result test program"); 322 } 323 ATF_TEST_CASE_BODY(result_newlines_skip) 324 { 325 ATF_SKIP("First line\nSecond line"); 326 } 327 328 ATF_TEST_CASE(result_exception); 329 ATF_TEST_CASE_HEAD(result_exception) { } 330 ATF_TEST_CASE_BODY(result_exception) 331 { 332 throw std::runtime_error("This is unhandled"); 333 } 334 335 // ------------------------------------------------------------------------ 336 // Main. 337 // ------------------------------------------------------------------------ 338 339 ATF_INIT_TEST_CASES(tcs) 340 { 341 // Add helper tests for t_config. 342 ATF_ADD_TEST_CASE(tcs, config_unset); 343 ATF_ADD_TEST_CASE(tcs, config_empty); 344 ATF_ADD_TEST_CASE(tcs, config_value); 345 ATF_ADD_TEST_CASE(tcs, config_multi_value); 346 347 // Add helper tests for t_expect. 348 ATF_ADD_TEST_CASE(tcs, expect_pass_and_pass); 349 ATF_ADD_TEST_CASE(tcs, expect_pass_but_fail_requirement); 350 ATF_ADD_TEST_CASE(tcs, expect_pass_but_fail_check); 351 ATF_ADD_TEST_CASE(tcs, expect_fail_and_fail_requirement); 352 ATF_ADD_TEST_CASE(tcs, expect_fail_and_fail_check); 353 ATF_ADD_TEST_CASE(tcs, expect_fail_but_pass); 354 ATF_ADD_TEST_CASE(tcs, expect_exit_any_and_exit); 355 ATF_ADD_TEST_CASE(tcs, expect_exit_code_and_exit); 356 ATF_ADD_TEST_CASE(tcs, expect_exit_but_pass); 357 ATF_ADD_TEST_CASE(tcs, expect_signal_any_and_signal); 358 ATF_ADD_TEST_CASE(tcs, expect_signal_no_and_signal); 359 ATF_ADD_TEST_CASE(tcs, expect_signal_but_pass); 360 ATF_ADD_TEST_CASE(tcs, expect_death_and_exit); 361 ATF_ADD_TEST_CASE(tcs, expect_death_and_signal); 362 ATF_ADD_TEST_CASE(tcs, expect_death_but_pass); 363 ATF_ADD_TEST_CASE(tcs, expect_timeout_and_hang); 364 ATF_ADD_TEST_CASE(tcs, expect_timeout_but_pass); 365 366 // Add helper tests for t_fork. 367 ATF_ADD_TEST_CASE(tcs, fork_stop); 368 369 // Add helper tests for t_meta_data. 370 ATF_ADD_TEST_CASE(tcs, metadata_no_descr); 371 ATF_ADD_TEST_CASE(tcs, metadata_no_head); 372 373 // Add helper tests for t_srcdir. 374 ATF_ADD_TEST_CASE(tcs, srcdir_exists); 375 376 // Add helper tests for t_result. 377 ATF_ADD_TEST_CASE(tcs, result_pass); 378 ATF_ADD_TEST_CASE(tcs, result_fail); 379 ATF_ADD_TEST_CASE(tcs, result_skip); 380 ATF_ADD_TEST_CASE(tcs, result_newlines_fail); 381 ATF_ADD_TEST_CASE(tcs, result_newlines_skip); 382 ATF_ADD_TEST_CASE(tcs, result_exception); 383 } 384