add a example and run tests with muliple methods

This commit is contained in:
Zane C. B-H 2019-07-17 04:56:32 -05:00
parent 290bba0b9e
commit 8f72345480
2 changed files with 124 additions and 6 deletions

View File

@ -22,6 +22,54 @@ our $VERSION = '0.0.0';
use Net::Connection::Match;
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
@ -32,6 +80,8 @@ This initializes a new check object.
It takes one value and thht is a hash ref with the key checks.
This is a array of hashes.
If new fails, it will die.
=head3 checks hash keys
=head4 type
@ -171,6 +221,14 @@ sub new{
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
sub match{
@ -232,10 +290,6 @@ sub match{
Error handling is provided by L<Error::Helper>.
=head2 1 / failedCheckInit
Calling new for one or more of the checks failed.
=head2 2 / notNCobj
Not a Net::Connection object. Either is is not defined

View File

@ -18,6 +18,15 @@ my $connection_args={
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=(
testing=>1,
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;
# makes sure we error with empty args
@ -69,7 +110,7 @@ eval{
};
ok( $checker->error eq '2', 'match null input check') or diag('match accepted null input');
# Create a connection with a matching general port and see if it matches
# Create a matching connection and see if it matches
my $conn=Net::Connection->new( $connection_args );
$returned=0;
eval{
@ -77,4 +118,27 @@ eval{
};
ok( $returned eq '1', 'match good conn check') or diag('match failed on a connection that should match');
done_testing(6);
# 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);