From 75785f70c79bb8e69aa7032c8a6c820573bebf9d Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Sat, 20 Apr 2019 09:28:06 +0100 Subject: [PATCH] #321 clean interface descriptions of null and trailing space --- lib/SNMP/Info/Layer2.pm | 4 ++++ lib/SNMP/Info/Layer3.pm | 4 ++++ xt/lib/Test/SNMP/Info/Layer2.pm | 16 ++++++++++++---- xt/lib/Test/SNMP/Info/Layer3.pm | 14 +++++++++++--- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/SNMP/Info/Layer2.pm b/lib/SNMP/Info/Layer2.pm index 917be9ac..9e954a42 100644 --- a/lib/SNMP/Info/Layer2.pm +++ b/lib/SNMP/Info/Layer2.pm @@ -150,6 +150,10 @@ sub interfaces { my $port = $i_descr->{$iid}; next unless defined $port; + my $port = SNMP::Info::munge_null($port); + $port =~ s/^\s+//; $port =~ s/\s+$//; + next unless length $port; + if ( $seen{$port}++ ) { # (#320) also fixup the port this is a duplicate of $interfaces->{ $first_seen_as{$port} } diff --git a/lib/SNMP/Info/Layer3.pm b/lib/SNMP/Info/Layer3.pm index 660928aa..8eefae47 100644 --- a/lib/SNMP/Info/Layer3.pm +++ b/lib/SNMP/Info/Layer3.pm @@ -335,6 +335,10 @@ sub interfaces { my $port = $i_descr->{$iid}; next unless defined $port; + my $port = SNMP::Info::munge_null($port); + $port =~ s/^\s+//; $port =~ s/\s+$//; + next unless length $port; + if ( $seen{$port}++ ) { # (#320) also fixup the port this is a duplicate of $interfaces->{ $first_seen_as{$port} } diff --git a/xt/lib/Test/SNMP/Info/Layer2.pm b/xt/lib/Test/SNMP/Info/Layer2.pm index 8d54e922..d6f8ad41 100644 --- a/xt/lib/Test/SNMP/Info/Layer2.pm +++ b/xt/lib/Test/SNMP/Info/Layer2.pm @@ -50,18 +50,22 @@ sub setup : Tests(setup) { '_i_index' => 1, '_i_description' => 1, 'store' => { - 'i_index' => {1 => 1, 2 => 2, 3 => 3, 4 => 4}, + 'i_index' => { map {($_ => $_)} (1 .. 8) }, 'i_description' => { 1 => 'Unique Interface Name', 2 => 'Duplicate Interface Name', - 3 => 'Duplicate Interface Name' + 3 => 'Duplicate Interface Name', + 4 => "\0", + 5 => "\0", + 6 => " \0", + 7 => "\0 ", }, }, }; $test->{info}->cache($cache_data); } -sub duplicates : Tests(2) { +sub interfaces : Tests(2) { my $test = shift; my $expected_data = { @@ -69,11 +73,15 @@ sub duplicates : Tests(2) { 2 => 'Duplicate Interface Name (2)', 3 => 'Duplicate Interface Name (3)', 4 => 4, + 5 => 5, + 6 => 6, + 7 => 7, + 8 => 8, }; can_ok($test->{info}, 'interfaces'); cmp_deeply($test->{info}->interfaces(), - $expected_data, 'Call to interfaces() removes duplicates'); + $expected_data, 'Call to interfaces() removes duplicates and cleans up'); } 1; diff --git a/xt/lib/Test/SNMP/Info/Layer3.pm b/xt/lib/Test/SNMP/Info/Layer3.pm index 9b77f74c..62298fd8 100644 --- a/xt/lib/Test/SNMP/Info/Layer3.pm +++ b/xt/lib/Test/SNMP/Info/Layer3.pm @@ -50,11 +50,15 @@ sub setup : Tests(setup) { '_i_index' => 1, '_i_description' => 1, 'store' => { - 'i_index' => {1 => 1, 2 => 2, 3 => 3, 4 => 4}, + 'i_index' => { map {($_ => $_)} (1 .. 8) }, 'i_description' => { 1 => 'Unique Interface Name', 2 => 'Duplicate Interface Name', - 3 => 'Duplicate Interface Name' + 3 => 'Duplicate Interface Name', + 4 => "\0", + 5 => "\0", + 6 => " \0", + 7 => "\0 ", }, }, }; @@ -69,11 +73,15 @@ sub duplicates : Tests(2) { 2 => 'Duplicate Interface Name (2)', 3 => 'Duplicate Interface Name (3)', 4 => 4, + 5 => 5, + 6 => 6, + 7 => 7, + 8 => 8, }; can_ok($test->{info}, 'interfaces'); cmp_deeply($test->{info}->interfaces(), - $expected_data, 'Call to interfaces() removes duplicates'); + $expected_data, 'Call to interfaces() removes duplicates and cleans up'); } 1;