2019-03-12 23:59:03 -05:00
< ? php
/**
* CommonTrapTest . php
*
* - Description -
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2021-02-09 00:29:04 +01:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2019-03-12 23:59:03 -05:00
*
2021-02-09 00:29:04 +01:00
* @ link https :// www . librenms . org
2019-03-12 23:59:03 -05:00
* @ copyright 2019 Tony Murray
* @ author Tony Murray < murraytony @ gmail . com >
*/
2020-04-03 17:13:18 -05:00
namespace LibreNMS\Tests\Feature\SnmpTraps ;
2019-03-12 23:59:03 -05:00
use App\Models\Device ;
use App\Models\Ipv4Address ;
use App\Models\Port ;
use LibreNMS\Snmptrap\Dispatcher ;
use LibreNMS\Snmptrap\Trap ;
use Log ;
2020-04-03 17:13:18 -05:00
class CommonTrapTest extends SnmpTrapTestCase
2019-03-12 23:59:03 -05:00
{
public function testGarbage ()
{
$trapText = " Garbage \n " ;
$trap = new Trap ( $trapText );
$this -> assertFalse ( Dispatcher :: handle ( $trap ), 'Found handler for trap with no snmpTrapOID' );
}
public function testFindByIp ()
{
2021-07-13 16:35:43 -05:00
$device = Device :: factory () -> create (); /** @var Device $device */
$port = Port :: factory () -> make (); /** @var Port $port */
2019-03-12 23:59:03 -05:00
$device -> ports () -> save ( $port );
2021-07-13 16:35:43 -05:00
// test ipv4 lookup of device
$ipv4 = Ipv4Address :: factory () -> make (); /** @var Ipv4Address $ipv4 */
2019-03-12 23:59:03 -05:00
$port -> ipv4 () -> save ( $ipv4 );
$trapText = " something
UDP : [ $ipv4 -> ipv4_address ] : 64610 -> [ 192.168 . 5.5 ] : 162
DISMAN - EVENT - MIB :: sysUpTimeInstance 198 : 2 : 10 : 48.91 \n " ;
2019-03-16 03:24:59 -05:00
Log :: shouldReceive ( 'info' ) -> once () -> with ( 'Unhandled trap snmptrap' , [ 'device' => $device -> hostname , 'oid' => null ]);
Log :: shouldReceive ( 'event' ) -> once () -> withArgs ( function ( $e_message , $e_device , $e_type ) use ( $device ) {
2020-09-23 17:00:02 +02:00
return $e_message == '' &&
2019-03-16 03:24:59 -05:00
$device -> is ( $e_device ) &&
$e_type == 'trap' ;
});
2019-03-12 23:59:03 -05:00
$trap = new Trap ( $trapText );
$this -> assertFalse ( Dispatcher :: handle ( $trap ), 'Found handler for trap with no snmpTrapOID' );
// check that the device was found
$this -> assertEquals ( $device -> hostname , $trap -> getDevice () -> hostname );
}
2019-03-16 03:24:59 -05:00
public function testGenericTrap ()
{
2021-07-13 16:35:43 -05:00
$device = Device :: factory () -> create (); /** @var Device $device */
2019-03-16 03:24:59 -05:00
$trapText = " $device->hostname
UDP : [ $device -> ip ] : 64610 -> [ 192.168 . 5.5 ] : 162
DISMAN - EVENT - MIB :: sysUpTimeInstance 198 : 2 : 10 : 48.91
SNMPv2 - MIB :: snmpTrapOID . 0 SNMPv2 - MIB :: someOid\n " ;
2020-09-21 14:54:51 +02:00
Log :: shouldReceive ( 'info' ) -> once () -> with ( 'Unhandled trap snmptrap' , [ 'device' => $device -> hostname , 'oid' => 'SNMPv2-MIB::someOid' ]);
2020-05-21 14:22:55 +01:00
Log :: shouldReceive ( 'event' ) -> once () -> withArgs ( function ( $e_message , $e_device , $e_type ) use ( $device ) {
2020-09-23 17:00:02 +02:00
return $e_message == 'SNMPv2-MIB::someOid' &&
2020-05-21 14:22:55 +01:00
$device -> is ( $e_device ) &&
$e_type == 'trap' ;
});
2019-03-16 03:24:59 -05:00
$trap = new Trap ( $trapText );
$this -> assertFalse ( Dispatcher :: handle ( $trap ));
}
2019-03-12 23:59:03 -05:00
public function testAuthorization ()
{
2021-07-13 16:35:43 -05:00
$device = Device :: factory () -> create (); /** @var Device $device */
2019-03-12 23:59:03 -05:00
$trapText = " $device->hostname
UDP : [ $device -> ip ] : 64610 -> [ 192.168 . 5.5 ] : 162
DISMAN - EVENT - MIB :: sysUpTimeInstance 198 : 2 : 10 : 48.91
SNMPv2 - MIB :: snmpTrapOID . 0 SNMPv2 - MIB :: authenticationFailure\n " ;
Log :: shouldReceive ( 'event' ) -> once () -> with ( 'SNMP Trap: Authentication Failure: ' . $device -> displayName (), $device -> device_id , 'auth' , 3 );
$trap = new Trap ( $trapText );
$this -> assertTrue ( Dispatcher :: handle ( $trap ));
// check that the device was found
$this -> assertEquals ( $device -> hostname , $trap -> getDevice () -> hostname );
}
2019-05-02 22:50:18 +02:00
public function testBridgeNewRoot ()
{
2021-07-13 16:35:43 -05:00
$device = Device :: factory () -> create (); /** @var Device $device */
2019-05-02 22:50:18 +02:00
$trapText = " $device->hostname
UDP : [ $device -> ip ] : 44298 -> [ 192.168 . 5.5 ] : 162
DISMAN - EVENT - MIB :: sysUpTimeInstance 3 : 4 : 17 : 32.35
SNMPv2 - MIB :: snmpTrapOID . 0 BRIDGE - MIB :: newRoot " ;
2019-05-10 04:45:22 +02:00
Log :: shouldReceive ( 'event' ) -> once () -> with ( 'SNMP Trap: Device ' . $device -> displayName () . ' was elected as new root on one of its Spanning Tree Instances' , $device -> device_id , 'stp' , 3 );
2019-05-02 22:50:18 +02:00
$trap = new Trap ( $trapText );
$this -> assertTrue ( Dispatcher :: handle ( $trap ));
// check that the device was found
$this -> assertEquals ( $device -> hostname , $trap -> getDevice () -> hostname );
}
public function testBridgeTopologyChanged ()
{
2021-07-13 16:35:43 -05:00
$device = Device :: factory () -> create (); /** @var Device $device */
2019-05-02 22:50:18 +02:00
$trapText = " $device->hostname
UDP : [ $device -> ip ] : 44298 -> [ 192.168 . 5.5 ] : 162
DISMAN - EVENT - MIB :: sysUpTimeInstance 3 : 4 : 17 : 32.35
SNMPv2 - MIB :: snmpTrapOID . 0 BRIDGE - MIB :: topologyChange " ;
2019-05-10 04:45:22 +02:00
Log :: shouldReceive ( 'event' ) -> once () -> with ( 'SNMP Trap: Topology of Spanning Tree Instance on device ' . $device -> displayName () . ' was changed' , $device -> device_id , 'stp' , 3 );
2019-05-02 22:50:18 +02:00
$trap = new Trap ( $trapText );
$this -> assertTrue ( Dispatcher :: handle ( $trap ));
// check that the device was found
$this -> assertEquals ( $device -> hostname , $trap -> getDevice () -> hostname );
}
public function testColdStart ()
{
2021-07-13 16:35:43 -05:00
$device = Device :: factory () -> create (); /** @var Device $device */
2019-05-02 22:50:18 +02:00
$trapText = " $device->hostname
UDP : [ $device -> ip ] : 44298 -> [ 192.168 . 5.5 ] : 162
DISMAN - EVENT - MIB :: sysUpTimeInstance 0 : 0 : 1 : 12.7
SNMPv2 - MIB :: snmpTrapOID . 0 SNMPv2 - MIB :: coldStart " ;
Log :: shouldReceive ( 'event' ) -> once () -> with ( 'SNMP Trap: Device ' . $device -> displayName () . ' cold booted' , $device -> device_id , 'reboot' , 4 );
$trap = new Trap ( $trapText );
$this -> assertTrue ( Dispatcher :: handle ( $trap ));
// check that the device was found
$this -> assertEquals ( $device -> hostname , $trap -> getDevice () -> hostname );
}
2020-05-10 21:50:19 +02:00
public function testWarmStart ()
{
2021-07-13 16:35:43 -05:00
$device = Device :: factory () -> create (); /** @var Device $device */
2020-05-10 21:50:19 +02:00
$trapText = " $device->hostname
UDP : [ $device -> ip ] : 44298 -> [ 192.168 . 5.5 ] : 162
DISMAN - EVENT - MIB :: sysUpTimeInstance 0 : 0 : 2 : 12.7
SNMPv2 - MIB :: snmpTrapOID . 0 SNMPv2 - MIB :: warmStart " ;
Log :: shouldReceive ( 'event' ) -> once () -> with ( 'SNMP Trap: Device ' . $device -> displayName () . ' warm booted' , $device -> device_id , 'reboot' , 4 );
$trap = new Trap ( $trapText );
$this -> assertTrue ( Dispatcher :: handle ( $trap ));
// check that the device was found
$this -> assertEquals ( $device -> hostname , $trap -> getDevice () -> hostname );
}
2019-05-02 22:50:18 +02:00
public function testEntityDatabaseChanged ()
{
2021-07-13 16:35:43 -05:00
$device = Device :: factory () -> create (); /** @var Device $device */
2019-05-02 22:50:18 +02:00
$trapText = " $device->hostname
UDP : [ $device -> ip ] : 44298 -> [ 192.168 . 5.5 ] : 162
DISMAN - EVENT - MIB :: sysUpTimeInstance 3 : 4 : 17 : 32.35
SNMPv2 - MIB :: snmpTrapOID . 0 ENTITY - MIB :: entConfigChange " ;
Log :: shouldReceive ( 'event' ) -> once () -> with ( 'SNMP Trap: Configuration of Entity Database on device ' . $device -> displayName () . ' was changed' , $device -> device_id , 'system' , 3 );
$trap = new Trap ( $trapText );
$this -> assertTrue ( Dispatcher :: handle ( $trap ));
// check that the device was found
$this -> assertEquals ( $device -> hostname , $trap -> getDevice () -> hostname );
}
2019-03-12 23:59:03 -05:00
}