add states checking as well as related tests

This commit is contained in:
Zane C. B-H 2019-07-08 01:50:02 -05:00
parent f94a63b192
commit 520f5f4874
3 changed files with 97 additions and 0 deletions

View File

@ -128,6 +128,15 @@ sub match{
return 0;
}
my $states_int=0;
while( defined( $self->{states}[$states_int] ) ){
if ( $self->{states}[$states_int] eq lc( $object->state ) ){
return 1;
}
$states_int++;
}
return 0;
}

View File

@ -0,0 +1,13 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
plan tests => 1;
BEGIN {
use_ok( 'Net::Connection::Match::States' ) || print "Bail out!\n";
}
diag( "Testing Net::Connection::Match::States $Net::Connection::Match::States::VERSION, Perl $], $^X" );

View File

@ -0,0 +1,75 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
use Net::Connection;
BEGIN {
use_ok( 'Net::Connection::Match::States' ) || 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=>'LISTEN',
};
my %args=(
states=>[
'LISTEN',
],
);
my $checker;
# makes sure we error with empty args
my $worked=0;
eval{
$checker=Net::Connection::Match::States->new();
$worked=1;
};
ok( $worked eq '0', 'empty init check') or diag('Calling new with empty args worked');
# makes sure we can init with good args
$worked=0;
eval{
$checker=Net::Connection::Match::States->new( \%args );
$worked=1;
};
ok( $worked eq '1', 'init check') or diag('Calling Net::Connection::Match::States->new resulted in... '.$@);
# make sure it will not accept null input
my $returned=1;
eval{
$returned=$checker->match;
};
ok( $returned eq '0', 'match undef check') or diag('match accepted undefined input');
# make sure it will not accept a improper ref type
$returned=1;
eval{
$returned=$checker->match($checker);
};
ok( $returned eq '0', 'match improper ref check') or diag('match accepted a ref other than Net::Connection');
# Create a connection with a matching state and see if it matches
my $conn=Net::Connection->new( $connection_args );
$returned=0;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'state match check') or diag('Failed to match a matching good state');
# Create a connection with a non-matching state and make sure it does not match
$connection_args->{state}='ESTABLISHED';
$conn=Net::Connection->new( $connection_args );
$returned=1;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '0', 'state non-match check') or diag('Matched a state that it should not of');
done_testing(7);