1 # Copyright (c) 2007 The NetBSD Foundation, Inc.
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
7 # 1. Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # 2. Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
13 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
14 # CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
15 # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 # IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
18 # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 # IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 # OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 # IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 # ------------------------------------------------------------------------
28 # ------------------------------------------------------------------------
30 # Values for the expect property.
34 # A boolean variable that indicates whether we are parsing a test case's
41 # The file to which the test case will print its result.
44 # The test program's source directory: i.e. where its auxiliary data files
45 # and helper utilities can be found. Can be overriden through the '-s' flag.
46 Source_Dir="$(dirname ${0})"
48 # Indicates the test case we are currently processing.
51 # List of meta-data variables for the current test case.
54 # The list of all test cases provided by the test program.
57 # ------------------------------------------------------------------------
59 # ------------------------------------------------------------------------
62 # atf_add_test_case tc-name
64 # Adds the given test case to the list of test cases that form the test
65 # program. The name provided here must be accompanied by two functions
66 # named after it: <tc-name>_head and <tc-name>_body, and optionally by
67 # a <tc-name>_cleanup function.
71 Test_Cases="${Test_Cases} ${1}"
75 # atf_check cmd expcode expout experr
77 # Executes atf-check with given arguments and automatically calls
78 # atf_fail in case of failure.
82 ${Atf_Check} "${@}" || \
83 atf_fail "atf-check failed; see the output of the test for details"
87 # atf_check_equal expected_expression actual_expression
89 # Checks that expected_expression's value matches actual_expression's
90 # and, if not, raises an error. Ideally expected_expression and
91 # actual_expression should be provided quoted (not expanded) so that
92 # the error message is helpful; otherwise it will only show the values,
93 # not the expressions themselves.
99 test "${_val1}" = "${_val2}" || \
100 atf_fail "${1} != ${2} (${_val1} != ${_val2})"
104 # atf_config_get varname [defvalue]
106 # Prints the value of a configuration variable. If it is not
107 # defined, prints the given default value.
111 _varname="__tc_config_var_$(_atf_normalize ${1})"
112 if [ ${#} -eq 1 ]; then
113 eval _value=\"\${${_varname}-__unset__}\"
114 [ "${_value}" = __unset__ ] && \
115 _atf_error 1 "Could not find configuration variable \`${1}'"
117 elif [ ${#} -eq 2 ]; then
118 eval echo \${${_varname}-${2}}
120 _atf_error 1 "Incorrect number of parameters for atf_config_get"
125 # atf_config_has varname
127 # Returns a boolean indicating if the given configuration variable is
132 _varname="__tc_config_var_$(_atf_normalize ${1})"
133 eval _value=\"\${${_varname}-__unset__}\"
134 [ "${_value}" != __unset__ ]
138 # atf_expect_death reason
140 # Sets the expectations to 'death'.
147 _atf_create_resfile "expected_death: ${*}"
151 # atf_expect_timeout reason
153 # Sets the expectations to 'timeout'.
160 _atf_create_resfile "expected_timeout: ${*}"
164 # atf_expect_exit exitcode reason
166 # Sets the expectations to 'exit'.
170 _exitcode="${1}"; shift
175 if [ "${_exitcode}" = "-1" ]; then
176 _atf_create_resfile "expected_exit: ${*}"
178 _atf_create_resfile "expected_exit(${_exitcode}): ${*}"
183 # atf_expect_fail reason
185 # Sets the expectations to 'fail'.
198 # Sets the expectations to 'pass'.
209 # atf_expect_signal signo reason
211 # Sets the expectations to 'signal'.
220 if [ "${_signo}" = "-1" ]; then
221 _atf_create_resfile "expected_signal: ${*}"
223 _atf_create_resfile "expected_signal(${_signo}): ${*}"
228 # atf_expected_failure msg1 [.. msgN]
230 # Makes the test case report an expected failure with the given error
231 # message. Multiple words can be provided, which are concatenated with
232 # a single blank space.
234 atf_expected_failure()
236 _atf_create_resfile "expected_failure: ${Expect_Reason}: ${*}"
241 # atf_fail msg1 [.. msgN]
243 # Makes the test case fail with the given error message. Multiple
244 # words can be provided, in which case they are joined by a single
251 atf_expected_failure "${@}"
254 _atf_create_resfile "failed: ${*}"
258 _atf_error 128 "Unreachable"
266 # Prints the value of a test case-specific variable. Given that one
267 # should not get the value of non-existent variables, it is fine to
268 # always use this function as 'val=$(atf_get var)'.
272 eval echo \${__tc_var_${Test_Case}_$(_atf_normalize ${1})}
278 # Prints the value of the test case's source directory.
288 # Makes the test case pass. Shouldn't be used in general, as a test
289 # case that does not explicitly fail is assumed to pass.
296 atf_fail "Test case was expecting a failure but got a pass instead"
299 _atf_create_resfile passed
303 _atf_error 128 "Unreachable"
309 # atf_require_prog prog
311 # Checks that the given program name (either provided as an absolute
312 # path or as a plain file name) can be found. If it is not available,
313 # automatically skips the test case with an appropriate message.
315 # Relative paths are not allowed because the test case cannot predict
316 # where it will be executed from.
325 atf_skip "The required program ${1} could not be found"
328 atf_fail "atf_require_prog does not accept relative path names \`${1}'"
331 _prog=$(_atf_find_in_path "${1}")
332 [ -n "${_prog}" ] || \
333 atf_skip "The required program ${1} could not be found" \
340 # atf_set varname val1 [.. valN]
342 # Sets the test case's variable 'varname' to the specified values
343 # which are concatenated using a single blank space. This function
344 # is supposed to be called form the test case's head only.
349 _atf_error 128 "atf_set called from the test case's body"
351 Test_Case_Vars="${Test_Case_Vars} ${1}"
352 _var=$(_atf_normalize ${1}); shift
353 eval __tc_var_${Test_Case}_${_var}=\"\${*}\"
357 # atf_skip msg1 [.. msgN]
359 # Skips the test case because of the reason provided. Multiple words
360 # can be given, in which case they are joined by a single blank space.
364 _atf_create_resfile "skipped: ${*}"
369 # atf_test_case tc-name cleanup
371 # Defines a new test case named tc-name. The name provided here must be
372 # accompanied by two functions named after it: <tc-name>_head and
373 # <tc-name>_body. If cleanup is set to 'cleanup', then this also expects
374 # a <tc-name>_cleanup function to be defined.
378 eval "${1}_head() { :; }"
379 eval "${1}_body() { atf_fail 'Test case not implemented'; }"
380 if [ "${2}" = cleanup ]; then
381 eval __has_cleanup_${1}=true
382 eval "${1}_cleanup() { :; }"
384 eval "${1}_cleanup() {
385 _atf_error 1 'Test case ${1} declared without a cleanup routine'; }"
389 # ------------------------------------------------------------------------
391 # ------------------------------------------------------------------------
394 # _atf_config_set varname val1 [.. valN]
396 # Sets the test case's private variable 'varname' to the specified
397 # values which are concatenated using a single blank space.
401 _var=$(_atf_normalize ${1}); shift
402 eval __tc_config_var_${_var}=\"\${*}\"
403 Config_Vars="${Config_Vars} __tc_config_var_${_var}"
407 # _atf_config_set_str varname=val
409 # Sets the test case's private variable 'varname' to the specified
410 # value. The parameter is of the form 'varname=val'.
412 _atf_config_set_from_str()
421 _atf_config_set "${_var}" "${_val}"
425 # _atf_create_resfile contents
427 # Creates the results file.
429 _atf_create_resfile()
431 if [ -n "${Results_File}" ]; then
432 echo "${*}" >"${Results_File}" || \
433 _atf_error 128 "Cannot create results file '${Results_File}'"
440 # _atf_error error_code [msg1 [.. msgN]]
442 # Prints the given error message (which can be composed of multiple
443 # arguments, in which case are joined by a single space) and exits
444 # with the specified error code.
446 # This must not be used by test programs themselves (hence making
447 # the function private) to indicate a test case's failure. They
448 # have to use the atf_fail function.
452 _error_code="${1}"; shift
454 echo "${Prog_Name}: ERROR:" "$@" 1>&2
459 # _atf_warning msg1 [.. msgN]
461 # Prints the given warning message (which can be composed of multiple
462 # arguments, in which case are joined by a single space).
466 echo "${Prog_Name}: WARNING:" "$@" 1>&2
470 # _atf_find_in_path program
472 # Looks for a program in the path and prints the full path to it or
473 # nothing if it could not be found. It also returns true in case of
484 if [ -x ${_dir}/${_prog} ]; then
486 echo ${_dir}/${_prog}
498 # Returns true if the given test case exists.
502 for _tc in ${Test_Cases}; do
503 [ "${_tc}" != "${1}" ] || return 0
511 # Describes all test cases and prints the list to the standard output.
515 echo 'Content-Type: application/X-atf-tp; version="1"'
519 while [ ${#} -gt 0 ]; do
522 echo "ident: $(atf_get ident)"
523 for _var in ${Test_Case_Vars}; do
524 [ "${_var}" != "ident" ] && echo "${_var}: $(atf_get ${_var})"
527 [ ${#} -gt 1 ] && echo
535 # Normalizes a string so that it is a valid shell variable name.
543 # _atf_parse_head tcname
545 # Evaluates a test case's head to gather its variables and prepares the
546 # test program to run it.
555 if _atf_has_cleanup "${1}"; then
556 atf_set has.cleanup "true"
568 # Runs the specified test case. Prints its exit status to the
569 # standard output and returns a boolean indicating if the test was
579 if [ "${_tcpart}" != body -a "${_tcpart}" != cleanup ]; then
580 _atf_syntax_error "Unknown test case part \`${_tcpart}'"
590 _atf_has_tc "${_tcname}" || _atf_syntax_error "Unknown test case \`${1}'"
592 if [ "${__RUNNING_INSIDE_ATF_RUN}" != "internal-yes-value" ]; then
593 _atf_warning "Running test cases outside of kyua(1) is unsupported"
594 _atf_warning "No isolation nor timeout control is being applied;" \
595 "you may get unexpected failures; see atf-test-case(4)"
598 _atf_parse_head ${_tcname}
602 if ${_tcname}_body; then
604 _atf_create_resfile passed
607 atf_fail "Test case body returned a non-ok exit code, but" \
608 "this is not allowed"
612 if _atf_has_cleanup "${_tcname}"; then
613 ${_tcname}_cleanup || _atf_error 128 "The test case cleanup" \
614 "returned a non-ok exit code, but this is not allowed"
618 _atf_error 128 "Unknown test case part"
624 # _atf_syntax_error msg1 [.. msgN]
626 # Formats and prints a syntax error message and terminates the
627 # program prematurely.
631 echo "${Prog_Name}: ERROR: ${@}" 1>&2
632 echo "${Prog_Name}: See atf-test-program(1) for usage details." 1>&2
637 # _atf_has_cleanup tc-name
639 # Returns a boolean indicating if the given test case has a cleanup
645 eval "[ x\"\${__has_cleanup_${1}}\" = xtrue ] || _found=false"
646 [ "${_found}" = true ]
650 # _atf_validate_expect
652 # Ensures that the current test case state is correct regarding the expect
655 _atf_validate_expect()
660 atf_fail "Test case was expected to terminate abruptly but it" \
661 "continued execution"
665 atf_fail "Test case was expected to exit cleanly but it continued" \
670 atf_fail "Test case was expecting a failure but none were raised"
676 atf_fail "Test case was expected to receive a termination signal" \
677 "but it continued execution"
681 atf_fail "Test case was expected to hang but it continued execution"
684 _atf_error 128 "Unreachable"
690 # _atf_warning [msg1 [.. msgN]]
692 # Prints the given warning message (which can be composed of multiple
693 # arguments, in which case are joined by a single space).
695 # This must not be used by test programs themselves (hence making
696 # the function private).
700 echo "${Prog_Name}: WARNING:" "$@" 1>&2
704 # main [options] test_case
706 # Test program's entry point.
710 # Process command-line options first.
713 while getopts :lr:s:v: arg; do
720 Results_File=${OPTARG}
728 _atf_config_set_from_str "${OPTARG}"
732 _atf_syntax_error "Unknown option -${OPTARG}."
737 shift `expr ${OPTIND} - 1`
739 case ${Source_Dir} in
743 Source_Dir=$(pwd)/${Source_Dir}
746 [ -f ${Source_Dir}/${Prog_Name} ] || \
747 _atf_error 1 "Cannot find the test program in the source" \
748 "directory \`${Source_Dir}'"
750 # Call the test program's hook to register all available test cases.
753 # Run or list test cases.
755 if [ ${#} -gt 0 ]; then
756 _atf_syntax_error "Cannot provide test case names with -l"
760 if [ ${#} -eq 0 ]; then
761 _atf_syntax_error "Must provide a test case name"
762 elif [ ${#} -gt 1 ]; then
763 _atf_syntax_error "Cannot provide more than one test case name"
770 # vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4