LC_ALL might trump LANG.
[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 }
1a8dcddb 8 if (!eval "require Socket") {
9 print "1..0 # no Socket\n"; exit 0;
10 }
8b14f033 11 if (ord('A') == 193 && !eval "require Convert::EBCDIC") {
12 print "1..0 # EBCDIC but no Convert::EBCDIC\n"; exit 0;
13 }
0d375cdb 14 $INC{'Socket.pm'} = 1;
c8570720 15}
16
0d375cdb 17package Socket;
18
19sub import {
20 my $pkg = caller();
21 no strict 'refs';
22 *{ $pkg . '::inet_aton' } = \&inet_aton;
23 *{ $pkg . '::inet_ntoa' } = \&inet_ntoa;
24}
25
26my $fail = 0;
27my %names;
28
29sub set_fail {
30 $fail = shift;
31}
32
33sub inet_aton {
34 return if $fail;
35 my $num = unpack('N', pack('C*', split(/\./, $_[0])));
36 $names{$num} = $_[0];
37 return $num;
38}
39
40sub inet_ntoa {
41 return if $fail;
42 return $names{$_[0]};
43}
44
45package main;
46
47
c8570720 48(my $libnet_t = __FILE__) =~ s/config.t/libnet_t.pl/;
49require $libnet_t;
50
51print "1..10\n";
52
53use Net::Config;
54ok( exists $INC{'Net/Config.pm'}, 'Net::Config should have been used' );
55ok( keys %NetConfig, '%NetConfig should be imported' );
56
0d375cdb 57Socket::set_fail(1);
c8570720 58undef $NetConfig{'ftp_firewall'};
59is( Net::Config->requires_firewall(), 0,
60 'requires_firewall() should return 0 without ftp_firewall defined' );
61
62$NetConfig{'ftp_firewall'} = 1;
0d375cdb 63is( Net::Config->requires_firewall('a.host.not.there'), -1,
c8570720 64 '... should return -1 without a valid hostname' );
65
0d375cdb 66Socket::set_fail(0);
c8570720 67delete $NetConfig{'local_netmask'};
68is( Net::Config->requires_firewall('127.0.0.1'), 0,
69 '... should return 0 without local_netmask defined' );
70
71$NetConfig{'local_netmask'} = '127.0.0.1/24';
72is( Net::Config->requires_firewall('127.0.0.1'), 0,
73 '... should return false if host is within netmask' );
74is( Net::Config->requires_firewall('192.168.10.0'), 1,
75 '... should return true if host is outside netmask' );
76
77# now try more netmasks
78$NetConfig{'local_netmask'} = [ '127.0.0.1/24', '10.0.0.0/8' ];
79is( Net::Config->requires_firewall('10.10.255.254'), 0,
80 '... should find success with mutiple local netmasks' );
81is( Net::Config->requires_firewall('192.168.10.0'), 1,
82 '... should handle failure with multiple local netmasks' );
83
84is( \&Net::Config::is_external, \&Net::Config::requires_firewall,
85 'is_external() should be an alias for requires_firewall()' );