2010-12-14 16:26:35 +00:00
|
|
|
<?php
|
2011-10-11 18:30:50 +00:00
|
|
|
/**
|
2013-10-21 21:09:13 +00:00
|
|
|
* Tests for IP validity functions.
|
|
|
|
|
*
|
|
|
|
|
* Ported from /t/inc/IP.t by avar.
|
|
|
|
|
*
|
2012-05-11 17:53:14 +00:00
|
|
|
* @group IP
|
2013-10-21 21:09:13 +00:00
|
|
|
* @todo Test methods in this call should be split into a method and a
|
|
|
|
|
* dataprovider.
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
2018-02-17 12:29:13 +00:00
|
|
|
class IPTest extends PHPUnit\Framework\TestCase {
|
2017-12-29 23:22:37 +00:00
|
|
|
|
|
|
|
|
use MediaWikiCoversValidator;
|
|
|
|
|
|
2010-12-28 18:23:43 +00:00
|
|
|
/**
|
2011-03-06 17:36:42 +00:00
|
|
|
* @covers IP::isIPAddress
|
2015-04-10 16:03:58 +00:00
|
|
|
* @dataProvider provideInvalidIPs
|
2010-12-28 18:23:43 +00:00
|
|
|
*/
|
2018-03-16 16:54:14 +00:00
|
|
|
public function testIsNotIPAddress( $val, $desc ) {
|
2015-04-10 16:03:58 +00:00
|
|
|
$this->assertFalse( IP::isIPAddress( $val ), $desc );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provide a list of things that aren't IP addresses
|
|
|
|
|
*/
|
|
|
|
|
public function provideInvalidIPs() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ false, 'Boolean false is not an IP' ],
|
|
|
|
|
[ true, 'Boolean true is not an IP' ],
|
|
|
|
|
[ '', 'Empty string is not an IP' ],
|
|
|
|
|
[ 'abc', 'Garbage IP string' ],
|
|
|
|
|
[ ':', 'Single ":" is not an IP' ],
|
|
|
|
|
[ '2001:0DB8::A:1::1', 'IPv6 with a double :: occurrence' ],
|
|
|
|
|
[ '2001:0DB8::A:1::', 'IPv6 with a double :: occurrence, last at end' ],
|
|
|
|
|
[ '::2001:0DB8::5:1', 'IPv6 with a double :: occurrence, firt at beginning' ],
|
|
|
|
|
[ '124.24.52', 'IPv4 not enough quads' ],
|
|
|
|
|
[ '24.324.52.13', 'IPv4 out of range' ],
|
|
|
|
|
[ '.24.52.13', 'IPv4 starts with period' ],
|
|
|
|
|
[ 'fc:100:300', 'IPv6 with only 3 words' ],
|
|
|
|
|
];
|
2015-04-10 16:03:58 +00:00
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2015-04-10 16:03:58 +00:00
|
|
|
/**
|
|
|
|
|
* @covers IP::isIPAddress
|
|
|
|
|
*/
|
|
|
|
|
public function testisIPAddress() {
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertTrue( IP::isIPAddress( '::' ), 'RFC 4291 IPv6 Unspecified Address' );
|
|
|
|
|
$this->assertTrue( IP::isIPAddress( '::1' ), 'RFC 4291 IPv6 Loopback Address' );
|
2016-09-22 03:18:08 +00:00
|
|
|
$this->assertTrue( IP::isIPAddress( '74.24.52.13/20' ), 'IPv4 range' );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertTrue( IP::isIPAddress( 'fc:100:a:d:1:e:ac:0/24' ), 'IPv6 range' );
|
|
|
|
|
$this->assertTrue( IP::isIPAddress( 'fc::100:a:d:1:e:ac/96' ), 'IPv6 range with "::"' );
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$validIPs = [ 'fc:100::', 'fc:100:a:d:1:e:ac::', 'fc::100', '::fc:100:a:d:1:e:ac',
|
|
|
|
|
'::fc', 'fc::100:a:d:1:e:ac', 'fc:100:a:d:1:e:ac:0', '124.24.52.13', '1.24.52.13' ];
|
2010-12-14 16:26:35 +00:00
|
|
|
foreach ( $validIPs as $ip ) {
|
|
|
|
|
$this->assertTrue( IP::isIPAddress( $ip ), "$ip is a valid IP address" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-06 17:36:42 +00:00
|
|
|
/**
|
|
|
|
|
* @covers IP::isIPv6
|
|
|
|
|
*/
|
2010-12-14 16:26:35 +00:00
|
|
|
public function testisIPv6() {
|
|
|
|
|
$this->assertFalse( IP::isIPv6( ':fc:100::' ), 'IPv6 starting with lone ":"' );
|
|
|
|
|
$this->assertFalse( IP::isIPv6( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
|
|
|
|
|
$this->assertFalse( IP::isIPv6( 'fc:300' ), 'IPv6 with only 2 words' );
|
|
|
|
|
$this->assertFalse( IP::isIPv6( 'fc:100:300' ), 'IPv6 with only 3 words' );
|
2011-10-26 03:45:13 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertTrue( IP::isIPv6( 'fc:100::' ) );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( 'fc:100:a::' ) );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( 'fc:100:a:d::' ) );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( 'fc:100:a:d:1::' ) );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e::' ) );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac::' ) );
|
2011-10-26 03:45:13 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
|
2013-11-07 17:10:50 +00:00
|
|
|
$this->assertFalse(
|
|
|
|
|
IP::isIPv6( 'fc:100:a:d:1:e:ac:0:1::' ),
|
|
|
|
|
'IPv6 with 9 words ending with "::"'
|
|
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
$this->assertFalse( IP::isIPv6( ':::' ) );
|
|
|
|
|
$this->assertFalse( IP::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
|
2011-10-26 03:45:13 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertTrue( IP::isIPv6( '::' ), 'IPv6 zero address' );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( '::0' ) );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( '::fc' ) );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( '::fc:100' ) );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( '::fc:100:a' ) );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( '::fc:100:a:d' ) );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( '::fc:100:a:d:1' ) );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e' ) );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e:ac' ) );
|
2011-10-26 03:45:13 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
|
|
|
|
|
$this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( IP::isIPv6( ':fc::100' ), 'IPv6 starting with lone ":"' );
|
|
|
|
|
$this->assertFalse( IP::isIPv6( 'fc::100:' ), 'IPv6 ending with lone ":"' );
|
|
|
|
|
$this->assertFalse( IP::isIPv6( 'fc:::100' ), 'IPv6 with ":::" in the middle' );
|
2011-10-26 03:45:13 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertTrue( IP::isIPv6( 'fc::100' ), 'IPv6 with "::" and 2 words' );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
|
2016-09-22 03:18:08 +00:00
|
|
|
$this->assertTrue( IP::isIPv6( 'fc::100:a:d' ), 'IPv6 with "::" and 4 words' );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertTrue( IP::isIPv6( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e' ), 'IPv6 with "::" and 6 words' );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertTrue( IP::isIPv6( '2001::df' ), 'IPv6 with "::" and 2 words' );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df' ), 'IPv6 with "::" and 5 words' );
|
|
|
|
|
$this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df:2' ), 'IPv6 with "::" and 6 words' );
|
2011-10-26 03:45:13 +00:00
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
|
|
|
|
|
$this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-06 17:36:42 +00:00
|
|
|
/**
|
|
|
|
|
* @covers IP::isIPv4
|
2015-04-10 16:03:58 +00:00
|
|
|
* @dataProvider provideInvalidIPv4Addresses
|
2011-03-06 17:36:42 +00:00
|
|
|
*/
|
2015-04-10 16:03:58 +00:00
|
|
|
public function testisNotIPv4( $bogusIP, $desc ) {
|
|
|
|
|
$this->assertFalse( IP::isIPv4( $bogusIP ), $desc );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideInvalidIPv4Addresses() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ false, 'Boolean false is not an IP' ],
|
|
|
|
|
[ true, 'Boolean true is not an IP' ],
|
|
|
|
|
[ '', 'Empty string is not an IP' ],
|
|
|
|
|
[ 'abc', 'Letters are not an IP' ],
|
|
|
|
|
[ ':', 'A colon is not an IP' ],
|
|
|
|
|
[ '124.24.52', 'IPv4 not enough quads' ],
|
|
|
|
|
[ '24.324.52.13', 'IPv4 out of range' ],
|
|
|
|
|
[ '.24.52.13', 'IPv4 starts with period' ],
|
|
|
|
|
];
|
2015-04-10 16:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers IP::isIPv4
|
|
|
|
|
* @dataProvider provideValidIPv4Address
|
|
|
|
|
*/
|
|
|
|
|
public function testIsIPv4( $ip, $desc ) {
|
|
|
|
|
$this->assertTrue( IP::isIPv4( $ip ), $desc );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provide some IPv4 addresses and ranges
|
|
|
|
|
*/
|
|
|
|
|
public function provideValidIPv4Address() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ '124.24.52.13', 'Valid IPv4 address' ],
|
|
|
|
|
[ '1.24.52.13', 'Another valid IPv4 address' ],
|
|
|
|
|
[ '74.24.52.13/20', 'An IPv4 range' ],
|
|
|
|
|
];
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-06 17:36:42 +00:00
|
|
|
/**
|
|
|
|
|
* @covers IP::isValid
|
|
|
|
|
*/
|
2010-12-14 16:26:35 +00:00
|
|
|
public function testValidIPs() {
|
|
|
|
|
foreach ( range( 0, 255 ) as $i ) {
|
|
|
|
|
$a = sprintf( "%03d", $i );
|
|
|
|
|
$b = sprintf( "%02d", $i );
|
|
|
|
|
$c = sprintf( "%01d", $i );
|
2016-02-17 09:09:32 +00:00
|
|
|
foreach ( array_unique( [ $a, $b, $c ] ) as $f ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$ip = "$f.$f.$f.$f";
|
2013-01-28 10:27:15 +00:00
|
|
|
$this->assertTrue( IP::isValid( $ip ), "$ip is a valid IPv4 address" );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-02-06 20:24:53 +00:00
|
|
|
foreach ( range( 0x0, 0xFFFF, 0xF ) as $i ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$a = sprintf( "%04x", $i );
|
|
|
|
|
$b = sprintf( "%03x", $i );
|
|
|
|
|
$c = sprintf( "%02x", $i );
|
2016-02-17 09:09:32 +00:00
|
|
|
foreach ( array_unique( [ $a, $b, $c ] ) as $f ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$ip = "$f:$f:$f:$f:$f:$f:$f:$f";
|
2013-01-28 10:27:15 +00:00
|
|
|
$this->assertTrue( IP::isValid( $ip ), "$ip is a valid IPv6 address" );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-02-13 23:24:48 +00:00
|
|
|
// test with some abbreviations
|
|
|
|
|
$this->assertFalse( IP::isValid( ':fc:100::' ), 'IPv6 starting with lone ":"' );
|
|
|
|
|
$this->assertFalse( IP::isValid( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
|
|
|
|
|
$this->assertFalse( IP::isValid( 'fc:300' ), 'IPv6 with only 2 words' );
|
|
|
|
|
$this->assertFalse( IP::isValid( 'fc:100:300' ), 'IPv6 with only 3 words' );
|
2011-10-26 03:45:13 +00:00
|
|
|
|
2011-02-13 23:24:48 +00:00
|
|
|
$this->assertTrue( IP::isValid( 'fc:100::' ) );
|
|
|
|
|
$this->assertTrue( IP::isValid( 'fc:100:a:d:1:e::' ) );
|
|
|
|
|
$this->assertTrue( IP::isValid( 'fc:100:a:d:1:e:ac::' ) );
|
2011-10-26 03:45:13 +00:00
|
|
|
|
2011-02-13 23:24:48 +00:00
|
|
|
$this->assertTrue( IP::isValid( 'fc::100' ), 'IPv6 with "::" and 2 words' );
|
|
|
|
|
$this->assertTrue( IP::isValid( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertTrue( IP::isValid( '2001::df' ), 'IPv6 with "::" and 2 words' );
|
|
|
|
|
$this->assertTrue( IP::isValid( '2001:5c0:1400:a::df' ), 'IPv6 with "::" and 5 words' );
|
|
|
|
|
$this->assertTrue( IP::isValid( '2001:5c0:1400:a::df:2' ), 'IPv6 with "::" and 6 words' );
|
2011-02-13 23:24:48 +00:00
|
|
|
$this->assertTrue( IP::isValid( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
|
|
|
|
|
$this->assertTrue( IP::isValid( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
|
2011-10-26 03:45:13 +00:00
|
|
|
|
2013-11-07 17:10:50 +00:00
|
|
|
$this->assertFalse(
|
|
|
|
|
IP::isValid( 'fc:100:a:d:1:e:ac:0::' ),
|
|
|
|
|
'IPv6 with 8 words ending with "::"'
|
|
|
|
|
);
|
|
|
|
|
$this->assertFalse(
|
|
|
|
|
IP::isValid( 'fc:100:a:d:1:e:ac:0:1::' ),
|
|
|
|
|
'IPv6 with 9 words ending with "::"'
|
|
|
|
|
);
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-06 17:36:42 +00:00
|
|
|
/**
|
|
|
|
|
* @covers IP::isValid
|
|
|
|
|
*/
|
2010-12-14 16:26:35 +00:00
|
|
|
public function testInvalidIPs() {
|
|
|
|
|
// Out of range...
|
|
|
|
|
foreach ( range( 256, 999 ) as $i ) {
|
|
|
|
|
$a = sprintf( "%03d", $i );
|
|
|
|
|
$b = sprintf( "%02d", $i );
|
|
|
|
|
$c = sprintf( "%01d", $i );
|
2016-02-17 09:09:32 +00:00
|
|
|
foreach ( array_unique( [ $a, $b, $c ] ) as $f ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$ip = "$f.$f.$f.$f";
|
|
|
|
|
$this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach ( range( 'g', 'z' ) as $i ) {
|
|
|
|
|
$a = sprintf( "%04s", $i );
|
|
|
|
|
$b = sprintf( "%03s", $i );
|
|
|
|
|
$c = sprintf( "%02s", $i );
|
2016-02-17 09:09:32 +00:00
|
|
|
foreach ( array_unique( [ $a, $b, $c ] ) as $f ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
$ip = "$f:$f:$f:$f:$f:$f:$f:$f";
|
2013-01-28 10:27:15 +00:00
|
|
|
$this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv6 address" );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Have CIDR
|
2016-02-17 09:09:32 +00:00
|
|
|
$ipCIDRs = [
|
2010-12-14 16:26:35 +00:00
|
|
|
'212.35.31.121/32',
|
|
|
|
|
'212.35.31.121/18',
|
|
|
|
|
'212.35.31.121/24',
|
|
|
|
|
'::ff:d:321:5/96',
|
|
|
|
|
'ff::d3:321:5/116',
|
|
|
|
|
'c:ff:12:1:ea:d:321:5/120',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2010-12-14 16:26:35 +00:00
|
|
|
foreach ( $ipCIDRs as $i ) {
|
|
|
|
|
$this->assertFalse( IP::isValid( $i ),
|
2017-08-23 00:07:35 +00:00
|
|
|
"$i is an invalid IP address because it is a range" );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
// Incomplete/garbage
|
2016-02-17 09:09:32 +00:00
|
|
|
$invalid = [
|
2010-12-14 16:26:35 +00:00
|
|
|
'www.xn--var-xla.net',
|
|
|
|
|
'216.17.184.G',
|
|
|
|
|
'216.17.184.1.',
|
|
|
|
|
'216.17.184',
|
|
|
|
|
'216.17.184.',
|
|
|
|
|
'256.17.184.1'
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2010-12-14 16:26:35 +00:00
|
|
|
foreach ( $invalid as $i ) {
|
|
|
|
|
$this->assertFalse( IP::isValid( $i ), "$i is an invalid IP address" );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-06 17:36:42 +00:00
|
|
|
/**
|
2017-08-23 00:07:35 +00:00
|
|
|
* Provide some valid IP ranges
|
2011-03-06 17:36:42 +00:00
|
|
|
*/
|
2017-08-23 00:07:35 +00:00
|
|
|
public function provideValidRanges() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ '116.17.184.5/32' ],
|
|
|
|
|
[ '0.17.184.5/30' ],
|
|
|
|
|
[ '16.17.184.1/24' ],
|
|
|
|
|
[ '30.242.52.14/1' ],
|
|
|
|
|
[ '10.232.52.13/8' ],
|
|
|
|
|
[ '30.242.52.14/0' ],
|
|
|
|
|
[ '::e:f:2001/96' ],
|
|
|
|
|
[ '::c:f:2001/128' ],
|
|
|
|
|
[ '::10:f:2001/70' ],
|
|
|
|
|
[ '::fe:f:2001/1' ],
|
|
|
|
|
[ '::6d:f:2001/8' ],
|
|
|
|
|
[ '::fe:f:2001/0' ],
|
|
|
|
|
];
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-06 17:36:42 +00:00
|
|
|
/**
|
2017-08-23 00:07:35 +00:00
|
|
|
* @covers IP::isValidRange
|
|
|
|
|
* @dataProvider provideValidRanges
|
2011-03-06 17:36:42 +00:00
|
|
|
*/
|
2017-08-23 00:07:35 +00:00
|
|
|
public function testValidRanges( $range ) {
|
|
|
|
|
$this->assertTrue( IP::isValidRange( $range ), "$range is a valid IP range" );
|
2015-04-10 16:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-08-23 00:07:35 +00:00
|
|
|
* @covers IP::isValidRange
|
|
|
|
|
* @dataProvider provideInvalidRanges
|
2015-04-10 16:03:58 +00:00
|
|
|
*/
|
2017-08-23 00:07:35 +00:00
|
|
|
public function testInvalidRanges( $invalid ) {
|
|
|
|
|
$this->assertFalse( IP::isValidRange( $invalid ), "$invalid is not a valid IP range" );
|
2015-04-10 16:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
2017-08-23 00:07:35 +00:00
|
|
|
public function provideInvalidRanges() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ '116.17.184.5/33' ],
|
|
|
|
|
[ '0.17.184.5/130' ],
|
|
|
|
|
[ '16.17.184.1/-1' ],
|
|
|
|
|
[ '10.232.52.13/*' ],
|
|
|
|
|
[ '7.232.52.13/ab' ],
|
|
|
|
|
[ '11.232.52.13/' ],
|
|
|
|
|
[ '::e:f:2001/129' ],
|
|
|
|
|
[ '::c:f:2001/228' ],
|
|
|
|
|
[ '::10:f:2001/-1' ],
|
|
|
|
|
[ '::6d:f:2001/*' ],
|
|
|
|
|
[ '::86:f:2001/ab' ],
|
|
|
|
|
[ '::23:f:2001/' ],
|
|
|
|
|
];
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-05 17:30:33 +00:00
|
|
|
/**
|
2015-05-02 16:48:04 +00:00
|
|
|
* @covers IP::sanitizeIP
|
|
|
|
|
* @dataProvider provideSanitizeIP
|
2011-03-05 17:30:33 +00:00
|
|
|
*/
|
2015-05-02 16:48:04 +00:00
|
|
|
public function testSanitizeIP( $expected, $input ) {
|
|
|
|
|
$result = IP::sanitizeIP( $input );
|
|
|
|
|
$this->assertEquals( $expected, $result );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provider for IP::testSanitizeIP()
|
|
|
|
|
*/
|
|
|
|
|
public static function provideSanitizeIP() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ '0.0.0.0', '0.0.0.0' ],
|
|
|
|
|
[ '0.0.0.0', '00.00.00.00' ],
|
|
|
|
|
[ '0.0.0.0', '000.000.000.000' ],
|
2018-05-22 21:12:30 +00:00
|
|
|
[ '0.0.0.0/24', '000.000.000.000/24' ],
|
2016-02-17 09:09:32 +00:00
|
|
|
[ '141.0.11.253', '141.000.011.253' ],
|
|
|
|
|
[ '1.2.4.5', '1.2.4.5' ],
|
|
|
|
|
[ '1.2.4.5', '01.02.04.05' ],
|
|
|
|
|
[ '1.2.4.5', '001.002.004.005' ],
|
|
|
|
|
[ '10.0.0.1', '010.0.000.1' ],
|
|
|
|
|
[ '80.72.250.4', '080.072.250.04' ],
|
|
|
|
|
[ 'Foo.1000.00', 'Foo.1000.00' ],
|
|
|
|
|
[ 'Bar.01', 'Bar.01' ],
|
|
|
|
|
[ 'Bar.010', 'Bar.010' ],
|
|
|
|
|
[ null, '' ],
|
|
|
|
|
[ null, ' ' ]
|
|
|
|
|
];
|
2011-03-05 17:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
Improve behavior of IP::toUnsigned on Windows
On Windows (and 32-bit systems), pow( 2, 32 ) - 1 is a float, which makes
IP::toUnsigned return a float sometimes (instead of string, int, or false,
as is specified in the documentation).
This makes problems for some callers (e.g. I0c9a4ae7 had to modify
wfBaseConvert because of this, while I believe this change would have made
that change unnecessary).
So to improve that, and make the result correspond to the documentation,
we ensure floats are converted to strings.
Plus, more phpunit coverage of IP::toUnsigned and the related IP::toHex.
Change-Id: Ic8e4d9c65497e78960b03555eab0558a6af7d8d2
2013-06-25 18:04:46 +00:00
|
|
|
/**
|
|
|
|
|
* @covers IP::toHex
|
|
|
|
|
* @dataProvider provideToHex
|
|
|
|
|
*/
|
|
|
|
|
public function testToHex( $expected, $input ) {
|
|
|
|
|
$result = IP::toHex( $input );
|
|
|
|
|
$this->assertTrue( $result === false || is_string( $result ) );
|
|
|
|
|
$this->assertEquals( $expected, $result );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provider for IP::testToHex()
|
|
|
|
|
*/
|
|
|
|
|
public static function provideToHex() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ '00000001', '0.0.0.1' ],
|
|
|
|
|
[ '01020304', '1.2.3.4' ],
|
|
|
|
|
[ '7F000001', '127.0.0.1' ],
|
|
|
|
|
[ '80000000', '128.0.0.0' ],
|
|
|
|
|
[ 'DEADCAFE', '222.173.202.254' ],
|
|
|
|
|
[ 'FFFFFFFF', '255.255.255.255' ],
|
|
|
|
|
[ '8D000BFD', '141.000.11.253' ],
|
|
|
|
|
[ false, 'IN.VA.LI.D' ],
|
|
|
|
|
[ 'v6-00000000000000000000000000000001', '::1' ],
|
|
|
|
|
[ 'v6-20010DB885A3000000008A2E03707334', '2001:0db8:85a3:0000:0000:8a2e:0370:7334' ],
|
|
|
|
|
[ 'v6-20010DB885A3000000008A2E03707334', '2001:db8:85a3::8a2e:0370:7334' ],
|
|
|
|
|
[ false, 'IN:VA::LI:D' ],
|
|
|
|
|
[ false, ':::1' ]
|
|
|
|
|
];
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-06 17:36:42 +00:00
|
|
|
/**
|
|
|
|
|
* @covers IP::isPublic
|
2015-06-10 19:07:48 +00:00
|
|
|
* @dataProvider provideIsPublic
|
2011-03-06 17:36:42 +00:00
|
|
|
*/
|
2015-06-10 19:07:48 +00:00
|
|
|
public function testIsPublic( $expected, $input ) {
|
|
|
|
|
$result = IP::isPublic( $input );
|
|
|
|
|
$this->assertEquals( $expected, $result );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provider for IP::testIsPublic()
|
|
|
|
|
*/
|
|
|
|
|
public static function provideIsPublic() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ false, 'fc00::3' ], # RFC 4193 (local)
|
|
|
|
|
[ false, 'fc00::ff' ], # RFC 4193 (local)
|
|
|
|
|
[ false, '127.1.2.3' ], # loopback
|
|
|
|
|
[ false, '::1' ], # loopback
|
|
|
|
|
[ false, 'fe80::1' ], # link-local
|
|
|
|
|
[ false, '169.254.1.1' ], # link-local
|
|
|
|
|
[ false, '10.0.0.1' ], # RFC 1918 (private)
|
|
|
|
|
[ false, '172.16.0.1' ], # RFC 1918 (private)
|
|
|
|
|
[ false, '192.168.0.1' ], # RFC 1918 (private)
|
|
|
|
|
[ true, '2001:5c0:1000:a::133' ], # public
|
|
|
|
|
[ true, 'fc::3' ], # public
|
|
|
|
|
[ true, '00FC::' ] # public
|
|
|
|
|
];
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Private wrapper used to test CIDR Parsing.
|
2013-02-14 11:22:13 +00:00
|
|
|
private function assertFalseCIDR( $CIDR, $msg = '' ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$ff = [ false, false ];
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Private wrapper to test network shifting using only dot notation
|
|
|
|
|
private function assertNet( $expected, $CIDR ) {
|
|
|
|
|
$parse = IP::parseCIDR( $CIDR );
|
|
|
|
|
$this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-06 17:36:42 +00:00
|
|
|
/**
|
|
|
|
|
* @covers IP::hexToQuad
|
2015-04-10 16:03:58 +00:00
|
|
|
* @dataProvider provideIPsAndHexes
|
2011-03-06 17:36:42 +00:00
|
|
|
*/
|
2015-04-10 16:03:58 +00:00
|
|
|
public function testHexToQuad( $ip, $hex ) {
|
|
|
|
|
$this->assertEquals( $ip, IP::hexToQuad( $hex ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provide some IP addresses and their equivalent hex representations
|
|
|
|
|
*/
|
|
|
|
|
public function provideIPsandHexes() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ '0.0.0.1', '00000001' ],
|
|
|
|
|
[ '255.0.0.0', 'FF000000' ],
|
|
|
|
|
[ '255.255.255.255', 'FFFFFFFF' ],
|
|
|
|
|
[ '10.188.222.255', '0ABCDEFF' ],
|
2015-04-10 16:03:58 +00:00
|
|
|
// hex not left-padded...
|
2016-02-17 09:09:32 +00:00
|
|
|
[ '0.0.0.0', '0' ],
|
|
|
|
|
[ '0.0.0.1', '1' ],
|
|
|
|
|
[ '0.0.0.255', 'FF' ],
|
|
|
|
|
[ '0.0.255.0', 'FF00' ],
|
|
|
|
|
];
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2011-03-06 17:36:42 +00:00
|
|
|
/**
|
|
|
|
|
* @covers IP::hexToOctet
|
2015-04-10 16:03:58 +00:00
|
|
|
* @dataProvider provideOctetsAndHexes
|
2011-03-06 17:36:42 +00:00
|
|
|
*/
|
2015-04-10 16:03:58 +00:00
|
|
|
public function testHexToOctet( $octet, $hex ) {
|
|
|
|
|
$this->assertEquals( $octet, IP::hexToOctet( $hex ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provide some hex and octet representations of the same IPs
|
|
|
|
|
*/
|
|
|
|
|
public function provideOctetsAndHexes() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ '0:0:0:0:0:0:0:1', '00000000000000000000000000000001' ],
|
|
|
|
|
[ '0:0:0:0:0:0:FF:3', '00000000000000000000000000FF0003' ],
|
|
|
|
|
[ '0:0:0:0:0:0:FF00:6', '000000000000000000000000FF000006' ],
|
|
|
|
|
[ '0:0:0:0:0:0:FCCF:FAFF', '000000000000000000000000FCCFFAFF' ],
|
|
|
|
|
[ 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF', 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ],
|
2015-04-10 16:03:58 +00:00
|
|
|
// hex not left-padded...
|
2016-02-17 09:09:32 +00:00
|
|
|
[ '0:0:0:0:0:0:0:0', '0' ],
|
|
|
|
|
[ '0:0:0:0:0:0:0:1', '1' ],
|
|
|
|
|
[ '0:0:0:0:0:0:0:FF', 'FF' ],
|
|
|
|
|
[ '0:0:0:0:0:0:0:FFD0', 'FFD0' ],
|
|
|
|
|
[ '0:0:0:0:0:0:FA00:0', 'FA000000' ],
|
|
|
|
|
[ '0:0:0:0:0:0:FCCF:FAFF', 'FCCFFAFF' ],
|
|
|
|
|
];
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-26 03:45:13 +00:00
|
|
|
/**
|
2010-12-14 16:26:35 +00:00
|
|
|
* IP::parseCIDR() returns an array containing a signed IP address
|
|
|
|
|
* representing the network mask and the bit mask.
|
2011-03-06 17:36:42 +00:00
|
|
|
* @covers IP::parseCIDR
|
2010-12-14 16:26:35 +00:00
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testCIDRParsing() {
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertFalseCIDR( '192.0.2.0', "missing mask" );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
|
|
|
|
|
|
|
|
|
|
// Verify if statement
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
|
|
|
|
|
$this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
// Check internal logic
|
2017-01-16 15:42:53 +00:00
|
|
|
# 0 mask always result in [ 0, 0 ]
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->assertEquals( [ 0, 0 ], IP::parseCIDR( '192.0.0.2/0' ) );
|
|
|
|
|
$this->assertEquals( [ 0, 0 ], IP::parseCIDR( '0.0.0.0/0' ) );
|
|
|
|
|
$this->assertEquals( [ 0, 0 ], IP::parseCIDR( '255.255.255.255/0' ) );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
2011-05-17 22:03:20 +00:00
|
|
|
// @todo FIXME: Add more tests.
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
# This part test network shifting
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertNet( '192.0.0.0', '192.0.0.2/24' );
|
|
|
|
|
$this->assertNet( '192.168.5.0', '192.168.5.13/24' );
|
|
|
|
|
$this->assertNet( '10.0.0.160', '10.0.0.161/28' );
|
|
|
|
|
$this->assertNet( '10.0.0.0', '10.0.0.3/28' );
|
|
|
|
|
$this->assertNet( '10.0.0.0', '10.0.0.3/30' );
|
|
|
|
|
$this->assertNet( '10.0.0.4', '10.0.0.4/30' );
|
2010-12-14 16:26:35 +00:00
|
|
|
$this->assertNet( '172.17.32.0', '172.17.35.48/21' );
|
2013-02-14 11:22:13 +00:00
|
|
|
$this->assertNet( '10.128.0.0', '10.135.0.0/9' );
|
|
|
|
|
$this->assertNet( '134.0.0.0', '134.0.5.1/8' );
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|
2011-03-05 17:30:33 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers IP::canonicalize
|
|
|
|
|
*/
|
|
|
|
|
public function testIPCanonicalizeOnValidIp() {
|
|
|
|
|
$this->assertEquals( '192.0.2.152', IP::canonicalize( '192.0.2.152' ),
|
2013-02-14 11:22:13 +00:00
|
|
|
'Canonicalization of a valid IP returns it unchanged' );
|
2011-03-05 17:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers IP::canonicalize
|
|
|
|
|
*/
|
|
|
|
|
public function testIPCanonicalizeMappedAddress() {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'192.0.2.152',
|
|
|
|
|
IP::canonicalize( '::ffff:192.0.2.152' )
|
|
|
|
|
);
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'192.0.2.152',
|
|
|
|
|
IP::canonicalize( '::192.0.2.152' )
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2011-10-26 03:45:13 +00:00
|
|
|
* Issues there are most probably from IP::toHex() or IP::parseRange()
|
2011-03-05 17:30:33 +00:00
|
|
|
* @covers IP::isInRange
|
|
|
|
|
* @dataProvider provideIPsAndRanges
|
|
|
|
|
*/
|
|
|
|
|
public function testIPIsInRange( $expected, $addr, $range, $message = '' ) {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$expected,
|
|
|
|
|
IP::isInRange( $addr, $range ),
|
|
|
|
|
$message
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Provider for testIPIsInRange() */
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideIPsAndRanges() {
|
2013-02-14 11:22:13 +00:00
|
|
|
# Format: (expected boolean, address, range, optional message)
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2011-03-05 17:30:33 +00:00
|
|
|
# IPv4
|
2016-02-17 09:09:32 +00:00
|
|
|
[ true, '192.0.2.0', '192.0.2.0/24', 'Network address' ],
|
|
|
|
|
[ true, '192.0.2.77', '192.0.2.0/24', 'Simple address' ],
|
|
|
|
|
[ true, '192.0.2.255', '192.0.2.0/24', 'Broadcast address' ],
|
2011-03-05 17:30:33 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
[ false, '0.0.0.0', '192.0.2.0/24' ],
|
|
|
|
|
[ false, '255.255.255', '192.0.2.0/24' ],
|
2011-03-05 17:30:33 +00:00
|
|
|
|
|
|
|
|
# IPv6
|
2016-02-17 09:09:32 +00:00
|
|
|
[ false, '::1', '2001:DB8::/32' ],
|
|
|
|
|
[ false, '::', '2001:DB8::/32' ],
|
|
|
|
|
[ false, 'FE80::1', '2001:DB8::/32' ],
|
|
|
|
|
|
|
|
|
|
[ true, '2001:DB8::', '2001:DB8::/32' ],
|
|
|
|
|
[ true, '2001:0DB8::', '2001:DB8::/32' ],
|
|
|
|
|
[ true, '2001:DB8::1', '2001:DB8::/32' ],
|
|
|
|
|
[ true, '2001:0DB8::1', '2001:DB8::/32' ],
|
|
|
|
|
[ true, '2001:0DB8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
|
|
|
|
|
'2001:DB8::/32' ],
|
|
|
|
|
|
|
|
|
|
[ false, '2001:0DB8:F::', '2001:DB8::/96' ],
|
|
|
|
|
];
|
2011-03-05 17:30:33 +00:00
|
|
|
}
|
2011-06-15 07:35:47 +00:00
|
|
|
|
|
|
|
|
/**
|
2017-12-29 23:44:13 +00:00
|
|
|
* @covers IP::splitHostAndPort()
|
2011-06-15 07:35:47 +00:00
|
|
|
* @dataProvider provideSplitHostAndPort
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testSplitHostAndPort( $expected, $input, $description ) {
|
2011-06-15 07:35:47 +00:00
|
|
|
$this->assertEquals( $expected, IP::splitHostAndPort( $input ), $description );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provider for IP::splitHostAndPort()
|
|
|
|
|
*/
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideSplitHostAndPort() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ false, '[', 'Unclosed square bracket' ],
|
|
|
|
|
[ false, '[::', 'Unclosed square bracket 2' ],
|
|
|
|
|
[ [ '::', false ], '::', 'Bare IPv6 0' ],
|
|
|
|
|
[ [ '::1', false ], '::1', 'Bare IPv6 1' ],
|
|
|
|
|
[ [ '::', false ], '[::]', 'Bracketed IPv6 0' ],
|
|
|
|
|
[ [ '::1', false ], '[::1]', 'Bracketed IPv6 1' ],
|
|
|
|
|
[ [ '::1', 80 ], '[::1]:80', 'Bracketed IPv6 with port' ],
|
|
|
|
|
[ false, '::x', 'Double colon but no IPv6' ],
|
|
|
|
|
[ [ 'x', 80 ], 'x:80', 'Hostname and port' ],
|
|
|
|
|
[ false, 'x:x', 'Hostname and invalid port' ],
|
|
|
|
|
[ [ 'x', false ], 'x', 'Plain hostname' ]
|
|
|
|
|
];
|
2011-06-15 07:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-12-29 23:44:13 +00:00
|
|
|
* @covers IP::combineHostAndPort()
|
2011-06-15 07:35:47 +00:00
|
|
|
* @dataProvider provideCombineHostAndPort
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testCombineHostAndPort( $expected, $input, $description ) {
|
2011-06-15 07:35:47 +00:00
|
|
|
list( $host, $port, $defaultPort ) = $input;
|
2011-10-26 03:45:13 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$expected,
|
|
|
|
|
IP::combineHostAndPort( $host, $port, $defaultPort ),
|
2011-06-15 07:35:47 +00:00
|
|
|
$description );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provider for IP::combineHostAndPort()
|
|
|
|
|
*/
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideCombineHostAndPort() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ '[::1]', [ '::1', 2, 2 ], 'IPv6 default port' ],
|
|
|
|
|
[ '[::1]:2', [ '::1', 2, 3 ], 'IPv6 non-default port' ],
|
|
|
|
|
[ 'x', [ 'x', 2, 2 ], 'Normal default port' ],
|
|
|
|
|
[ 'x:2', [ 'x', 2, 3 ], 'Normal non-default port' ],
|
|
|
|
|
];
|
2011-06-15 07:35:47 +00:00
|
|
|
}
|
|
|
|
|
|
2011-10-01 04:54:44 +00:00
|
|
|
/**
|
2017-12-29 23:44:13 +00:00
|
|
|
* @covers IP::sanitizeRange()
|
2011-10-01 04:54:44 +00:00
|
|
|
* @dataProvider provideIPCIDRs
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testSanitizeRange( $input, $expected, $description ) {
|
2011-10-01 04:54:44 +00:00
|
|
|
$this->assertEquals( $expected, IP::sanitizeRange( $input ), $description );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provider for IP::testSanitizeRange()
|
|
|
|
|
*/
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideIPCIDRs() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ '35.56.31.252/16', '35.56.0.0/16', 'IPv4 range' ],
|
|
|
|
|
[ '135.16.21.252/24', '135.16.21.0/24', 'IPv4 range' ],
|
|
|
|
|
[ '5.36.71.252/32', '5.36.71.252/32', 'IPv4 silly range' ],
|
|
|
|
|
[ '5.36.71.252', '5.36.71.252', 'IPv4 non-range' ],
|
|
|
|
|
[ '0:1:2:3:4:c5:f6:7/96', '0:1:2:3:4:C5:0:0/96', 'IPv6 range' ],
|
|
|
|
|
[ '0:1:2:3:4:5:6:7/120', '0:1:2:3:4:5:6:0/120', 'IPv6 range' ],
|
|
|
|
|
[ '0:e1:2:3:4:5:e6:7/128', '0:E1:2:3:4:5:E6:7/128', 'IPv6 silly range' ],
|
|
|
|
|
[ '0:c1:A2:3:4:5:c6:7', '0:C1:A2:3:4:5:C6:7', 'IPv6 non range' ],
|
|
|
|
|
];
|
2011-10-01 04:54:44 +00:00
|
|
|
}
|
2012-05-11 17:53:14 +00:00
|
|
|
|
|
|
|
|
/**
|
2017-12-29 23:44:13 +00:00
|
|
|
* @covers IP::prettifyIP()
|
2012-05-11 17:53:14 +00:00
|
|
|
* @dataProvider provideIPsToPrettify
|
|
|
|
|
*/
|
2013-10-23 22:51:31 +00:00
|
|
|
public function testPrettifyIP( $ip, $prettified ) {
|
2012-05-11 17:53:14 +00:00
|
|
|
$this->assertEquals( $prettified, IP::prettifyIP( $ip ), "Prettify of $ip" );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provider for IP::testPrettifyIP()
|
|
|
|
|
*/
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideIPsToPrettify() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[ '0:0:0:0:0:0:0:0', '::' ],
|
|
|
|
|
[ '0:0:0::0:0:0', '::' ],
|
|
|
|
|
[ '0:0:0:1:0:0:0:0', '0:0:0:1::' ],
|
|
|
|
|
[ '0:0::f', '::f' ],
|
|
|
|
|
[ '0::0:0:0:33:fef:b', '::33:fef:b' ],
|
|
|
|
|
[ '3f:535:0:0:0:0:e:fbb', '3f:535::e:fbb' ],
|
|
|
|
|
[ '0:0:fef:0:0:0:e:fbb', '0:0:fef::e:fbb' ],
|
|
|
|
|
[ 'abbc:2004::0:0:0:0', 'abbc:2004::' ],
|
|
|
|
|
[ 'cebc:2004:f:0:0:0:0:0', 'cebc:2004:f::' ],
|
|
|
|
|
[ '0:0:0:0:0:0:0:0/16', '::/16' ],
|
|
|
|
|
[ '0:0:0::0:0:0/64', '::/64' ],
|
|
|
|
|
[ '0:0::f/52', '::f/52' ],
|
|
|
|
|
[ '::0:0:33:fef:b/52', '::33:fef:b/52' ],
|
|
|
|
|
[ '3f:535:0:0:0:0:e:fbb/48', '3f:535::e:fbb/48' ],
|
|
|
|
|
[ '0:0:fef:0:0:0:e:fbb/96', '0:0:fef::e:fbb/96' ],
|
|
|
|
|
[ 'abbc:2004:0:0::0:0/40', 'abbc:2004::/40' ],
|
|
|
|
|
[ 'aebc:2004:f:0:0:0:0:0/80', 'aebc:2004:f::/80' ],
|
|
|
|
|
];
|
2012-05-11 17:53:14 +00:00
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
}
|