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
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the "LICENSE.md"
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2010-10-18 06:26:02 +02:00
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
2017-11-04 11:21:40 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
2015-08-16 20:00:05 +02:00
|
|
|
/**
|
|
|
|
* @group rule
|
2017-02-04 14:01:14 +01:00
|
|
|
* @covers \Respect\Validation\Exceptions\IpException
|
2018-07-23 20:35:14 +02:00
|
|
|
* @covers \Respect\Validation\Rules\Ip
|
2018-11-25 17:56:04 +01:00
|
|
|
*
|
|
|
|
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
* @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
|
|
|
*/
|
2017-11-04 11:21:40 +01:00
|
|
|
class IpTest extends TestCase
|
2010-10-18 06:26:02 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider providerForIp
|
2018-07-23 20:35:14 +02:00
|
|
|
*
|
|
|
|
* @test
|
2010-10-18 06:26:02 +02:00
|
|
|
*/
|
2018-07-23 20:35:14 +02:00
|
|
|
public function validIpsShouldReturnTrue($input, $options = null): void
|
2010-10-18 06:26:02 +02:00
|
|
|
{
|
2013-01-09 15:48:11 +01:00
|
|
|
$ipValidator = new Ip($options);
|
2017-11-12 14:33:39 +01:00
|
|
|
self::assertTrue($ipValidator->__invoke($input));
|
2018-01-28 17:16:53 +01:00
|
|
|
$ipValidator->assert($input);
|
2018-01-28 16:14:00 +01:00
|
|
|
$ipValidator->check($input);
|
2013-01-09 15:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerForIpBetweenRange
|
2018-07-23 20:35:14 +02:00
|
|
|
*
|
|
|
|
* @test
|
2013-01-09 15:48:11 +01:00
|
|
|
*/
|
2018-07-23 20:35:14 +02:00
|
|
|
public function ipsBetweenRangeShouldReturnTrue($input, $networkRange): void
|
2013-01-09 15:48:11 +01:00
|
|
|
{
|
|
|
|
$ipValidator = new Ip($networkRange);
|
2017-11-12 14:33:39 +01:00
|
|
|
self::assertTrue($ipValidator->__invoke($input));
|
2018-01-28 17:16:53 +01:00
|
|
|
$ipValidator->assert($input);
|
2018-01-28 16:14:00 +01:00
|
|
|
$ipValidator->check($input);
|
2010-10-18 06:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerForNotIp
|
2017-02-04 14:01:14 +01:00
|
|
|
* @expectedException \Respect\Validation\Exceptions\IpException
|
2018-07-23 20:35:14 +02:00
|
|
|
*
|
|
|
|
* @test
|
2010-10-18 06:26:02 +02:00
|
|
|
*/
|
2018-07-23 20:35:14 +02:00
|
|
|
public function invalidIpsShouldThrowIpException($input, $options = null): void
|
2010-10-18 06:26:02 +02:00
|
|
|
{
|
2013-01-09 15:48:11 +01:00
|
|
|
$ipValidator = new Ip($options);
|
2017-11-12 14:33:39 +01:00
|
|
|
self::assertFalse($ipValidator->__invoke($input));
|
2018-01-28 17:16:53 +01:00
|
|
|
$ipValidator->assert($input);
|
2013-01-09 15:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerForIpOutsideRange
|
2017-02-04 14:01:14 +01:00
|
|
|
* @expectedException \Respect\Validation\Exceptions\IpException
|
2018-07-23 20:35:14 +02:00
|
|
|
*
|
|
|
|
* @test
|
2013-01-09 15:48:11 +01:00
|
|
|
*/
|
2018-07-23 20:35:14 +02:00
|
|
|
public function ipsOutsideRangeShouldReturnFalse($input, $networkRange): void
|
2013-01-09 15:48:11 +01:00
|
|
|
{
|
|
|
|
$ipValidator = new Ip($networkRange);
|
2017-11-12 14:33:39 +01:00
|
|
|
self::assertFalse($ipValidator->__invoke($input));
|
2018-01-28 17:16:53 +01:00
|
|
|
$ipValidator->assert($input);
|
2010-10-18 06:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function providerForIp()
|
|
|
|
{
|
2015-10-18 03:44:47 +02:00
|
|
|
return [
|
|
|
|
['127.0.0.1'],
|
|
|
|
];
|
2010-10-18 06:26:02 +02:00
|
|
|
}
|
|
|
|
|
2013-01-09 15:48:11 +01:00
|
|
|
public function providerForIpBetweenRange()
|
|
|
|
{
|
2015-10-18 03:44:47 +02:00
|
|
|
return [
|
|
|
|
['127.0.0.1', '127.*'],
|
|
|
|
['127.0.0.1', '127.0.*'],
|
|
|
|
['127.0.0.1', '127.0.0.*'],
|
|
|
|
['192.168.2.6', '192.168.*.6'],
|
|
|
|
['192.168.2.6', '192.*.2.6'],
|
|
|
|
['10.168.2.6', '*.168.2.6'],
|
|
|
|
['192.168.2.6', '192.168.*.*'],
|
|
|
|
['192.10.2.6', '192.*.*.*'],
|
|
|
|
['192.168.255.156', '*'],
|
|
|
|
['192.168.255.156', '*.*.*.*'],
|
|
|
|
['127.0.0.1', '127.0.0.0-127.0.0.255'],
|
|
|
|
['192.168.2.6', '192.168.0.0-192.168.255.255'],
|
|
|
|
['192.10.2.6', '192.0.0.0-192.255.255.255'],
|
|
|
|
['192.168.255.156', '0.0.0.0-255.255.255.255'],
|
|
|
|
['220.78.173.2', '220.78.168/21'],
|
|
|
|
['220.78.173.2', '220.78.168.0/21'],
|
|
|
|
['220.78.173.2', '220.78.168.0/255.255.248.0'],
|
|
|
|
];
|
2013-01-09 15:48:11 +01:00
|
|
|
}
|
|
|
|
|
2010-10-18 06:26:02 +02:00
|
|
|
public function providerForNotIp()
|
|
|
|
{
|
2015-10-18 03:44:47 +02:00
|
|
|
return [
|
|
|
|
[''],
|
|
|
|
[null],
|
|
|
|
['j'],
|
|
|
|
[' '],
|
|
|
|
['Foo'],
|
|
|
|
['192.168.0.1', FILTER_FLAG_NO_PRIV_RANGE],
|
|
|
|
];
|
2010-10-18 06:26:02 +02:00
|
|
|
}
|
|
|
|
|
2013-01-09 15:48:11 +01:00
|
|
|
public function providerForIpOutsideRange()
|
|
|
|
{
|
2015-10-18 03:44:47 +02:00
|
|
|
return [
|
|
|
|
['127.0.0.1', '127.0.1.*'],
|
|
|
|
['192.168.2.6', '192.163.*.*'],
|
|
|
|
['192.10.2.6', '193.*.*.*'],
|
|
|
|
['127.0.0.1', '127.0.1.0-127.0.1.255'],
|
|
|
|
['192.168.2.6', '192.163.0.0-192.163.255.255'],
|
|
|
|
['192.10.2.6', '193.168.0.0-193.255.255.255'],
|
|
|
|
['220.78.176.1', '220.78.168/21'],
|
|
|
|
['220.78.176.2', '220.78.168.0/21'],
|
|
|
|
['220.78.176.3', '220.78.168.0/255.255.248.0'],
|
|
|
|
];
|
2013-01-09 15:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerForInvalidRanges
|
2017-02-04 14:01:14 +01:00
|
|
|
* @expectedException \Respect\Validation\Exceptions\ComponentException
|
2018-07-23 20:35:14 +02:00
|
|
|
*
|
|
|
|
* @test
|
2013-01-09 15:48:11 +01:00
|
|
|
*/
|
2018-07-23 20:35:14 +02:00
|
|
|
public function invalidRangeShouldRaiseException($range): void
|
2013-01-09 15:48:11 +01:00
|
|
|
{
|
|
|
|
$o = new Ip($range);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providerForInvalidRanges()
|
|
|
|
{
|
2015-10-18 03:44:47 +02:00
|
|
|
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'],
|
|
|
|
];
|
2013-01-09 15:48:11 +01:00
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|