tests and works properly

This commit is contained in:
Zane C. B-H 2019-07-23 22:59:26 -05:00
parent 73cfeb1e5c
commit b5c8eafba3
2 changed files with 47 additions and 28 deletions

View File

@ -1,13 +1,12 @@
package Net::Connection::Sort::host_f;
package Net::Connection::Sort::uid;
use 5.006;
use strict;
use warnings;
use Net::IP;
=head1 NAME
Net::Connection::Sort::host_f - Sorts the connections via the foreign host.
Net::Connection::Sort::uid - Sorts the connections via the UID.
=head1 VERSION
@ -34,6 +33,7 @@ our $VERSION = '0.0.0';
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'uid' => 33,
}),
Net::Connection->new({
'foreign_host' => '1.1.1.1',
@ -44,6 +44,7 @@ our $VERSION = '0.0.0';
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'uid' => 0,
}),
Net::Connection->new({
'foreign_host' => '5.5.5.5',
@ -54,7 +55,9 @@ our $VERSION = '0.0.0';
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'uid' => 1000,
}),
# as no UID is specified, the value of 0 will just be used instead
Net::Connection->new({
'foreign_host' => '3.3.3.3',
'local_host' => '4.4.4.4',
@ -67,7 +70,7 @@ our $VERSION = '0.0.0';
}),
);
my $sorter=$sorter=Net::Connection::Sort::host_f->new;
my $sorter=$sorter=Net::Connection::Sort::uid->new;
@objects=$sorter->sorter( \@objects );
@ -81,7 +84,7 @@ This initiates the module.
No arguments are taken and this will always succeed.
my $sorter=$sorter=Net::Connection::Sort::host_f->new;
my $sorter=$sorter=Net::Connection::Sort::uid->new;
=cut
@ -124,7 +127,7 @@ sub sorter{
}
@objects=sort {
&helper( $a->foreign_host ) <=> &helper( $b->foreign_host )
&helper( $a->uid ) <=> &helper( $b->uid )
} @objects;
return @objects;
@ -134,21 +137,15 @@ sub sorter{
This is a internal function.
If no UID is defined, returns 0.
=cut
sub helper{
if (
( !defined($_[0]) ) ||
( $_[0] eq '*' ) ||
( $_[0] =~ /[g-zG-Z]/ )
){
if ( !defined($_[0]) ){
return 0;
}
my $host=eval { Net::IP->new( $_[0] )->intip} ;
if (!defined( $host )){
return 0;
}
return $host;
return $_[0];
}
=head1 AUTHOR

View File

@ -14,7 +14,8 @@ my @objects=(
'sendq' => '1',
'recvq' => '0',
'state' => 'LISTEN',
'proto' => 'tcp6'
'proto' => 'tcp6',
'uid' => 1000,
}),
Net::Connection->new({
'foreign_host' => '1.1.1.1',
@ -24,7 +25,8 @@ my @objects=(
'sendq' => '1',
'recvq' => '0',
'state' => 'FIN_WAIT_2',
'proto' => 'udp4'
'proto' => 'udp4',
'uid' => 33,
}),
Net::Connection->new({
'foreign_host' => '5.5.5.5',
@ -34,7 +36,8 @@ my @objects=(
'sendq' => '1',
'recvq' => '0',
'state' => 'TIME_WAIT',
'proto' => 'udp6'
'proto' => 'udp6',
'uid' => 0,
}),
Net::Connection->new({
'foreign_host' => '3.3.3.3',
@ -44,23 +47,23 @@ my @objects=(
'sendq' => '1',
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'proto' => 'tcp4',
}),
);
BEGIN {
use_ok( 'Net::Connection::Sort::proto' ) || print "Bail out!\n";
use_ok( 'Net::Connection::Sort::uid' ) || print "Bail out!\n";
}
diag( "Testing Net::Connection::Sort::proto $Net::Connection::Sort::proto::VERSION, Perl $], $^X" );
diag( "Testing Net::Connection::Sort::uid $Net::Connection::Sort::uid::VERSION, Perl $], $^X" );
my $sorter;
my $worked=0;
eval{
$sorter=Net::Connection::Sort::proto->new;
$sorter=Net::Connection::Sort::uid->new;
$worked=1;
};
ok( $worked eq 1, 'sorter init') or die ('Net::Connection::Sort::proto->new resulted in... '.$@);
ok( $worked eq 1, 'sorter init') or die ('Net::Connection::Sort::uid->new resulted in... '.$@);
my @sorted;
$worked=0;
@ -70,9 +73,28 @@ eval{
};
ok( $worked eq 1, 'sort') or die ('Net::Connection::Sort::proto->sorter(@objects) resulted in... '.$@);
ok( $sorted[0]->proto =~ 'tcp4', 'sort order 0') or die ('The proto for 0 is not tcp4');
ok( $sorted[1]->proto =~ 'tcp6', 'sort order 1') or die ('The proto for 1 is not tcp6');
ok( $sorted[2]->proto =~ 'udp4', 'sort order 2') or die ('The proto for 2 is not udp4');
ok( $sorted[3]->proto =~ 'udp6', 'sort order 2') or die ('The proto for 3 is not udp6');
# 0 and 1 can end up in any order, make sure they are as expected
my $is_defined=1;
if( !defined( $sorted[0]->uid ) ||
!defined( $sorted[1]->uid )
){
$is_defined=0;
}
my $is_zero=0;
if( (
defined( $sorted[0]->uid ) &&
( $sorted[0]->uid eq '0' )
) || (
defined( $sorted[1]->uid ) &&
( $sorted[1]->uid eq '0' )
)
){
$is_zero=1;
}
ok( $is_defined eq '0', 'sort order 0') or die ('The UID for 0/1 is not 0');
ok( $is_zero eq '1', 'sort order 1') or die ('The UID for 0/1 is not 0');
ok( $sorted[2]->uid eq '33', 'sort order 2') or die ('The UID for 2 is not 33');
ok( $sorted[3]->uid eq '1000', 'sort order 2') or die ('The UID for 3 is not 1000');
done_testing(7);