Commit | Line | Data |
---|---|---|
fedf0971 RS |
1 | #!./perl -w |
2 | ||
3 | BEGIN { | |
67b60da2 NC |
4 | chdir 't' if -d 't'; |
5 | @INC = '../lib'; | |
0dcb3408 MS |
6 | } |
7 | ||
e69a2255 | 8 | use Test::More; |
0dcb3408 MS |
9 | |
10 | BEGIN { | |
67b60da2 NC |
11 | require Config; import Config; |
12 | if ($Config{'extensions'} !~ /\bSocket\b/ && | |
0dcb3408 MS |
13 | !(($^O eq 'VMS') && $Config{d_socket})) |
14 | { | |
15 | plan skip_all => "Test uses Socket, Socket not built"; | |
67b60da2 | 16 | } |
64971c1d JH |
17 | if ($^O eq 'MacOS' || ($^O eq 'irix' && $Config{osvers} == 5)) { |
18 | plan skip_all => "Test relies on resolution of localhost, fails on $^O ($Config{osvers})"; | |
e69a2255 | 19 | } |
fedf0971 RS |
20 | } |
21 | ||
e69a2255 | 22 | use Test::More tests => 7; |
fedf0971 | 23 | |
0dcb3408 | 24 | BEGIN { use_ok 'Net::hostent' } |
fedf0971 | 25 | |
0dcb3408 | 26 | # Remind me to add this to Test::More. |
c4fbe247 MS |
27 | sub DIE { |
28 | print "# @_\n"; | |
29 | exit 1; | |
0dcb3408 | 30 | } |
fedf0971 RS |
31 | |
32 | # test basic resolution of localhost <-> 127.0.0.1 | |
33 | use Socket; | |
34 | ||
35 | my $h = gethost('localhost'); | |
0dcb3408 | 36 | ok(defined $h, "gethost('localhost')") || |
c4fbe247 | 37 | DIE("Can't continue without working gethost: $!"); |
fedf0971 | 38 | |
0dcb3408 | 39 | is( inet_ntoa($h->addr), "127.0.0.1", 'addr from gethost' ); |
fedf0971 | 40 | |
0dcb3408 MS |
41 | my $i = gethostbyaddr(inet_aton("127.0.0.1")); |
42 | ok(defined $i, "gethostbyaddr('127.0.0.1')") || | |
c4fbe247 | 43 | DIE("Can't continue without working gethostbyaddr: $!"); |
0dcb3408 MS |
44 | |
45 | is( inet_ntoa($i->addr), "127.0.0.1", 'addr from gethostbyaddr' ); | |
fedf0971 RS |
46 | |
47 | # need to skip the name comparisons on Win32 because windows will | |
48 | # return the name of the machine instead of "localhost" when resolving | |
49 | # 127.0.0.1 or even "localhost" | |
50 | ||
7e981d16 JH |
51 | # - VMS returns "LOCALHOST" under tcp/ip services V4.1 ECO 2, possibly others |
52 | # - OS/390 returns localhost.YADDA.YADDA | |
d26aeb84 | 53 | |
0dcb3408 MS |
54 | SKIP: { |
55 | skip "Windows will return the machine name instead of 'localhost'", 2 | |
56 | if $^O eq 'MSWin32' or $^O eq 'NetWare' or $^O eq 'cygwin'; | |
57 | ||
2f3b333f | 58 | print "# name = " . $h->name . ", aliases = " . join (",", @{$h->aliases}) . "\n"; |
7e981d16 | 59 | |
0dcb3408 MS |
60 | my $in_alias; |
61 | unless ($h->name =~ /^localhost(?:\..+)?$/i) { | |
62 | foreach (@{$h->aliases}) { | |
63 | if (/^localhost(?:\..+)?$/i) { | |
64 | $in_alias = 1; | |
65 | last; | |
66 | } | |
67 | } | |
7e981d16 JH |
68 | ok( $in_alias ); |
69 | } else { | |
70 | ok( 1 ); | |
0dcb3408 MS |
71 | } |
72 | ||
0dcb3408 MS |
73 | if ($in_alias) { |
74 | # If we found it in the aliases before, expect to find it there again. | |
75 | foreach (@{$h->aliases}) { | |
76 | if (/^localhost(?:\..+)?$/i) { | |
77 | # This time, clear the flag if we see "localhost" | |
78 | undef $in_alias; | |
79 | last; | |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | if( $in_alias ) { | |
85 | like( $i->name, qr/^localhost(?:\..+)?$/i ); | |
c9491a76 | 86 | } |
0dcb3408 MS |
87 | else { |
88 | ok( !$in_alias ); | |
2f3b333f | 89 | print "# " . $h->name . " " . join (",", @{$h->aliases}) . "\n"; |
c9491a76 | 90 | } |
fedf0971 | 91 | } |