xref: /freebsd/sbin/ping/tests/ping_test.sh (revision 815b7436a7c6302365b6514194d27d41cb736227)
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
109ping_c1t6_head()
110{
111	atf_set "descr" "-t6 is not interpreted as -t -6 by ping"
112}
113ping_c1t6_body()
114{
115	require_ipv4
116	atf_check -s exit:0 -o ignore -e empty ping -c1 -t6 127.0.0.1
117}
118
119ping6_c1t4_head()
120{
121	atf_set "descr" "-t4 is not interpreted as -t -4 by ping6"
122}
123ping6_c1t4_body()
124{
125	require_ipv6
126	atf_check -s exit:0 -o ignore -e empty ping6 -c1 -t4 ::1
127}
128
129ping_46_head()
130{
131	atf_set "descr" "-4 and -6 cannot be used simultaneously"
132}
133ping_46_body()
134{
135	require_ipv4
136	require_ipv6
137	atf_check -s exit:1 \
138	    -e match:"-4 and -6 cannot be used simultaneously" \
139	    ping -4 -6 localhost
140}
141
142ping6_46_head()
143{
144	atf_set "descr" "-4 and -6 cannot be used simultaneously"
145}
146ping6_46_body()
147{
148	require_ipv4
149	require_ipv6
150	atf_check -s exit:1 \
151	    -e match:"-4 and -6 cannot be used simultaneously" \
152	    ping6 -4 -6 localhost
153}
154
155atf_test_case "inject_opts" "cleanup"
156inject_opts_head()
157{
158	atf_set "descr" "Inject an ECHO REPLY with IP options"
159	atf_set "require.user" "root"
160	atf_set "require.progs" "python3" "scapy"
161}
162inject_opts_body()
163{
164	atf_check -s exit:0 -o match:"wrong total length" -o match:"NOP" python3 $(atf_get_srcdir)/injection.py opts
165}
166inject_opts_cleanup()
167{
168	ifconfig `cat tun.txt` destroy
169}
170
171atf_test_case "inject_pip" "cleanup"
172inject_pip_head()
173{
174	atf_set "descr" "Inject an ICMP error with a quoted packet with IP options"
175	atf_set "require.user" "root"
176	atf_set "require.progs" "python3" "scapy"
177}
178inject_pip_body()
179{
180	atf_check -s exit:2 -o match:"Destination Host Unreachable" -o match:"(01){40}" python3 $(atf_get_srcdir)/injection.py pip
181}
182inject_pip_cleanup()
183{
184	ifconfig `cat tun.txt` destroy
185}
186
187# This is redundant with the ping_ tests, but it serves to ensure that scapy.py
188# is working correctly.
189atf_test_case "inject_reply" "cleanup"
190inject_reply_head()
191{
192	atf_set "descr" "Basic ping test with packet injection"
193	atf_set "require.user" "root"
194	atf_set "require.progs" "python3" "scapy"
195}
196inject_reply_body()
197{
198	atf_check -s exit:0 -o match:"1 packets transmitted, 1 packets received" python3 $(atf_get_srcdir)/injection.py reply
199}
200inject_reply_cleanup()
201{
202	ifconfig `cat tun.txt` destroy
203}
204
205atf_init_test_cases()
206{
207	atf_add_test_case ping_c1_s56_t1
208	atf_add_test_case ping_c1_s56_t1_S127
209	atf_add_test_case ping_6_c1_s8_t1
210	atf_add_test_case ping_c1_s8_t1_S1
211	atf_add_test_case ping6_c1_s8_t1
212	atf_add_test_case ping_c1t6
213	atf_add_test_case ping6_c1t4
214	atf_add_test_case ping_46
215	atf_add_test_case ping6_46
216	atf_add_test_case inject_opts
217	atf_add_test_case inject_pip
218	atf_add_test_case inject_reply
219}
220
221check_ping_statistics()
222{
223	sed -e 's/0.[0-9]\{3\}//g' \
224	    -e 's/[1-9][0-9]*.[0-9]\{3\}//g' \
225	    -e 's/localhost ([0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{1,3\})/localhost/' \
226	    -e 's/from [0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{1,3\}/from/' \
227	    -e 's/ttl=[0-9][0-9]*/ttl=/' \
228	    -e 's/hlim=[0-9][0-9]*/hlim=/' \
229	    "$1" >"$1".filtered
230	atf_check -s exit:0 diff -u "$1".filtered "$2"
231}
232