PID works

This commit is contained in:
Zane C. B-H 2019-07-23 23:14:17 -05:00
parent 08e8854d06
commit ec172891f4
2 changed files with 29 additions and 23 deletions

View File

@ -1,4 +1,4 @@
package Net::Connection::Sort::uid;
package Net::Connection::Sort::pid;
use 5.006;
use strict;
@ -6,7 +6,7 @@ use warnings;
=head1 NAME
Net::Connection::Sort::uid - Sorts the connections via the UID.
Net::Connection::Sort::pid - Sorts the connections via the PID.
=head1 VERSION
@ -19,7 +19,7 @@ our $VERSION = '0.0.0';
=head1 SYNOPSIS
Please keep in mind that UID is not a requirement and if not specified is set to 0,
Please keep in mind that PID is not a requirement and if not specified is set to 0,
meaning it will show up earlier.
use Net::Connection::Sort::host_f;
@ -35,8 +35,9 @@ meaning it will show up earlier.
'sendq' => '1',
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'uid' => 33,
'proto' => 'tcp4',
'uid' => 22,
'pid' => 2,
}),
Net::Connection->new({
'foreign_host' => '1.1.1.1',
@ -46,8 +47,9 @@ meaning it will show up earlier.
'sendq' => '1',
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'uid' => 0,
'proto' => 'tcp4',
'uid' => 1000,
'pid' => 0,
}),
Net::Connection->new({
'foreign_host' => '5.5.5.5',
@ -57,10 +59,11 @@ meaning it will show up earlier.
'sendq' => '1',
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'uid' => 1000,
'proto' => 'tcp4',
'uid' => 1,
'pid' => 44,
}),
# as no UID is specified, the value of 0 will just be used instead
# as no PID 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',
@ -69,7 +72,7 @@ meaning it will show up earlier.
'sendq' => '1',
'recvq' => '0',
'state' => 'ESTABLISHED',
'proto' => 'tcp4'
'proto' => 'tcp4',
}),
);
@ -130,7 +133,7 @@ sub sorter{
}
@objects=sort {
&helper( $a->uid ) <=> &helper( $b->uid )
&helper( $a->{pid} ) <=> &helper( $b->{pid} )
} @objects;
return @objects;
@ -140,7 +143,7 @@ sub sorter{
This is a internal function.
If no UID is defined, returns 0.
If no PID is defined, returns 0.
=cut

View File

@ -16,6 +16,7 @@ my @objects=(
'state' => 'LISTEN',
'proto' => 'tcp6',
'uid' => 1000,
'pid' => 2,
}),
Net::Connection->new({
'foreign_host' => '1.1.1.1',
@ -27,6 +28,7 @@ my @objects=(
'state' => 'FIN_WAIT_2',
'proto' => 'udp4',
'uid' => 33,
'pid' => 0,
}),
Net::Connection->new({
'foreign_host' => '5.5.5.5',
@ -38,6 +40,7 @@ my @objects=(
'state' => 'TIME_WAIT',
'proto' => 'udp6',
'uid' => 0,
'pid' => 1,
}),
Net::Connection->new({
'foreign_host' => '3.3.3.3',
@ -75,26 +78,26 @@ ok( $worked eq 1, 'sort') or die ('Net::Connection::Sort::proto->sorter(@objects
# 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 )
if( !defined( $sorted[0]->{pid} ) ||
!defined( $sorted[1]->{pid} )
){
$is_defined=0;
}
my $is_zero=0;
if( (
defined( $sorted[0]->uid ) &&
( $sorted[0]->uid eq '0' )
defined( $sorted[0]->{pid} ) &&
( $sorted[0]->{pid} eq '0' )
) || (
defined( $sorted[1]->uid ) &&
( $sorted[1]->uid eq '0' )
defined( $sorted[1]->{pid} ) &&
( $sorted[1]->{pid} 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');
ok( $is_defined eq '0', 'sort order 0') or die ('The PID for 0/1 is not 0');
ok( $is_zero eq '1', 'sort order 1') or die ('The PID for 0/1 is not 0');
ok( $sorted[2]->{pid} eq '1', 'sort order 2') or die ('The PID for 2 is not 33');
ok( $sorted[3]->{pid} eq '2', 'sort order 2') or die ('The PID for 3 is not 1000');
done_testing(7);