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_test_case "inject_opts" "cleanup" 157inject_opts_head() 158{ 159 atf_set "descr" "Inject an ECHO REPLY with IP options" 160 atf_set "require.user" "root" 161 atf_set "require.progs" "python3" "scapy" 162} 163inject_opts_body() 164{ 165 atf_check -s exit:0 -o match:"wrong total length" -o match:"NOP" python3 $(atf_get_srcdir)/injection.py opts 166} 167inject_opts_cleanup() 168{ 169 ifconfig `cat tun.txt` destroy 170} 171 172atf_test_case "inject_pip" "cleanup" 173inject_pip_head() 174{ 175 atf_set "descr" "Inject an ICMP error with a quoted packet with IP options" 176 atf_set "require.user" "root" 177 atf_set "require.progs" "python3" "scapy" 178} 179inject_pip_body() 180{ 181 atf_check -s exit:2 -o match:"Destination Host Unreachable" -o match:"(01){40}" python3 $(atf_get_srcdir)/injection.py pip 182} 183inject_pip_cleanup() 184{ 185 ifconfig `cat tun.txt` destroy 186} 187 188# This is redundant with the ping_ tests, but it serves to ensure that scapy.py 189# is working correctly. 190atf_test_case "inject_reply" "cleanup" 191inject_reply_head() 192{ 193 atf_set "descr" "Basic ping test with packet injection" 194 atf_set "require.user" "root" 195 atf_set "require.progs" "python3" "scapy" 196} 197inject_reply_body() 198{ 199 atf_check -s exit:0 -o match:"1 packets transmitted, 1 packets received" python3 $(atf_get_srcdir)/injection.py reply 200} 201inject_reply_cleanup() 202{ 203 ifconfig `cat tun.txt` destroy 204} 205 206atf_init_test_cases() 207{ 208 atf_add_test_case ping_c1_s56_t1 209 atf_add_test_case ping_c1_s56_t1_S127 210 atf_add_test_case ping_6_c1_s8_t1 211 atf_add_test_case ping_c1_s8_t1_S1 212 atf_add_test_case ping6_c1_s8_t1 213 atf_add_test_case ping_c1t6 214 atf_add_test_case ping6_c1t4 215 atf_add_test_case ping_46 216 atf_add_test_case ping6_46 217 atf_add_test_case inject_opts 218 atf_add_test_case inject_pip 219 atf_add_test_case inject_reply 220} 221 222check_ping_statistics() 223{ 224 sed -e 's/0.[0-9]\{3\}//g' \ 225 -e 's/[1-9][0-9]*.[0-9]\{3\}//g' \ 226 -e 's/localhost ([0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{1,3\})/localhost/' \ 227 -e 's/from [0-9]\{1,3\}\(\.[0-9]\{1,3\}\)\{1,3\}/from/' \ 228 -e 's/ttl=[0-9][0-9]*/ttl=/' \ 229 -e 's/hlim=[0-9][0-9]*/hlim=/' \ 230 "$1" >"$1".filtered 231 atf_check -s exit:0 diff -u "$1".filtered "$2" 232} 233