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
2021-09-10 20:09:53 +02:00
*
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 ;
2022-11-05 14:43:54 -05:00
use App\Models\Eventlog ;
2019-03-12 23:59:03 -05:00
use App\Models\Ipv4Address ;
use App\Models\Port ;
2022-11-05 14:43:54 -05:00
use Illuminate\Foundation\Testing\DatabaseTransactions ;
2023-08-05 12:12:36 -05:00
use LibreNMS\Enum\Severity ;
2019-03-12 23:59:03 -05:00
use LibreNMS\Snmptrap\Dispatcher ;
use LibreNMS\Snmptrap\Trap ;
2022-11-05 14:43:54 -05:00
use LibreNMS\Tests\Traits\RequiresDatabase ;
2019-03-12 23:59:03 -05:00
use Log ;
2020-04-03 17:13:18 -05:00
class CommonTrapTest extends SnmpTrapTestCase
2019-03-12 23:59:03 -05:00
{
2022-11-05 14:43:54 -05:00
use RequiresDatabase ;
use DatabaseTransactions ;
public function testGarbage () : void
2019-03-12 23:59:03 -05:00
{
$trapText = " Garbage \n " ;
$trap = new Trap ( $trapText );
$this -> assertFalse ( Dispatcher :: handle ( $trap ), 'Found handler for trap with no snmpTrapOID' );
}
2022-11-05 14:43:54 -05:00
public function testFindByIp () : void
2019-03-12 23:59:03 -05:00
{
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 " ;
2022-11-05 14:43:54 -05:00
Log :: partialMock () -> shouldReceive ( 'info' ) -> once () -> with ( 'Unhandled trap snmptrap' , [ 'device' => $device -> hostname , 'oid' => null ]);
2019-03-16 03:24:59 -05:00
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 );
2022-11-05 14:43:54 -05:00
// check that eventlog was logged
$eventlog = Eventlog :: latest ( 'event_id' ) -> first ();
$this -> assertEquals ( $device -> device_id , $eventlog -> device_id , 'Trap eventlog device incorrect' );
$this -> assertEquals ( '' , $eventlog -> message , 'Trap eventlog message incorrect' );
$this -> assertEquals ( 'trap' , $eventlog -> type , 'Trap eventlog type incorrect' );
2023-08-05 12:12:36 -05:00
$this -> assertEquals ( Severity :: Info , $eventlog -> severity , 'Trap eventlog severity incorrect' );
2019-03-12 23:59:03 -05:00
}
2022-11-05 14:43:54 -05:00
public function testGenericTrap () : void
2019-03-16 03:24:59 -05:00
{
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 " ;
2022-11-05 14:43:54 -05:00
Log :: partialMock () -> shouldReceive ( 'info' ) -> once () -> with ( 'Unhandled trap snmptrap' , [ 'device' => $device -> hostname , 'oid' => 'SNMPv2-MIB::someOid' ]);
2020-05-21 14:22:55 +01:00
2019-03-16 03:24:59 -05:00
$trap = new Trap ( $trapText );
$this -> assertFalse ( Dispatcher :: handle ( $trap ));
2022-11-05 14:43:54 -05:00
// check that eventlog was logged
$eventlog = Eventlog :: latest ( 'event_id' ) -> first ();
$this -> assertEquals ( $device -> device_id , $eventlog -> device_id , 'Trap eventlog device incorrect' );
$this -> assertEquals ( 'SNMPv2-MIB::someOid' , $eventlog -> message , 'Trap eventlog message incorrect' );
$this -> assertEquals ( 'trap' , $eventlog -> type , 'Trap eventlog type incorrect' );
2023-08-05 12:12:36 -05:00
$this -> assertEquals ( Severity :: Info , $eventlog -> severity , 'Trap eventlog severity incorrect' );
2019-03-16 03:24:59 -05:00
}
2022-11-05 14:43:54 -05:00
public function testAuthorization () : void
2019-03-12 23:59:03 -05:00
{
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 " ;
$trap = new Trap ( $trapText );
$this -> assertTrue ( Dispatcher :: handle ( $trap ));
// check that the device was found
$this -> assertEquals ( $device -> hostname , $trap -> getDevice () -> hostname );
2022-11-05 14:43:54 -05:00
// check that eventlog was logged
$eventlog = Eventlog :: latest ( 'event_id' ) -> first ();
$this -> assertEquals ( $device -> device_id , $eventlog -> device_id , 'Trap eventlog device incorrect' );
$this -> assertEquals ( 'SNMP Trap: Authentication Failure: ' . $device -> displayName (), $eventlog -> message , 'Trap eventlog message incorrect' );
$this -> assertEquals ( 'auth' , $eventlog -> type , 'Trap eventlog type incorrect' );
2023-08-05 12:12:36 -05:00
$this -> assertEquals ( Severity :: Notice , $eventlog -> severity , 'Trap eventlog severity incorrect' );
2019-03-12 23:59:03 -05:00
}
2019-05-02 22:50:18 +02:00
2022-11-05 14:43:54 -05:00
public function testBridgeNewRoot () : void
2019-05-02 22:50:18 +02:00
{
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 " ;
$trap = new Trap ( $trapText );
$this -> assertTrue ( Dispatcher :: handle ( $trap ));
// check that the device was found
$this -> assertEquals ( $device -> hostname , $trap -> getDevice () -> hostname );
2022-11-05 14:43:54 -05:00
// check that eventlog was logged
$eventlog = Eventlog :: latest ( 'event_id' ) -> first ();
$this -> assertEquals ( $device -> device_id , $eventlog -> device_id , 'Trap eventlog device incorrect' );
$this -> assertEquals ( 'SNMP Trap: Device ' . $device -> displayName () . ' was elected as new root on one of its Spanning Tree Instances' , $eventlog -> message , 'Trap eventlog message incorrect' );
$this -> assertEquals ( 'stp' , $eventlog -> type , 'Trap eventlog type incorrect' );
2023-08-05 12:12:36 -05:00
$this -> assertEquals ( Severity :: Notice , $eventlog -> severity , 'Trap eventlog severity incorrect' );
2019-05-02 22:50:18 +02:00
}
2022-11-05 14:43:54 -05:00
public function testBridgeTopologyChanged () : void
2019-05-02 22:50:18 +02:00
{
2022-11-05 14:43:54 -05:00
$this -> assertTrapLogsMessage ( <<< 'TRAP'
{{ hostname }}
UDP : [{{ ip }}] : 44298 -> [ 192.168 . 5.5 ] : 162
2019-05-02 22:50:18 +02:00
DISMAN - EVENT - MIB :: sysUpTimeInstance 3 : 4 : 17 : 32.35
2022-11-05 14:43:54 -05:00
SNMPv2 - MIB :: snmpTrapOID . 0 BRIDGE - MIB :: topologyChange
TRAP ,
'SNMP Trap: Topology of Spanning Tree Instance on device {{ hostname }} was changed' , // assertTrapLogsMessage sets display to hostname
'Failed to handle BRIDGE-MIB::topologyChange' ,
2023-08-05 12:12:36 -05:00
[ Severity :: Notice , 'stp' ],
2022-11-05 14:43:54 -05:00
);
2019-05-02 22:50:18 +02:00
}
2022-11-05 14:43:54 -05:00
public function testColdStart () : void
2019-05-02 22:50:18 +02:00
{
2022-11-05 14:43:54 -05:00
$this -> assertTrapLogsMessage ( <<< 'TRAP'
{{ hostname }}
UDP : [{{ ip }}] : 44298 -> [ 192.168 . 5.5 ] : 162
2019-05-02 22:50:18 +02:00
DISMAN - EVENT - MIB :: sysUpTimeInstance 0 : 0 : 1 : 12.7
2022-11-05 14:43:54 -05:00
SNMPv2 - MIB :: snmpTrapOID . 0 SNMPv2 - MIB :: coldStart
TRAP ,
'SNMP Trap: Device {{ hostname }} cold booted' ,
'Failed to handle SNMPv2-MIB::coldStart' ,
2023-08-05 12:12:36 -05:00
[ Severity :: Warning , 'reboot' ],
2022-11-05 14:43:54 -05:00
);
2019-05-02 22:50:18 +02:00
}
2022-11-05 14:43:54 -05:00
public function testWarmStart () : void
2020-05-10 21:50:19 +02:00
{
2022-11-05 14:43:54 -05:00
$this -> assertTrapLogsMessage ( <<< 'TRAP'
{{ hostname }}
UDP : [{{ ip }}] : 44298 -> [ 192.168 . 5.5 ] : 162
2020-05-10 21:50:19 +02:00
DISMAN - EVENT - MIB :: sysUpTimeInstance 0 : 0 : 2 : 12.7
2022-11-05 14:43:54 -05:00
SNMPv2 - MIB :: snmpTrapOID . 0 SNMPv2 - MIB :: warmStart
TRAP ,
'SNMP Trap: Device {{ hostname }} warm booted' ,
'Failed to handle SNMPv2-MIB::warmStart' ,
2023-08-05 12:12:36 -05:00
[ Severity :: Warning , 'reboot' ],
2022-11-05 14:43:54 -05:00
);
2020-05-10 21:50:19 +02:00
}
2019-05-02 22:50:18 +02:00
2022-11-05 14:43:54 -05:00
public function testEntityDatabaseChanged () : void
2019-05-02 22:50:18 +02:00
{
2022-11-05 14:43:54 -05:00
$this -> assertTrapLogsMessage ( <<< 'TRAP'
{{ hostname }}
UDP : [{{ ip }}] : 44298 -> [ 192.168 . 5.5 ] : 162
2019-05-02 22:50:18 +02:00
DISMAN - EVENT - MIB :: sysUpTimeInstance 3 : 4 : 17 : 32.35
2022-11-05 14:43:54 -05:00
SNMPv2 - MIB :: snmpTrapOID . 0 ENTITY - MIB :: entConfigChange
TRAP ,
'SNMP Trap: Configuration of Entity Database on device {{ hostname }} was changed' ,
'Failed to handle ENTITY-MIB::entConfigChange' ,
2023-08-05 12:12:36 -05:00
[ Severity :: Notice , 'system' ],
2022-11-05 14:43:54 -05:00
);
2019-05-02 22:50:18 +02:00
}
2019-03-12 23:59:03 -05:00
}