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