Cleanup and Cygwin98 oddity
[p5sagit/p5-mst-13.2.git] / lib / Net / hostent.t
1 #!./perl -w
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6 }
7
8 use Test::More tests => 7;
9
10 BEGIN {
11     require Config; import Config;
12     if ($Config{'extensions'} !~ /\bSocket\b/ && 
13         !(($^O eq 'VMS') && $Config{d_socket})) 
14     {
15         plan skip_all => "Test uses Socket, Socket not built";
16     }
17 }
18
19
20 BEGIN { use_ok 'Net::hostent' }
21
22 # Remind me to add this to Test::More.
23 sub BAILOUT {
24     print "Bail Out! @_\n";
25     exit;
26 }
27
28 # test basic resolution of localhost <-> 127.0.0.1
29 use Socket;
30
31 my $h = gethost('localhost');
32 ok(defined $h,  "gethost('localhost')") ||
33   BAILOUT("Can't continue without working gethost: $!");
34
35 is( inet_ntoa($h->addr), "127.0.0.1",   'addr from gethost' );
36
37 my $i = gethostbyaddr(inet_aton("127.0.0.1"));
38 ok(defined $i,  "gethostbyaddr('127.0.0.1')") || 
39   BAILOUT("Can't continue without working gethostbyaddr: $!");
40
41 is( inet_ntoa($i->addr), "127.0.0.1",   'addr from gethostbyaddr' );
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
47 # VMS returns "LOCALHOST" under tcp/ip services V4.1 ECO 2, possibly others
48 # OS/390 returns localhost.YADDA.YADDA
49
50 SKIP: {
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
54     my $in_alias;
55     unless ($h->name =~ /^localhost(?:\..+)?$/i) {
56         foreach (@{$h->aliases}) {
57             if (/^localhost(?:\..+)?$/i) {
58                 $in_alias = 1;
59                 last;
60             }
61         }
62     }
63     
64     unless( ok( $in_alias ) ) {
65         print "# ",$h->name, " ", join (",", @{$h->aliases}), "\n";
66     }
67
68     if ($in_alias) {
69         # If we found it in the aliases before, expect to find it there again.
70         foreach (@{$h->aliases}) {
71             if (/^localhost(?:\..+)?$/i) {
72                 # This time, clear the flag if we see "localhost"
73                 undef $in_alias;
74                 last;
75             }
76         }
77     } 
78
79     if( $in_alias ) {
80         like( $i->name, qr/^localhost(?:\..+)?$/i );
81     }
82     else {
83         ok( !$in_alias );
84         print "# ",$h->name, " ", join (",", @{$h->aliases}), "\n";
85     }
86 }