librenms/tests/FunctionsTest.php

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

157 lines
7.0 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* FunctionsTest.php
*
* tests functions in includes/functions.php
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
2021-09-10 18:09:53 +00:00
*
* @copyright 2017 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Tests;
Update IronWare sensors & bgp-peers discovery, allow skip_values to target a specific index appended to the OID (#10941) * Migrate ironware sensor discovery from php to yaml. * More sensors, add grouping. * dynamic_discovery_get_value() becomes getValueFromData() * Target a specific index with skip_values. * Improve Brocade BGP session discovery/polling This commit allows for the correct discovery of BGP sessions with 32-bit ASNs, IPv6 neighbors using the BGP4V2-MIB which is based on draft-ietf-idr-bgp4-mibv2-11 and also polls for IPv4 unicast received routes through the FOUNDRY-SN-BGP4-GROUP-MIB. Copied most of the code from PR#8877 by @Mikeburke14, cleaned up the code a little bit to match the normal LibreNMS style, and fixed bgpPeers_cbgp discovery as well as polling for the ipv4.unicast neighbors. Note that older Brocade IronWare firmware versions are known to have multiple defects relating to the BGP4V2-MIB which might result in certain missing non-established neighbors. Related vendor defect numbers: - DEFECT000633962 -- Symptom: The OID bgp4V2PeerAdminStatus does not return the correct value -- Reported: NI 05.7.00 -- Resolved: NI 05.8.00g - DEFECT000583319 -- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2) does not display all the BGP entries -- Reported: NI 05.6.00 -- Resolved: NI 05.8.00e - DEFECT000550309 -- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2) does not display the full information -- Reported: NI 05.7.00 -- Resolved: NI 05.8.00c * Add ironware CER & ICX platform test data. * Re-add ironware.json compatible with current master branch.
2020-02-01 22:28:03 +00:00
use LibreNMS\Device\YamlDiscovery;
use LibreNMS\Enum\IntegerType;
use LibreNMS\Util\Number;
Entity Physical discovery: Rewrite to modern style (#16289) * Initial entity-physical code * Split out Entity-MIB trait * Cisco Cellular inventory * Fix bad test data * alfo80hd - we now include all entPhysical entries * Correct aos7 test data * Add entPhysicalClass as last resort for label in ui * aos add previously filtered data * Fixup arista-eos data * Update ariast_eos data * Arris, clean garbage in Rev fields * Aruba Instant custom inventory ported * ArubaOS CX add vendor type mib * aviat-wtm test data refresh * axos add shelf fix data fields a bit * ciena-rls * ciena-sds * Skip cimc for now... no test data * Cisco updates * Comware data update * Update dnos * Clean Edgeos garbage, make code from Arris shareable * Relaxed ifIndex match, some devices cheat and send back static strings instead of formatted OIDs * Regex refinement and updated edgeos with new clean data * Update edgeswitch data * Update eltex-mes21xx data * eltex-mes23xx * Guess at eltex-mes24xx since there is no test data * Update eurostor, fix firmware version * Apply fixes from StyleCI * fixes * Update fortigate data * Update fortiweb, ftd, and fusion * Update linux LSI * Fix hexToAscii null removal with different seperator handling * icotera add final snmprec data to avoid snmpsim bug * Update IOS data * Update mrv-od * Add junos translation * Generic data updates n-r * ruijie workaround snmpsim bug * Port saf-cfm * Recode Schleifenbauer, and fix entPhysicalIndex values * SmartAX fixes * sm-os and tait-infra93 * timos inventory was not right, fix it up * ubiquoss-pon * VRP, has custom data collection on top of normal adapt port ifIndex lookup to handle it * VRP exceeded the string length specified in ENTITY-MIB... * data updates * Final data update and code cleanup * Apply fixes from StyleCI * Lint fixes * Add missing SnmpResponse->pluck() code * Update db_schema.yaml * Fix bad test data * Another instant-on update * oops * Remove some unused code # Conflicts: # includes/html/pages/device/overview.inc.php --------- Co-authored-by: Tony Murray <murrant@users.noreply.github.com>
2024-08-21 06:12:09 +00:00
use LibreNMS\Util\StringHelpers;
use LibreNMS\Util\Time;
Update IronWare sensors & bgp-peers discovery, allow skip_values to target a specific index appended to the OID (#10941) * Migrate ironware sensor discovery from php to yaml. * More sensors, add grouping. * dynamic_discovery_get_value() becomes getValueFromData() * Target a specific index with skip_values. * Improve Brocade BGP session discovery/polling This commit allows for the correct discovery of BGP sessions with 32-bit ASNs, IPv6 neighbors using the BGP4V2-MIB which is based on draft-ietf-idr-bgp4-mibv2-11 and also polls for IPv4 unicast received routes through the FOUNDRY-SN-BGP4-GROUP-MIB. Copied most of the code from PR#8877 by @Mikeburke14, cleaned up the code a little bit to match the normal LibreNMS style, and fixed bgpPeers_cbgp discovery as well as polling for the ipv4.unicast neighbors. Note that older Brocade IronWare firmware versions are known to have multiple defects relating to the BGP4V2-MIB which might result in certain missing non-established neighbors. Related vendor defect numbers: - DEFECT000633962 -- Symptom: The OID bgp4V2PeerAdminStatus does not return the correct value -- Reported: NI 05.7.00 -- Resolved: NI 05.8.00g - DEFECT000583319 -- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2) does not display all the BGP entries -- Reported: NI 05.6.00 -- Resolved: NI 05.8.00e - DEFECT000550309 -- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2) does not display the full information -- Reported: NI 05.7.00 -- Resolved: NI 05.8.00c * Add ironware CER & ICX platform test data. * Re-add ironware.json compatible with current master branch.
2020-02-01 22:28:03 +00:00
class FunctionsTest extends TestCase
{
public function testHex2Str(): void
{
$this->assertEquals('Big 10 UP', hex2str('426967203130205550'));
}
public function testSnmpHexstring(): void
{
$input = '4c 61 72 70 69 6e 67 20 34 20 55 00 0a';
$this->assertEquals("Larping 4 U\n", snmp_hexstring($input));
}
public function testIsHexString(): void
{
Entity Physical discovery: Rewrite to modern style (#16289) * Initial entity-physical code * Split out Entity-MIB trait * Cisco Cellular inventory * Fix bad test data * alfo80hd - we now include all entPhysical entries * Correct aos7 test data * Add entPhysicalClass as last resort for label in ui * aos add previously filtered data * Fixup arista-eos data * Update ariast_eos data * Arris, clean garbage in Rev fields * Aruba Instant custom inventory ported * ArubaOS CX add vendor type mib * aviat-wtm test data refresh * axos add shelf fix data fields a bit * ciena-rls * ciena-sds * Skip cimc for now... no test data * Cisco updates * Comware data update * Update dnos * Clean Edgeos garbage, make code from Arris shareable * Relaxed ifIndex match, some devices cheat and send back static strings instead of formatted OIDs * Regex refinement and updated edgeos with new clean data * Update edgeswitch data * Update eltex-mes21xx data * eltex-mes23xx * Guess at eltex-mes24xx since there is no test data * Update eurostor, fix firmware version * Apply fixes from StyleCI * fixes * Update fortigate data * Update fortiweb, ftd, and fusion * Update linux LSI * Fix hexToAscii null removal with different seperator handling * icotera add final snmprec data to avoid snmpsim bug * Update IOS data * Update mrv-od * Add junos translation * Generic data updates n-r * ruijie workaround snmpsim bug * Port saf-cfm * Recode Schleifenbauer, and fix entPhysicalIndex values * SmartAX fixes * sm-os and tait-infra93 * timos inventory was not right, fix it up * ubiquoss-pon * VRP, has custom data collection on top of normal adapt port ifIndex lookup to handle it * VRP exceeded the string length specified in ENTITY-MIB... * data updates * Final data update and code cleanup * Apply fixes from StyleCI * Lint fixes * Add missing SnmpResponse->pluck() code * Update db_schema.yaml * Fix bad test data * Another instant-on update * oops * Remove some unused code # Conflicts: # includes/html/pages/device/overview.inc.php --------- Co-authored-by: Tony Murray <murrant@users.noreply.github.com>
2024-08-21 06:12:09 +00:00
$this->assertTrue(StringHelpers::isHex('af 28 02'));
$this->assertTrue(StringHelpers::isHex('aF 28 02 CE'));
$this->assertFalse(StringHelpers::isHex('a5 fj 53'));
$this->assertFalse(StringHelpers::isHex('a5fe53'));
}
public function testDynamicDiscoveryGetValue(): void
{
$pre_cache = [
'firstdata' => [
0 => ['temp' => 1],
1 => ['temp' => 2],
],
'high' => [
0 => ['high' => 3],
1 => ['high' => 4],
],
'table' => [
0 => ['first' => 5, 'second' => 6],
1 => ['first' => 7, 'second' => 8],
],
'single' => ['something' => 9],
'oneoff' => 10,
'singletable' => [
11 => ['singletable' => 'Pickle'],
],
'doubletable' => [
12 => ['doubletable' => 'Mustard'],
13 => ['doubletable' => 'BBQ'],
],
];
$data = ['value' => 'temp', 'oid' => 'firstdata'];
Update IronWare sensors & bgp-peers discovery, allow skip_values to target a specific index appended to the OID (#10941) * Migrate ironware sensor discovery from php to yaml. * More sensors, add grouping. * dynamic_discovery_get_value() becomes getValueFromData() * Target a specific index with skip_values. * Improve Brocade BGP session discovery/polling This commit allows for the correct discovery of BGP sessions with 32-bit ASNs, IPv6 neighbors using the BGP4V2-MIB which is based on draft-ietf-idr-bgp4-mibv2-11 and also polls for IPv4 unicast received routes through the FOUNDRY-SN-BGP4-GROUP-MIB. Copied most of the code from PR#8877 by @Mikeburke14, cleaned up the code a little bit to match the normal LibreNMS style, and fixed bgpPeers_cbgp discovery as well as polling for the ipv4.unicast neighbors. Note that older Brocade IronWare firmware versions are known to have multiple defects relating to the BGP4V2-MIB which might result in certain missing non-established neighbors. Related vendor defect numbers: - DEFECT000633962 -- Symptom: The OID bgp4V2PeerAdminStatus does not return the correct value -- Reported: NI 05.7.00 -- Resolved: NI 05.8.00g - DEFECT000583319 -- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2) does not display all the BGP entries -- Reported: NI 05.6.00 -- Resolved: NI 05.8.00e - DEFECT000550309 -- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2) does not display the full information -- Reported: NI 05.7.00 -- Resolved: NI 05.8.00c * Add ironware CER & ICX platform test data. * Re-add ironware.json compatible with current master branch.
2020-02-01 22:28:03 +00:00
$this->assertNull(YamlDiscovery::getValueFromData('missing', 0, $data, $pre_cache));
$this->assertSame('yar', YamlDiscovery::getValueFromData('default', 0, $data, $pre_cache, 'yar'));
$this->assertSame(2, YamlDiscovery::getValueFromData('value', 1, $data, $pre_cache));
$data = ['oid' => 'high'];
Update IronWare sensors & bgp-peers discovery, allow skip_values to target a specific index appended to the OID (#10941) * Migrate ironware sensor discovery from php to yaml. * More sensors, add grouping. * dynamic_discovery_get_value() becomes getValueFromData() * Target a specific index with skip_values. * Improve Brocade BGP session discovery/polling This commit allows for the correct discovery of BGP sessions with 32-bit ASNs, IPv6 neighbors using the BGP4V2-MIB which is based on draft-ietf-idr-bgp4-mibv2-11 and also polls for IPv4 unicast received routes through the FOUNDRY-SN-BGP4-GROUP-MIB. Copied most of the code from PR#8877 by @Mikeburke14, cleaned up the code a little bit to match the normal LibreNMS style, and fixed bgpPeers_cbgp discovery as well as polling for the ipv4.unicast neighbors. Note that older Brocade IronWare firmware versions are known to have multiple defects relating to the BGP4V2-MIB which might result in certain missing non-established neighbors. Related vendor defect numbers: - DEFECT000633962 -- Symptom: The OID bgp4V2PeerAdminStatus does not return the correct value -- Reported: NI 05.7.00 -- Resolved: NI 05.8.00g - DEFECT000583319 -- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2) does not display all the BGP entries -- Reported: NI 05.6.00 -- Resolved: NI 05.8.00e - DEFECT000550309 -- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2) does not display the full information -- Reported: NI 05.7.00 -- Resolved: NI 05.8.00c * Add ironware CER & ICX platform test data. * Re-add ironware.json compatible with current master branch.
2020-02-01 22:28:03 +00:00
$this->assertSame(3, YamlDiscovery::getValueFromData('high', 0, $data, $pre_cache));
$data = ['oid' => 'table'];
Update IronWare sensors & bgp-peers discovery, allow skip_values to target a specific index appended to the OID (#10941) * Migrate ironware sensor discovery from php to yaml. * More sensors, add grouping. * dynamic_discovery_get_value() becomes getValueFromData() * Target a specific index with skip_values. * Improve Brocade BGP session discovery/polling This commit allows for the correct discovery of BGP sessions with 32-bit ASNs, IPv6 neighbors using the BGP4V2-MIB which is based on draft-ietf-idr-bgp4-mibv2-11 and also polls for IPv4 unicast received routes through the FOUNDRY-SN-BGP4-GROUP-MIB. Copied most of the code from PR#8877 by @Mikeburke14, cleaned up the code a little bit to match the normal LibreNMS style, and fixed bgpPeers_cbgp discovery as well as polling for the ipv4.unicast neighbors. Note that older Brocade IronWare firmware versions are known to have multiple defects relating to the BGP4V2-MIB which might result in certain missing non-established neighbors. Related vendor defect numbers: - DEFECT000633962 -- Symptom: The OID bgp4V2PeerAdminStatus does not return the correct value -- Reported: NI 05.7.00 -- Resolved: NI 05.8.00g - DEFECT000583319 -- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2) does not display all the BGP entries -- Reported: NI 05.6.00 -- Resolved: NI 05.8.00e - DEFECT000550309 -- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2) does not display the full information -- Reported: NI 05.7.00 -- Resolved: NI 05.8.00c * Add ironware CER & ICX platform test data. * Re-add ironware.json compatible with current master branch.
2020-02-01 22:28:03 +00:00
$this->assertSame(5, YamlDiscovery::getValueFromData('first', 0, $data, $pre_cache));
$this->assertSame(7, YamlDiscovery::getValueFromData('first', 1, $data, $pre_cache));
$this->assertSame(6, YamlDiscovery::getValueFromData('second', 0, $data, $pre_cache));
$this->assertSame(8, YamlDiscovery::getValueFromData('second', 1, $data, $pre_cache));
Update IronWare sensors & bgp-peers discovery, allow skip_values to target a specific index appended to the OID (#10941) * Migrate ironware sensor discovery from php to yaml. * More sensors, add grouping. * dynamic_discovery_get_value() becomes getValueFromData() * Target a specific index with skip_values. * Improve Brocade BGP session discovery/polling This commit allows for the correct discovery of BGP sessions with 32-bit ASNs, IPv6 neighbors using the BGP4V2-MIB which is based on draft-ietf-idr-bgp4-mibv2-11 and also polls for IPv4 unicast received routes through the FOUNDRY-SN-BGP4-GROUP-MIB. Copied most of the code from PR#8877 by @Mikeburke14, cleaned up the code a little bit to match the normal LibreNMS style, and fixed bgpPeers_cbgp discovery as well as polling for the ipv4.unicast neighbors. Note that older Brocade IronWare firmware versions are known to have multiple defects relating to the BGP4V2-MIB which might result in certain missing non-established neighbors. Related vendor defect numbers: - DEFECT000633962 -- Symptom: The OID bgp4V2PeerAdminStatus does not return the correct value -- Reported: NI 05.7.00 -- Resolved: NI 05.8.00g - DEFECT000583319 -- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2) does not display all the BGP entries -- Reported: NI 05.6.00 -- Resolved: NI 05.8.00e - DEFECT000550309 -- Symptom: SNMP polling on bgp4V2PeerTable (OID brcdIp.3.5.1.1.2) does not display the full information -- Reported: NI 05.7.00 -- Resolved: NI 05.8.00c * Add ironware CER & ICX platform test data. * Re-add ironware.json compatible with current master branch.
2020-02-01 22:28:03 +00:00
$this->assertSame(9, YamlDiscovery::getValueFromData('single', 0, $data, $pre_cache));
$this->assertSame(10, YamlDiscovery::getValueFromData('oneoff', 3, $data, $pre_cache));
$this->assertSame('Pickle', YamlDiscovery::getValueFromData('singletable', 11, $data, $pre_cache));
$this->assertSame('BBQ', YamlDiscovery::getValueFromData('doubletable', 13, $data, $pre_cache));
}
public function testParseAtTime(): void
{
$this->assertEquals(time(), Time::parseAt('now'), 'now did not match');
$this->assertEquals(time() + 180, Time::parseAt('+3m'), '+3m did not match');
$this->assertEquals(time() + 7200, Time::parseAt('+2h'), '+2h did not match');
$this->assertEquals(time() + 172800, Time::parseAt('+2d'), '+2d did not match');
$this->assertEquals(time() + 63115200, Time::parseAt('+2y'), '+2y did not match');
$this->assertEquals(time() - 180, Time::parseAt('-3m'), '-3m did not match');
$this->assertEquals(time() - 7200, Time::parseAt('-2h'), '-2h did not match');
$this->assertEquals(time() - 172800, Time::parseAt('-2d'), '-2d did not match');
$this->assertEquals(time() - 63115200, Time::parseAt('-2y'), '-2y did not match');
$this->assertEquals(429929439, Time::parseAt('429929439'));
$this->assertEquals(212334234, Time::parseAt(212334234));
$this->assertEquals(time() - 43, Time::parseAt('-43'), '-43 did not match');
$this->assertEquals(0, Time::parseAt('invalid'));
$this->assertEquals(606614400, Time::parseAt('March 23 1989 UTC'));
$this->assertEquals(time() + 86400, Time::parseAt('+1 day'));
}
public function testNumberCast()
{
$this->assertSame(-14.3, Number::cast(-14.3));
$this->assertSame(0, Number::cast('b -35')); // cast must start with the number as old style php cast did
$this->assertSame(0, Number::cast('0 43 51'));
$this->assertSame(14.35, Number::cast('14.35 a'));
$this->assertSame(-43.332, Number::cast('-43.332 a'));
$this->assertSame(-12325234523.43, Number::cast('-12325234523.43asdf'));
$this->assertSame(1, Number::cast(1.0));
$this->assertSame(2, Number::cast('2.000'));
}
public function testNumberAsUnsigned()
{
$this->assertSame(42, Number::constrainInteger('42', IntegerType::int32)); /** @phpstan-ignore-line */
$this->assertSame(2147483647, Number::constrainInteger(2147483647, IntegerType::int32));
$this->assertSame(-2147483648, Number::constrainInteger(2147483648, IntegerType::int32));
$this->assertSame(-2147483647, Number::constrainInteger(2147483649, IntegerType::int32));
$this->assertSame(-1, Number::constrainInteger(4294967295, IntegerType::int32));
$this->assertSame(-3757, Number::constrainInteger(61779, IntegerType::int16));
$this->assertSame(0, Number::constrainInteger(0, IntegerType::uint32));
$this->assertSame(42, Number::constrainInteger(42, IntegerType::uint32));
$this->assertSame(4294967252, Number::constrainInteger(-42, IntegerType::uint32));
$this->assertSame(2147483648, Number::constrainInteger(-2147483646, IntegerType::uint32));
$this->assertSame(2147483647, Number::constrainInteger(-2147483647, IntegerType::uint32));
$this->assertSame(2147483646, Number::constrainInteger(-2147483648, IntegerType::uint32));
$this->assertSame(2147483645, Number::constrainInteger(-2147483649, IntegerType::uint32));
}
public function testNumberAsUnsignedValueExceedsMaxUnsignedValue()
{
$this->expectException(\InvalidArgumentException::class);
// Exceeds the maximum representable value for a 16-bit unsigned integer
Number::constrainInteger(4294967296, IntegerType::int16);
}
}