xref: /openbsd/gnu/usr.bin/perl/lib/Net/hostent.t (revision 256a93a4)
1#!./perl -w
2
3BEGIN {
4    chdir 't' if -d 't';
5    @INC = '../lib';
6}
7
8use Test::More;
9
10BEGIN {
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    if ($^O eq 'irix' && $Config{osvers} == 5) {
18	plan skip_all => "Test relies on resolution of localhost, fails on $^O ($Config{osvers})";
19    }
20}
21
22use Test::More;
23
24BEGIN { use_ok 'Net::hostent' }
25
26# Remind me to add this to Test::More.
27sub DIE {
28    print "# @_\n";
29    exit 1;
30}
31
32# test basic resolution of localhost <-> 127.0.0.1
33use Socket;
34
35my $h = gethost('localhost');
36SKIP: {
37skip "Can't resolve localhost and you don't have /etc/hosts", 6
38    if (!defined($h) && !-e '/etc/hosts');
39
40ok(defined $h,  "gethost('localhost')") ||
41  DIE("Can't continue without working gethost: $!");
42
43is( inet_ntoa($h->addr), "127.0.0.1",   'addr from gethost' );
44
45my $i = gethostbyaddr(inet_aton("127.0.0.1"));
46ok(defined $i,  "gethostbyaddr('127.0.0.1')") ||
47  DIE("Can't continue without working gethostbyaddr: $!");
48
49is( inet_ntoa($i->addr), "127.0.0.1",   'addr from gethostbyaddr' );
50
51$i = gethost("127.0.0.1");
52ok(defined $i,  "gethost('127.0.0.1')");
53is( inet_ntoa($i->addr), "127.0.0.1",   'addr from gethost' );
54
55"127.0.0.1" =~ /(.*)/;
56$i = gethost($1);
57ok(defined $i, 'gethost on capture variable');
58
59# need to skip the name comparisons on Win32 because windows will
60# return the name of the machine instead of "localhost" when resolving
61# 127.0.0.1 or even "localhost"
62
63# - VMS returns "LOCALHOST" under tcp/ip services V4.1 ECO 2, possibly others
64# - OS/390 returns localhost.YADDA.YADDA
65
66SKIP: {
67    skip "Windows will return the machine name instead of 'localhost'", 2
68      if $^O eq 'MSWin32' or $^O eq 'cygwin';
69
70    print "# name = " . $h->name . ", aliases = " . join (",", @{$h->aliases}) . "\n";
71
72    my $in_alias;
73    unless ($h->name =~ /^localhost(?:\..+)?$/i) {
74        foreach (@{$h->aliases}) {
75            if (/^localhost(?:\..+)?$/i) {
76                $in_alias = 1;
77                last;
78            }
79        }
80	ok( $in_alias );
81    } else {
82	ok( 1 );
83    }
84
85    if ($in_alias) {
86        # If we found it in the aliases before, expect to find it there again.
87        foreach (@{$h->aliases}) {
88            if (/^localhost(?:\..+)?$/i) {
89                # This time, clear the flag if we see "localhost"
90                undef $in_alias;
91                last;
92            }
93        }
94    }
95
96    if( $in_alias ) {
97        like( $i->name, qr/^localhost(?:\..+)?$/i );
98    }
99    else {
100        ok( !$in_alias );
101        print "# " . $h->name . " " . join (",", @{$h->aliases}) . "\n";
102    }
103}
104}
105
106done_testing();
107