CIDR checking now works and is tested

This commit is contained in:
Zane C. B-H 2019-07-08 00:26:48 -05:00
parent 008303cefa
commit 3a2572e50e
2 changed files with 49 additions and 3 deletions

View File

@ -91,7 +91,26 @@ sub match{
if ( ref( $object ) ne 'Net::Connection' ){
return 0;
}
my $cidrs_int=0;
while( defined( $self->{cidrs}[$cidrs_int] ) ){
if (
(
( $object->foreign_host ne '*' ) &&
( eval{ Net::CIDR::cidrlookup( $object->foreign_host, $self->{cidrs}[$cidrs_int] ) })
) ||
(
( $object->local_host ne '*' ) &&
( eval{ Net::CIDR::cidrlookup( $object->local_host, $self->{cidrs}[$cidrs_int] ) })
)
){
return 1;
}
$cidrs_int++;
}
return 0;
}
=head1 AUTHOR

View File

@ -3,11 +3,20 @@ use 5.006;
use strict;
use warnings;
use Test::More;
use Net::Connection;
BEGIN {
use_ok( 'Net::Connection::Match::CIDR' ) || print "Bail out!\n";
}
my $connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'22',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'ESTABLISHED',
};
my %bad_args=(
cidrs=>[
@ -55,11 +64,29 @@ eval{
};
ok( $returned eq '0', 'match undef check') or diag('match accepted undefined input');
# make sure it will not accept null input
# make sure it will not accept a improper ref type
$returned=1;
eval{
$returned=$cidr_checker->match($cidr_checker);
};
ok( $returned eq '0', 'match improper ref check') or diag('match accepted a ref other than Net::Connection');
done_testing(6);
# Create a connection with a matching CIDR and see if it matches
my $conn=Net::Connection->new( $connection_args );
$returned=0;
eval{
$returned=$cidr_checker->match( $conn );
};
ok( $returned eq '1', 'CIDR match check') or diag('Failed to match a matching good CIDR');
# Create a connection with a non-matching CIDR and make sure it does not match
$connection_args->{foreign_host}='1.1.1.1';
$connection_args->{local_host}='1.1.1.2';
$conn=Net::Connection->new( $connection_args );
$returned=1;
eval{
$returned=$cidr_checker->match( $conn );
};
ok( $returned eq '0', 'CIDR non-match check') or diag('Matched a CIDR that it should not of');
done_testing(8);