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