2010-10-18 06:26:02 +02:00
|
|
|
<?php
|
2015-06-08 16:47:14 +02:00
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
2015-06-08 17:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2010-10-18 06:26:02 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2018-12-07 19:16:42 +01:00
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
|
|
use Respect\Validation\Test\RuleTestCase;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2020-10-04 11:07:18 +02:00
|
|
|
use function extension_loaded;
|
|
|
|
|
2019-02-17 19:41:55 +01:00
|
|
|
use const FILTER_FLAG_IPV6;
|
|
|
|
use const FILTER_FLAG_NO_PRIV_RANGE;
|
2017-11-04 11:21:40 +01:00
|
|
|
|
2015-08-16 20:00:05 +02:00
|
|
|
/**
|
2018-12-07 19:16:42 +01:00
|
|
|
* @group rule
|
|
|
|
*
|
2018-07-23 20:35:14 +02:00
|
|
|
* @covers \Respect\Validation\Rules\Ip
|
2018-11-25 17:56:04 +01:00
|
|
|
*
|
2023-02-13 05:59:06 +01:00
|
|
|
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
2018-12-07 19:16:42 +01:00
|
|
|
* @author Danilo Benevides <danilobenevides01@gmail.com>
|
2018-11-25 17:56:04 +01:00
|
|
|
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
|
|
|
* @author Luís Otávio Cobucci Oblonczyk <lcobucci@gmail.com>
|
2015-08-16 20:00:05 +02:00
|
|
|
*/
|
2018-12-07 19:16:42 +01:00
|
|
|
final class IpTest extends RuleTestCase
|
2010-10-18 06:26:02 +02:00
|
|
|
{
|
2023-04-02 16:00:37 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*
|
|
|
|
* @dataProvider providerForInvalidRanges
|
|
|
|
*
|
|
|
|
* @throws ComponentException
|
|
|
|
*/
|
|
|
|
public function invalidRangeShouldRaiseException(string $range): void
|
|
|
|
{
|
|
|
|
$this->expectException(ComponentException::class);
|
|
|
|
|
|
|
|
new Ip($range);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[][]
|
|
|
|
*/
|
|
|
|
public static function providerForInvalidRanges(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['192.168'],
|
|
|
|
['asd'],
|
|
|
|
['192.168.0.0-192.168.0.256'],
|
|
|
|
['192.168.0.0-192.168.0.1/4'],
|
|
|
|
['192.168.0/1'],
|
|
|
|
['192.168.2.0/256.256.256.256'],
|
|
|
|
['192.168.2.0/8.256.256.256'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2010-10-18 06:26:02 +02:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2013-01-09 15:48:11 +01:00
|
|
|
*/
|
2023-04-02 16:00:37 +02:00
|
|
|
public static function providerForValidInput(): array
|
2010-10-18 06:26:02 +02:00
|
|
|
{
|
2015-10-18 03:44:47 +02:00
|
|
|
return [
|
2018-12-07 19:16:42 +01:00
|
|
|
[new Ip('127.*'), '127.0.0.1'],
|
|
|
|
[new Ip('127.0.*'), '127.0.0.1'],
|
|
|
|
[new Ip('127.0.0.*'), '127.0.0.1'],
|
|
|
|
[new Ip('192.168.*.6'), '192.168.2.6'],
|
|
|
|
[new Ip('192.*.2.6'), '192.168.2.6'],
|
|
|
|
[new Ip('*.168.2.6'), '10.168.2.6'],
|
|
|
|
[new Ip('192.168.*.*'), '192.168.2.6'],
|
|
|
|
[new Ip('192.*.*.*'), '192.168.2.6'],
|
|
|
|
[new Ip('*'), '192.168.255.156'],
|
|
|
|
[new Ip('*.*.*.*'), '192.168.255.156'],
|
|
|
|
[new Ip('127.0.0.0-127.0.0.255'), '127.0.0.1'],
|
|
|
|
[new Ip('192.168.0.0-192.168.255.255'), '192.168.2.6'],
|
|
|
|
[new Ip('192.0.0.0-192.255.255.255'), '192.168.2.6'],
|
|
|
|
[new Ip('0.0.0.0-255.255.255.255'), '192.168.2.6'],
|
|
|
|
[new Ip('220.78.168/21'), '220.78.173.2'],
|
|
|
|
[new Ip('220.78.168.0/21'), '220.78.173.2'],
|
|
|
|
[new Ip('220.78.168.0/255.255.248.0'), '220.78.173.2'],
|
2023-02-18 22:36:32 +01:00
|
|
|
[new Ip('127.0.0.1-127.0.0.5'), '127.0.0.2'],
|
2018-12-07 19:38:27 +01:00
|
|
|
[new Ip('*', FILTER_FLAG_IPV6), '2001:0db8:85a3:08d3:1319:8a2e:0370:7334'],
|
2015-10-18 03:44:47 +02:00
|
|
|
];
|
2013-01-09 15:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2013-01-09 15:48:11 +01:00
|
|
|
*/
|
2023-04-02 16:00:37 +02:00
|
|
|
public static function providerForInvalidInput(): array
|
2013-01-09 15:48:11 +01:00
|
|
|
{
|
2018-12-07 19:16:42 +01:00
|
|
|
return [
|
2019-02-07 19:45:57 +01:00
|
|
|
[new Ip('127.*'), '192.0.1.0'],
|
|
|
|
[new Ip(), ''],
|
|
|
|
[new Ip(), null],
|
|
|
|
[new Ip(), 'j'],
|
|
|
|
[new Ip(), ' '],
|
|
|
|
[new Ip(), 'Foo'],
|
|
|
|
[new Ip('*', FILTER_FLAG_NO_PRIV_RANGE), '192.168.0.1'],
|
|
|
|
[new Ip('127.0.1.*'), '127.0.0.1'],
|
|
|
|
[new Ip('192.163.*.*'), '192.168.2.6'],
|
|
|
|
[new Ip('193.*.*.*'), '192.10.2.6'],
|
|
|
|
[new Ip('127.0.1.0-127.0.1.255'), '127.0.0.1'],
|
|
|
|
[new Ip('192.163.0.0-192.163.255.255'), '192.168.2.6'],
|
|
|
|
[new Ip('193.168.0.0-193.255.255.255'), '192.10.2.6'],
|
|
|
|
[new Ip('220.78.168/21'), '220.78.176.1'],
|
|
|
|
[new Ip('220.78.168.0/21'), '220.78.176.2'],
|
2023-02-18 22:36:32 +01:00
|
|
|
[new Ip('127.0.0.1-127.0.0.5'), '127.0.0.10'],
|
2019-02-07 19:45:57 +01:00
|
|
|
[new Ip('220.78.168.0/255.255.248.0'), '220.78.176.3'],
|
2019-02-03 18:58:30 +01:00
|
|
|
];
|
2013-01-09 15:48:11 +01:00
|
|
|
}
|
|
|
|
|
2020-10-04 11:07:18 +02:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
if (extension_loaded('bcmath')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->markTestSkipped('You need bcmath to execute this test');
|
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|