|
| 1 | +#!/usr/bin/perl -w |
| 2 | +# SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | +# Copyright (c) 2025 Petr Vorel <pvorel@suse.cz> |
| 4 | + |
| 5 | +use Socket qw(AF_INET AF_INET6 AF_UNSPEC AI_IDN AI_CANONIDN AI_CANONNAME IPPROTO_UDP SOCK_STREAM); |
| 6 | +use Socket::GetAddrInfo qw(getaddrinfo); |
| 7 | +use Test::Command tests => 11; |
| 8 | +use Test::More; |
| 9 | + |
| 10 | +my $lib = File::Basename::dirname(Cwd::abs_path($0)) . '/../lib.pl'; |
| 11 | +require "$lib"; |
| 12 | + |
| 13 | +my $ping = get_cmd($ARGV[0] // 'ping'); |
| 14 | + |
| 15 | +# Mock getaddrinfo() related code in ping/ping.c main() followed by logic in |
| 16 | +# ping4_run() and ping6_run(). |
| 17 | +sub get_target |
| 18 | +{ |
| 19 | + my $target = shift; |
| 20 | + my $target_ai_family = shift // AF_UNSPEC; |
| 21 | + |
| 22 | + # ping/ping.c main(): getaddrinfo() to detect IPv4 vs. IPv6 |
| 23 | + my $hints = { |
| 24 | + flags => AI_CANONNAME, |
| 25 | + family => $target_ai_family, |
| 26 | + socktype => SOCK_STREAM, |
| 27 | + }; |
| 28 | + |
| 29 | + die "invalid family: '$target_ai_family'" unless ($target_ai_family == AF_INET |
| 30 | + || $target_ai_family == AF_INET6 || $target_ai_family == AF_UNSPEC); |
| 31 | + |
| 32 | + my ($err, @res) = getaddrinfo($target, undef, $hints); |
| 33 | + die "getaddrinfo error: $err\n" if $err; |
| 34 | + |
| 35 | + foreach my $ai (@res) { |
| 36 | + next if $target_ai_family != AF_UNSPEC && $target_ai_family != $ai->{family}; |
| 37 | + |
| 38 | + # ping/ping.c ping6_run(): AF_INET6 does not perform getaddrinfo() |
| 39 | + return $target if ($ai->{family} == AF_INET6); |
| 40 | + |
| 41 | + # ping/ping6_common.c ping4_run(): AF_INET has extra getaddrinfo() to |
| 42 | + # get canonname. |
| 43 | + $hints = { |
| 44 | + ai_family => AF_INET, |
| 45 | + ai_protocol => IPPROTO_UDP, |
| 46 | + # getaddrinfo_flags: USE_IDN=true adds AI_IDN | AI_CANONIDN |
| 47 | + ai_flags => AI_CANONNAME | AI_IDN | AI_CANONIDN, |
| 48 | + }; |
| 49 | + my ($err, @res) = getaddrinfo($target, undef, $hints); |
| 50 | + die "getaddrinfo error: $err\n" if $err; |
| 51 | + |
| 52 | + return defined($ai->{canonname}) ? $ai->{canonname} : $target; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +# -V |
| 57 | +{ |
| 58 | + my $cmd = Test::Command->new(cmd => "$ping -V"); |
| 59 | + $cmd->exit_is_num(0); |
| 60 | + subtest 'output' => sub { |
| 61 | + $cmd->stdout_like(qr/^ping from iputils /, 'Print version'); |
| 62 | + $cmd->stdout_like(qr/libcap: (yes|no), IDN: (yes|no), NLS: (yes|no), error.h: (yes|no), getrandom\(\): (yes|no), __fpending\(\): (yes|no)$/, 'Print config'); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +# 127.0.0.1 |
| 67 | +{ |
| 68 | + my $cmd = Test::Command->new(cmd => "$ping -c1 127.0.0.1"); |
| 69 | + $cmd->exit_is_num(0); |
| 70 | + subtest 'output' => sub { |
| 71 | + $cmd->stdout_like(qr/64 bytes from 127\.0\.0\.1/, 'Ping received from 127.0.0.1'); |
| 72 | + $cmd->stdout_like(qr/0% packet loss/, 'No packet loss'); |
| 73 | + $cmd->stdout_like(qr/time=\d+\.\d+ ms/, 'Ping time present'); |
| 74 | + $cmd->stdout_like(qr~rtt min/avg/max/mdev = \d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3} ms$~, |
| 75 | + 'RTT time present'); |
| 76 | + $cmd->stdout_like(qr{^PING 127\.0\.0\.1 \(127\.0\.0\.1\) 56\(84\) bytes of data\. |
| 77 | +64 bytes from 127\.0\.0\.1: icmp_seq=1 ttl=\d+ time=\d\.\d{3} ms |
| 78 | +
|
| 79 | +--- 127.0.0.1 ping statistics --- |
| 80 | +1 packets transmitted, 1 received, 0% packet loss, time \d+ms |
| 81 | +rtt min/avg/max/mdev = \d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3} ms$}, |
| 82 | +'Entire ping output matched exactly'); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +# ::1 |
| 87 | +SKIP: { |
| 88 | + if ($ENV{SKIP_IPV6}) { |
| 89 | + skip 'IPv6 tests', 2; |
| 90 | + } |
| 91 | + my $cmd = Test::Command->new(cmd => "$ping -c1 ::1"); |
| 92 | + $cmd->exit_is_num(0); |
| 93 | + subtest 'output' => sub { |
| 94 | + $cmd->stdout_like(qr/64 bytes from ::1/, 'Ping received from ::1'); |
| 95 | + $cmd->stdout_like(qr/0% packet loss/, 'No packet loss'); |
| 96 | + $cmd->stdout_like(qr/time=\d+\.\d+ ms/, 'Ping time present'); |
| 97 | + $cmd->stdout_like(qr~rtt min/avg/max/mdev = \d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3} ms$~, |
| 98 | + 'RTT time present'); |
| 99 | + $cmd->stdout_like(qr{^PING ::1 \(::1\) 56 data bytes |
| 100 | +64 bytes from ::1: icmp_seq=1 ttl=\d+ time=\d\.\d{3} ms |
| 101 | +
|
| 102 | +--- ::1 ping statistics --- |
| 103 | +1 packets transmitted, 1 received, 0% packet loss, time \d+ms |
| 104 | +rtt min/avg/max/mdev = \d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3} ms$}, |
| 105 | +'Entire ping output matched exactly'); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +my $localhost = "localhost"; |
| 110 | +my $localhost_target_ipv4 = get_target($localhost, AF_INET); |
| 111 | +my $localhost_target_ipv6 = get_target($localhost, AF_INET6); |
| 112 | +diag("localhost_cannon_ipv4: '$localhost_target_ipv4'"); |
| 113 | +diag("localhost_cannon_ipv6: '$localhost_target_ipv6'"); |
| 114 | +die "Undefined cannonical name for $localhost on IPv4" unless defined $localhost_target_ipv4; |
| 115 | +die "Undefined cannonical name for $localhost on IPv6" unless defined $localhost_target_ipv6; |
| 116 | + |
| 117 | +# localhost |
| 118 | +{ |
| 119 | + my $cmd = Test::Command->new(cmd => "$ping -c1 $localhost"); |
| 120 | + $cmd->exit_is_num(0); |
| 121 | +} |
| 122 | + |
| 123 | +# -4 localhost |
| 124 | +{ |
| 125 | + my $cmd = Test::Command->new(cmd => "$ping -c1 -4 $localhost"); |
| 126 | + $cmd->exit_is_num(0); |
| 127 | + subtest 'output' => sub { |
| 128 | + $cmd->stdout_like(qr/64 bytes from $localhost_target_ipv4 \(127\.0\.0\.1\)/, "Ping received from $localhost (IPv4)"); |
| 129 | + $cmd->stdout_like(qr/0% packet loss/, 'No packet loss'); |
| 130 | + $cmd->stdout_like(qr/time=\d+\.\d+ ms/, 'Ping time present'); |
| 131 | + $cmd->stdout_like(qr~rtt min/avg/max/mdev = \d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3} ms$~, |
| 132 | + 'RTT time present'); |
| 133 | + $cmd->stdout_like(qr{^PING $localhost_target_ipv4 \(127\.0\.0\.1\) 56\(84\) bytes of data\. |
| 134 | +64 bytes from $localhost_target_ipv4 \(127\.0\.0\.1\): icmp_seq=1 ttl=\d+ time=\d\.\d{3} ms |
| 135 | +
|
| 136 | +--- $localhost_target_ipv4 ping statistics --- |
| 137 | +1 packets transmitted, 1 received, 0% packet loss, time \d+ms |
| 138 | +rtt min/avg/max/mdev = \d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3} ms$}, |
| 139 | +'Entire ping output matched exactly'); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +# -6 localhost |
| 144 | +SKIP: { |
| 145 | + if ($ENV{SKIP_IPV6}) { |
| 146 | + skip 'IPv6 tests', 2; |
| 147 | + } |
| 148 | + my $cmd = Test::Command->new(cmd => "$ping -c1 -6 $localhost"); |
| 149 | + $cmd->exit_is_num(0); |
| 150 | + subtest 'output' => sub { |
| 151 | + $cmd->stdout_like(qr/64 bytes from $localhost_target_ipv6 \(::1\)/, "Ping received from $localhost (IPv6)"); |
| 152 | + $cmd->stdout_like(qr/0% packet loss/, 'No packet loss'); |
| 153 | + $cmd->stdout_like(qr/time=\d+\.\d+ ms/, 'Ping time present'); |
| 154 | + $cmd->stdout_like(qr~rtt min/avg/max/mdev = \d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3}/\d+\.\d{3} ms$~, |
| 155 | + 'RTT time present'); |
| 156 | + } |
| 157 | +} |
0 commit comments