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