Proyectos de Subversion Moodle

Rev

Rev 11 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 11 Rev 1441
Línea 22... Línea 22...
22
 * @package    core
22
 * @package    core
23
 * @covers     \core\ip_utils
23
 * @covers     \core\ip_utils
24
 * @copyright  2016 Jake Dallimore <jrhdallimore@gmail.com>
24
 * @copyright  2016 Jake Dallimore <jrhdallimore@gmail.com>
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
26
 */
27
class ip_utils_test extends \basic_testcase {
27
final class ip_utils_test extends \basic_testcase {
28
    /**
28
    /**
29
     * Test for \core\ip_utils::is_domain_name().
29
     * Test for \core\ip_utils::is_domain_name().
30
     *
30
     *
31
     * @param string $domainname the domain name to validate.
31
     * @param string $domainname the domain name to validate.
32
     * @param bool $expected the expected result.
32
     * @param bool $expected the expected result.
Línea 39... Línea 39...
39
    /**
39
    /**
40
     * Data provider for test_is_domain_name().
40
     * Data provider for test_is_domain_name().
41
     *
41
     *
42
     * @return array
42
     * @return array
43
     */
43
     */
44
    public function domain_name_data_provider() {
44
    public static function domain_name_data_provider(): array {
45
        return [
45
        return [
46
            ["com", true],
46
            ["com", true],
47
            ["i.net", true], // Single char, alpha tertiary domain.
47
            ["i.net", true], // Single char, alpha tertiary domain.
48
            ["0.org", true], // Single char, non-alpha tertiary domain.
48
            ["0.org", true], // Single char, non-alpha tertiary domain.
49
            ["0.a", true], // Single char, alpha top level domain.
49
            ["0.a", true], // Single char, alpha top level domain.
Línea 89... Línea 89...
89
    /**
89
    /**
90
     * Data provider for test_is_domain_matching_pattern().
90
     * Data provider for test_is_domain_matching_pattern().
91
     *
91
     *
92
     * @return array
92
     * @return array
93
     */
93
     */
94
    public function domain_matching_patterns_data_provider() {
94
    public static function domain_matching_patterns_data_provider(): array {
95
        return [
95
        return [
96
            ["*.com", true],
96
            ["*.com", true],
97
            ["*.example.com", true],
97
            ["*.example.com", true],
98
            ["*.example.com", true],
98
            ["*.example.com", true],
99
            ["*.sub.example.com", true],
99
            ["*.sub.example.com", true],
Línea 134... Línea 134...
134
    /**
134
    /**
135
     * Data provider for test_is_ip_address().
135
     * Data provider for test_is_ip_address().
136
     *
136
     *
137
     * @return array
137
     * @return array
138
     */
138
     */
139
    public function ip_address_data_provider() {
139
    public static function ip_address_data_provider(): array {
140
        return [
140
        return [
141
            ["127.0.0.1", true],
141
            ["127.0.0.1", true],
142
            ["10.1", false],
142
            ["10.1", false],
143
            ["0.0.0.0", true],
143
            ["0.0.0.0", true],
144
            ["255.255.255.255", true],
144
            ["255.255.255.255", true],
Línea 181... Línea 181...
181
    /**
181
    /**
182
     * Data provider for test_is_ipv4_address().
182
     * Data provider for test_is_ipv4_address().
183
     *
183
     *
184
     * @return array
184
     * @return array
185
     */
185
     */
186
    public function ipv4_address_data_provider() {
186
    public static function ipv4_address_data_provider(): array {
187
        return [
187
        return [
188
            ["127.0.0.1", true],
188
            ["127.0.0.1", true],
189
            ["0.0.0.0", true],
189
            ["0.0.0.0", true],
190
            ["255.255.255.255", true],
190
            ["255.255.255.255", true],
191
            [" 127.0.0.1", false],
191
            [" 127.0.0.1", false],
Línea 217... Línea 217...
217
    /**
217
    /**
218
     * Data provider for test_is_ipv4_range().
218
     * Data provider for test_is_ipv4_range().
219
     *
219
     *
220
     * @return array
220
     * @return array
221
     */
221
     */
222
    public function ipv4_range_data_provider() {
222
    public static function ipv4_range_data_provider(): array {
223
        return [
223
        return [
224
            ["127.0.0.1/24", true],
224
            ["127.0.0.1/24", true],
225
            ["127.0.0.20-20", true],
225
            ["127.0.0.20-20", true],
226
            ["127.0.0.20-50", true],
226
            ["127.0.0.20-50", true],
227
            ["127.0.0.0-255", true],
227
            ["127.0.0.0-255", true],
Línea 258... Línea 258...
258
    /**
258
    /**
259
     * Data provider for test_is_ipv6_address().
259
     * Data provider for test_is_ipv6_address().
260
     *
260
     *
261
     * @return array
261
     * @return array
262
     */
262
     */
263
    public function ipv6_address_data_provider() {
263
    public static function ipv6_address_data_provider(): array {
264
        return [
264
        return [
265
            ["::", true],
265
            ["::", true],
266
            ["::0", true],
266
            ["::0", true],
267
            ["0::", true],
267
            ["0::", true],
268
            ["0::0", true],
268
            ["0::0", true],
Línea 298... Línea 298...
298
    /**
298
    /**
299
     * Data provider for test_is_ipv6_range().
299
     * Data provider for test_is_ipv6_range().
300
     *
300
     *
301
     * @return array
301
     * @return array
302
     */
302
     */
303
    public function ipv6_range_data_provider() {
303
    public static function ipv6_range_data_provider(): array {
304
        return [
304
        return [
305
            ["::/128", true],
305
            ["::/128", true],
306
            ["::1/128", true],
306
            ["::1/128", true],
307
            ["fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80/128", true],
307
            ["fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80/128", true],
308
            ["fe80::dddd/128", true],
308
            ["fe80::dddd/128", true],
Línea 350... Línea 350...
350
    /**
350
    /**
351
     * Data provider for test_check_domain_against_allowed_domains.
351
     * Data provider for test_check_domain_against_allowed_domains.
352
     *
352
     *
353
     * @return array
353
     * @return array
354
     */
354
     */
355
    public function data_domain_addresses() {
355
    public static function data_domain_addresses(): array {
356
        return [
356
        return [
357
            [true, 'example.com'],
357
            [true, 'example.com'],
358
            [true, 'ExAmPle.com'],
358
            [true, 'ExAmPle.com'],
359
            [false, 'sub.example.com'],
359
            [false, 'sub.example.com'],
360
            [false, 'example.com.au'],
360
            [false, 'example.com.au'],
Línea 376... Línea 376...
376
    /**
376
    /**
377
     * Data provider for test_is_ip_in_subnet_list.
377
     * Data provider for test_is_ip_in_subnet_list.
378
     *
378
     *
379
     * @return array
379
     * @return array
380
     */
380
     */
381
    public function data_is_ip_in_subnet_list() {
381
    public static function data_is_ip_in_subnet_list(): array {
382
        return [
382
        return [
383
            [true, '1.1.1.1', '1.1.1.1', "\n"],
383
            [true, '1.1.1.1', '1.1.1.1', "\n"],
384
            [false, '1.1.1.1', '2.2.2.2', "\n"],
384
            [false, '1.1.1.1', '2.2.2.2', "\n"],
385
            [true, '1.1.1.1', "1.1.1.5\n1.1.1.1", "\n"],
385
            [true, '1.1.1.1', "1.1.1.5\n1.1.1.1", "\n"],
386
            [true, '1.1.1.1', "1.1.1.5,1.1.1.1", ","],
386
            [true, '1.1.1.1', "1.1.1.5,1.1.1.1", ","],
Línea 398... Línea 398...
398
     */
398
     */
399
    public function test_is_ip_in_subnet_list($expected, $ip, $list, $delim): void {
399
    public function test_is_ip_in_subnet_list($expected, $ip, $list, $delim): void {
400
        $this->assertEquals($expected, \core\ip_utils::is_ip_in_subnet_list($ip, $list, $delim));
400
        $this->assertEquals($expected, \core\ip_utils::is_ip_in_subnet_list($ip, $list, $delim));
401
    }
401
    }
Línea -... Línea 402...
-
 
402
 
-
 
403
    /**
-
 
404
     * Data provider for test_normalize_internet_address.
-
 
405
     *
-
 
406
     * @return array
-
 
407
     */
-
 
408
    public static function normalize_internet_address_provider(): array {
-
 
409
        return [
-
 
410
            'Strip all white spaces on IP address' => [
-
 
411
                '   192.168.5.5  ',
-
 
412
                '192.168.5.5',
-
 
413
            ],
-
 
414
            'Strip all white spaces on domain name' => [
-
 
415
                ' www.moodle.org   ',
-
 
416
                'www.moodle.org',
-
 
417
            ],
-
 
418
            'Preserve IPv4 address' => [
-
 
419
                '127.0.0.1',
-
 
420
                '127.0.0.1',
-
 
421
            ],
-
 
422
            'Preserve IPv4 address range' => [
-
 
423
                '192.168.0.0/16',
-
 
424
                '192.168.0.0/16',
-
 
425
            ],
-
 
426
            'Preserve IPv6 address' => [
-
 
427
                'fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80',
-
 
428
                'fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80',
-
 
429
            ],
-
 
430
            'Preserve IPv6 address range' => [
-
 
431
                'fe80::ffff',
-
 
432
                'fe80::ffff',
-
 
433
            ],
-
 
434
            'Preserve valid domain' => [
-
 
435
                'localhost',
-
 
436
                'localhost',
-
 
437
            ],
-
 
438
            'Preserve valid FQDN' => [
-
 
439
                'www.moodle.org',
-
 
440
                'www.moodle.org',
-
 
441
            ],
-
 
442
            'Preserve valid FQDN with trailing dot' => [
-
 
443
                'www.moodle.com.',
-
 
444
                'www.moodle.com',
-
 
445
            ],
-
 
446
            'Preserve valid domain with wildcard' => [
-
 
447
                '*.moodledev.io',
-
 
448
                '*.moodledev.io',
-
 
449
            ],
-
 
450
            'Convert previous allowed "127." format to CIDR format (127.0.0.0/8)' => [
-
 
451
                '127.',
-
 
452
                '127.0.0.0/8',
-
 
453
            ],
-
 
454
            'Convert previous allowed "169.8." format to CIDR format (169.8.0.0/16)' => [
-
 
455
                '169.8.',
-
 
456
                '169.8.0.0/16',
-
 
457
            ],
-
 
458
            'Convert previous allowed "192.168.10." format to CIDR format (192.168.10.0/24)' => [
-
 
459
                '192.168.10.',
-
 
460
                '192.168.10.0/24',
-
 
461
            ],
-
 
462
            'Convert previous allowed ".moodle.org" subdomain format to new format (*.moodle.org)' => [
-
 
463
                '.moodle.org',
-
 
464
                '*.moodle.org',
-
 
465
            ],
-
 
466
            'Ignore invalid IPv4' => [
-
 
467
                '327.0.0.1',
-
 
468
                '',
-
 
469
            ],
-
 
470
            'Ignore invalid IPv4 range' => [
-
 
471
                '192.168',
-
 
472
                '',
-
 
473
            ],
-
 
474
            'Ignore invalid IPv6' => [
-
 
475
                'fe80::ddddd',
-
 
476
                '',
-
 
477
            ],
-
 
478
            'Ignore invalid IPv6 range' => [
-
 
479
                'fe80:',
-
 
480
                '',
-
 
481
            ],
-
 
482
            'Ignore invalid domain' => [
-
 
483
                '-example.com',
-
 
484
                '',
-
 
485
            ],
-
 
486
        ];
-
 
487
    }
-
 
488
 
-
 
489
    /**
-
 
490
     * Test if input address value is correctly normalized.
-
 
491
     *
-
 
492
     * @covers ::normalize_internet_address
-
 
493
     *
-
 
494
     * @dataProvider normalize_internet_address_provider
-
 
495
     *
-
 
496
     * @param string $input    Raw input value.
-
 
497
     * @param string $expected Expected value after normalization.
-
 
498
     */
-
 
499
    public function test_normalize_internet_address(string $input, string $expected): void {
-
 
500
        $this->assertEquals($expected, \core\ip_utils::normalize_internet_address($input));
-
 
501
    }
-
 
502
 
-
 
503
    /**
-
 
504
     * Data provider for test_normalize_internet_address_list.
-
 
505
     *
-
 
506
     * @return array
-
 
507
     */
-
 
508
    public static function normalize_internet_address_list_provider(): array {
-
 
509
        return [
-
 
510
            'Strip all white spaces' => [
-
 
511
                '   192.168.5.5, 127.0.0.1,    www.moodle.org   ',
-
 
512
                '192.168.5.5,127.0.0.1,www.moodle.org',
-
 
513
            ],
-
 
514
            'Trim input' => [
-
 
515
                '    192.168.5.5,127.0.0.1,www.moodle.org   ',
-
 
516
                '192.168.5.5,127.0.0.1,www.moodle.org',
-
 
517
            ],
-
 
518
            'Preserve valid full and partial IP' => [
-
 
519
                '127.0.0.1,192.168.0.0/16,fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80,fe80::ffff',
-
 
520
                '127.0.0.1,192.168.0.0/16,fe80:fe80:fe80:fe80:fe80:fe80:fe80:fe80,fe80::ffff',
-
 
521
            ],
-
 
522
            'Convert previous allowed format to new allowed format' => [
-
 
523
                '127.,169.8.,192.168.10.,.moodle.org',
-
 
524
                '127.0.0.0/8,169.8.0.0/16,192.168.10.0/24,*.moodle.org',
-
 
525
            ],
-
 
526
            'Preserve valid domain and pattern domain' => [
-
 
527
                'localhost,www.moodle.org,.moodle.com,*.moodledev.io',
-
 
528
                'localhost,www.moodle.org,*.moodle.com,*.moodledev.io',
-
 
529
            ],
-
 
530
            'Remove all invalid IP and domains' => [
-
 
531
                '327.0.0.1,192.168,fe80::ddddd,fe80:,-example.com',
-
 
532
                '',
-
 
533
            ],
-
 
534
            'Remove duplicate values' => [
-
 
535
                '.moodle.org,*.moodle.org,*.moodle.org,.moodle.org',
-
 
536
                '*.moodle.org',
-
 
537
            ],
-
 
538
        ];
-
 
539
    }
-
 
540
 
-
 
541
    /**
-
 
542
     * Test if input address list is correctly normalized.
-
 
543
     *
-
 
544
     * @covers ::normalize_internet_address_list
-
 
545
     *
-
 
546
     * @dataProvider normalize_internet_address_list_provider
-
 
547
     *
-
 
548
     * @param string $input    Raw input value.
-
 
549
     * @param string $expected Expected value after normalization.
-
 
550
     */
-
 
551
    public function test_normalize_internet_address_list(string $input, string $expected): void {
-
 
552
        $this->assertEquals($expected, \core\ip_utils::normalize_internet_address_list($input));
402
 
553
    }