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