xref: /freebsd/sbin/ping/tests/ping_test.sh (revision 766145637dd5f1316c2ac4a20956e1b17bf8df42)
1#
2# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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# $FreeBSD$
29
30require_ipv4()
31{
32	if ! getaddrinfo -f inet localhost 1>/dev/null 2>&1; then
33		atf_skip "IPv4 is not configured"
34	fi
35}
36require_ipv6()
37{
38	if ! getaddrinfo -f inet6 localhost 1>/dev/null 2>&1; then
39		atf_skip "IPv6 is not configured"
40	fi
41}
42
43atf_test_case ping_c1_s56_t1
44ping_c1_s56_t1_head()
45{
46	atf_set "descr" "Stop after receiving 1 ECHO_RESPONSE packet"
47}
48ping_c1_s56_t1_body()
49{
50	require_ipv4
51	atf_check -s exit:0 -o save:std.out -e empty \
52	    ping -4 -c 1 -s 56 -t 1 localhost
53	check_ping_statistics std.out $(atf_get_srcdir)/ping_c1_s56_t1.out
54}
55
56atf_test_case ping_c1_s56_t1_S127
57ping_c1_s56_t1_S127_head()
58{
59	atf_set "descr" "Check that ping -S 127.0.0.1 localhost succeeds"
60}
61ping_c1_s56_t1_S127_body()
62{
63	require_ipv4
64	require_ipv6
65	atf_check -s exit:0 -o save:std.out -e empty \
66	    ping -c 1 -s 56 -t 1 -S 127.0.0.1 localhost
67	check_ping_statistics std.out $(atf_get_srcdir)/ping_c1_s56_t1_S127.out
68}
69
70atf_test_case ping_6_c1_s8_t1
71ping_6_c1_s8_t1_head()
72{
73	atf_set "descr" "Stop after receiving 1 ECHO_RESPONSE packet"
74}
75ping_6_c1_s8_t1_body()
76{
77	require_ipv6
78	atf_check -s exit:0 -o save:std.out -e empty \
79	    ping -6 -c 1 -s 8 -t 1 localhost
80	check_ping_statistics std.out $(atf_get_srcdir)/ping_6_c1_s8_t1.out
81}
82
83atf_test_case ping_c1_s8_t1_S1
84ping_c1_s8_t1_S1_head()
85{
86	atf_set "descr" "Check that ping -S ::1 localhost succeeds"
87}
88ping_c1_s8_t1_S1_body()
89{
90	require_ipv4
91	require_ipv6
92	atf_check -s exit:0 -o save:std.out -e empty \
93	    ping -c 1 -s 8 -t 1 -S ::1 localhost
94	check_ping_statistics std.out $(atf_get_srcdir)/ping_c1_s8_t1_S1.out
95}
96
97atf_test_case ping6_c1_s8_t1
98ping6_c1_s8_t1_head()
99{
100	atf_set "descr" "Use IPv6 when invoked as ping6"
101}
102ping6_c1_s8_t1_body()
103{
104	require_ipv6
105	atf_check -s exit:0 -o save:std.out -e empty \
106	    ping6 -c 1 -s 8 -t 1 localhost
107	check_ping_statistics std.out $(atf_get_srcdir)/ping_6_c1_s8_t1.out
108}
109
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
120ping6_c1t4_head()
121{
122	atf_set "descr" "-t4 is not interpreted as -t -4 by ping6"
123}
124ping6_c1t4_body()
125{
126	require_ipv6
127	atf_check -s exit:0 -o ignore -e empty ping6 -c1 -t4 ::1
128}
129
130ping_46_head()
131{
132	atf_set "descr" "-4 and -6 cannot be used simultaneously"
133}
134ping_46_body()
135{
136	require_ipv4
137	require_ipv6
138	atf_check -s exit:1 \
139	    -e match:"-4 and -6 cannot be used simultaneously" \
140	    ping -4 -6 localhost
141}
142
143ping6_46_head()
144{
145	atf_set "descr" "-4 and -6 cannot be used simultaneously"
146}
147ping6_46_body()
148{
149	require_ipv4
150	require_ipv6
151	atf_check -s exit:1 \
152	    -e match:"-4 and -6 cannot be used simultaneously" \
153	    ping6 -4 -6 localhost
154}
155
156atf_init_test_cases()
157{
158	atf_add_test_case ping_c1_s56_t1
159	atf_add_test_case ping_c1_s56_t1_S127
160	atf_add_test_case ping_6_c1_s8_t1
161	atf_add_test_case ping_c1_s8_t1_S1
162	atf_add_test_case ping6_c1_s8_t1
163	atf_add_test_case ping_c1t6
164	atf_add_test_case ping6_c1t4
165	atf_add_test_case ping_46
166	atf_add_test_case ping6_46
167}
168
169check_ping_statistics()
170{
171	sed -e 's/0.[0-9]\{3\}//g' \
172	    -e 's/[1-9][0-9]*.[0-9]\{3\}//g' \
173	    -e 's/localhost ([0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{1,3\})/localhost/' \
174	    -e 's/from [0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{1,3\}/from/' \
175	    -e 's/ttl=[0-9][0-9]*/ttl=/' \
176	    -e 's/hlim=[0-9][0-9]*/hlim=/' \
177	    "$1" >"$1".filtered
178	atf_check -s exit:0 diff -u "$1".filtered "$2"
179}
180