This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
further sync blead with Net-Ping-2.36
[perl5.git] / lib / Net / hostent.t
CommitLineData
fedf0971
RS
1#!./perl -w
2
3BEGIN {
67b60da2
NC
4 chdir 't' if -d 't';
5 @INC = '../lib';
0dcb3408
MS
6}
7
e69a2255 8use Test::More;
0dcb3408
MS
9
10BEGIN {
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 22use Test::More tests => 7;
fedf0971 23
0dcb3408 24BEGIN { use_ok 'Net::hostent' }
fedf0971 25
0dcb3408 26# Remind me to add this to Test::More.
c4fbe247
MS
27sub DIE {
28 print "# @_\n";
29 exit 1;
0dcb3408 30}
fedf0971
RS
31
32# test basic resolution of localhost <-> 127.0.0.1
33use Socket;
34
35my $h = gethost('localhost');
932dfdf1
DC
36SKIP: {
37skip "Can't resolve localhost and you don't have /etc/hosts", 6
38 if (!defined($h) && !-e '/etc/hosts');
39
0dcb3408 40ok(defined $h, "gethost('localhost')") ||
c4fbe247 41 DIE("Can't continue without working gethost: $!");
fedf0971 42
0dcb3408 43is( inet_ntoa($h->addr), "127.0.0.1", 'addr from gethost' );
fedf0971 44
0dcb3408
MS
45my $i = gethostbyaddr(inet_aton("127.0.0.1"));
46ok(defined $i, "gethostbyaddr('127.0.0.1')") ||
c4fbe247 47 DIE("Can't continue without working gethostbyaddr: $!");
0dcb3408
MS
48
49is( inet_ntoa($i->addr), "127.0.0.1", 'addr from gethostbyaddr' );
fedf0971
RS
50
51# need to skip the name comparisons on Win32 because windows will
52# return the name of the machine instead of "localhost" when resolving
53# 127.0.0.1 or even "localhost"
54
7e981d16
JH
55# - VMS returns "LOCALHOST" under tcp/ip services V4.1 ECO 2, possibly others
56# - OS/390 returns localhost.YADDA.YADDA
d26aeb84 57
0dcb3408
MS
58SKIP: {
59 skip "Windows will return the machine name instead of 'localhost'", 2
60 if $^O eq 'MSWin32' or $^O eq 'NetWare' or $^O eq 'cygwin';
61
2f3b333f 62 print "# name = " . $h->name . ", aliases = " . join (",", @{$h->aliases}) . "\n";
7e981d16 63
0dcb3408
MS
64 my $in_alias;
65 unless ($h->name =~ /^localhost(?:\..+)?$/i) {
66 foreach (@{$h->aliases}) {
67 if (/^localhost(?:\..+)?$/i) {
68 $in_alias = 1;
69 last;
70 }
71 }
7e981d16
JH
72 ok( $in_alias );
73 } else {
74 ok( 1 );
0dcb3408
MS
75 }
76
0dcb3408
MS
77 if ($in_alias) {
78 # If we found it in the aliases before, expect to find it there again.
79 foreach (@{$h->aliases}) {
80 if (/^localhost(?:\..+)?$/i) {
81 # This time, clear the flag if we see "localhost"
82 undef $in_alias;
83 last;
84 }
85 }
86 }
87
88 if( $in_alias ) {
89 like( $i->name, qr/^localhost(?:\..+)?$/i );
c9491a76 90 }
0dcb3408
MS
91 else {
92 ok( !$in_alias );
2f3b333f 93 print "# " . $h->name . " " . join (",", @{$h->aliases}) . "\n";
c9491a76 94 }
fedf0971 95}
932dfdf1 96}