1#
2# Automated Testing Framework (atf)
3#
4# Copyright (c) 2007, 2008, 2010 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
30check_result() {
31    file="${1}"; shift
32
33    atf_check -s eq:0 -o match:"${*}" -e empty cat "${file}"
34    rm "${file}"
35}
36
37atf_test_case expect_pass
38expect_pass_body() {
39    for h in $(get_helpers); do
40        atf_check -s eq:0 "${h}" -r result expect_pass_and_pass
41        check_result result "passed"
42
43        atf_check -s eq:1 "${h}" -r result expect_pass_but_fail_requirement
44        check_result result "failed: Some reason"
45
46        # atf-sh does not support non-fatal failures yet; skip checks for
47        # such conditions.
48        case "${h}" in *sh_helpers*) continue ;; esac
49
50        atf_check -s eq:1 -o empty -e match:"Some reason" \
51            "${h}" -r result expect_pass_but_fail_check
52        check_result result "failed: 1 checks failed"
53    done
54}
55
56atf_test_case expect_fail
57expect_fail_body() {
58    for h in $(get_helpers c_helpers cpp_helpers); do
59        atf_check -s eq:0 "${h}" -r result expect_fail_and_fail_requirement
60        check_result result "expected_failure: Fail reason: The failure"
61
62        atf_check -s eq:1 -e match:"Expected check failure: Fail first: abc" \
63            -e not-match:"And fail again" "${h}" -r result expect_fail_but_pass
64        check_result result "failed: .*expecting a failure"
65
66        # atf-sh does not support non-fatal failures yet; skip checks for
67        # such conditions.
68        case "${h}" in *sh_helpers*) continue ;; esac
69
70        atf_check -s eq:0 -e match:"Expected check failure: Fail first: abc" \
71            -e match:"Expected check failure: And fail again: def" \
72            "${h}" -r result expect_fail_and_fail_check
73        check_result result "expected_failure: And fail again: 2 checks" \
74            "failed as expected"
75    done
76
77    # atf-sh does not support non-fatal failures yet; skip checks for
78    # such conditions.
79    for h in $(get_helpers sh_helpers); do
80        atf_check -s eq:0 "${h}" -r result expect_fail_and_fail_requirement
81        check_result result "expected_failure: Fail reason: The failure"
82
83        atf_check -s eq:1 "${h}" -r result expect_fail_but_pass
84        check_result result "failed: .*expecting a failure"
85    done
86}
87
88atf_test_case expect_exit
89expect_exit_body() {
90    for h in $(get_helpers); do
91        atf_check -s eq:0 "${h}" -r result expect_exit_any_and_exit
92        check_result result "expected_exit: Call will exit"
93
94        atf_check -s eq:123 "${h}" -r result expect_exit_code_and_exit
95        check_result result "expected_exit\(123\): Call will exit"
96
97        atf_check -s eq:1 "${h}" -r result expect_exit_but_pass
98        check_result result "failed: .*expected to exit"
99    done
100}
101
102atf_test_case expect_signal
103expect_signal_body() {
104    for h in $(get_helpers); do
105        atf_check -s signal:9 "${h}" -r result expect_signal_any_and_signal
106        check_result result "expected_signal: Call will signal"
107
108        atf_check -s signal:hup "${h}" -r result expect_signal_no_and_signal
109        check_result result "expected_signal\(1\): Call will signal"
110
111        atf_check -s eq:1 "${h}" -r result expect_signal_but_pass
112        check_result result "failed: .*termination signal"
113    done
114}
115
116atf_test_case expect_death
117expect_death_body() {
118    for h in $(get_helpers); do
119        atf_check -s eq:123 "${h}" -r result expect_death_and_exit
120        check_result result "expected_death: Exit case"
121
122        atf_check -s signal:kill "${h}" -r result expect_death_and_signal
123        check_result result "expected_death: Signal case"
124
125        atf_check -s eq:1 "${h}" -r result expect_death_but_pass
126        check_result result "failed: .*terminate abruptly"
127    done
128}
129
130atf_test_case expect_timeout
131expect_timeout_body() {
132    for h in $(get_helpers); do
133        atf_check -s eq:1 "${h}" -r result expect_timeout_but_pass
134        check_result result "failed: Test case was expected to hang but it" \
135            "continued execution"
136    done
137}
138
139atf_init_test_cases()
140{
141    atf_add_test_case expect_pass
142    atf_add_test_case expect_fail
143    atf_add_test_case expect_exit
144    atf_add_test_case expect_signal
145    atf_add_test_case expect_death
146    atf_add_test_case expect_timeout
147}
148
149# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
150