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