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