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