summaryrefslogtreecommitdiff
path: root/lib/Net/hostent.t
blob: 3ee198098877dd9c81e2e6664feb76482013fee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!./perl -w

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
}

use Test::More;

BEGIN {
    require Config; import Config;
    if ($Config{'extensions'} !~ /\bSocket\b/ && 
        !(($^O eq 'VMS') && $Config{d_socket})) 
    {
	plan skip_all => "Test uses Socket, Socket not built";
    }
    if ($^O eq 'irix' && $Config{osvers} == 5) {
	plan skip_all => "Test relies on resolution of localhost, fails on $^O ($Config{osvers})";
    }
}

use Test::More;

BEGIN { use_ok 'Net::hostent' }

# Remind me to add this to Test::More.
sub DIE {
    print "# @_\n";
    exit 1;
}

# test basic resolution of localhost <-> 127.0.0.1
use Socket;

my $h = gethost('localhost');
SKIP: {
skip "Can't resolve localhost and you don't have /etc/hosts", 6
    if (!defined($h) && !-e '/etc/hosts');

ok(defined $h,  "gethost('localhost')") ||
  DIE("Can't continue without working gethost: $!");

is( inet_ntoa($h->addr), "127.0.0.1",   'addr from gethost' );

my $i = gethostbyaddr(inet_aton("127.0.0.1"));
ok(defined $i,  "gethostbyaddr('127.0.0.1')") || 
  DIE("Can't continue without working gethostbyaddr: $!");

is( inet_ntoa($i->addr), "127.0.0.1",   'addr from gethostbyaddr' );

$i = gethost("127.0.0.1");
ok(defined $i,  "gethost('127.0.0.1')");
is( inet_ntoa($i->addr), "127.0.0.1",   'addr from gethost' );

"127.0.0.1" =~ /(.*)/;
$i = gethost($1);
ok(defined $i, 'gethost on capture variable');

# need to skip the name comparisons on Win32 because windows will
# return the name of the machine instead of "localhost" when resolving
# 127.0.0.1 or even "localhost"

# - VMS returns "LOCALHOST" under tcp/ip services V4.1 ECO 2, possibly others
# - OS/390 returns localhost.YADDA.YADDA

SKIP: {
    skip "Windows will return the machine name instead of 'localhost'", 2
      if $^O eq 'MSWin32' or $^O eq 'cygwin';

    print "# name = " . $h->name . ", aliases = " . join (",", @{$h->aliases}) . "\n";

    my $in_alias;
    unless ($h->name =~ /^localhost(?:\..+)?$/i) {
        foreach (@{$h->aliases}) {
            if (/^localhost(?:\..+)?$/i) {
                $in_alias = 1;
                last;
            }
        }
	ok( $in_alias );
    } else {
	ok( 1 );
    }
    
    if ($in_alias) {
        # If we found it in the aliases before, expect to find it there again.
        foreach (@{$h->aliases}) {
            if (/^localhost(?:\..+)?$/i) {
                # This time, clear the flag if we see "localhost"
                undef $in_alias;
                last;
            }
        }
    } 

    if( $in_alias ) {
        like( $i->name, qr/^localhost(?:\..+)?$/i );
    }
    else {
        ok( !$in_alias );
        print "# " . $h->name . " " . join (",", @{$h->aliases}) . "\n";
    }
}
}

done_testing();