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