librenms-librenms/tests/CommonFunctionsTest.php
Tony Murray fff66d3c00 Feature: Generic discovery and poller tests (#7873)
* Processor Tests!

* Capture data from live devices easily.

* fix up some stuff, remove powerconnect things as they seem to be just broken.

* formatting, fix missing assignment
add netonix processor data

* fix multi-line, always add sysDescr and sysObjectID
ios cpm test file

* revert composer change and fix whitespace issues

* add help text

* missed help text

* tighter debug output

* handle empty strings properly and mibs with numbers

* use keys for sorting as intended

* fix type with empty data

* oops :)

* whitespace fix

* try installing fping

* Fix TestCase collision + cleanup

* mark TestCase as abstract
don't run two instances of snmpsim

* better database dumps, improved capture

* style fixes

* fix quotes add a few more tables

* add --prefer-new, properly merge data

* Support separate discovery and poller data. But don't waste space if they aren't needed.

* refactor to use class
collects all code in one place for reusability

* reorganize

* Print out when not saving.

* Support for running multiple (or all) modules at once.

* tidy

* Change unit test to a generic name and test all modules we have data for.

* Add documentation and a few more tidies

* whitespace fixes

* Fix tests, add a couple more modules, more docs

* More docs updates

* Fix scrutinizer issues

* add bgp-peers
2017-12-20 08:36:49 -06:00

184 lines
7.7 KiB
PHP

<?php
/**
* CommonFunctionsTest.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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2016 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Tests;
class CommonFunctionsTest extends TestCase
{
public function testStrContains()
{
$data = 'This is a test. Just Testing.';
$this->assertTrue(str_contains($data, 'Just'));
$this->assertFalse(str_contains($data, 'just'));
$this->assertTrue(str_contains($data, 'juSt', true));
$this->assertFalse(str_contains($data, 'nope', true));
$this->assertTrue(str_contains($data, array('not', 'this', 'This')));
$this->assertFalse(str_contains($data, array('not', 'this')));
$this->assertTrue(str_contains($data, array('not', 'thIs'), true));
$this->assertFalse(str_contains($data, array('not', 'anything'), true));
}
public function testStartsWith()
{
$data = 'This is a test. Just Testing that.';
$this->assertTrue(starts_with($data, 'This'));
$this->assertFalse(starts_with($data, 'this'));
$this->assertTrue(starts_with($data, 'thIs', true));
$this->assertFalse(starts_with($data, 'test', true));
$this->assertTrue(starts_with($data, array('this', 'Test', 'This')));
$this->assertFalse(starts_with($data, array('this', 'Test')));
$this->assertTrue(starts_with($data, array('Test', 'no', 'thiS'), true));
$this->assertFalse(starts_with($data, array('just', 'Test'), true));
}
public function testEndsWith()
{
$data = 'This is a test. Just Testing';
$this->assertTrue(ends_with($data, 'Testing'));
$this->assertFalse(ends_with($data, 'testing'));
$this->assertTrue(ends_with($data, 'testIng', true));
$this->assertFalse(ends_with($data, 'test', true));
$this->assertTrue(ends_with($data, array('this', 'Testing', 'This')));
$this->assertFalse(ends_with($data, array('this', 'Test')));
$this->assertTrue(ends_with($data, array('this', 'tesTing', 'no'), true));
$this->assertFalse(ends_with($data, array('this', 'Test'), true));
}
public function testRrdDescriptions()
{
$data = 'Toner, S/N:CR_UM-16021314488.';
$this->assertEquals('Toner, S/N CR_UM-16021314488.', safedescr($data));
}
public function testSetNull()
{
$this->assertNull(set_null('BAD-DATA'));
$this->assertEquals(0, set_null(0));
$this->assertEquals(25, set_null(25));
$this->assertEquals(-25, set_null(-25));
$this->assertEquals(99, set_null(' ', 99));
$this->assertNull(set_null(-25, null, 0));
$this->assertEquals(2, set_null(2, 0, 2));
}
public function testDisplay()
{
$this->assertEquals('&lt;html&gt;string&lt;/html&gt;', display('<html>string</html>'));
$this->assertEquals('&lt;script&gt;alert("test")&lt;/script&gt;', display('<script>alert("test")</script>'));
$tmp_config = array(
'HTML.Allowed' => 'b,iframe,i,ul,li,h1,h2,h3,h4,br,p',
'HTML.Trusted' => true,
'HTML.SafeIframe' => true,
);
$this->assertEquals('<b>Bold</b>', display('<b>Bold</b>', $tmp_config));
$this->assertEquals('', display('<script>alert("test")</script>', $tmp_config));
}
public function testStringToClass()
{
$this->assertSame('LibreNMS\OS\Os', str_to_class('OS', 'LibreNMS\\OS\\'));
$this->assertSame('SpacesName', str_to_class('spaces name'));
$this->assertSame('DashName', str_to_class('dash-name'));
$this->assertSame('UnderscoreName', str_to_class('underscore_name'));
$this->assertSame('LibreNMS\\AllOfThemName', str_to_class('all OF-thEm_NaMe', 'LibreNMS\\'));
}
public function testIsValidHostname()
{
$this->assertTrue(is_valid_hostname('a'), 'a');
$this->assertTrue(is_valid_hostname('a.'), 'a.');
$this->assertTrue(is_valid_hostname('0'), '0');
$this->assertTrue(is_valid_hostname('a.b'), 'a.b');
$this->assertTrue(is_valid_hostname('localhost'), 'localhost');
$this->assertTrue(is_valid_hostname('google.com'), 'google.com');
$this->assertTrue(is_valid_hostname('news.google.co.uk'), 'news.google.co.uk');
$this->assertTrue(is_valid_hostname('xn--fsqu00a.xn--0zwm56d'), 'xn--fsqu00a.xn--0zwm56d');
$this->assertTrue(is_valid_hostname('www.averylargedomainthatdoesnotreallyexist.com'), 'www.averylargedomainthatdoesnotreallyexist.com');
$this->assertTrue(is_valid_hostname('cont-ains.h-yph-en-s.com'), 'cont-ains.h-yph-en-s.com');
$this->assertTrue(is_valid_hostname('cisco-3750x'), 'cisco-3750x');
$this->assertFalse(is_valid_hostname('cisco_3750x'), 'cisco_3750x');
$this->assertFalse(is_valid_hostname('goo gle.com'), 'goo gle.com');
$this->assertFalse(is_valid_hostname('google..com'), 'google..com');
$this->assertFalse(is_valid_hostname('google.com '), 'google.com ');
$this->assertFalse(is_valid_hostname('google-.com'), 'google-.com');
$this->assertFalse(is_valid_hostname('.google.com'), '.google.com');
$this->assertFalse(is_valid_hostname('..google.com'), '..google.com');
$this->assertFalse(is_valid_hostname('<script'), '<script');
$this->assertFalse(is_valid_hostname('alert('), 'alert(');
$this->assertFalse(is_valid_hostname('.'), '.');
$this->assertFalse(is_valid_hostname('..'), '..');
$this->assertFalse(is_valid_hostname(' '), 'Just a space');
$this->assertFalse(is_valid_hostname('-'), '-');
$this->assertFalse(is_valid_hostname(''), 'Empty string');
}
public function testResolveGlues()
{
if (getenv('DBTEST')) {
dbConnect();
dbBeginTransaction();
} else {
$this->markTestSkipped('Database tests not enabled. Set DBTEST=1 to enable.');
}
$this->assertFalse(ResolveGlues(array('dbSchema'), 'device_id'));
$this->assertSame(array('devices.device_id'), ResolveGlues(array('devices'), 'device_id'));
$this->assertSame(array('sensors.device_id'), ResolveGlues(array('sensors'), 'device_id'));
// does not work right with current code
// $expected = array('bill_data.bill_id', 'bill_ports.port_id', 'ports.device_id');
// $this->assertSame($expected, ResolveGlues(array('bill_data'), 'device_id'));
$expected = array('application_metrics.app_id', "applications.device_id");
$this->assertSame($expected, ResolveGlues(array('application_metrics'), 'device_id'));
$expected = array('state_translations.state_index_id', 'sensors_to_state_indexes.sensor_id', 'sensors.device_id');
$this->assertSame($expected, ResolveGlues(array('state_translations'), 'device_id'));
$expected = array('ipv4_addresses.port_id', 'ports.device_id');
$this->assertSame($expected, ResolveGlues(array('ipv4_addresses'), 'device_id'));
if (getenv('DBTEST')) {
dbRollbackTransaction();
}
}
}