2010-10-18 06:26:02 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Respect\Validation\Rules;
|
|
|
|
|
|
|
|
class IpTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider providerForIp
|
|
|
|
*
|
|
|
|
*/
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testValidIpsShouldReturnTrue($input, $options=null)
|
2010-10-18 06:26:02 +02:00
|
|
|
{
|
2013-01-09 15:48:11 +01:00
|
|
|
$ipValidator = new Ip($options);
|
|
|
|
$this->assertTrue($ipValidator->validate($input));
|
|
|
|
$this->assertTrue($ipValidator->assert($input));
|
|
|
|
$this->assertTrue($ipValidator->check($input));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerForIpBetweenRange
|
|
|
|
*/
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testIpsBetweenRangeShouldReturnTrue($input, $networkRange)
|
2013-01-09 15:48:11 +01:00
|
|
|
{
|
|
|
|
$ipValidator = new Ip($networkRange);
|
|
|
|
$this->assertTrue($ipValidator->validate($input));
|
|
|
|
$this->assertTrue($ipValidator->assert($input));
|
|
|
|
$this->assertTrue($ipValidator->check($input));
|
2010-10-18 06:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerForNotIp
|
2010-12-07 20:17:43 +01:00
|
|
|
* @expectedException Respect\Validation\Exceptions\IpException
|
2010-10-18 06:26:02 +02:00
|
|
|
*/
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testInvalidIpsShouldThrowIpException($input, $options=null)
|
2010-10-18 06:26:02 +02:00
|
|
|
{
|
2013-01-09 15:48:11 +01:00
|
|
|
$ipValidator = new Ip($options);
|
|
|
|
$this->assertFalse($ipValidator->validate($input));
|
|
|
|
$this->assertFalse($ipValidator->assert($input));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerForIpOutsideRange
|
|
|
|
* @expectedException Respect\Validation\Exceptions\IpException
|
|
|
|
*/
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testIpsOutsideRangeShouldReturnFalse($input, $networkRange)
|
2013-01-09 15:48:11 +01:00
|
|
|
{
|
|
|
|
$ipValidator = new Ip($networkRange);
|
|
|
|
$this->assertFalse($ipValidator->validate($input));
|
|
|
|
$this->assertFalse($ipValidator->assert($input));
|
2010-10-18 06:26:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function providerForIp()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('127.0.0.1'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-01-09 15:48:11 +01:00
|
|
|
public function providerForIpBetweenRange()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('127.0.0.1', '127.*'),
|
|
|
|
array('127.0.0.1', '127.0.*'),
|
|
|
|
array('127.0.0.1', '127.0.0.*'),
|
|
|
|
array('192.168.2.6', '192.168.*.6'),
|
|
|
|
array('192.168.2.6', '192.*.2.6'),
|
|
|
|
array('10.168.2.6', '*.168.2.6'),
|
|
|
|
array('192.168.2.6', '192.168.*.*'),
|
|
|
|
array('192.10.2.6', '192.*.*.*'),
|
|
|
|
array('192.168.255.156', '*'),
|
|
|
|
array('192.168.255.156', '*.*.*.*'),
|
|
|
|
array('127.0.0.1', '127.0.0.0-127.0.0.255'),
|
|
|
|
array('192.168.2.6', '192.168.0.0-192.168.255.255'),
|
|
|
|
array('192.10.2.6', '192.0.0.0-192.255.255.255'),
|
|
|
|
array('192.168.255.156', '0.0.0.0-255.255.255.255'),
|
|
|
|
array('220.78.173.2', '220.78.168/21'),
|
|
|
|
array('220.78.173.2', '220.78.168.0/21'),
|
2013-01-14 15:35:04 +01:00
|
|
|
array('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()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(null),
|
|
|
|
array('j'),
|
|
|
|
array(' '),
|
|
|
|
array('Foo'),
|
|
|
|
array(''),
|
|
|
|
array('192.168.0.1', FILTER_FLAG_NO_PRIV_RANGE),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-01-09 15:48:11 +01:00
|
|
|
public function providerForIpOutsideRange()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('127.0.0.1', '127.0.1.*'),
|
|
|
|
array('192.168.2.6', '192.163.*.*'),
|
|
|
|
array('192.10.2.6', '193.*.*.*'),
|
|
|
|
array('127.0.0.1', '127.0.1.0-127.0.1.255'),
|
|
|
|
array('192.168.2.6', '192.163.0.0-192.163.255.255'),
|
|
|
|
array('192.10.2.6', '193.168.0.0-193.255.255.255'),
|
|
|
|
array('220.78.176.1', '220.78.168/21'),
|
|
|
|
array('220.78.176.2', '220.78.168.0/21'),
|
2013-01-14 15:35:04 +01:00
|
|
|
array('220.78.176.3', '220.78.168.0/255.255.248.0'),
|
2013-01-09 15:48:11 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerForInvalidRanges
|
|
|
|
* @expectedException Respect\Validation\Exceptions\ComponentException
|
|
|
|
*/
|
2013-01-14 23:44:49 +01:00
|
|
|
public function testInvalidRangeShouldRaiseException($range)
|
2013-01-09 15:48:11 +01:00
|
|
|
{
|
|
|
|
$o = new Ip($range);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providerForInvalidRanges()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('192.168'),
|
|
|
|
array('asd'),
|
|
|
|
array('192.168.0.0-192.168.0.256'),
|
|
|
|
array('192.168.0.0-192.168.0.1/4'),
|
|
|
|
array('192.168.0/1'),
|
|
|
|
array('192.168.2.0/256.256.256.256'),
|
|
|
|
array('192.168.2.0/8.256.256.256'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|