1# 2# Automated Testing Framework (atf) 3# 4# Copyright (c) 2008 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# The Atf_Check and Atf-Shell variables are set by atf-sh. 31 32h_pass() 33{ 34 cmd="$1"; shift 35 36 echo "Running [atf-check $*] against [${cmd}]" 37 38 cat >script.sh <<EOF 39#! ${Atf_Shell} 40${cmd} 41EOF 42 chmod +x script.sh 43 44 if ! ${Atf_Check} "${@}" ./script.sh >tmp; then 45 cat tmp 46 atf_fail "atf-check failed" 47 fi 48} 49 50h_fail() 51{ 52 cmd="$1"; shift 53 54 echo "Running [atf-check $*] against [${cmd}]" 55 56 cat >script.sh <<EOF 57#! ${Atf_Shell} 58${cmd} 59EOF 60 chmod +x script.sh 61 62 if ${Atf_Check} "${@}" ./script.sh 2>tmp; then 63 cat tmp 64 atf_fail "atf-check succeeded but should fail" 65 fi 66} 67 68atf_test_case sflag_eq_ne 69sflag_eq_ne_head() 70{ 71 atf_set "descr" "Tests for the -s option using the 'eq' and 'ne' qualifiers" 72} 73sflag_eq_ne_body() 74{ 75 h_pass "true" -s eq:0 76 h_pass "false" -s ne:0 77 h_pass "exit 255" -s eq:255 78 h_pass "exit 0" -s ne:255 79 80 h_fail "exit 256" -s eq:256 81 h_fail "exit -1" -s eq:-1 82 h_fail "true" -s ne:256 83 h_fail "true" -s ne:-1 84} 85 86atf_test_case sflag_exit 87sflag_exit_head() 88{ 89 atf_set "descr" "Tests for the -s option using the 'exit' qualifier" 90} 91sflag_exit_body() 92{ 93 h_pass 'true' -s exit:0 94 h_pass 'false' -s not-exit:0 95 h_pass 'exit 255' -s exit:255 96 h_pass 'exit 0' -s not-exit:255 97 98 h_fail 'exit 256' -s exit:256 99 h_fail 'exit -1' -s exit:-1 100 h_fail 'true' -s not-exit:256 101 h_fail 'true' -s not-exit:-1 102 103 h_pass 'true' -s exit 104 h_pass 'false' -s exit 105 if ${Atf_Check} -s exit -x 'kill $$'; then 106 atf_fail "Signal detected as clean exit" 107 fi 108} 109 110atf_test_case sflag_ignore 111sflag_ignore_head() 112{ 113 atf_set "descr" "Tests for the -s option using the 'ignore' qualifier" 114} 115sflag_ignore_body() 116{ 117 h_pass 'true' -s ignore 118 h_pass 'false' -s ignore 119 if ${Atf_Check} -s ignored -x 'kill $$'; then 120 atf_fail "Signal not ignored" 121 fi 122} 123 124atf_test_case sflag_signal 125sflag_signal_head() 126{ 127 atf_set "descr" "Tests for the -s option using the 'signal' qualifier" 128} 129sflag_signal_body() 130{ 131 ${Atf_Check} -s signal:hup -x 'kill -1 $$' || atf_fail "Signal not detected" 132 ${Atf_Check} -s signal:sighup -x 'kill -1 $$' || atf_fail "Signal not" \ 133 "detected" 134 ${Atf_Check} -s signal:1 -x 'kill -1 $$' || atf_fail "Signal not detected" 135 ${Atf_Check} -s signal -x 'kill -1 $$' || atf_fail "Signal not detected" 136 137 ${Atf_Check} -s not-signal:kill -x 'kill -9 $$' && \ 138 atf_fail "not-signal:kill matched kill -9" 139 ${Atf_Check} -s not-signal:kill -x 'kill -1 $$' || \ 140 atf_fail "not-signal:kill did not match kill -1" 141 142 h_fail 'true' -s signal 143 h_fail 'false' -s signal 144} 145 146atf_test_case xflag 147xflag_head() 148{ 149 atf_set "descr" "Tests for the -x option" 150} 151xflag_body() 152{ 153 ${Atf_Check} -s ne:0 -o ignore -e ignore "echo foo 2>&1" || \ 154 atf_fail "Shell command succeeded without -x" 155 156 ${Atf_Check} -e inline:"foo\n" -x "echo foo 1>&2" || \ 157 atf_fail "Cannot run command with -x" 158 159 ${Atf_Check} -o inline:"foo\n" -x echo foo || \ 160 atf_fail "Using -x does not respect all provided arguments" 161} 162 163atf_test_case oflag_empty 164oflag_empty_head() 165{ 166 atf_set "descr" "Tests for the -o option using the 'empty' argument" 167} 168oflag_empty_body() 169{ 170 h_pass "true" -o empty 171 h_fail "echo foo" -o empty 172} 173 174atf_test_case oflag_ignore 175oflag_ignore_head() 176{ 177 atf_set "descr" "Tests for the -o option using the 'ignore' argument" 178} 179oflag_ignore_body() 180{ 181 h_pass "true" -o ignore 182 h_pass "echo foo" -o ignore 183} 184 185atf_test_case oflag_file 186oflag_file_head() 187{ 188 atf_set "descr" "Tests for the -o option using the 'file:' argument" 189} 190oflag_file_body() 191{ 192 touch empty 193 h_pass "true" -o file:empty 194 195 echo foo >text 196 h_pass "echo foo" -o file:text 197 h_fail "echo bar" -o file:text 198 199 dd if=/dev/urandom of=bin bs=1k count=10 200 h_pass "cat bin" -o file:bin 201} 202 203atf_test_case oflag_inline 204oflag_inline_head() 205{ 206 atf_set "descr" "Tests for the -o option using the 'inline:' argument" 207} 208oflag_inline_body() 209{ 210 h_pass "true" -o inline: 211 h_pass "echo foo bar" -o inline:"foo bar\n" 212 h_pass "printf 'foo bar'" -o inline:"foo bar" 213 h_pass "printf '\t\n\t\n'" -o inline:"\t\n\t\n" 214 # XXX Ugly hack to workaround the lack of \e in FreeBSD. Also, \e doesn't 215 # seem to work as expected in Linux. Look for a nicer solution. 216 case $(uname) in 217 Darwin|FreeBSD|Linux) 218 h_pass "printf '\a\b\f\n\r\t\v'" -o inline:"\a\b\f\n\r\t\v" 219 ;; 220 *) 221 h_pass "printf '\a\b\e\f\n\r\t\v'" -o inline:"\a\b\e\f\n\r\t\v" 222 ;; 223 esac 224 h_pass "printf '\011\022\033\012'" -o inline:"\011\022\033\012" 225 226 h_fail "echo foo bar" -o inline:"foo bar" 227 h_fail "echo -n foo bar" -o inline:"foo bar\n" 228} 229 230atf_test_case oflag_match 231oflag_match_head() 232{ 233 atf_set "descr" "Tests for the -o option using the 'match:' argument" 234} 235oflag_match_body() 236{ 237 h_pass "printf no-newline" -o "match:^no-newline" 238 h_pass "echo line1; echo foo bar" -o "match:^foo" 239 h_pass "echo foo bar" -o "match:o b" 240 h_fail "echo foo bar" -o "match:baz" 241 h_fail "echo foo bar" -o "match:^bar" 242} 243 244atf_test_case oflag_save 245oflag_save_head() 246{ 247 atf_set "descr" "Tests for the -o option using the 'save:' argument" 248} 249oflag_save_body() 250{ 251 h_pass "echo foo" -o save:out 252 echo foo >exp 253 cmp -s out exp || atf_fail "Saved output does not match expected results" 254} 255 256atf_test_case oflag_multiple 257oflag_multiple_head() 258{ 259 atf_set "descr" "Tests for multiple occurrences of the -o option" 260} 261oflag_multiple_body() 262{ 263 h_pass "echo foo bar" -o match:foo -o match:bar 264 h_pass "echo foo; echo bar" -o match:foo -o match:bar 265 h_fail "echo foo baz" -o match:bar -o match:foo 266 h_fail "echo foo; echo baz" -o match:bar -o match:foo 267} 268 269atf_test_case oflag_negated 270oflag_negated_head() 271{ 272 atf_set "descr" "Tests for negated occurrences of the -o option" 273} 274oflag_negated_body() 275{ 276 h_fail "echo foo" -o empty 277 h_pass "echo foo" -o not-empty 278 279 h_pass "echo foo bar" -o match:foo 280 h_fail "echo foo bar" -o not-match:foo 281} 282 283atf_test_case eflag_empty 284eflag_empty_head() 285{ 286 atf_set "descr" "Tests for the -e option using the 'empty' argument" 287} 288eflag_empty_body() 289{ 290 h_pass "true 1>&2" -e empty 291 h_fail "echo foo 1>&2" -e empty 292} 293 294atf_test_case eflag_ignore 295eflag_ignore_head() 296{ 297 atf_set "descr" "Tests for the -e option using the 'ignore' argument" 298} 299eflag_ignore_body() 300{ 301 h_pass "true 1>&2" -e ignore 302 h_pass "echo foo 1>&2" -e ignore 303} 304 305atf_test_case eflag_file 306eflag_file_head() 307{ 308 atf_set "descr" "Tests for the -e option using the 'file:' argument" 309} 310eflag_file_body() 311{ 312 touch empty 313 h_pass "true 1>&2" -e file:empty 314 315 echo foo >text 316 h_pass "echo foo 1>&2" -e file:text 317 h_fail "echo bar 1>&2" -e file:text 318 319 dd if=/dev/urandom of=bin bs=1k count=10 320 h_pass "cat bin 1>&2" -e file:bin 321} 322 323atf_test_case eflag_inline 324eflag_inline_head() 325{ 326 atf_set "descr" "Tests for the -e option using the 'inline:' argument" 327} 328eflag_inline_body() 329{ 330 h_pass "true 1>&2" -e inline: 331 h_pass "echo foo bar 1>&2" -e inline:"foo bar\n" 332 h_pass "printf 'foo bar' 1>&2" -e inline:"foo bar" 333 h_pass "printf '\t\n\t\n' 1>&2" -e inline:"\t\n\t\n" 334 # XXX Ugly hack to workaround the lack of \e in FreeBSD. Also, \e doesn't 335 # seem to work as expected in Linux. Look for a nicer solution. 336 case $(uname) in 337 Darwin|FreeBSD|Linux) 338 h_pass "printf '\a\b\f\n\r\t\v' 1>&2" -e inline:"\a\b\f\n\r\t\v" 339 ;; 340 *) 341 h_pass "printf '\a\b\e\f\n\r\t\v' 1>&2" -e inline:"\a\b\e\f\n\r\t\v" 342 ;; 343 esac 344 h_pass "printf '\011\022\033\012' 1>&2" -e inline:"\011\022\033\012" 345 346 h_fail "echo foo bar 1>&2" -e inline:"foo bar" 347 h_fail "echo -n foo bar 1>&2" -e inline:"foo bar\n" 348} 349 350atf_test_case eflag_save 351eflag_save_head() 352{ 353 atf_set "descr" "Tests for the -e option using the 'save:' argument" 354} 355eflag_save_body() 356{ 357 h_pass "echo foo 1>&2" -e save:out 358 echo foo >exp 359 cmp -s out exp || atf_fail "Saved output does not match expected results" 360} 361 362atf_test_case eflag_match 363eflag_match_head() 364{ 365 atf_set "descr" "Tests for the -e option using the 'match:' argument" 366} 367eflag_match_body() 368{ 369 h_pass "printf no-newline 1>&2" -e "match:^no-newline" 370 h_pass "echo line1 1>&2; echo foo bar 1>&2" -e "match:^foo" 371 h_pass "echo foo bar 1>&2" -e "match:o b" 372 h_fail "echo foo bar 1>&2" -e "match:baz" 373 h_fail "echo foo bar 1>&2" -e "match:^bar" 374} 375 376atf_test_case eflag_multiple 377eflag_multiple_head() 378{ 379 atf_set "descr" "Tests for multiple occurrences of the -e option" 380} 381eflag_multiple_body() 382{ 383 h_pass "echo foo bar 1>&2" -e match:foo -e match:bar 384 h_pass "echo foo 1>&2; echo bar 1>&2" -e match:foo -e match:bar 385 h_fail "echo foo baz 1>&2" -e match:bar -e match:foo 386 h_fail "echo foo 1>&2; echo baz 1>&2" -e match:bar -e match:foo 387} 388 389atf_test_case eflag_negated 390eflag_negated_head() 391{ 392 atf_set "descr" "Tests for negated occurrences of the -e option" 393} 394eflag_negated_body() 395{ 396 h_fail "echo foo 1>&2" -e empty 397 h_pass "echo foo 1>&2" -e not-empty 398 399 h_pass "echo foo bar 1>&2" -e match:foo 400 h_fail "echo foo bar 1>&2" -e not-match:foo 401} 402 403atf_test_case stdin 404stdin_head() 405{ 406 atf_set "descr" "Tests that stdin is preserved" 407} 408stdin_body() 409{ 410 echo "hello" | ${Atf_Check} -o match:"hello" cat || \ 411 atf_fail "atf-check does not seem to respect stdin" 412} 413 414atf_test_case invalid_umask 415invalid_umask_head() 416{ 417 atf_set "descr" "Tests for a correct error condition if the umask is" \ 418 "too restrictive" 419} 420invalid_umask_body() 421{ 422 umask 0222 423 ${Atf_Check} false 2>stderr && \ 424 atf_fail "atf-check returned 0 but it should have failed" 425 cat stderr 426 grep 'temporary.*current umask.*0222' stderr >/dev/null || \ 427 atf_fail "atf-check did not report an error related to the" \ 428 "current umask" 429} 430 431atf_init_test_cases() 432{ 433 atf_add_test_case sflag_eq_ne 434 atf_add_test_case sflag_exit 435 atf_add_test_case sflag_ignore 436 atf_add_test_case sflag_signal 437 438 atf_add_test_case xflag 439 440 atf_add_test_case oflag_empty 441 atf_add_test_case oflag_ignore 442 atf_add_test_case oflag_file 443 atf_add_test_case oflag_inline 444 atf_add_test_case oflag_match 445 atf_add_test_case oflag_save 446 atf_add_test_case oflag_multiple 447 atf_add_test_case oflag_negated 448 449 atf_add_test_case eflag_empty 450 atf_add_test_case eflag_ignore 451 atf_add_test_case eflag_file 452 atf_add_test_case eflag_inline 453 atf_add_test_case eflag_match 454 atf_add_test_case eflag_save 455 atf_add_test_case eflag_multiple 456 atf_add_test_case eflag_negated 457 458 atf_add_test_case stdin 459 460 atf_add_test_case invalid_umask 461} 462 463# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4 464