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