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