username works

This commit is contained in:
Zane C. B-H 2019-07-24 00:32:06 -05:00
parent fff4731c85
commit 5d464d6ea6
2 changed files with 30 additions and 20 deletions

View File

@ -1,4 +1,4 @@
package Net::Connection::Sort::pid;
package Net::Connection::Sort::user;
use 5.006;
use strict;
@ -6,7 +6,7 @@ use warnings;
=head1 NAME
Net::Connection::Sort::pid - Sorts the connections via the PID.
Net::Connection::Sort::user - Sorts the connections via the username
=head1 VERSION
@ -19,10 +19,10 @@ our $VERSION = '0.0.0';
=head1 SYNOPSIS
Please keep in mind that PID is not a requirement and if not specified is set to 0,
Please keep in mind that username is not a requirement and if not specified is set to 0,
meaning it will show up earlier.
use Net::Connection::Sort::host_f;
use Net::Connection::Sort::user;
use Net::Connection;
use Data::Dumper;
@ -38,6 +38,8 @@ meaning it will show up earlier.
'proto' => 'tcp4',
'uid' => 22,
'pid' => 2,
'username' => 'toor',
'uid_resolve' => 0,
}),
Net::Connection->new({
'foreign_host' => '1.1.1.1',
@ -50,6 +52,8 @@ meaning it will show up earlier.
'proto' => 'tcp4',
'uid' => 1000,
'pid' => 0,
'username' => 'root',
'uid_resolve' => 0,
}),
Net::Connection->new({
'foreign_host' => '5.5.5.5',
@ -62,8 +66,10 @@ meaning it will show up earlier.
'proto' => 'tcp4',
'uid' => 1,
'pid' => 44,
'username' => 'foo',
'uid_resolve' => 0,
}),
# as no PID is specified, the value of 0 will just be used instead
# as no username 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',
@ -76,7 +82,7 @@ meaning it will show up earlier.
}),
);
my $sorter=$sorter=Net::Connection::Sort::uid->new;
my $sorter=$sorter=Net::Connection::Sort::user->new;
@objects=$sorter->sorter( \@objects );
@ -132,10 +138,8 @@ sub sorter{
die 'The passed item is either not a array or undefined';
}
# whoops... just realized I forgot to create a method for this in Net::Connection... doing it this way foreign
# compatibility with Net::Connection 0.0.0 as of currently
@objects=sort {
&helper( $a->{pid} ) <=> &helper( $b->{pid} )
&helper( $a->username ) cmp &helper( $b->username )
} @objects;
return @objects;
@ -145,7 +149,7 @@ sub sorter{
This is a internal function.
If no PID is defined, returns 0.
If no UID is defined, returns 0.
=cut

View File

@ -17,6 +17,8 @@ my @objects=(
'proto' => 'tcp6',
'uid' => 1000,
'pid' => 2,
'username' => 'toor',
'uid_resolve' => 0,
}),
Net::Connection->new({
'foreign_host' => '1.1.1.1',
@ -29,6 +31,8 @@ my @objects=(
'proto' => 'udp4',
'uid' => 33,
'pid' => 0,
'username' => 'root',
'uid_resolve' => 0,
}),
Net::Connection->new({
'foreign_host' => '5.5.5.5',
@ -41,6 +45,8 @@ my @objects=(
'proto' => 'udp6',
'uid' => 0,
'pid' => 1,
'username'=> 'foo',
'uid_resolve' => 0,
}),
Net::Connection->new({
'foreign_host' => '3.3.3.3',
@ -83,21 +89,21 @@ if( !defined( $sorted[0]->{pid} ) ||
){
$is_defined=0;
}
my $is_zero=0;
my $is_foo=0;
if( (
defined( $sorted[0]->{pid} ) &&
( $sorted[0]->{pid} eq '0' )
defined( $sorted[0]->username ) &&
( $sorted[0]->username eq '0' )
) || (
defined( $sorted[1]->{pid} ) &&
( $sorted[1]->{pid} eq '0' )
defined( $sorted[1]->username ) &&
( $sorted[1]->username =~ 'foo' )
)
){
$is_zero=1;
$is_foo=1;
}
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');
ok( $is_defined eq '0', 'sort order 0') or die ('The username for 0/1 is not undef');
ok( $is_foo eq '1', 'sort order 1') or die ('The username for 0/1 is not foo ');
ok( $sorted[2]->username =~ 'root', 'sort order 2') or die ('The username for 2 is not 33');
ok( $sorted[3]->username =~ 'toor', 'sort order 2') or die ('The username for 3 is not 1000');
done_testing(7);