xref: /netbsd-src/external/bsd/atf/dist/test-programs/expect_test.sh (revision 179b12252ecaf3553d9c2b7458ce62b6a2203d0c)
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_head() {
39    atf_set "use.fs" "true"
40}
41expect_pass_body() {
42    for h in $(get_helpers); do
43        atf_check -s eq:0 "${h}" -r result expect_pass_and_pass
44        check_result result "passed"
45
46        atf_check -s eq:1 "${h}" -r result expect_pass_but_fail_requirement
47        check_result result "failed: Some reason"
48
49        # atf-sh does not support non-fatal failures yet; skip checks for
50        # such conditions.
51        case "${h}" in *sh_helpers*) continue ;; esac
52
53        atf_check -s eq:1 -o empty -e match:"Some reason" \
54            "${h}" -r result expect_pass_but_fail_check
55        check_result result "failed: 1 checks failed"
56    done
57}
58
59atf_test_case expect_fail
60expect_fail_head() {
61    atf_set "use.fs" "true"
62}
63expect_fail_body() {
64    for h in $(get_helpers c_helpers cpp_helpers); do
65        atf_check -s eq:0 "${h}" -r result expect_fail_and_fail_requirement
66        check_result result "expected_failure: Fail reason: The failure"
67
68        atf_check -s eq:1 -e match:"Expected check failure: Fail first: abc" \
69            -e not-match:"And fail again" "${h}" -r result expect_fail_but_pass
70        check_result result "failed: .*expecting a failure"
71
72        # atf-sh does not support non-fatal failures yet; skip checks for
73        # such conditions.
74        case "${h}" in *sh_helpers*) continue ;; esac
75
76        atf_check -s eq:0 -e match:"Expected check failure: Fail first: abc" \
77            -e match:"Expected check failure: And fail again: def" \
78            "${h}" -r result expect_fail_and_fail_check
79        check_result result "expected_failure: And fail again: 2 checks" \
80            "failed as expected"
81    done
82
83    # atf-sh does not support non-fatal failures yet; skip checks for
84    # such conditions.
85    for h in $(get_helpers sh_helpers); do
86        atf_check -s eq:0 "${h}" -r result expect_fail_and_fail_requirement
87        check_result result "expected_failure: Fail reason: The failure"
88
89        atf_check -s eq:1 "${h}" -r result expect_fail_but_pass
90        check_result result "failed: .*expecting a failure"
91    done
92}
93
94atf_test_case expect_exit
95expect_exit_head() {
96    atf_set "use.fs" "true"
97}
98expect_exit_body() {
99    for h in $(get_helpers); do
100        atf_check -s eq:0 "${h}" -r result expect_exit_any_and_exit
101        check_result result "expected_exit: Call will exit"
102
103        atf_check -s eq:123 "${h}" -r result expect_exit_code_and_exit
104        check_result result "expected_exit\(123\): Call will exit"
105
106        atf_check -s eq:1 "${h}" -r result expect_exit_but_pass
107        check_result result "failed: .*expected to exit"
108    done
109}
110
111atf_test_case expect_signal
112expect_signal_head() {
113    atf_set "use.fs" "true"
114}
115expect_signal_body() {
116    for h in $(get_helpers); do
117        atf_check -s signal:9 "${h}" -r result expect_signal_any_and_signal
118        check_result result "expected_signal: Call will signal"
119
120        atf_check -s signal:hup "${h}" -r result expect_signal_no_and_signal
121        check_result result "expected_signal\(1\): Call will signal"
122
123        atf_check -s eq:1 "${h}" -r result expect_signal_but_pass
124        check_result result "failed: .*termination signal"
125    done
126}
127
128atf_test_case expect_death
129expect_death_head() {
130    atf_set "use.fs" "true"
131}
132expect_death_body() {
133    for h in $(get_helpers); do
134        atf_check -s eq:123 "${h}" -r result expect_death_and_exit
135        check_result result "expected_death: Exit case"
136
137        atf_check -s signal:kill "${h}" -r result expect_death_and_signal
138        check_result result "expected_death: Signal case"
139
140        atf_check -s eq:1 "${h}" -r result expect_death_but_pass
141        check_result result "failed: .*terminate abruptly"
142    done
143}
144
145atf_test_case expect_timeout
146expect_timeout_head() {
147    atf_set "use.fs" "true"
148}
149expect_timeout_body() {
150    for h in $(get_helpers); do
151        atf_check -s eq:1 "${h}" -r result expect_timeout_but_pass
152        check_result result "failed: Test case was expected to hang but it" \
153            "continued execution"
154    done
155}
156
157atf_init_test_cases()
158{
159    atf_add_test_case expect_pass
160    atf_add_test_case expect_fail
161    atf_add_test_case expect_exit
162    atf_add_test_case expect_signal
163    atf_add_test_case expect_death
164    atf_add_test_case expect_timeout
165}
166
167# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
168