2018-08-14 01:56:16 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* DeviceTest.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/>.
|
2018-08-14 01:56:16 -05:00
|
|
|
*
|
2021-02-09 00:29:04 +01:00
|
|
|
* @link https://www.librenms.org
|
2021-09-10 20:09:53 +02:00
|
|
|
*
|
2018-08-14 01:56:16 -05:00
|
|
|
* @copyright 2018 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace LibreNMS\Tests\Unit;
|
|
|
|
|
|
|
|
use App\Models\Device;
|
|
|
|
use App\Models\Ipv4Address;
|
|
|
|
use App\Models\Port;
|
|
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
2019-10-13 13:40:38 +00:00
|
|
|
use LibreNMS\Tests\DBTestCase;
|
2018-08-14 01:56:16 -05:00
|
|
|
|
2019-10-13 13:40:38 +00:00
|
|
|
class DeviceTest extends DBTestCase
|
2018-08-14 01:56:16 -05:00
|
|
|
{
|
|
|
|
use DatabaseTransactions;
|
|
|
|
|
2023-05-24 22:21:54 +02:00
|
|
|
public function testFindByHostname(): void
|
2018-08-14 01:56:16 -05:00
|
|
|
{
|
2021-07-13 16:35:43 -05:00
|
|
|
$device = Device::factory()->create(); /** @var Device $device */
|
2018-08-14 01:56:16 -05:00
|
|
|
$found = Device::findByHostname($device->hostname);
|
|
|
|
$this->assertNotNull($found);
|
|
|
|
$this->assertEquals($device->device_id, $found->device_id, 'Did not find the correct device');
|
|
|
|
}
|
|
|
|
|
2023-05-24 22:21:54 +02:00
|
|
|
public function testFindByIpFail(): void
|
2018-08-14 01:56:16 -05:00
|
|
|
{
|
|
|
|
$found = Device::findByIp('this is not an ip');
|
|
|
|
$this->assertNull($found);
|
|
|
|
}
|
|
|
|
|
2023-05-24 22:21:54 +02:00
|
|
|
public function testFindByIpv4Fail(): void
|
2018-08-14 01:56:16 -05:00
|
|
|
{
|
|
|
|
$found = Device::findByIp('182.43.219.43');
|
|
|
|
$this->assertNull($found);
|
|
|
|
}
|
|
|
|
|
2023-05-24 22:21:54 +02:00
|
|
|
public function testFindByIpv6Fail(): void
|
2018-08-14 01:56:16 -05:00
|
|
|
{
|
|
|
|
$found = Device::findByIp('341a:234d:3429:9845:909f:fd32:1930:32dc');
|
|
|
|
$this->assertNull($found);
|
|
|
|
}
|
|
|
|
|
2023-05-24 22:21:54 +02:00
|
|
|
public function testFindIpButNoPort(): void
|
2018-08-14 01:56:16 -05:00
|
|
|
{
|
2021-07-13 16:35:43 -05:00
|
|
|
$ipv4 = Ipv4Address::factory()->create(); /** @var Ipv4Address $ipv4 */
|
2018-08-14 01:56:16 -05:00
|
|
|
Port::destroy($ipv4->port_id);
|
|
|
|
|
|
|
|
$found = Device::findByIp($ipv4->ipv4_address);
|
|
|
|
$this->assertNull($found);
|
|
|
|
}
|
|
|
|
|
2023-05-24 22:21:54 +02:00
|
|
|
public function testFindByIp(): void
|
2018-08-14 01:56:16 -05:00
|
|
|
{
|
2021-07-13 16:35:43 -05:00
|
|
|
$device = Device::factory()->create(); /** @var Device $device */
|
2018-08-14 01:56:16 -05:00
|
|
|
$found = Device::findByIp($device->ip);
|
|
|
|
$this->assertNotNull($found);
|
|
|
|
$this->assertEquals($device->device_id, $found->device_id, 'Did not find the correct device');
|
|
|
|
}
|
|
|
|
|
2023-05-24 22:21:54 +02:00
|
|
|
public function testFindByIpHostname(): void
|
2018-08-14 01:56:16 -05:00
|
|
|
{
|
|
|
|
$ip = '192.168.234.32';
|
2021-07-13 16:35:43 -05:00
|
|
|
$device = Device::factory()->create(['hostname' => $ip]); /** @var Device $device */
|
2018-08-14 01:56:16 -05:00
|
|
|
$found = Device::findByIp($ip);
|
|
|
|
$this->assertNotNull($found);
|
|
|
|
$this->assertEquals($device->device_id, $found->device_id, 'Did not find the correct device');
|
|
|
|
}
|
|
|
|
|
2023-05-24 22:21:54 +02:00
|
|
|
public function testFindByIpThroughPort(): void
|
2018-08-14 01:56:16 -05:00
|
|
|
{
|
2021-07-13 16:35:43 -05:00
|
|
|
$device = Device::factory()->create(); /** @var Device $device */
|
|
|
|
$port = Port::factory()->make(); /** @var Port $port */
|
2018-08-14 01:56:16 -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 */
|
2018-08-14 01:56:16 -05:00
|
|
|
$port->ipv4()->save($ipv4);
|
|
|
|
|
|
|
|
$found = Device::findByIp($ipv4->ipv4_address);
|
|
|
|
$this->assertNotNull($found);
|
|
|
|
$this->assertEquals($device->device_id, $found->device_id, 'Did not find the correct device');
|
|
|
|
}
|
|
|
|
}
|