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