Compare commits

...

2 Commits

2 changed files with 135 additions and 10 deletions

View File

@ -22,6 +22,54 @@ our $VERSION = '0.0.0';
use Net::Connection::Match; use Net::Connection::Match;
use Net::Connection; use Net::Connection;
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 $conn=Net::Connection->new( $connection_args );
my %args=(
checks=>[
{
type=>'Ports',
invert=>0,
args=>{
ports=>[
'22',
],
lports=>[
'53',
],
fports=>[
'12345',
],
}
},
{
type=>'Protos',
invert=>0,
args=>{
protos=>[
'tcp4',
],
}
}
]
);
my $checker;
eval{
$checker=Net::Connection::Match->new( \%args );
};
if ( $check->match( $conn ) ){
print "It matched!\n";
}
=head1 METHODS =head1 METHODS
@ -32,6 +80,8 @@ This initializes a new check object.
It takes one value and thht is a hash ref with the key checks. It takes one value and thht is a hash ref with the key checks.
This is a array of hashes. This is a array of hashes.
If new fails, it will die.
=head3 checks hash keys =head3 checks hash keys
=head4 type =head4 type
@ -171,6 +221,14 @@ sub new{
Checks if a single Net::Connection object matches the stack. Checks if a single Net::Connection object matches the stack.
One object is argument is taken and that is the Net::Connection to check.
The return value is a boolean.
if ( $ncm->match( $conn ) ){
print "It matched.\n";
}
=cut =cut
sub match{ sub match{
@ -194,12 +252,12 @@ sub match{
} }
# Stores the number of hits # Stores the number of hits
my $hits; my $hits=0;
my $required=0; my $required=0;
foreach my $check ( @{ $self->{checks} } ){ foreach my $check ( @{ $self->{checks} } ){
my $hit; my $hit;
eval{ eval{
$hit=$check->{check}->($conn); $hit=$check->{check}->match($conn);
}; };
# If $hits is undef, then one of the checks errored and we skip processing the results. # If $hits is undef, then one of the checks errored and we skip processing the results.
@ -220,7 +278,7 @@ sub match{
} }
# if these are the same, then we have a match # if these are the same, then we have a match
if ( $required == $hits ){ if ( $required eq $hits ){
return 1; return 1;
} }
@ -232,10 +290,6 @@ sub match{
Error handling is provided by L<Error::Helper>. Error handling is provided by L<Error::Helper>.
=head2 1 / failedCheckInit
Calling new for one or more of the checks failed.
=head2 2 / notNCobj =head2 2 / notNCobj
Not a Net::Connection object. Either is is not defined Not a Net::Connection object. Either is is not defined

View File

@ -18,6 +18,15 @@ my $connection_args={
state=>'LISTEN', state=>'LISTEN',
}; };
my $nm_connection_args={
foreign_host=>'10.0.0.1',
foreign_port=>'80',
local_host=>'10.0.0.2',
local_port=>'12322',
proto=>'tcp4',
state=>'LISTEN',
};
my %args=( my %args=(
testing=>1, testing=>1,
checks=>[ checks=>[
@ -38,6 +47,38 @@ my %args=(
} }
] ]
); );
my %args2=(
testing=>1,
checks=>[
{
type=>'Ports',
invert=>0,
args=>{
ports=>[
'22',
],
lports=>[
'53',
],
fports=>[
'12345',
],
}
},
{
type=>'Protos',
invert=>0,
args=>{
protos=>[
'tcp4',
],
}
}
]
);
my $checker; my $checker;
# makes sure we error with empty args # makes sure we error with empty args
@ -63,11 +104,41 @@ eval{
}; };
ok( $checker->error eq '2', 'match improper ref check') or diag('match accepted a ref other than Net::Connection'); ok( $checker->error eq '2', 'match improper ref check') or diag('match accepted a ref other than Net::Connection');
# make sure it will not accept null input # make sure it will not accept null input
eval{ eval{
$returned=$checker->match(); $returned=$checker->match();
}; };
ok( $checker->error eq '2', 'match null input check') or diag('match accepted a ref other than Net::Connection'); ok( $checker->error eq '2', 'match null input check') or diag('match accepted null input');
done_testing(5); # Create a matching connection and see if it matches
my $conn=Net::Connection->new( $connection_args );
$returned=0;
eval{
$returned=$checker->match($conn);
};
ok( $returned eq '1', 'match good conn check') or diag('match failed on a connection that should match');
# Create a non-matching connection and see if it matches
my $nm_conn=Net::Connection->new( $nm_connection_args );
$returned=0;
eval{
$returned=$checker->match($nm_conn);
};
ok( $returned eq '0', 'match bad conn check') or diag('match on a connection that should not match');
# makes sure we can init with good args
$worked=0;
eval{
$checker=Net::Connection::Match->new( \%args2 );
$worked=1;
};
ok( $worked eq '1', 'init check2') or diag('Calling Net::Connection::Match::Ports->new resulted in... '.$@);
# Create a matching connection and see if it matches
$returned=0;
eval{
$returned=$checker->match($conn);
};
ok( $returned eq '1', 'match good conn check2') or diag('match failed on a connection that should match');
done_testing(9);