From 6fbd3da8762b4c42a3877780ca3490670ac3c6e4 Mon Sep 17 00:00:00 2001 From: Antoine Musso Date: Sat, 5 Mar 2011 17:30:33 +0000 Subject: [PATCH] tests for IP class, mostly incompletes * IP::sanitizeIP() * IP::canonicalize() * IP::isInRange() --- tests/phpunit/includes/IPTest.php | 73 +++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/phpunit/includes/IPTest.php b/tests/phpunit/includes/IPTest.php index d9ec870a4a..b54222de2a 100644 --- a/tests/phpunit/includes/IPTest.php +++ b/tests/phpunit/includes/IPTest.php @@ -229,6 +229,15 @@ class IPTest extends MediaWikiTestCase { } } + /** + * Improve IP::sanitizeIP() code coverage + * @todo Most probably incomplete + */ + public function testSanitizeIP() { + $this->assertNull( IP::sanitizeIP('') ); + $this->assertNull( IP::sanitizeIP(' ') ); + } + /** * test wrapper around ip2long which might return -1 or false depending on PHP version */ @@ -323,4 +332,68 @@ class IPTest extends MediaWikiTestCase { $this->assertNet( '10.128.0.0' , '10.135.0.0/9' ); $this->assertNet( '134.0.0.0' , '134.0.5.1/8' ); } + + + /** + * @covers IP::canonicalize + */ + public function testIPCanonicalizeOnValidIp() { + $this->assertEquals( '192.0.2.152', IP::canonicalize( '192.0.2.152' ), + 'Canonicalization of a valid IP returns it unchanged' ); + } + + /** + * @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' ) + ); + } + + /** + * Issues there are most probably from IP::toHex() or IP::parseRange() + * @covers IP::isInRange + * @dataProvider provideIPsAndRanges + */ + public function testIPIsInRange( $expected, $addr, $range, $message = '' ) { + $this->assertEquals( + $expected, + IP::isInRange( $addr, $range ), + $message + ); + } + + /** Provider for testIPIsInRange() */ + function provideIPsAndRanges() { + # Format: (expected boolean, address, range, optional message) + return array( + # IPv4 + array( true , '192.0.2.0' , '192.0.2.0/24', 'Network address' ), + array( true , '192.0.2.77' , '192.0.2.0/24', 'Simple address' ), + array( true , '192.0.2.255' , '192.0.2.0/24', 'Broadcast address' ), + + array( false, '0.0.0.0' , '192.0.2.0/24' ), + array( false, '255.255.255' , '192.0.2.0/24' ), + + # IPv6 + array( false, '::1' , '2001:DB8::/32' ), + array( false, '::' , '2001:DB8::/32' ), + array( false, 'FE80::1', '2001:DB8::/32' ), + + array( true , '2001:DB8::' , '2001:DB8::/32' ), + array( true , '2001:0DB8::' , '2001:DB8::/32' ), + array( true , '2001:DB8::1' , '2001:DB8::/32' ), + array( true , '2001:0DB8::1', '2001:DB8::/32' ), + array( true , '2001:0DB8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF', + '2001:DB8::/32' ), + + array( false, '2001:0DB8:F::', '2001:DB8::/96' ), + ); + } } -- 2.20.1