add a testing arg to make testing less noisy with expected alert messages and begin work on the tests for the Match module
This commit is contained in:
parent
74894707d0
commit
6fcfbb57d7
|
@ -27,6 +27,33 @@ our $VERSION = '0.0.0';
|
||||||
|
|
||||||
=head2 new
|
=head2 new
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
=head3 checks hash keys
|
||||||
|
|
||||||
|
=head4 type
|
||||||
|
|
||||||
|
This is the name of the check relative to 'Net::Connection::Match::'.
|
||||||
|
|
||||||
|
So 'Net::Connection::Match::PTR' would become 'PTR'.
|
||||||
|
|
||||||
|
=head4 args
|
||||||
|
|
||||||
|
This is a hash or args to pash to the check. These are passed to the new
|
||||||
|
method of the check module.
|
||||||
|
|
||||||
|
=head4 invert
|
||||||
|
|
||||||
|
This is either boolean on if the check should be inverted or not.
|
||||||
|
|
||||||
|
my $mce;
|
||||||
|
eval{
|
||||||
|
$ncm=Net::Connection::Match->new( $args );
|
||||||
|
};
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
sub new{
|
sub new{
|
||||||
|
@ -41,14 +68,14 @@ sub new{
|
||||||
if ( ! defined( $args{checks} ) ){
|
if ( ! defined( $args{checks} ) ){
|
||||||
die ('No check key specified in the argument hash');
|
die ('No check key specified in the argument hash');
|
||||||
}
|
}
|
||||||
if ( ref( $args{checks} ) eq 'ARRAY' ){
|
if ( ref( @{ $args{checks} } ) eq 'ARRAY' ){
|
||||||
die ('The checks key is not a array');
|
die ('The checks key is not a array');
|
||||||
}
|
}
|
||||||
# Will never match anything.
|
# Will never match anything.
|
||||||
if ( ! defined $args{checks}[0] ){
|
if ( ! defined $args{checks}[0] ){
|
||||||
die ('Nothing in the checks array');
|
die ('Nothing in the checks array');
|
||||||
}
|
}
|
||||||
if ( ref( $args{checks}[0] ) eq 'HASH' ){
|
if ( ref( %{ $args{checks}[0] } ) eq 'HASH' ){
|
||||||
die ('The first item in the checks array is not a hash');
|
die ('The first item in the checks array is not a hash');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,6 +83,7 @@ sub new{
|
||||||
perror=>undef,
|
perror=>undef,
|
||||||
error=>undef,
|
error=>undef,
|
||||||
errorString=>"",
|
errorString=>"",
|
||||||
|
testing=>0,
|
||||||
errorExtra=>{
|
errorExtra=>{
|
||||||
flags=>{
|
flags=>{
|
||||||
1=>'failedCheckInit',
|
1=>'failedCheckInit',
|
||||||
|
@ -122,11 +150,7 @@ sub new{
|
||||||
eval( $eval_string );
|
eval( $eval_string );
|
||||||
|
|
||||||
if (!defined( $check )){
|
if (!defined( $check )){
|
||||||
$self->{error}=1;
|
die 'Failed to init the check for '.$check_int.' as it returned undef... '.$@;
|
||||||
$self->{errorString}='Failed to init the check for '.$check_int.' as it returned undef... '.$@;
|
|
||||||
$self->warn;
|
|
||||||
$self->{perror}=1;
|
|
||||||
return $self;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$new_check{check}=$check;
|
$new_check{check}=$check;
|
||||||
|
@ -136,16 +160,20 @@ sub new{
|
||||||
$check_int++;
|
$check_int++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( $args{testing} ){
|
||||||
|
$self->{testing}=1;
|
||||||
|
}
|
||||||
|
|
||||||
return $self;
|
return $self;
|
||||||
}
|
}
|
||||||
|
|
||||||
=head2 matches
|
=head2 match
|
||||||
|
|
||||||
Checks if a single Net::Connection object matches the stack.
|
Checks if a single Net::Connection object matches the stack.
|
||||||
|
|
||||||
=cut
|
=cut
|
||||||
|
|
||||||
sub matches{
|
sub match{
|
||||||
my $self=$_[0];
|
my $self=$_[0];
|
||||||
my $conn=$_[1];
|
my $conn=$_[1];
|
||||||
|
|
||||||
|
@ -159,7 +187,9 @@ sub matches{
|
||||||
){
|
){
|
||||||
$self->{error}=2;
|
$self->{error}=2;
|
||||||
$self->{errorString}='Either the connection is undefined or is not a Net::Connection object';
|
$self->{errorString}='Either the connection is undefined or is not a Net::Connection object';
|
||||||
$self->warn;
|
if ( ! $self->{testing} ){
|
||||||
|
$self->warn;
|
||||||
|
}
|
||||||
return undef;
|
return undef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -198,6 +228,19 @@ sub matches{
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
=head1 ERROR HANDLING / FLAGS
|
||||||
|
|
||||||
|
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
|
||||||
|
or what is being passed is not a Net::Connection object.
|
||||||
|
|
||||||
=head1 AUTHOR
|
=head1 AUTHOR
|
||||||
|
|
||||||
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
|
Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
#!perl -T
|
||||||
|
use 5.006;
|
||||||
|
use strict;
|
||||||
|
use warnings;
|
||||||
|
use Test::More;
|
||||||
|
use Net::Connection;
|
||||||
|
|
||||||
|
BEGIN {
|
||||||
|
use_ok( 'Net::Connection::Match' ) || 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=(
|
||||||
|
testing=>1,
|
||||||
|
checks=>[
|
||||||
|
{
|
||||||
|
type=>'Ports',
|
||||||
|
invert=>0,
|
||||||
|
args=>{
|
||||||
|
ports=>[
|
||||||
|
'22',
|
||||||
|
],
|
||||||
|
lports=>[
|
||||||
|
'53',
|
||||||
|
],
|
||||||
|
fports=>[
|
||||||
|
'12345',
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
);
|
||||||
|
my $checker;
|
||||||
|
|
||||||
|
# makes sure we error with empty args
|
||||||
|
my $worked=0;
|
||||||
|
eval{
|
||||||
|
$checker=Net::Connection::Match->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->new( \%args );
|
||||||
|
$worked=1;
|
||||||
|
};
|
||||||
|
ok( $worked eq '1', 'init check') or diag('Calling Net::Connection::Match::Ports->new resulted in... '.$@);
|
||||||
|
|
||||||
|
# make sure it will not accept a improper ref type
|
||||||
|
my $returned;
|
||||||
|
eval{
|
||||||
|
$returned=$checker->match($checker);
|
||||||
|
};
|
||||||
|
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
|
||||||
|
eval{
|
||||||
|
$returned=$checker->match();
|
||||||
|
};
|
||||||
|
ok( $checker->error eq '2', 'match null input check') or diag('match accepted a ref other than Net::Connection');
|
||||||
|
|
||||||
|
done_testing(5);
|
Loading…
Reference in New Issue