RegexPTR should be done... start work on the test

This commit is contained in:
Zane C. B-H 2019-08-06 01:54:30 -05:00
parent 182031912d
commit 1593c53230
4 changed files with 181 additions and 56 deletions

View File

@ -1,5 +1,8 @@
Revision history for Net-Connection-Match
0 ?/?
- Misc. POD fixes.
0.1.0 2019-07-31/02:00
- Ports can now match '*'.

View File

@ -11,11 +11,11 @@ Net::Connection::Match::PTR - Runs a PTR check against a Net::Connection object.
=head1 VERSION
Version 0.0.0
Version 0.0.1
=cut
our $VERSION = '0.0.0';
our $VERSION = '0.0.1';
=head1 SYNOPSIS
@ -52,7 +52,7 @@ our $VERSION = '0.0.0';
],
);
my $checker=Net::Connection::Match::Ports->new( \%args );
my $checker=Net::Connection::Match::PTR->new( \%args );
if ( $checker->match( $conn ) ){
print "It matches.\n";
@ -76,23 +76,23 @@ This intiates the object.
],
);
my $checker=Net::Connection::Match::Ports->new( \%args );
my $checker=Net::Connection::Match::PTR->new( \%args );
=head3 args
Atleast one of the following need used.
=keys ptrs
=head4 ptrs
This is a array of PTRs to match in for either foreign
or local side.
=keys fptrs
=head4 fptrs
This is a array of PTRs to match in for the foreign side.
=keys lptrs
=head4 lptrs
This is a array of PTRs to match in for the local side.

View File

@ -1,4 +1,4 @@
package Net::Connection::Match::PTR;
package Net::Connection::Match::RegexPTR;
use 5.006;
use strict;
@ -7,7 +7,7 @@ use Net::DNS;
=head1 NAME
Net::Connection::Match::PTR - Runs a PTR check against a Net::Connection object.
Net::Connection::Match::RegexPTR - Runs a PTR check against a Net::Connection object using regular expressions.
=head1 VERSION
@ -20,7 +20,7 @@ our $VERSION = '0.0.0';
=head1 SYNOPSIS
use Net::Connection::Match::PTR;
use Net::Connection::Match::RegexPTR;
use Net::Connection;
# The *_ptr feilds do not need populated.
@ -52,7 +52,7 @@ our $VERSION = '0.0.0';
],
);
my $checker=Net::Connection::Match::Ports->new( \%args );
my $checker=Net::Connection::Match::RegexPTR->new( \%args );
if ( $checker->match( $conn ) ){
print "It matches.\n";
@ -76,23 +76,23 @@ This intiates the object.
],
);
my $checker=Net::Connection::Match::Ports->new( \%args );
my $checker=Net::Connection::Match::RegexPTR->new( \%args );
=head3 args
Atleast one of the following need used.
=keys ptrs
=head4 ptrs
This is a array of PTRs to match in for either foreign
or local side.
=keys fptrs
=head4 fptrs
This is a array of PTRs to match in for the foreign side.
=keys lptrs
=head4 lptrs
This is a array of PTRs to match in for the local side.
@ -130,45 +130,21 @@ sub new{
}
my $self = {
ptrs=>{},
fptrs=>{},
lptrs=>{},
ptrs=>[],
lptrs=>[],
fptrs=>[],
resolver=>Net::DNS::Resolver->new,
};
bless $self;
##
## These are all stored as lower case to make matching easier.
##
# Process the ports for matching either
my $ptrs_int=0;
if ( defined( $args{ptrs} ) ){
while (defined( $args{ptrs}[$ptrs_int] )) {
$self->{ptrs}{ $args{ptrs}[$ptrs_int] }=lc( $args{ptrs}[$ptrs_int] );
$ptrs_int++;
}
if ( defined( $args{ptrs}[0] ) ){
$self->{ptrs}=$args{ptrs};
}
# Process the ports for matching local ports
$ptrs_int=0;
if ( defined( $args{lptrs} ) ){
while (defined( $args{lptrs}[$ptrs_int] )) {
$self->{lptrs}{ $args{lptrs}[$ptrs_int] }=lc( $args{lptrs}[$ptrs_int] );
$ptrs_int++;
}
if ( defined( $args{lptrs}[0] ) ){
$self->{lptrs}=$args{lptrs};
}
# Process the ports for matching foreign ports
$ptrs_int=0;
if ( defined( $args{fptrs} ) ){
while (defined( $args{fptrs}[$ptrs_int] )) {
$self->{fptrs}{ $args{fptrs}[$ptrs_int] }=lc( $args{fptrs}[$ptrs_int] );
$ptrs_int++;
}
if ( defined( $args{fptrs}[0] ) ){
$self->{fptrs}=$args{fptrs};
}
return $self;
@ -236,14 +212,23 @@ sub match{
}
}
# If we matched exactly, we found it.
if (
defined( $self->{ptrs}{ $l_ptr } ) ||
defined( $self->{ptrs}{ $f_ptr } ) ||
defined( $self->{lptrs}{ $l_ptr } ) ||
defined( $self->{fptrs}{ $f_ptr } )
){
return 1;
foreach my $regex ( @{ $self->{ptrs} } ){
if ( $l_ptr =~ /$regex/ ){
return 1;
}
if ( $f_ptr =~ /$regex/ ){
return 1;
}
}
foreach my $regex ( @{ $self->{lptrs} } ){
if ( $l_ptr =~ /$regex/ ){
return 1;
}
}
foreach my $regex ( @{ $self->{fptrs} } ){
if ( $f_ptr =~ /$regex/ ){
return 1;
}
}
return 0;

View File

@ -0,0 +1,137 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
use Net::Connection;
BEGIN {
use_ok( 'Net::Connection::Match::PTR' ) || 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',
local_ptr=>'foo.bar',
foreign_ptr=>'test',
};
my %args=(
ptrs=>[
'foo.bar',
],
);
my %largs=(
lptrs=>[
'foo.bar',
],
);
my %fargs=(
fptrs=>[
'foo.bar',
],
);
my $checker;
# makes sure we error with empty args
my $worked=0;
eval{
$checker=Net::Connection::Match::PTR->new();
$worked=1;
};
ok( $worked eq '0', 'empty init check') or diag('Calling new with empty args worked');
# makes sure we can init with general good args
$worked=0;
eval{
$checker=Net::Connection::Match::PTR->new( \%args );
$worked=1;
};
ok( $worked eq '1', 'init check, general') or diag('Calling Net::Connection::Match::PTR->new resulted in... '.$@);
# make sure it will not accept null input
my $returned=1;
eval{
$returned=$checker->match;
};
ok( $returned eq '0', 'proto 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');
# make sure the general PTR check works, testing local
$returned=0;
my $conn=Net::Connection->new( $connection_args );
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'general PTR match check, local') or diag('failed to match a Net::Connection for a general PTR check when one of the two matches');
# make sure the general PTR check works, testing foreign
$connection_args->{local_ptr}='test';
$connection_args->{foreign_ptr}='foo.bar';
$conn=Net::Connection->new( $connection_args );
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'general PTR match check, foriegn') or diag('failed to match a Net::Connection for a general PTR check when one of the two matches');
# makes sure we can init with local good args
$worked=0;
eval{
$checker=Net::Connection::Match::PTR->new( \%largs );
$worked=1;
};
ok( $worked eq '1', 'init check, local') or diag('Calling Net::Connection::Match::PTR->new resulted in... '.$@);
# make sure the local PTR check works, testing foreign
$returned=0;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '0', 'local PTR match check, foreign') or diag('matched a Net::Connection object when looking for local PTRs when the foreign matches');
# make sure the local PTR check works, testing local
$returned=0;
$connection_args->{foreign_ptr}='test';
$connection_args->{local_ptr}='foo.bar';
$conn=Net::Connection->new( $connection_args );
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'local PTR match check, local') or diag('did not match a Net::Connection object when looking for local PTRs when the local matches');
# makes sure we can init with foreign good args
$worked=0;
eval{
$checker=Net::Connection::Match::PTR->new( \%fargs );
$worked=1;
};
ok( $worked eq '1', 'init check, foreign') or diag('Calling Net::Connection::Match::PTR->new resulted in... '.$@);
# make sure the foreign PTR check works, testing local
$returned=0;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '0', 'foreign PTR match check, local') or diag('matched a Net::Connection object when looking for foreign PTRs when the local matches');
# make sure the foreign PTR check works, testing foreign
$returned=0;
$connection_args->{local_ptr}='test';
$connection_args->{foreign_ptr}='foo.bar';
$conn=Net::Connection->new( $connection_args );
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'local PTR match check, foreign') or diag('did not match a Net::Connection object when looking for foreign PTRs when the foreign matches');
done_testing(13);