Some devices use ifIndex when LLDP remote type is 'local', don't lldpRemPortId in these cases when it contains all digits
Cover LLDP class with tests
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# SNMP::Info::LLDP
|
||||
# $Id$
|
||||
#
|
||||
# Copyright (c) 2008 Eric Miller
|
||||
# Copyright (c) 2018 Eric Miller
|
||||
# All rights reserved.
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without
|
||||
@@ -116,7 +116,8 @@ sub hasLLDP {
|
||||
my $lldp_cap = $lldp->lldp_sys_cap();
|
||||
return 1 if defined $lldp_cap;
|
||||
|
||||
# If the device doesn't return local system capabilities, fallback by checking if it would report neighbors
|
||||
# If the device doesn't return local system capabilities, fallback
|
||||
# by checking if it would report neighbors
|
||||
my $lldp_rem = $lldp->lldp_rem_id() || {};
|
||||
return 1 if scalar keys %$lldp_rem;
|
||||
|
||||
@@ -138,13 +139,18 @@ sub lldp_if {
|
||||
my @aOID = split( '\.', $key );
|
||||
my $port = $aOID[1];
|
||||
next unless $port;
|
||||
# Local LLDP port may not equate to ifIndex, see LldpPortNumber TEXTUAL-CONVENTION in LLDP-MIB.
|
||||
# Cross reference lldpLocPortDesc with ifDescr and ifAlias to get ifIndex,
|
||||
# prefer ifDescr over ifAlias because using cross ref with description is correct behavior
|
||||
# according to the LLDP-MIB. Some devices (eg H3C gear) seem to use ifAlias though.
|
||||
|
||||
# Local LLDP port may not equate to ifIndex, see LldpPortNumber
|
||||
# TEXTUAL-CONVENTION in LLDP-MIB. Cross reference lldpLocPortDesc
|
||||
# with ifDescr and ifAlias to get ifIndex, prefer ifDescr over
|
||||
# ifAlias because using cross ref with description is correct
|
||||
# behavior according to the LLDP-MIB. Some devices (eg H3C gear)
|
||||
# seem to use ifAlias though.
|
||||
my $lldp_desc = $lldp->lldpLocPortDesc($port);
|
||||
my $desc = $lldp_desc->{$port};
|
||||
# If cross reference is successful use it, otherwise stick with lldpRemLocalPortNum
|
||||
|
||||
# If cross reference is successful use it, otherwise stick with
|
||||
# lldpRemLocalPortNum
|
||||
if ( $desc && exists $r_i_descr{$desc} ) {
|
||||
$port = $r_i_descr{$desc};
|
||||
}
|
||||
@@ -233,7 +239,10 @@ sub lldp_port {
|
||||
foreach my $key ( sort keys %$pid ) {
|
||||
my $port = $pdesc->{$key};
|
||||
my $type = $ptype->{$key};
|
||||
if ( $type and ($type eq 'interfaceName' or $type eq 'local') ) {
|
||||
if ( $type
|
||||
and ( $type eq 'interfaceName' or $type eq 'local' )
|
||||
and ( defined $pid->{$key} and $pid->{$key} !~ /^\d+$/ ) )
|
||||
{
|
||||
|
||||
# If the pid claims to be an interface name,
|
||||
# believe it.
|
||||
@@ -287,7 +296,8 @@ sub lldp_id {
|
||||
elsif ( $type eq 'networkAddress' ) {
|
||||
if ( length( unpack( 'H*', $id ) ) == 10 ) {
|
||||
|
||||
# IP address (first octet is sign, I guess)
|
||||
# IP address - first octet is IANA Address Family Number, need
|
||||
# walk with IPv6
|
||||
my @octets
|
||||
= ( map { sprintf "%02x", $_ } unpack( 'C*', $id ) )
|
||||
[ 1 .. 4 ];
|
||||
@@ -417,7 +427,7 @@ sub _lldp_addr_index {
|
||||
# IPv6
|
||||
elsif ( $proto == 2 ) {
|
||||
return ( $index, $proto,
|
||||
join(':', unpack('(H4)*', pack('C*', @oids)) ) );
|
||||
join( ':', unpack( '(H4)*', pack( 'C*', @oids ) ) ) );
|
||||
}
|
||||
|
||||
# MAC
|
||||
|
||||
@@ -33,23 +33,381 @@ use Test::Class::Most parent => 'My::Test::Class';
|
||||
|
||||
use SNMP::Info::LLDP;
|
||||
|
||||
# Remove this startup override once we have full method coverage
|
||||
sub startup : Tests(startup => 1) {
|
||||
my $test = shift;
|
||||
$test->SUPER::startup();
|
||||
|
||||
$test->todo_methods(1);
|
||||
}
|
||||
|
||||
sub setup : Tests(setup) {
|
||||
my $test = shift;
|
||||
$test->SUPER::setup;
|
||||
|
||||
# Start with a common cache that will serve most tests
|
||||
my $cache_data = {
|
||||
'store' => {},
|
||||
'_lldp_sys_cap' => pack("H*", '2800'),
|
||||
'_i_description' => 1,
|
||||
'_i_alias' => 1,
|
||||
'_lldp_rem_pid' => 1,
|
||||
'_lldp_rman_addr' => 1,
|
||||
'_lldp_rem_pid_type' => 1,
|
||||
'_lldp_rem_desc' => 1,
|
||||
'_lldp_rem_sysdesc' => 1,
|
||||
'_lldp_rem_sysname' => 1,
|
||||
'_lldp_rem_id_type' => 1,
|
||||
'_lldp_rem_id' => 1,
|
||||
'_lldp_rem_cap_spt' => 1,
|
||||
'_lldp_rem_media_cap_spt' => 1,
|
||||
'store' => {
|
||||
'i_description' =>
|
||||
{'10' => 'GigabitEthernet0/0/6', '12' => 'GigabitEthernet0/0/8',},
|
||||
'i_alias' => {'12' => 'My uplink alias'},
|
||||
'lldp_rem_pid' => {'0.6.1' => 'Gi0/48'},
|
||||
'lldp_rman_addr' => {'0.6.1.1.4.1.2.3.4' => 'unknown'},
|
||||
'lldp_rem_pid_type' => {'0.6.1' => 'interfaceName'},
|
||||
'lldp_rem_desc' => {'0.6.1' => 'GigabitEthernet0/48'},
|
||||
'lldp_rem_sysdesc' =>
|
||||
{'0.6.1' => 'C2960 Software (C2960-LANBASEK9-M), Version 12.2(37)SE'},
|
||||
'lldp_rem_sysname' => {'0.6.1' => 'My C2960'},
|
||||
'lldp_rem_id_type' => {'0.6.1' => 'macAddress'},
|
||||
'lldp_rem_id' => {'0.6.1' => pack("H*", 'ABCD123456')},
|
||||
'lldp_rem_cap_spt' => {'0.6.1' => pack("H*", '2800')},
|
||||
'lldp_rem_media_cap_spt' => {'0.6.1' => pack("H*", '4C')},
|
||||
}
|
||||
};
|
||||
$test->{info}->cache($cache_data);
|
||||
}
|
||||
|
||||
sub hasLLDP : Tests(4) {
|
||||
my $test = shift;
|
||||
|
||||
can_ok($test->{info}, 'hasLLDP');
|
||||
is($test->{info}->hasLLDP(), 1, q(Has 'lldpLocSysCapEnabled' has LLDP));
|
||||
|
||||
delete $test->{info}{_lldp_sys_cap};
|
||||
is($test->{info}->hasLLDP(),
|
||||
1, q(No 'lldpLocSysCapEnabled', but has neighbors, has LLDP));
|
||||
|
||||
$test->{info}->clear_cache();
|
||||
is($test->{info}->hasLLDP(),
|
||||
undef, q(No 'lldpLocSysCapEnabled' and no neighbors, no LLDP undef));
|
||||
}
|
||||
|
||||
sub lldp_if : Tests(5) {
|
||||
my $test = shift;
|
||||
|
||||
can_ok($test->{info}, 'lldp_if');
|
||||
|
||||
# The LLDP class ISA 'SNMP::Info' but does not include %SNMP::Info::FUNCS
|
||||
# in %SNMP::Info::LLDP::FUNCS so we need to insert i_description and i_alias
|
||||
# so that we can test this method, otherwise even though values are in cache
|
||||
# the AUTOLOAD method for them won't be created
|
||||
$test->{info}{funcs}{i_description} = 'ifDescr';
|
||||
$test->{info}{funcs}{i_alias} = 'ifAlias';
|
||||
|
||||
# Method uses a partial fetch which ignores the cache and reloads data
|
||||
# therefore we must use the mocked session. Populate the session data
|
||||
# so that the mock_getnext() has data to fetch.
|
||||
my $data = {'LLDP-MIB::lldpLocPortDesc' => {6 => 'GigabitEthernet0/0/6'}};
|
||||
$test->{info}{sess}{Data} = $data;
|
||||
|
||||
my $expected = {'0.6.1' => '10'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_if(),
|
||||
$expected, q(Mapping of LLDP interface using 'ifDescr' has expected value));
|
||||
|
||||
# Case where ifIndex isn't used as LldpPortNumber and
|
||||
# lldpLocPortDesc cross references to ifAlias. This is from a
|
||||
# Huawei VRP S5720
|
||||
# Use a different cache index to ensure different test results
|
||||
$test->{info}{store}{lldp_rem_pid} = {'5656.8.1' => 'interfaceName'};
|
||||
$data = {'LLDP-MIB::lldpLocPortDesc' => {8 => 'My uplink alias'}};
|
||||
$test->{info}{sess}{Data} = $data;
|
||||
|
||||
$expected = {'5656.8.1' => '12'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_if(),
|
||||
$expected, q(Mapping of LLDP interface using 'ifAlias' has expected value));
|
||||
|
||||
# Default / last resort no matching ifDescr or ifAlias so assume
|
||||
# LldpPortNumber is the same as ifIndex
|
||||
# Use a different cache index to ensure different test results
|
||||
$test->{info}{store}{lldp_rem_pid} = {'0.11.1' => 'interfaceName'};
|
||||
$test->{info}{sess}{Data} = {};
|
||||
|
||||
$expected = {'0.11.1' => '11'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_if(),
|
||||
$expected, q(Mapping of LLDP interface using 'ifIndex' has expected value));
|
||||
|
||||
$test->{info}->clear_cache();
|
||||
cmp_deeply($test->{info}->lldp_if(), {}, q(No data returns empty hash));
|
||||
}
|
||||
|
||||
sub lldp_ip : Tests(4) {
|
||||
my $test = shift;
|
||||
|
||||
can_ok($test->{info}, 'lldp_ip');
|
||||
|
||||
my $expected = {'0.6.1' => '1.2.3.4'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_ip(),
|
||||
$expected, q(Remote LLDP IPv4 has expected value));
|
||||
|
||||
# Exchange the IPv4 address with the same IPv6 address
|
||||
$test->{info}{store}{lldp_rman_addr}
|
||||
= {'0.6.1.2.16.0.0.0.0.0.0.0.0.0.0.255.255.1.2.3.4' => 'unknown'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_ip(),
|
||||
{}, q(Address format other than IPv4 returns empty hash));
|
||||
|
||||
$test->{info}->clear_cache();
|
||||
cmp_deeply($test->{info}->lldp_ip(), {}, q(No data returns empty hash));
|
||||
}
|
||||
|
||||
sub lldp_ipv6 : Tests(4) {
|
||||
my $test = shift;
|
||||
|
||||
can_ok($test->{info}, 'lldp_ipv6');
|
||||
|
||||
my $expected = {'0.6.1' => '0000:0000:0000:0000:0000:ffff:0102:0304'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_ipv6(),
|
||||
{}, q(Address format other than IPv6 returns empty hash));
|
||||
|
||||
# Exchange the IPv4 address with the same IPv6 address
|
||||
$test->{info}{store}{lldp_rman_addr}
|
||||
= {'0.6.1.2.16.0.0.0.0.0.0.0.0.0.0.255.255.1.2.3.4' => 'ifIndex'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_ipv6(),
|
||||
$expected, q(Remote LLDP IPv6 has expected value));
|
||||
|
||||
$test->{info}->clear_cache();
|
||||
cmp_deeply($test->{info}->lldp_ip(), {}, q(No data returns empty hash));
|
||||
}
|
||||
|
||||
sub lldp_mac : Tests(4) {
|
||||
my $test = shift;
|
||||
|
||||
can_ok($test->{info}, 'lldp_mac');
|
||||
|
||||
my $expected = {'0.6.1' => '01:23:45:67:89:ab'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_mac(),
|
||||
{}, q(Address format other than MAC returns empty hash));
|
||||
|
||||
# Exchange the IPv4 address with MAC
|
||||
$test->{info}{store}{lldp_rman_addr}
|
||||
= {'0.6.1.6.6.01.35.69.103.137.171' => 'ifIndex'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_mac(),
|
||||
$expected, q(Remote LLDP MAC has expected value));
|
||||
|
||||
$test->{info}->clear_cache();
|
||||
cmp_deeply($test->{info}->lldp_mac(), {}, q(No data returns empty hash));
|
||||
}
|
||||
|
||||
# This has been really been tested in the lldp_ip, lldp_ipv6, and lldp_mac but
|
||||
# tested here for completeness
|
||||
sub lldp_addr : Tests(3) {
|
||||
my $test = shift;
|
||||
|
||||
can_ok($test->{info}, 'lldp_addr');
|
||||
|
||||
$test->{info}{store}{lldp_rman_addr} = {
|
||||
'0.6.1.1.4.1.2.3.4' => 'unknown',
|
||||
'0.8.1.2.16.0.0.0.0.0.0.0.0.0.0.255.255.1.2.3.4' => 'ifIndex',
|
||||
'0.10.1.6.6.01.35.69.103.137.171' => 'ifIndex'
|
||||
};
|
||||
|
||||
my $expected = {
|
||||
'0.6.1' => '1.2.3.4',
|
||||
'0.8.1' => '0000:0000:0000:0000:0000:ffff:0102:0304',
|
||||
'0.10.1' => '01:23:45:67:89:ab',
|
||||
};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_addr(),
|
||||
$expected, q(Remote LLDP addresses have expected values));
|
||||
|
||||
$test->{info}->clear_cache();
|
||||
cmp_deeply($test->{info}->lldp_addr(), {}, q(No data returns empty hash));
|
||||
}
|
||||
|
||||
sub lldp_port : Tests(10) {
|
||||
my $test = shift;
|
||||
|
||||
can_ok($test->{info}, 'lldp_port');
|
||||
|
||||
my $expected = {'0.6.1' => 'Gi0/48'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_port(),
|
||||
$expected, q(Remote port type 'interfaceName' uses 'lldpRemPortId'));
|
||||
|
||||
# Default to lldpRemPortDesc by making type interfaceAlias
|
||||
$test->{info}{store}{lldp_rem_pid_type} = {'0.6.1' => 'interfaceAlias'};
|
||||
|
||||
$expected = {'0.6.1' => 'GigabitEthernet0/48'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_port(),
|
||||
$expected, q(Remote port type 'interfaceAlias' uses 'lldpRemPortDesc'));
|
||||
|
||||
# Netgear XSM7224S - local type w/ ifName
|
||||
$test->{info}{store} = {
|
||||
lldp_rem_pid_type => {'0.11.1' => 'local'},
|
||||
lldp_rem_desc => {'0.11.1' => ''},
|
||||
lldp_rem_pid => {'0.11.1' => '1/0/1'},
|
||||
};
|
||||
|
||||
$expected = {'0.11.1' => '1/0/1'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_port(),
|
||||
$expected, q(Remote port type 'local' and 'lldpRemPortId' not digits));
|
||||
|
||||
# Alcatel/Nokia - local type w/ ifIndex
|
||||
$test->{info}{store} = {
|
||||
lldp_rem_pid_type => {'0.15.1' => 'local'},
|
||||
lldp_rem_desc => {'0.15.1' => 'My port descr'},
|
||||
lldp_rem_pid => {'0.15.1' => '123'},
|
||||
};
|
||||
|
||||
$expected = {'0.15.1' => 'My port descr'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_port(),
|
||||
$expected,
|
||||
q(Remote port type 'local' and 'ifIndex' uses 'lldpRemPortDesc'));
|
||||
|
||||
# MAC /w descr
|
||||
$test->{info}{store} = {
|
||||
lldp_rem_pid_type => {'0.16.1' => 'macAddress'},
|
||||
lldp_rem_desc => {'0.16.1' => 'My mac port descr'},
|
||||
lldp_rem_pid => {'0.16.1' => pack("H*", '12345678AB')},
|
||||
};
|
||||
|
||||
$expected = {'0.16.1' => 'My mac port descr'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_port(),
|
||||
$expected, q(Remote port type 'macAddress' uses 'lldpRemPortDesc'));
|
||||
|
||||
# MAC w/o descr
|
||||
$test->{info}{store} = {
|
||||
lldp_rem_pid_type => {'0.16.1' => 'macAddress'},
|
||||
lldp_rem_desc => {'0.16.1' => ''},
|
||||
lldp_rem_pid => {'0.16.1' => pack("H*", '2345678ABC')},
|
||||
};
|
||||
|
||||
$expected = {'0.16.1' => '23:45:67:8a:bc'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_port(), $expected,
|
||||
q(Remote port type 'macAddress' no 'lldpRemPortDesc' uses 'lldpRemPortId'));
|
||||
|
||||
# Ethernet Routing Switch single
|
||||
$test->{info}{store} = {
|
||||
lldp_rem_sysdesc => {'0.25.1' => 'Ethernet Routing Switch 4550T-PWR'},
|
||||
lldp_rem_pid_type => {'0.25.1' => 'macAddress'},
|
||||
lldp_rem_desc => {'0.25.1' => 'Port 50'},
|
||||
lldp_rem_pid => {'0.25.1' => pack("H*", '2345678ABC')},
|
||||
};
|
||||
|
||||
$expected = {'0.25.1' => '1.50'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_port(),
|
||||
$expected, q(Remote Ethernet Routing Switch 'lldpRemPortDesc' munged));
|
||||
|
||||
# Ethernet Routing Switch single
|
||||
$test->{info}{store} = {
|
||||
lldp_rem_sysdesc => {'1.25.1' => 'Ethernet Routing Switch 4550T-PWR'},
|
||||
lldp_rem_pid_type => {'1.25.1' => 'macAddress'},
|
||||
lldp_rem_desc => {'1.25.1' => 'Unit 2 Port 50'},
|
||||
lldp_rem_pid => {'1.25.1' => pack("H*", '2345678ABC')},
|
||||
};
|
||||
|
||||
$expected = {'1.25.1' => '2.50'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_port(),
|
||||
$expected,
|
||||
q(Remote Ethernet Routing Switch stack 'lldpRemPortDesc' munged));
|
||||
|
||||
$test->{info}->clear_cache();
|
||||
cmp_deeply($test->{info}->lldp_port(), {}, q(No data returns empty hash));
|
||||
}
|
||||
|
||||
sub lldp_id : Tests(4) {
|
||||
my $test = shift;
|
||||
|
||||
can_ok($test->{info}, 'lldp_id');
|
||||
|
||||
my $expected = {'0.6.1' => 'ab:cd:12:34:56'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_id(),
|
||||
$expected, q(Remote LLDP ID type 'macAddress' has expected value));
|
||||
|
||||
$test->{info}{store} = {
|
||||
lldp_rem_id_type => {'1.25.1' => 'networkAddress'},
|
||||
lldp_rem_id => {'1.25.1' => pack("H*", '010A141E28')},
|
||||
};
|
||||
|
||||
$expected = {'1.25.1' => '10.20.30.40'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_id(),
|
||||
$expected, q(Remote LLDP ID type 'networkAddress' has expected value));
|
||||
|
||||
$test->{info}->clear_cache();
|
||||
cmp_deeply($test->{info}->lldp_id(), {}, q(No data returns empty hash));
|
||||
}
|
||||
|
||||
sub lldp_platform : Tests(4) {
|
||||
my $test = shift;
|
||||
|
||||
can_ok($test->{info}, 'lldp_platform');
|
||||
|
||||
my $expected
|
||||
= {'0.6.1' => 'C2960 Software (C2960-LANBASEK9-M), Version 12.2(37)SE'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_platform(),
|
||||
$expected, q(Remote platform using 'lldpRemSysDesc'));
|
||||
|
||||
delete $test->{info}{_lldp_rem_sysdesc};
|
||||
|
||||
$expected = {'0.6.1' => 'My C2960'};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_platform(),
|
||||
$expected, q(Remote platform using 'lldpRemSysName'));
|
||||
|
||||
$test->{info}->clear_cache();
|
||||
cmp_deeply($test->{info}->lldp_platform(), {}, q(No data returns empty hash));
|
||||
}
|
||||
|
||||
sub lldp_cap : Tests(4) {
|
||||
my $test = shift;
|
||||
|
||||
can_ok($test->{info}, 'lldp_cap');
|
||||
|
||||
my $expected = {'0.6.1' => ['bridge', 'router']};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_cap(), $expected,
|
||||
q(Caps emumerated correctly));
|
||||
|
||||
$test->{info}{store}{lldp_rem_cap_spt} = {'0.6.1' => pack("H*", '0000')};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_cap(), {}, q(Cap of zeros return empty hash));
|
||||
|
||||
$test->{info}->clear_cache();
|
||||
cmp_deeply($test->{info}->lldp_cap(), {}, q(No data returns empty hash));
|
||||
}
|
||||
|
||||
sub lldp_media_cap : Tests(4) {
|
||||
my $test = shift;
|
||||
|
||||
can_ok($test->{info}, 'lldp_media_cap');
|
||||
|
||||
my $expected = {'0.6.1' => ['networkPolicy', 'extendedPD', 'inventory']};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_media_cap(),
|
||||
$expected, q(Caps emumerated correctly));
|
||||
|
||||
$test->{info}{store}{lldp_rem_media_cap_spt}
|
||||
= {'0.6.1' => pack("H*", '0000')};
|
||||
|
||||
cmp_deeply($test->{info}->lldp_media_cap(),
|
||||
{}, q(Cap of zeros return empty hash));
|
||||
|
||||
$test->{info}->clear_cache();
|
||||
cmp_deeply($test->{info}->lldp_media_cap(), {},
|
||||
q(No data returns empty hash));
|
||||
}
|
||||
|
||||
1;
|
||||
Reference in New Issue
Block a user