Browse Source

ports mostly done and testing in progress

tags/0.0.0
Zane C. B-H 5 months ago
parent
commit
8b45f4ba28
2 changed files with 107 additions and 0 deletions
  1. +26
    -0
      Net-Connection-Match/lib/Net/Connection/Match/Ports.pm
  2. +81
    -0
      Net-Connection-Match/t/Ports.t

+ 26
- 0
Net-Connection-Match/lib/Net/Connection/Match/Ports.pm View File

@@ -213,6 +213,32 @@ sub match{
return 0;
}

my $lport=$object->local_port;
my $fport=$object->foreign_port;

# If either are non-numeric, resolve them if possible
if ( $lport !~ /^[0-9]+$/ ){
my $lport_number=(getservbyname( $lport , '' ))[2];
if ( defined( $lport_number ) ){
$lport=$lport_number;
}
}
if ( $fport !~ /^[0-9]+$/ ){
my $fport_number=(getservbyname( $fport , '' ))[2];
if ( defined( $fport_number ) ){
$fport=$fport_number;
}
}

# check if this is one of the ones we are looking for
if (
defined( $self->{ports}{ $lport } ) ||
defined( $self->{ports}{ $fport } ) ||
defined( $self->{lports}{ $lport } ) ||
defined( $self->{fports}{ $fport } )
){
return 1;
}

return 0;
}

+ 81
- 0
Net-Connection-Match/t/Ports.t View File

@@ -0,0 +1,81 @@
#!perl -T
use 5.006;
use strict;
use warnings;
use Test::More;
use Net::Connection;

BEGIN {
use_ok( 'Net::Connection::Match::Ports' ) || 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=(
ports=>[
'22',
],
lports=>[
'53',
],
fports=>[
'12345',
],
);
my $checker;

# makes sure we error with empty args
my $worked=0;
eval{
$checker=Net::Connection::Match::Ports->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::Ports->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 null input
my $returned=1;
eval{
$returned=$checker->match;
};
ok( $returned eq '0', 'ports 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 protocol and see if it matches
my $conn=Net::Connection->new( $connection_args );
$returned=0;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'port match check') or diag('General port match failed');

# Create a connection with a non-matching protocol and make sure it does not match
$connection_args->{local_port}='53';
$conn=Net::Connection->new( $connection_args );
$returned=1;
eval{
$returned=$checker->match( $conn );
};
ok( $returned eq '1', 'local port match check') or diag('Failed to matching local port');

done_testing(7);

Loading…
Cancel
Save