xref: /freebsd/contrib/atf/test-programs/expect_test.sh (revision c243e4902be8df1e643c76b5f18b68bb77cc5268)
1*c243e490SMarcel Moolenaar#
2*c243e490SMarcel Moolenaar# Automated Testing Framework (atf)
3*c243e490SMarcel Moolenaar#
4*c243e490SMarcel Moolenaar# Copyright (c) 2007 The NetBSD Foundation, Inc.
5*c243e490SMarcel Moolenaar# All rights reserved.
6*c243e490SMarcel Moolenaar#
7*c243e490SMarcel Moolenaar# Redistribution and use in source and binary forms, with or without
8*c243e490SMarcel Moolenaar# modification, are permitted provided that the following conditions
9*c243e490SMarcel Moolenaar# are met:
10*c243e490SMarcel Moolenaar# 1. Redistributions of source code must retain the above copyright
11*c243e490SMarcel Moolenaar#    notice, this list of conditions and the following disclaimer.
12*c243e490SMarcel Moolenaar# 2. Redistributions in binary form must reproduce the above copyright
13*c243e490SMarcel Moolenaar#    notice, this list of conditions and the following disclaimer in the
14*c243e490SMarcel Moolenaar#    documentation and/or other materials provided with the distribution.
15*c243e490SMarcel Moolenaar#
16*c243e490SMarcel Moolenaar# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17*c243e490SMarcel Moolenaar# CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18*c243e490SMarcel Moolenaar# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19*c243e490SMarcel Moolenaar# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20*c243e490SMarcel Moolenaar# IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21*c243e490SMarcel Moolenaar# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*c243e490SMarcel Moolenaar# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23*c243e490SMarcel Moolenaar# GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24*c243e490SMarcel Moolenaar# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25*c243e490SMarcel Moolenaar# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26*c243e490SMarcel Moolenaar# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27*c243e490SMarcel Moolenaar# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*c243e490SMarcel Moolenaar#
29*c243e490SMarcel Moolenaar
30*c243e490SMarcel Moolenaarcheck_result() {
31*c243e490SMarcel Moolenaar    file="${1}"; shift
32*c243e490SMarcel Moolenaar
33*c243e490SMarcel Moolenaar    atf_check -s eq:0 -o match:"${*}" -e empty cat "${file}"
34*c243e490SMarcel Moolenaar    rm "${file}"
35*c243e490SMarcel Moolenaar}
36*c243e490SMarcel Moolenaar
37*c243e490SMarcel Moolenaaratf_test_case expect_pass
38*c243e490SMarcel Moolenaarexpect_pass_body() {
39*c243e490SMarcel Moolenaar    for h in $(get_helpers); do
40*c243e490SMarcel Moolenaar        atf_check -s eq:0 -e ignore "${h}" -r result expect_pass_and_pass
41*c243e490SMarcel Moolenaar        check_result result "passed"
42*c243e490SMarcel Moolenaar
43*c243e490SMarcel Moolenaar        atf_check -s eq:1 -e ignore "${h}" -r result \
44*c243e490SMarcel Moolenaar            expect_pass_but_fail_requirement
45*c243e490SMarcel Moolenaar        check_result result "failed: Some reason"
46*c243e490SMarcel Moolenaar
47*c243e490SMarcel Moolenaar        # atf-sh does not support non-fatal failures yet; skip checks for
48*c243e490SMarcel Moolenaar        # such conditions.
49*c243e490SMarcel Moolenaar        case "${h}" in *sh_helpers*) continue ;; esac
50*c243e490SMarcel Moolenaar
51*c243e490SMarcel Moolenaar        atf_check -s eq:1 -o empty -e match:"Some reason" \
52*c243e490SMarcel Moolenaar            "${h}" -r result expect_pass_but_fail_check
53*c243e490SMarcel Moolenaar        check_result result "failed: 1 checks failed"
54*c243e490SMarcel Moolenaar    done
55*c243e490SMarcel Moolenaar}
56*c243e490SMarcel Moolenaar
57*c243e490SMarcel Moolenaaratf_test_case expect_fail
58*c243e490SMarcel Moolenaarexpect_fail_body() {
59*c243e490SMarcel Moolenaar    for h in $(get_helpers c_helpers cpp_helpers); do
60*c243e490SMarcel Moolenaar        atf_check -s eq:0 "${h}" -r result expect_fail_and_fail_requirement
61*c243e490SMarcel Moolenaar        check_result result "expected_failure: Fail reason: The failure"
62*c243e490SMarcel Moolenaar
63*c243e490SMarcel Moolenaar        atf_check -s eq:1 -e match:"Expected check failure: Fail first: abc" \
64*c243e490SMarcel Moolenaar            -e not-match:"And fail again" "${h}" -r result expect_fail_but_pass
65*c243e490SMarcel Moolenaar        check_result result "failed: .*expecting a failure"
66*c243e490SMarcel Moolenaar
67*c243e490SMarcel Moolenaar        # atf-sh does not support non-fatal failures yet; skip checks for
68*c243e490SMarcel Moolenaar        # such conditions.
69*c243e490SMarcel Moolenaar        case "${h}" in *sh_helpers*) continue ;; esac
70*c243e490SMarcel Moolenaar
71*c243e490SMarcel Moolenaar        atf_check -s eq:0 -e match:"Expected check failure: Fail first: abc" \
72*c243e490SMarcel Moolenaar            -e match:"Expected check failure: And fail again: def" \
73*c243e490SMarcel Moolenaar            "${h}" -r result expect_fail_and_fail_check
74*c243e490SMarcel Moolenaar        check_result result "expected_failure: And fail again: 2 checks" \
75*c243e490SMarcel Moolenaar            "failed as expected"
76*c243e490SMarcel Moolenaar    done
77*c243e490SMarcel Moolenaar
78*c243e490SMarcel Moolenaar    # atf-sh does not support non-fatal failures yet; skip checks for
79*c243e490SMarcel Moolenaar    # such conditions.
80*c243e490SMarcel Moolenaar    for h in $(get_helpers sh_helpers); do
81*c243e490SMarcel Moolenaar        atf_check -s eq:0 -e ignore "${h}" -r result \
82*c243e490SMarcel Moolenaar            expect_fail_and_fail_requirement
83*c243e490SMarcel Moolenaar        check_result result "expected_failure: Fail reason: The failure"
84*c243e490SMarcel Moolenaar
85*c243e490SMarcel Moolenaar        atf_check -s eq:1 -e ignore "${h}" -r result expect_fail_but_pass
86*c243e490SMarcel Moolenaar        check_result result "failed: .*expecting a failure"
87*c243e490SMarcel Moolenaar    done
88*c243e490SMarcel Moolenaar}
89*c243e490SMarcel Moolenaar
90*c243e490SMarcel Moolenaaratf_test_case expect_exit
91*c243e490SMarcel Moolenaarexpect_exit_body() {
92*c243e490SMarcel Moolenaar    for h in $(get_helpers); do
93*c243e490SMarcel Moolenaar        atf_check -s eq:0 -e ignore "${h}" -r result expect_exit_any_and_exit
94*c243e490SMarcel Moolenaar        check_result result "expected_exit: Call will exit"
95*c243e490SMarcel Moolenaar
96*c243e490SMarcel Moolenaar        atf_check -s eq:123 -e ignore "${h}" -r result expect_exit_code_and_exit
97*c243e490SMarcel Moolenaar        check_result result "expected_exit\(123\): Call will exit"
98*c243e490SMarcel Moolenaar
99*c243e490SMarcel Moolenaar        atf_check -s eq:1 -e ignore "${h}" -r result expect_exit_but_pass
100*c243e490SMarcel Moolenaar        check_result result "failed: .*expected to exit"
101*c243e490SMarcel Moolenaar    done
102*c243e490SMarcel Moolenaar}
103*c243e490SMarcel Moolenaar
104*c243e490SMarcel Moolenaaratf_test_case expect_signal
105*c243e490SMarcel Moolenaarexpect_signal_body() {
106*c243e490SMarcel Moolenaar    for h in $(get_helpers); do
107*c243e490SMarcel Moolenaar        atf_check -s signal:9 -e ignore "${h}" -r result \
108*c243e490SMarcel Moolenaar            expect_signal_any_and_signal
109*c243e490SMarcel Moolenaar        check_result result "expected_signal: Call will signal"
110*c243e490SMarcel Moolenaar
111*c243e490SMarcel Moolenaar        atf_check -s signal:hup -e ignore "${h}" -r result \
112*c243e490SMarcel Moolenaar            expect_signal_no_and_signal
113*c243e490SMarcel Moolenaar        check_result result "expected_signal\(1\): Call will signal"
114*c243e490SMarcel Moolenaar
115*c243e490SMarcel Moolenaar        atf_check -s eq:1 -e ignore "${h}" -r result \
116*c243e490SMarcel Moolenaar            expect_signal_but_pass
117*c243e490SMarcel Moolenaar        check_result result "failed: .*termination signal"
118*c243e490SMarcel Moolenaar    done
119*c243e490SMarcel Moolenaar}
120*c243e490SMarcel Moolenaar
121*c243e490SMarcel Moolenaaratf_test_case expect_death
122*c243e490SMarcel Moolenaarexpect_death_body() {
123*c243e490SMarcel Moolenaar    for h in $(get_helpers); do
124*c243e490SMarcel Moolenaar        atf_check -s eq:123 -e ignore "${h}" -r result expect_death_and_exit
125*c243e490SMarcel Moolenaar        check_result result "expected_death: Exit case"
126*c243e490SMarcel Moolenaar
127*c243e490SMarcel Moolenaar        atf_check -s signal:kill -e ignore "${h}" -r result \
128*c243e490SMarcel Moolenaar            expect_death_and_signal
129*c243e490SMarcel Moolenaar        check_result result "expected_death: Signal case"
130*c243e490SMarcel Moolenaar
131*c243e490SMarcel Moolenaar        atf_check -s eq:1 -e ignore "${h}" -r result expect_death_but_pass
132*c243e490SMarcel Moolenaar        check_result result "failed: .*terminate abruptly"
133*c243e490SMarcel Moolenaar    done
134*c243e490SMarcel Moolenaar}
135*c243e490SMarcel Moolenaar
136*c243e490SMarcel Moolenaaratf_test_case expect_timeout
137*c243e490SMarcel Moolenaarexpect_timeout_body() {
138*c243e490SMarcel Moolenaar    for h in $(get_helpers); do
139*c243e490SMarcel Moolenaar        atf_check -s eq:1 -e ignore "${h}" -r result expect_timeout_but_pass
140*c243e490SMarcel Moolenaar        check_result result "failed: Test case was expected to hang but it" \
141*c243e490SMarcel Moolenaar            "continued execution"
142*c243e490SMarcel Moolenaar    done
143*c243e490SMarcel Moolenaar}
144*c243e490SMarcel Moolenaar
145*c243e490SMarcel Moolenaaratf_init_test_cases()
146*c243e490SMarcel Moolenaar{
147*c243e490SMarcel Moolenaar    atf_add_test_case expect_pass
148*c243e490SMarcel Moolenaar    atf_add_test_case expect_fail
149*c243e490SMarcel Moolenaar    atf_add_test_case expect_exit
150*c243e490SMarcel Moolenaar    atf_add_test_case expect_signal
151*c243e490SMarcel Moolenaar    atf_add_test_case expect_death
152*c243e490SMarcel Moolenaar    atf_add_test_case expect_timeout
153*c243e490SMarcel Moolenaar}
154*c243e490SMarcel Moolenaar
155*c243e490SMarcel Moolenaar# vim: syntax=sh:expandtab:shiftwidth=4:softtabstop=4
156