xref: /freebsd/sbin/ping/tests/ping_test.sh (revision 6137b5f7b8c183ee8806d79b3f1d8e5e3ddb3df3)
1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (C) 2019 Jan Sucan <jansucan@FreeBSD.org>
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 AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29require_ipv4()
30{
31	if ! getaddrinfo -f inet localhost 1>/dev/null 2>&1; then
32		atf_skip "IPv4 is not configured"
33	fi
34}
35require_ipv6()
36{
37	if ! getaddrinfo -f inet6 localhost 1>/dev/null 2>&1; then
38		atf_skip "IPv6 is not configured"
39	fi
40}
41
42atf_test_case ping_c1_s56_t1
43ping_c1_s56_t1_head()
44{
45	atf_set "descr" "Stop after receiving 1 ECHO_RESPONSE packet"
46}
47ping_c1_s56_t1_body()
48{
49	require_ipv4
50	atf_check -s exit:0 -o save:std.out -e empty \
51	    ping -4 -c 1 -s 56 -t 1 localhost
52	check_ping_statistics std.out $(atf_get_srcdir)/ping_c1_s56_t1.out
53}
54
55atf_test_case ping_c1_s56_t1_S127
56ping_c1_s56_t1_S127_head()
57{
58	atf_set "descr" "Check that ping -S 127.0.0.1 localhost succeeds"
59}
60ping_c1_s56_t1_S127_body()
61{
62	require_ipv4
63	require_ipv6
64	atf_check -s exit:0 -o save:std.out -e empty \
65	    ping -c 1 -s 56 -t 1 -S 127.0.0.1 localhost
66	check_ping_statistics std.out $(atf_get_srcdir)/ping_c1_s56_t1_S127.out
67}
68
69atf_test_case ping_6_c1_s8_t1
70ping_6_c1_s8_t1_head()
71{
72	atf_set "descr" "Stop after receiving 1 ECHO_RESPONSE packet"
73}
74ping_6_c1_s8_t1_body()
75{
76	require_ipv6
77	atf_check -s exit:0 -o save:std.out -e empty \
78	    ping -6 -c 1 -s 8 -t 1 localhost
79	check_ping_statistics std.out $(atf_get_srcdir)/ping_6_c1_s8_t1.out
80}
81
82atf_test_case ping_c1_s8_t1_S1
83ping_c1_s8_t1_S1_head()
84{
85	atf_set "descr" "Check that ping -S ::1 localhost succeeds"
86}
87ping_c1_s8_t1_S1_body()
88{
89	require_ipv4
90	require_ipv6
91	atf_check -s exit:0 -o save:std.out -e empty \
92	    ping -c 1 -s 8 -t 1 -S ::1 localhost
93	check_ping_statistics std.out $(atf_get_srcdir)/ping_c1_s8_t1_S1.out
94}
95
96atf_test_case ping6_c1_s8_t1
97ping6_c1_s8_t1_head()
98{
99	atf_set "descr" "Use IPv6 when invoked as ping6"
100}
101ping6_c1_s8_t1_body()
102{
103	require_ipv6
104	atf_check -s exit:0 -o save:std.out -e empty \
105	    ping6 -c 1 -s 8 -t 1 localhost
106	check_ping_statistics std.out $(atf_get_srcdir)/ping_6_c1_s8_t1.out
107}
108
109atf_test_case ping_c1t6
110ping_c1t6_head()
111{
112	atf_set "descr" "-t6 is not interpreted as -t -6 by ping"
113}
114ping_c1t6_body()
115{
116	require_ipv4
117	atf_check -s exit:0 -o ignore -e empty ping -c1 -t6 127.0.0.1
118}
119
120atf_test_case ping_c1t4
121ping6_c1t4_head()
122{
123	atf_set "descr" "-t4 is not interpreted as -t -4 by ping6"
124}
125ping6_c1t4_body()
126{
127	require_ipv6
128	atf_check -s exit:0 -o ignore -e empty ping6 -c1 -t4 ::1
129}
130
131atf_test_case ping_46
132ping_46_head()
133{
134	atf_set "descr" "-4 and -6 cannot be used simultaneously"
135}
136ping_46_body()
137{
138	require_ipv4
139	require_ipv6
140	atf_check -s exit:1 \
141	    -e match:"illegal option -- 6" \
142	    ping -4 -6 localhost
143}
144
145atf_test_case ping_64
146ping_64_head()
147{
148	atf_set "descr" "-4 and -6 cannot be used simultaneously"
149}
150ping_64_body()
151{
152	require_ipv4
153	require_ipv6
154	atf_check -s exit:1 \
155	    -e match:"illegal option -- 4" \
156	    ping -6 -4 localhost
157}
158
159atf_test_case ping6_4
160ping6_4_head()
161{
162	atf_set "descr" "ping6 does not accept -4"
163}
164ping6_4_body()
165{
166	require_ipv4
167	require_ipv6
168	atf_check -s exit:1 \
169	    -e match:"illegal option -- 4" \
170	    ping6 -4 localhost
171}
172
173atf_test_case ping_nohost
174ping_nohost_head()
175{
176	atf_set "descr" "ping a nonexistent host"
177}
178ping_nohost_body()
179{
180	atf_check -s exit:68 -e match:"cannot resolve" \
181	    ping nonexistent.in-addr.arpa.
182}
183
184atf_test_case ping4_nohost
185ping4_nohost_head()
186{
187	atf_set "descr" "ping -4 a nonexistent host"
188}
189ping4_nohost_body()
190{
191	atf_check -s exit:68 -e match:"cannot resolve" \
192	    ping -4 nonexistent.in-addr.arpa.
193}
194
195atf_test_case ping6_nohost
196ping6_nohost_head()
197{
198	atf_set "descr" "ping -6 a nonexistent host"
199}
200ping6_nohost_body()
201{
202	atf_check -s exit:68 -e match:"cannot resolve" \
203	    ping -6 nonexistent.in-addr.arpa.
204}
205
206atf_test_case "inject_opts" "cleanup"
207inject_opts_head()
208{
209	atf_set "descr" "Inject an ECHO REPLY with IP options"
210	atf_set "require.user" "root"
211	atf_set "require.progs" "python3" "scapy"
212}
213inject_opts_body()
214{
215	atf_check -s exit:0 -o match:"wrong total length" -o match:"NOP" python3 $(atf_get_srcdir)/injection.py opts
216}
217inject_opts_cleanup()
218{
219	ifconfig `cat tun.txt` destroy
220}
221
222atf_test_case "inject_pip" "cleanup"
223inject_pip_head()
224{
225	atf_set "descr" "Inject an ICMP error with a quoted packet with IP options"
226	atf_set "require.user" "root"
227	atf_set "require.progs" "python3" "scapy"
228}
229inject_pip_body()
230{
231	atf_check -s exit:2 -o match:"Destination Host Unreachable" -o match:"(01){40}" python3 $(atf_get_srcdir)/injection.py pip
232}
233inject_pip_cleanup()
234{
235	ifconfig `cat tun.txt` destroy
236}
237
238# This is redundant with the ping_ tests, but it serves to ensure that scapy.py
239# is working correctly.
240atf_test_case "inject_reply" "cleanup"
241inject_reply_head()
242{
243	atf_set "descr" "Basic ping test with packet injection"
244	atf_set "require.user" "root"
245	atf_set "require.progs" "python3" "scapy"
246}
247inject_reply_body()
248{
249	atf_check -s exit:0 -o match:"1 packets transmitted, 1 packets received" python3 $(atf_get_srcdir)/injection.py reply
250}
251inject_reply_cleanup()
252{
253	ifconfig `cat tun.txt` destroy
254}
255
256atf_init_test_cases()
257{
258	atf_add_test_case ping_c1_s56_t1
259	atf_add_test_case ping_c1_s56_t1_S127
260	atf_add_test_case ping_6_c1_s8_t1
261	atf_add_test_case ping_c1_s8_t1_S1
262	atf_add_test_case ping6_c1_s8_t1
263	atf_add_test_case ping_c1t6
264	atf_add_test_case ping6_c1t4
265	atf_add_test_case ping_46
266	atf_add_test_case ping_64
267	atf_add_test_case ping6_4
268	atf_add_test_case ping_nohost
269	atf_add_test_case ping4_nohost
270	atf_add_test_case ping6_nohost
271	atf_add_test_case inject_opts
272	atf_add_test_case inject_pip
273	atf_add_test_case inject_reply
274}
275
276check_ping_statistics()
277{
278	sed -e 's/0.[0-9]\{3\}//g' \
279	    -e 's/[1-9][0-9]*.[0-9]\{3\}//g' \
280	    -e 's/localhost ([0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{1,3\})/localhost/' \
281	    -e 's/from [0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{1,3\}/from/' \
282	    -e 's/ttl=[0-9][0-9]*/ttl=/' \
283	    -e 's/hlim=[0-9][0-9]*/hlim=/' \
284	    "$1" >"$1".filtered
285	atf_check -s exit:0 diff -u "$1".filtered "$2"
286}
287