This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Sync with libnet-1.12
[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 }
e69a2255
JH
17 if ($^O eq 'MacOS') {
18 plan skip_all => "Test relies on resolution of localhost, fails on Mac OS";
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');
0dcb3408 36ok(defined $h, "gethost('localhost')") ||
c4fbe247 37 DIE("Can't continue without working gethost: $!");
fedf0971 38
0dcb3408 39is( inet_ntoa($h->addr), "127.0.0.1", 'addr from gethost' );
fedf0971 40
0dcb3408
MS
41my $i = gethostbyaddr(inet_aton("127.0.0.1"));
42ok(defined $i, "gethostbyaddr('127.0.0.1')") ||
c4fbe247 43 DIE("Can't continue without working gethostbyaddr: $!");
0dcb3408
MS
44
45is( 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
54SKIP: {
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}