xref: /linux/tools/testing/selftests/net/forwarding/lib_sh_test.sh (revision daa2be74b1b2302004945b2a5e32424e177cc7da)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# This tests the operation of lib.sh itself.
5
6ALL_TESTS="
7	test_ret
8	test_exit_status
9"
10NUM_NETIFS=0
11source lib.sh
12
13# Simulated checks.
14
15do_test()
16{
17	local msg=$1; shift
18
19	"$@"
20	check_err $? "$msg"
21}
22
23tpass()
24{
25	do_test "tpass" true
26}
27
28tfail()
29{
30	do_test "tfail" false
31}
32
33txfail()
34{
35	FAIL_TO_XFAIL=yes do_test "txfail" false
36}
37
38# Simulated tests.
39
40pass()
41{
42	RET=0
43	do_test "true" true
44	log_test "true"
45}
46
47fail()
48{
49	RET=0
50	do_test "false" false
51	log_test "false"
52}
53
54xfail()
55{
56	RET=0
57	FAIL_TO_XFAIL=yes do_test "xfalse" false
58	log_test "xfalse"
59}
60
61skip()
62{
63	RET=0
64	log_test_skip "skip"
65}
66
67slow_xfail()
68{
69	RET=0
70	xfail_on_slow do_test "slow_false" false
71	log_test "slow_false"
72}
73
74# lib.sh tests.
75
76ret_tests_run()
77{
78	local t
79
80	RET=0
81	retmsg=
82	for t in "$@"; do
83		$t
84	done
85	echo "$retmsg"
86	return $RET
87}
88
89ret_subtest()
90{
91	local expect_ret=$1; shift
92	local expect_retmsg=$1; shift
93	local -a tests=( "$@" )
94
95	local status_names=(pass fail xfail xpass skip)
96	local ret
97	local out
98
99	RET=0
100
101	# Run this in a subshell, so that our environment is intact.
102	out=$(ret_tests_run "${tests[@]}")
103	ret=$?
104
105	(( ret == expect_ret ))
106	check_err $? "RET=$ret expected $expect_ret"
107
108	[[ $out == $expect_retmsg ]]
109	check_err $? "retmsg=$out expected $expect_retmsg"
110
111	log_test "RET $(echo ${tests[@]}) -> ${status_names[$ret]}"
112}
113
114test_ret()
115{
116	ret_subtest $ksft_pass ""
117
118	ret_subtest $ksft_pass "" tpass
119	ret_subtest $ksft_fail "tfail" tfail
120	ret_subtest $ksft_xfail "txfail" txfail
121
122	ret_subtest $ksft_pass "" tpass tpass
123	ret_subtest $ksft_fail "tfail" tpass tfail
124	ret_subtest $ksft_xfail "txfail" tpass txfail
125
126	ret_subtest $ksft_fail "tfail" tfail tpass
127	ret_subtest $ksft_xfail "txfail" txfail tpass
128
129	ret_subtest $ksft_fail "tfail" tfail tfail
130	ret_subtest $ksft_fail "tfail" tfail txfail
131
132	ret_subtest $ksft_fail "tfail" txfail tfail
133
134	ret_subtest $ksft_xfail "txfail" txfail txfail
135}
136
137exit_status_tests_run()
138{
139	EXIT_STATUS=0
140	tests_run > /dev/null
141	return $EXIT_STATUS
142}
143
144exit_status_subtest()
145{
146	local expect_exit_status=$1; shift
147	local tests=$1; shift
148	local what=$1; shift
149
150	local status_names=(pass fail xfail xpass skip)
151	local exit_status
152	local out
153
154	RET=0
155
156	# Run this in a subshell, so that our environment is intact.
157	out=$(TESTS="$tests" exit_status_tests_run)
158	exit_status=$?
159
160	(( exit_status == expect_exit_status ))
161	check_err $? "EXIT_STATUS=$exit_status, expected $expect_exit_status"
162
163	log_test "EXIT_STATUS $tests$what -> ${status_names[$exit_status]}"
164}
165
166test_exit_status()
167{
168	exit_status_subtest $ksft_pass ":"
169
170	exit_status_subtest $ksft_pass "pass"
171	exit_status_subtest $ksft_fail "fail"
172	exit_status_subtest $ksft_pass "xfail"
173	exit_status_subtest $ksft_skip "skip"
174
175	exit_status_subtest $ksft_pass "pass pass"
176	exit_status_subtest $ksft_fail "pass fail"
177	exit_status_subtest $ksft_pass "pass xfail"
178	exit_status_subtest $ksft_skip "pass skip"
179
180	exit_status_subtest $ksft_fail "fail pass"
181	exit_status_subtest $ksft_pass "xfail pass"
182	exit_status_subtest $ksft_skip "skip pass"
183
184	exit_status_subtest $ksft_fail "fail fail"
185	exit_status_subtest $ksft_fail "fail xfail"
186	exit_status_subtest $ksft_fail "fail skip"
187
188	exit_status_subtest $ksft_fail "xfail fail"
189	exit_status_subtest $ksft_fail "skip fail"
190
191	exit_status_subtest $ksft_pass "xfail xfail"
192	exit_status_subtest $ksft_skip "xfail skip"
193	exit_status_subtest $ksft_skip "skip xfail"
194
195	exit_status_subtest $ksft_skip "skip skip"
196
197	KSFT_MACHINE_SLOW=yes \
198		exit_status_subtest $ksft_pass "slow_xfail" ": slow"
199
200	KSFT_MACHINE_SLOW=no \
201		exit_status_subtest $ksft_fail "slow_xfail" ": fast"
202}
203
204trap pre_cleanup EXIT
205
206tests_run
207
208exit $EXIT_STATUS
209