1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
|
#!/usr/bin/perl
# Attempt some traceroutes using the system traceroute. They aren't
# all guaranteed to work, since OS issues, parsability of traceroute,
# and network configuration all interact with this test, and we
# frequently can't predict the issues.
use strict;
use warnings;
use Test::More;
use Net::Traceroute;
use Socket;
use Sys::Hostname;
require "t/testlib.pl";
os_must_unixexec();
####
# Probe PATH, plus some well known locations, for a traceroute
# program. skip_all this test if we can't find one.
my @path = split(":", $ENV{PATH});
my $has_traceroute;
foreach my $component (@path) {
if(-x "$component/traceroute") {
$has_traceroute = 1;
last;
}
}
if(!defined($has_traceroute)) {
# Check for traceroute in /usr/sbin or /sbin. The check is
# redundant if PATH already contains one of them, but it won't hurt.
foreach my $component ("/usr/sbin", "/sbin") {
if(-x "$component/traceroute") {
$ENV{PATH} .= join(":", @path, $component);
goto runtest;
}
}
plan skip_all => "Cannot find a traceroute executable";
}
runtest:
plan tests => 2;
####
# Get this sytem's hostname, and traceroute to it. Don't bother
# trying localhost; its quirky on systems like netbsd.
my $name = hostname();
# Use 127.0.0.1, the hostname might not be in /etc/hosts; cf. #1002229
$name = '127.0.0.1';
# Wrinkle: while our specification is that we will use whatever
# traceroute is in path, it's pretty common for testing to be done
# where there is no traceroute in path (especially automated testers).
my $tr1 = eval { Net::Traceroute->new(host => $name, timeout => 30) };
if($@) {
die unless(exists($ENV{AUTOMATED_TESTING}));
# If we're in an automated tester, rerun with debug => 9 so we get
# a better clue of what's going wrong.
$tr1 = Net::Traceroute->new(host => $name, timeout => 30, debug => 9);
}
my $packed_addr = inet_aton($name);
my $addr = inet_ntoa($packed_addr);
TODO: {
local $TODO = 'Tests are fragile and fail with 2 IP addresses or no IPv4 or no DNS ...';
is($tr1->hops, 1);
is($tr1->hop_query_host(1, 0), $addr);
}
|