Issues (2963)

tests/CommonFunctionsTest.php (7 issues)

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
 *
22
 * @copyright  2016 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace LibreNMS\Tests;
27
28
use Illuminate\Support\Str;
29
use LibreNMS\Config;
30
use LibreNMS\Util\Clean;
31
use LibreNMS\Util\Validate;
32
33
class CommonFunctionsTest extends TestCase
34
{
35
    public function testStrContains()
36
    {
37
        $data = 'This is a test. Just Testing.';
38
39
        $this->assertTrue(Str::contains($data, 'Just'));
40
        $this->assertFalse(Str::contains($data, 'just'));
41
42
        $this->assertTrue(str_i_contains($data, 'juSt'));
43
        $this->assertFalse(str_i_contains($data, 'nope'));
44
45
        $this->assertTrue(Str::contains($data, ['not', 'this', 'This']));
46
        $this->assertFalse(Str::contains($data, ['not', 'this']));
47
48
        $this->assertTrue(str_i_contains($data, ['not', 'thIs']));
49
        $this->assertFalse(str_i_contains($data, ['not', 'anything']));
50
    }
51
52
    public function testStartsWith()
53
    {
54
        $data = 'This is a test. Just Testing that.';
55
56
        $this->assertTrue(Str::startsWith($data, 'This'));
57
        $this->assertFalse(Str::startsWith($data, 'this'));
58
59
        $this->assertTrue(Str::startsWith($data, ['this', 'Test', 'This']));
60
        $this->assertFalse(Str::startsWith($data, ['this', 'Test']));
61
    }
62
63
    public function testEndsWith()
64
    {
65
        $data = 'This is a test. Just Testing';
66
67
        $this->assertTrue(Str::endsWith($data, 'Testing'));
68
        $this->assertFalse(Str::endsWith($data, 'testing'));
69
70
        $this->assertTrue(Str::endsWith($data, ['this', 'Testing', 'This']));
71
        $this->assertFalse(Str::endsWith($data, ['this', 'Test']));
72
    }
73
74
    public function testRrdDescriptions()
75
    {
76
        $data = 'Toner, S/N:CR_UM-16021314488.';
77
        $this->assertEquals('Toner, S/N CR_UM-16021314488.', \LibreNMS\Data\Store\Rrd::safeDescr($data));
78
    }
79
80
    public function testSetNull()
81
    {
82
        $this->assertNull(set_null('BAD-DATA'));
0 ignored issues
show
Are you sure the usage of set_null('BAD-DATA') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
83
        $this->assertEquals(0, set_null(0));
0 ignored issues
show
Are you sure the usage of set_null(0) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
84
        $this->assertEquals(25, set_null(25));
0 ignored issues
show
Are you sure the usage of set_null(25) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
        $this->assertEquals(-25, set_null(-25));
0 ignored issues
show
Are you sure the usage of set_null(-25) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
86
        $this->assertEquals(99, set_null(' ', 99));
0 ignored issues
show
Are you sure the usage of set_null(' ', 99) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
87
        $this->assertNull(set_null(-25, null, 0));
0 ignored issues
show
Are you sure the usage of set_null(-25, null, 0) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
88
        $this->assertEquals(2, set_null(2, 0, 2));
0 ignored issues
show
Are you sure the usage of set_null(2, 0, 2) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
89
    }
90
91
    public function testDisplay()
92
    {
93
        $this->assertEquals('&lt;html&gt;string&lt;/html&gt;', Clean::html('<html>string</html>', []));
94
        $this->assertEquals('&lt;script&gt;alert("test")&lt;/script&gt;', Clean::html('<script>alert("test")</script>', []));
95
96
        $tmp_config = [
97
            'HTML.Allowed'    => 'b,iframe,i,ul,li,h1,h2,h3,h4,br,p',
98
            'HTML.Trusted'    => true,
99
            'HTML.SafeIframe' => true,
100
        ];
101
102
        $this->assertEquals('<b>Bold</b>', Clean::html('<b>Bold</b>', $tmp_config));
103
        $this->assertEquals('', Clean::html('<script>alert("test")</script>', $tmp_config));
104
    }
105
106
    public function testStringToClass()
107
    {
108
        $this->assertSame('LibreNMS\OS\Os', str_to_class('OS', 'LibreNMS\\OS\\'));
109
        $this->assertSame('SpacesName', str_to_class('spaces name'));
110
        $this->assertSame('DashName', str_to_class('dash-name'));
111
        $this->assertSame('UnderscoreName', str_to_class('underscore_name'));
112
        $this->assertSame('LibreNMS\\AllOfThemName', str_to_class('all OF-thEm_NaMe', 'LibreNMS\\'));
113
    }
114
115
    public function testIsValidHostname()
116
    {
117
        $this->assertTrue(Validate::hostname('a'), 'a');
118
        $this->assertTrue(Validate::hostname('a.'), 'a.');
119
        $this->assertTrue(Validate::hostname('0'), '0');
120
        $this->assertTrue(Validate::hostname('a.b'), 'a.b');
121
        $this->assertTrue(Validate::hostname('localhost'), 'localhost');
122
        $this->assertTrue(Validate::hostname('google.com'), 'google.com');
123
        $this->assertTrue(Validate::hostname('news.google.co.uk'), 'news.google.co.uk');
124
        $this->assertTrue(Validate::hostname('xn--fsqu00a.xn--0zwm56d'), 'xn--fsqu00a.xn--0zwm56d');
125
        $this->assertTrue(Validate::hostname('www.averylargedomainthatdoesnotreallyexist.com'), 'www.averylargedomainthatdoesnotreallyexist.com');
126
        $this->assertTrue(Validate::hostname('cont-ains.h-yph-en-s.com'), 'cont-ains.h-yph-en-s.com');
127
        $this->assertTrue(Validate::hostname('cisco-3750x'), 'cisco-3750x');
128
        $this->assertFalse(Validate::hostname('cisco_3750x'), 'cisco_3750x');
129
        $this->assertFalse(Validate::hostname('goo gle.com'), 'goo gle.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('..google.com'), '..google.com');
135
        $this->assertFalse(Validate::hostname('<script'), '<script');
136
        $this->assertFalse(Validate::hostname('alert('), 'alert(');
137
        $this->assertFalse(Validate::hostname('.'), '.');
138
        $this->assertFalse(Validate::hostname('..'), '..');
139
        $this->assertFalse(Validate::hostname(' '), 'Just a space');
140
        $this->assertFalse(Validate::hostname('-'), '-');
141
        $this->assertFalse(Validate::hostname(''), 'Empty string');
142
    }
143
144
    public function testResolveGlues()
145
    {
146
        $this->dbSetUp();
147
148
        $this->assertFalse(ResolveGlues(['dbSchema'], 'device_id'));
149
150
        $this->assertSame(['devices.device_id'], ResolveGlues(['devices'], 'device_id'));
151
        $this->assertSame(['sensors.device_id'], ResolveGlues(['sensors'], 'device_id'));
152
153
        // does not work right with current code
154
//        $expected = array('bill_data.bill_id', 'bill_ports.port_id', 'ports.device_id');
155
//        $this->assertSame($expected, ResolveGlues(array('bill_data'), 'device_id'));
156
157
        $expected = ['application_metrics.app_id', 'applications.device_id'];
158
        $this->assertSame($expected, ResolveGlues(['application_metrics'], 'device_id'));
159
160
        $expected = ['state_translations.state_index_id', 'sensors_to_state_indexes.sensor_id', 'sensors.device_id'];
161
        $this->assertSame($expected, ResolveGlues(['state_translations'], 'device_id'));
162
163
        $expected = ['ipv4_addresses.port_id', 'ports.device_id'];
164
        $this->assertSame($expected, ResolveGlues(['ipv4_addresses'], 'device_id'));
165
166
        $this->dbTearDown();
167
    }
168
169
    public function testFormatHostname()
170
    {
171
        $device_dns = [
172
            'hostname' => 'test.librenms.org',
173
            'sysName' => 'Testing DNS',
174
        ];
175
        $device_ip = [
176
            'hostname' => '192.168.1.2',
177
            'sysName' => 'Testing IP',
178
        ];
179
180
        // both false
181
        Config::set('force_ip_to_sysname', false);
182
        Config::set('force_hostname_to_sysname', false);
183
        $this->assertEquals('test.librenms.org', format_hostname($device_dns));
184
        $this->assertEquals('Not DNS', format_hostname($device_dns, 'Not DNS'));
185
        $this->assertEquals('192.168.5.5', format_hostname($device_dns, '192.168.5.5'));
186
        $this->assertEquals('192.168.1.2', format_hostname($device_ip));
187
        $this->assertEquals('hostname.like', format_hostname($device_ip, 'hostname.like'));
188
        $this->assertEquals('10.10.10.10', format_hostname($device_ip, '10.10.10.10'));
189
190
        // ip to sysname
191
        Config::set('force_ip_to_sysname', true);
192
        Config::set('force_hostname_to_sysname', false);
193
        $this->assertEquals('test.librenms.org', format_hostname($device_dns));
194
        $this->assertEquals('Not DNS', format_hostname($device_dns, 'Not DNS'));
195
        $this->assertEquals('Testing DNS', format_hostname($device_dns, '192.168.5.5'));
196
        $this->assertEquals('Testing IP', format_hostname($device_ip));
197
        $this->assertEquals('hostname.like', format_hostname($device_ip, 'hostname.like'));
198
        $this->assertEquals('Testing IP', format_hostname($device_ip, '10.10.10.10'));
199
200
        // dns to sysname
201
        Config::set('force_ip_to_sysname', false);
202
        Config::set('force_hostname_to_sysname', true);
203
        $this->assertEquals('Testing DNS', format_hostname($device_dns));
204
        $this->assertEquals('Not DNS', format_hostname($device_dns, 'Not DNS'));
205
        $this->assertEquals('192.168.5.5', format_hostname($device_dns, '192.168.5.5'));
206
        $this->assertEquals('192.168.1.2', format_hostname($device_ip));
207
        $this->assertEquals('Testing IP', format_hostname($device_ip, 'hostname.like'));
208
        $this->assertEquals('10.10.10.10', format_hostname($device_ip, '10.10.10.10'));
209
210
        // both true
211
        Config::set('force_ip_to_sysname', true);
212
        Config::set('force_hostname_to_sysname', true);
213
        $this->assertEquals('Testing DNS', format_hostname($device_dns));
214
        $this->assertEquals('Not DNS', format_hostname($device_dns, 'Not DNS'));
215
        $this->assertEquals('Testing DNS', format_hostname($device_dns, '192.168.5.5'));
216
        $this->assertEquals('Testing IP', format_hostname($device_ip));
217
        $this->assertEquals('Testing IP', format_hostname($device_ip, 'hostname.like'));
218
        $this->assertEquals('Testing IP', format_hostname($device_ip, '10.10.10.10'));
219
    }
220
221
    public function testPortAssociation()
222
    {
223
        $modes = [
224
            1 => 'ifIndex',
225
            2 => 'ifName',
226
            3 => 'ifDescr',
227
            4 => 'ifAlias',
228
        ];
229
230
        $this->assertEquals($modes, get_port_assoc_modes());
231
        $this->assertEquals('ifIndex', get_port_assoc_mode_name(1));
232
        $this->assertEquals(1, get_port_assoc_mode_id('ifIndex'));
233
        $this->assertFalse(get_port_assoc_mode_name(666));
234
        $this->assertFalse(get_port_assoc_mode_id('lucifer'));
235
    }
236
}
237