debug some sorting stuff

This commit is contained in:
Zane C. B-H 2019-02-25 01:54:39 -06:00
parent 7c73b80649
commit 0450809453
2 changed files with 47 additions and 39 deletions

View File

@ -3,6 +3,8 @@ package Parse::Netstat::Search::Sort;
use 5.006;
use strict;
use warnings;
use base 'Error::Helper';
use Net::IP;
=head1 NAME
@ -39,22 +41,25 @@ Perhaps a little code snippet.
The supported sort methods are as below.
host_ff Host, Foreign First (default)
host_lf Host, Local First
host_f Host, Foreign
host_f Host, Foreign (default)
host_l Host, Local
port_ff Port, Foreign First
port_lf Port, Local First
port_f Port, Foriegn
port_l Port, Local
state State
protocol Protocol
q_rf Queue, Receive First
q_sf Queue, Send First
q_r Queue, Receive
q_s Queue, Send
none No sorting is done.
The ones below are currently present, but sometimes produce eratic results.
host_ff Host, Foreign First
host_lf Host, Local First
port_ff Port, Foreign First
port_lf Port, Local First
q_rf Queue, Receive First
q_sf Queue, Send First
=head1 Methods
=head2 new
@ -72,8 +77,9 @@ sub new{
errorString=>'',
errorExtra=>{
1 => 'badSort',
2 => 'badArray',
},
sort=>'host_ff',
sort=>'host_f',
invert=>undef,
sort_check=>{
host_ff=>1,
@ -100,13 +106,9 @@ sub new{
=head2 get_sort
This returns the current sort method and invert
values.
This returns the current sort method.
Please be aware as the invert value is a boolean,
it may not be defined(which it is not by default).
my ($sort, $invert)=$pnc->get_sort;
my $sort=$pnc->get_sort;
=cut
@ -117,7 +119,7 @@ sub get_sort{
return undef;
}
return $self->{sort}, $self->{invert};
return $self->{sort};
}
=head2 set_sort
@ -132,7 +134,7 @@ Leaving either undef resets the undef value back to the default.
The supported sorting methods are as below.
$ sorter->set_sort( $sort_method, $invert );
$ sorter->set_sort( $sort_method );
if( $sorter->error ){
warn( '"'.$sort_method.'"' is not a valid sort method );
}
@ -141,35 +143,23 @@ The supported sorting methods are as below.
$sorter->set_sort
# Set the sort method to host_f and invert.
$sorter->set_sort( 'host_f', 1)
# leave the sort method the same while telling it to invert
$sorter->set_sort( ( $sorter->get_sort )[0], 1 );
$sorter->set_sort( 'host_f' )
=cut
sub set_sort{
my $self=$_[0];
my $sort=$_[1];
my $invert=$_[2];
if( ! $self->errorblank ){
return undef;
}
if (!defined( $sort ) ){
$sort='host_ff';
}
if ( ! defined( $self->{sort_check}{$sort} ) ){
$self->{error}=1;
$self->{errorString}='"'.$sort.'" is not a valid sort value';
$self->warn;
return undef;
$sort='host_f';
}
$self->{sort}=$sort;
$self->{invert}=$invert;
return 1;
}
@ -286,9 +276,9 @@ sub host_sort_helper{
){
return 0;
}
my $host=eval {Net::IP->new($_[0] )->intip} ;
my $host=eval { Net::IP->new( $_[0] )->intip} ;
if (!defined( $host )){
return 0;
return 0;
}
return $host;
}

View File

@ -1,7 +1,6 @@
use strict;
use Test::More;
use Data::Dumper;
use Parse::Netstat::Search;
BEGIN {
@ -11,7 +10,7 @@ BEGIN {
my @found=(
{
'foreign_host'=>'1.1.1.1',
'local_host'=>'10.0.0.2',
'local_host'=>'2.2.2.1',
'foreign_port'=>'22222',
'local_port'=>'22',
'sendq'=>'0',
@ -21,7 +20,7 @@ my @found=(
},
{
'foreign_host'=>'1.1.1.2',
'local_host'=>'10.0.0.2',
'local_host'=>'2.2.2.5',
'foreign_port'=>'22',
'local_port'=>'2222',
'sendq'=>'0',
@ -31,7 +30,7 @@ my @found=(
},
{
'foreign_host'=>'1.1.1.1',
'local_host'=>'192.168.0.1',
'local_host'=>'2.2.2.3',
'foreign_port'=>'22',
'local_port'=>'2222',
'sendq'=>'0',
@ -40,13 +39,32 @@ my @found=(
'proto' => 'tcp4',
},
{
'foreign_host'=>'1.1.1.1',,
'local_host'=>'10.0.0.2',
'foreign_host'=>'1.1.1.5',
'local_host'=>'2.2.2.2',
'foreign_port'=>'22',
'local_port'=>'2222',
'sendq'=>'0',
'recvq'=>'0',
'state' => 'ESTABLISHED',
'state' => 't',
'proto' => 'tcp4',
},
);
my $sorter=Parse::Netstat::Search::Sort->new;
use Data::Dumper;
my @sorted=$sorter->sort( \@found );
ok( $sorted[0]->{foreign_host} eq '1.1.1.1', 'host_f, 1') or diag('"'.$sorted[0]->{foreign_host}.'" returned for $sorted[0]->{foreign_host} instead of "1.1.1.1"');
ok( $sorted[1]->{foreign_host} eq '1.1.1.1', 'host_f, 2') or diag('"'.$sorted[1]->{foreign_host}.'" returned for $sorted[1]->{foreign_host} instead of "1.1.1.2"');
$sorter->set_sort('host_l');
my ($sort_type, $invert)=$sorter->get_sort;
ok( $sort_type eq 'host_l', 'get_sort, type') or diag('"'.$sort_type.'" returned instead of "host_l"');
@sorted=$sorter->sort( \@found );
ok( $sorted[0]->{local_host} eq '2.2.2.1', 'host_l, 1') or diag('"'.$sorted[0]->{local_host}.'" returned for $sorted[0]->{local_host} instead of "2.2.2.5"');
ok( $sorted[1]->{local_host} eq '2.2.2.2', 'host_l, 2') or diag('"'.$sorted[1]->{local_host}.'" returned for $sorted[1]->{local_host} instead of "2.2.2.2"');
done_testing(9);