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