1<?php 2/** 3 * CommonFunctionsTest.php 4 * 5 * -Description- 6 * 7 * This program is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program. If not, see <https://www.gnu.org/licenses/>. 19 * 20 * @link https://www.librenms.org 21 * @copyright 2016 Tony Murray 22 * @author Tony Murray <murraytony@gmail.com> 23 */ 24 25namespace LibreNMS\Tests; 26 27use Illuminate\Support\Str; 28use LibreNMS\Config; 29use LibreNMS\Util\Clean; 30use LibreNMS\Util\Validate; 31 32class CommonFunctionsTest extends TestCase 33{ 34 public function testStrContains() 35 { 36 $data = 'This is a test. Just Testing.'; 37 38 $this->assertTrue(Str::contains($data, 'Just')); 39 $this->assertFalse(Str::contains($data, 'just')); 40 41 $this->assertTrue(str_i_contains($data, 'juSt')); 42 $this->assertFalse(str_i_contains($data, 'nope')); 43 44 $this->assertTrue(Str::contains($data, ['not', 'this', 'This'])); 45 $this->assertFalse(Str::contains($data, ['not', 'this'])); 46 47 $this->assertTrue(str_i_contains($data, ['not', 'thIs'])); 48 $this->assertFalse(str_i_contains($data, ['not', 'anything'])); 49 } 50 51 public function testStartsWith() 52 { 53 $data = 'This is a test. Just Testing that.'; 54 55 $this->assertTrue(Str::startsWith($data, 'This')); 56 $this->assertFalse(Str::startsWith($data, 'this')); 57 58 $this->assertTrue(Str::startsWith($data, ['this', 'Test', 'This'])); 59 $this->assertFalse(Str::startsWith($data, ['this', 'Test'])); 60 } 61 62 public function testEndsWith() 63 { 64 $data = 'This is a test. Just Testing'; 65 66 $this->assertTrue(Str::endsWith($data, 'Testing')); 67 $this->assertFalse(Str::endsWith($data, 'testing')); 68 69 $this->assertTrue(Str::endsWith($data, ['this', 'Testing', 'This'])); 70 $this->assertFalse(Str::endsWith($data, ['this', 'Test'])); 71 } 72 73 public function testRrdDescriptions() 74 { 75 $data = 'Toner, S/N:CR_UM-16021314488.'; 76 $this->assertEquals('Toner, S/N CR_UM-16021314488.', \LibreNMS\Data\Store\Rrd::safeDescr($data)); 77 } 78 79 public function testSetNull() 80 { 81 $this->assertNull(set_null('BAD-DATA')); 82 $this->assertEquals(0, set_null(0)); 83 $this->assertEquals(25, set_null(25)); 84 $this->assertEquals(-25, set_null(-25)); 85 $this->assertEquals(99, set_null(' ', 99)); 86 $this->assertNull(set_null(-25, null, 0)); 87 $this->assertEquals(2, set_null(2, 0, 2)); 88 } 89 90 public function testDisplay() 91 { 92 $this->assertEquals('<html>string</html>', Clean::html('<html>string</html>', [])); 93 $this->assertEquals('<script>alert("test")</script>', Clean::html('<script>alert("test")</script>', [])); 94 95 $tmp_config = [ 96 'HTML.Allowed' => 'b,iframe,i,ul,li,h1,h2,h3,h4,br,p', 97 'HTML.Trusted' => true, 98 'HTML.SafeIframe' => true, 99 ]; 100 101 $this->assertEquals('<b>Bold</b>', Clean::html('<b>Bold</b>', $tmp_config)); 102 $this->assertEquals('', Clean::html('<script>alert("test")</script>', $tmp_config)); 103 } 104 105 public function testStringToClass() 106 { 107 $this->assertSame('LibreNMS\OS\Os', str_to_class('OS', 'LibreNMS\\OS\\')); 108 $this->assertSame('SpacesName', str_to_class('spaces name')); 109 $this->assertSame('DashName', str_to_class('dash-name')); 110 $this->assertSame('UnderscoreName', str_to_class('underscore_name')); 111 $this->assertSame('LibreNMS\\AllOfThemName', str_to_class('all OF-thEm_NaMe', 'LibreNMS\\')); 112 } 113 114 public function testIsValidHostname() 115 { 116 $this->assertTrue(Validate::hostname('a'), 'a'); 117 $this->assertTrue(Validate::hostname('a.'), 'a.'); 118 $this->assertTrue(Validate::hostname('0'), '0'); 119 $this->assertTrue(Validate::hostname('a.b'), 'a.b'); 120 $this->assertTrue(Validate::hostname('localhost'), 'localhost'); 121 $this->assertTrue(Validate::hostname('google.com'), 'google.com'); 122 $this->assertTrue(Validate::hostname('news.google.co.uk'), 'news.google.co.uk'); 123 $this->assertTrue(Validate::hostname('xn--fsqu00a.xn--0zwm56d'), 'xn--fsqu00a.xn--0zwm56d'); 124 $this->assertTrue(Validate::hostname('www.averylargedomainthatdoesnotreallyexist.com'), 'www.averylargedomainthatdoesnotreallyexist.com'); 125 $this->assertTrue(Validate::hostname('cont-ains.h-yph-en-s.com'), 'cont-ains.h-yph-en-s.com'); 126 $this->assertTrue(Validate::hostname('cisco-3750x'), 'cisco-3750x'); 127 $this->assertFalse(Validate::hostname('cisco_3750x'), 'cisco_3750x'); 128 $this->assertFalse(Validate::hostname('goo gle.com'), 'goo gle.com'); 129 $this->assertFalse(Validate::hostname('google..com'), 'google..com'); 130 $this->assertFalse(Validate::hostname('google.com '), 'google.com '); 131 $this->assertFalse(Validate::hostname('google-.com'), 'google-.com'); 132 $this->assertFalse(Validate::hostname('.google.com'), '.google.com'); 133 $this->assertFalse(Validate::hostname('..google.com'), '..google.com'); 134 $this->assertFalse(Validate::hostname('<script'), '<script'); 135 $this->assertFalse(Validate::hostname('alert('), 'alert('); 136 $this->assertFalse(Validate::hostname('.'), '.'); 137 $this->assertFalse(Validate::hostname('..'), '..'); 138 $this->assertFalse(Validate::hostname(' '), 'Just a space'); 139 $this->assertFalse(Validate::hostname('-'), '-'); 140 $this->assertFalse(Validate::hostname(''), 'Empty string'); 141 } 142 143 public function testResolveGlues() 144 { 145 $this->dbSetUp(); 146 147 $this->assertFalse(ResolveGlues(['dbSchema'], 'device_id')); 148 149 $this->assertSame(['devices.device_id'], ResolveGlues(['devices'], 'device_id')); 150 $this->assertSame(['sensors.device_id'], ResolveGlues(['sensors'], 'device_id')); 151 152 // does not work right with current code 153// $expected = array('bill_data.bill_id', 'bill_ports.port_id', 'ports.device_id'); 154// $this->assertSame($expected, ResolveGlues(array('bill_data'), 'device_id')); 155 156 $expected = ['application_metrics.app_id', 'applications.device_id']; 157 $this->assertSame($expected, ResolveGlues(['application_metrics'], 'device_id')); 158 159 $expected = ['state_translations.state_index_id', 'sensors_to_state_indexes.sensor_id', 'sensors.device_id']; 160 $this->assertSame($expected, ResolveGlues(['state_translations'], 'device_id')); 161 162 $expected = ['ipv4_addresses.port_id', 'ports.device_id']; 163 $this->assertSame($expected, ResolveGlues(['ipv4_addresses'], 'device_id')); 164 165 $this->dbTearDown(); 166 } 167 168 public function testFormatHostname() 169 { 170 $device_dns = [ 171 'hostname' => 'test.librenms.org', 172 'sysName' => 'Testing DNS', 173 ]; 174 $device_ip = [ 175 'hostname' => '192.168.1.2', 176 'sysName' => 'Testing IP', 177 ]; 178 179 // both false 180 Config::set('force_ip_to_sysname', false); 181 Config::set('force_hostname_to_sysname', false); 182 $this->assertEquals('test.librenms.org', format_hostname($device_dns)); 183 $this->assertEquals('Not DNS', format_hostname($device_dns, 'Not DNS')); 184 $this->assertEquals('192.168.5.5', format_hostname($device_dns, '192.168.5.5')); 185 $this->assertEquals('192.168.1.2', format_hostname($device_ip)); 186 $this->assertEquals('hostname.like', format_hostname($device_ip, 'hostname.like')); 187 $this->assertEquals('10.10.10.10', format_hostname($device_ip, '10.10.10.10')); 188 189 // ip to sysname 190 Config::set('force_ip_to_sysname', true); 191 Config::set('force_hostname_to_sysname', false); 192 $this->assertEquals('test.librenms.org', format_hostname($device_dns)); 193 $this->assertEquals('Not DNS', format_hostname($device_dns, 'Not DNS')); 194 $this->assertEquals('Testing DNS', format_hostname($device_dns, '192.168.5.5')); 195 $this->assertEquals('Testing IP', format_hostname($device_ip)); 196 $this->assertEquals('hostname.like', format_hostname($device_ip, 'hostname.like')); 197 $this->assertEquals('Testing IP', format_hostname($device_ip, '10.10.10.10')); 198 199 // dns to sysname 200 Config::set('force_ip_to_sysname', false); 201 Config::set('force_hostname_to_sysname', true); 202 $this->assertEquals('Testing DNS', format_hostname($device_dns)); 203 $this->assertEquals('Not DNS', format_hostname($device_dns, 'Not DNS')); 204 $this->assertEquals('192.168.5.5', format_hostname($device_dns, '192.168.5.5')); 205 $this->assertEquals('192.168.1.2', format_hostname($device_ip)); 206 $this->assertEquals('Testing IP', format_hostname($device_ip, 'hostname.like')); 207 $this->assertEquals('10.10.10.10', format_hostname($device_ip, '10.10.10.10')); 208 209 // both true 210 Config::set('force_ip_to_sysname', true); 211 Config::set('force_hostname_to_sysname', true); 212 $this->assertEquals('Testing DNS', format_hostname($device_dns)); 213 $this->assertEquals('Not DNS', format_hostname($device_dns, 'Not DNS')); 214 $this->assertEquals('Testing DNS', format_hostname($device_dns, '192.168.5.5')); 215 $this->assertEquals('Testing IP', format_hostname($device_ip)); 216 $this->assertEquals('Testing IP', format_hostname($device_ip, 'hostname.like')); 217 $this->assertEquals('Testing IP', format_hostname($device_ip, '10.10.10.10')); 218 } 219 220 public function testPortAssociation() 221 { 222 $modes = [ 223 1 => 'ifIndex', 224 2 => 'ifName', 225 3 => 'ifDescr', 226 4 => 'ifAlias', 227 ]; 228 229 $this->assertEquals($modes, get_port_assoc_modes()); 230 $this->assertEquals('ifIndex', get_port_assoc_mode_name(1)); 231 $this->assertEquals(1, get_port_assoc_mode_id('ifIndex')); 232 $this->assertFalse(get_port_assoc_mode_name(666)); 233 $this->assertFalse(get_port_assoc_mode_id('lucifer')); 234 } 235} 236