Just sorting to guarantee order is not enough.
[p5sagit/p5-mst-13.2.git] / lib / Net / t / config.t
CommitLineData
c8570720 1#!./perl -w
2
3BEGIN {
4 if ($ENV{PERL_CORE}) {
5 chdir 't' if -d 't';
6 @INC = '../lib';
7 }
0d375cdb 8 $INC{'Socket.pm'} = 1;
c8570720 9}
10
0d375cdb 11package Socket;
12
13sub import {
14 my $pkg = caller();
15 no strict 'refs';
16 *{ $pkg . '::inet_aton' } = \&inet_aton;
17 *{ $pkg . '::inet_ntoa' } = \&inet_ntoa;
18}
19
20my $fail = 0;
21my %names;
22
23sub set_fail {
24 $fail = shift;
25}
26
27sub inet_aton {
28 return if $fail;
29 my $num = unpack('N', pack('C*', split(/\./, $_[0])));
30 $names{$num} = $_[0];
31 return $num;
32}
33
34sub inet_ntoa {
35 return if $fail;
36 return $names{$_[0]};
37}
38
39package main;
40
41
c8570720 42(my $libnet_t = __FILE__) =~ s/config.t/libnet_t.pl/;
43require $libnet_t;
44
45print "1..10\n";
46
47use Net::Config;
48ok( exists $INC{'Net/Config.pm'}, 'Net::Config should have been used' );
49ok( keys %NetConfig, '%NetConfig should be imported' );
50
0d375cdb 51Socket::set_fail(1);
c8570720 52undef $NetConfig{'ftp_firewall'};
53is( Net::Config->requires_firewall(), 0,
54 'requires_firewall() should return 0 without ftp_firewall defined' );
55
56$NetConfig{'ftp_firewall'} = 1;
0d375cdb 57is( Net::Config->requires_firewall('a.host.not.there'), -1,
c8570720 58 '... should return -1 without a valid hostname' );
59
0d375cdb 60Socket::set_fail(0);
c8570720 61delete $NetConfig{'local_netmask'};
62is( Net::Config->requires_firewall('127.0.0.1'), 0,
63 '... should return 0 without local_netmask defined' );
64
65$NetConfig{'local_netmask'} = '127.0.0.1/24';
66is( Net::Config->requires_firewall('127.0.0.1'), 0,
67 '... should return false if host is within netmask' );
68is( Net::Config->requires_firewall('192.168.10.0'), 1,
69 '... should return true if host is outside netmask' );
70
71# now try more netmasks
72$NetConfig{'local_netmask'} = [ '127.0.0.1/24', '10.0.0.0/8' ];
73is( Net::Config->requires_firewall('10.10.255.254'), 0,
74 '... should find success with mutiple local netmasks' );
75is( Net::Config->requires_firewall('192.168.10.0'), 1,
76 '... should handle failure with multiple local netmasks' );
77
78is( \&Net::Config::is_external, \&Net::Config::requires_firewall,
79 'is_external() should be an alias for requires_firewall()' );