MediaWiki  REL1_21
IPTest.php
Go to the documentation of this file.
00001 <?php
00007 class IPTest extends MediaWikiTestCase {
00012         public function testisIPAddress() {
00013                 $this->assertFalse( IP::isIPAddress( false ), 'Boolean false is not an IP' );
00014                 $this->assertFalse( IP::isIPAddress( true ), 'Boolean true is not an IP' );
00015                 $this->assertFalse( IP::isIPAddress( "" ), 'Empty string is not an IP' );
00016                 $this->assertFalse( IP::isIPAddress( 'abc' ), 'Garbage IP string' );
00017                 $this->assertFalse( IP::isIPAddress( ':' ), 'Single ":" is not an IP' );
00018                 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::1' ), 'IPv6 with a double :: occurrence' );
00019                 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::' ), 'IPv6 with a double :: occurrence, last at end' );
00020                 $this->assertFalse( IP::isIPAddress( '::2001:0DB8::5:1' ), 'IPv6 with a double :: occurrence, firt at beginning' );
00021                 $this->assertFalse( IP::isIPAddress( '124.24.52' ), 'IPv4 not enough quads' );
00022                 $this->assertFalse( IP::isIPAddress( '24.324.52.13' ), 'IPv4 out of range' );
00023                 $this->assertFalse( IP::isIPAddress( '.24.52.13' ), 'IPv4 starts with period' );
00024                 $this->assertFalse( IP::isIPAddress( 'fc:100:300' ), 'IPv6 with only 3 words' );
00025 
00026                 $this->assertTrue( IP::isIPAddress( '::' ), 'RFC 4291 IPv6 Unspecified Address' );
00027                 $this->assertTrue( IP::isIPAddress( '::1' ), 'RFC 4291 IPv6 Loopback Address' );
00028                 $this->assertTrue( IP::isIPAddress( '74.24.52.13/20', 'IPv4 range' ) );
00029                 $this->assertTrue( IP::isIPAddress( 'fc:100:a:d:1:e:ac:0/24' ), 'IPv6 range' );
00030                 $this->assertTrue( IP::isIPAddress( 'fc::100:a:d:1:e:ac/96' ), 'IPv6 range with "::"' );
00031 
00032                 $validIPs = array( 'fc:100::', 'fc:100:a:d:1:e:ac::', 'fc::100', '::fc:100:a:d:1:e:ac',
00033                         '::fc', 'fc::100:a:d:1:e:ac', 'fc:100:a:d:1:e:ac:0', '124.24.52.13', '1.24.52.13' );
00034                 foreach ( $validIPs as $ip ) {
00035                         $this->assertTrue( IP::isIPAddress( $ip ), "$ip is a valid IP address" );
00036                 }
00037         }
00038 
00042         public function testisIPv6() {
00043                 $this->assertFalse( IP::isIPv6( ':fc:100::' ), 'IPv6 starting with lone ":"' );
00044                 $this->assertFalse( IP::isIPv6( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
00045                 $this->assertFalse( IP::isIPv6( 'fc:300' ), 'IPv6 with only 2 words' );
00046                 $this->assertFalse( IP::isIPv6( 'fc:100:300' ), 'IPv6 with only 3 words' );
00047 
00048                 $this->assertTrue( IP::isIPv6( 'fc:100::' ) );
00049                 $this->assertTrue( IP::isIPv6( 'fc:100:a::' ) );
00050                 $this->assertTrue( IP::isIPv6( 'fc:100:a:d::' ) );
00051                 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1::' ) );
00052                 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e::' ) );
00053                 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac::' ) );
00054 
00055                 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
00056                 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
00057 
00058                 $this->assertFalse( IP::isIPv6( ':::' ) );
00059                 $this->assertFalse( IP::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
00060 
00061                 $this->assertTrue( IP::isIPv6( '::' ), 'IPv6 zero address' );
00062                 $this->assertTrue( IP::isIPv6( '::0' ) );
00063                 $this->assertTrue( IP::isIPv6( '::fc' ) );
00064                 $this->assertTrue( IP::isIPv6( '::fc:100' ) );
00065                 $this->assertTrue( IP::isIPv6( '::fc:100:a' ) );
00066                 $this->assertTrue( IP::isIPv6( '::fc:100:a:d' ) );
00067                 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1' ) );
00068                 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e' ) );
00069                 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e:ac' ) );
00070 
00071                 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
00072                 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
00073 
00074                 $this->assertFalse( IP::isIPv6( ':fc::100' ), 'IPv6 starting with lone ":"' );
00075                 $this->assertFalse( IP::isIPv6( 'fc::100:' ), 'IPv6 ending with lone ":"' );
00076                 $this->assertFalse( IP::isIPv6( 'fc:::100' ), 'IPv6 with ":::" in the middle' );
00077 
00078                 $this->assertTrue( IP::isIPv6( 'fc::100' ), 'IPv6 with "::" and 2 words' );
00079                 $this->assertTrue( IP::isIPv6( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
00080                 $this->assertTrue( IP::isIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' ) );
00081                 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
00082                 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e' ), 'IPv6 with "::" and 6 words' );
00083                 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
00084                 $this->assertTrue( IP::isIPv6( '2001::df' ), 'IPv6 with "::" and 2 words' );
00085                 $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df' ), 'IPv6 with "::" and 5 words' );
00086                 $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df:2' ), 'IPv6 with "::" and 6 words' );
00087 
00088                 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
00089                 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
00090 
00091                 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
00092         }
00093 
00097         public function testisIPv4() {
00098                 $this->assertFalse( IP::isIPv4( false ), 'Boolean false is not an IP' );
00099                 $this->assertFalse( IP::isIPv4( true ), 'Boolean true is not an IP' );
00100                 $this->assertFalse( IP::isIPv4( "" ), 'Empty string is not an IP' );
00101                 $this->assertFalse( IP::isIPv4( 'abc' ) );
00102                 $this->assertFalse( IP::isIPv4( ':' ) );
00103                 $this->assertFalse( IP::isIPv4( '124.24.52' ), 'IPv4 not enough quads' );
00104                 $this->assertFalse( IP::isIPv4( '24.324.52.13' ), 'IPv4 out of range' );
00105                 $this->assertFalse( IP::isIPv4( '.24.52.13' ), 'IPv4 starts with period' );
00106 
00107                 $this->assertTrue( IP::isIPv4( '124.24.52.13' ) );
00108                 $this->assertTrue( IP::isIPv4( '1.24.52.13' ) );
00109                 $this->assertTrue( IP::isIPv4( '74.24.52.13/20', 'IPv4 range' ) );
00110         }
00111 
00115         public function testValidIPs() {
00116                 foreach ( range( 0, 255 ) as $i ) {
00117                         $a = sprintf( "%03d", $i );
00118                         $b = sprintf( "%02d", $i );
00119                         $c = sprintf( "%01d", $i );
00120                         foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
00121                                 $ip = "$f.$f.$f.$f";
00122                                 $this->assertTrue( IP::isValid( $ip ), "$ip is a valid IPv4 address" );
00123                         }
00124                 }
00125                 foreach ( range( 0x0, 0xFFFF, 0xF ) as $i ) {
00126                         $a = sprintf( "%04x", $i );
00127                         $b = sprintf( "%03x", $i );
00128                         $c = sprintf( "%02x", $i );
00129                         foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
00130                                 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
00131                                 $this->assertTrue( IP::isValid( $ip ), "$ip is a valid IPv6 address" );
00132                         }
00133                 }
00134                 // test with some abbreviations
00135                 $this->assertFalse( IP::isValid( ':fc:100::' ), 'IPv6 starting with lone ":"' );
00136                 $this->assertFalse( IP::isValid( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
00137                 $this->assertFalse( IP::isValid( 'fc:300' ), 'IPv6 with only 2 words' );
00138                 $this->assertFalse( IP::isValid( 'fc:100:300' ), 'IPv6 with only 3 words' );
00139 
00140                 $this->assertTrue( IP::isValid( 'fc:100::' ) );
00141                 $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e::' ) );
00142                 $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e:ac::' ) );
00143 
00144                 $this->assertTrue( IP::isValid( 'fc::100' ), 'IPv6 with "::" and 2 words' );
00145                 $this->assertTrue( IP::isValid( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
00146                 $this->assertTrue( IP::isValid( '2001::df' ), 'IPv6 with "::" and 2 words' );
00147                 $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df' ), 'IPv6 with "::" and 5 words' );
00148                 $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df:2' ), 'IPv6 with "::" and 6 words' );
00149                 $this->assertTrue( IP::isValid( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
00150                 $this->assertTrue( IP::isValid( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
00151 
00152                 $this->assertFalse( IP::isValid( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
00153                 $this->assertFalse( IP::isValid( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
00154         }
00155 
00159         public function testInvalidIPs() {
00160                 // Out of range...
00161                 foreach ( range( 256, 999 ) as $i ) {
00162                         $a = sprintf( "%03d", $i );
00163                         $b = sprintf( "%02d", $i );
00164                         $c = sprintf( "%01d", $i );
00165                         foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
00166                                 $ip = "$f.$f.$f.$f";
00167                                 $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
00168                         }
00169                 }
00170                 foreach ( range( 'g', 'z' ) as $i ) {
00171                         $a = sprintf( "%04s", $i );
00172                         $b = sprintf( "%03s", $i );
00173                         $c = sprintf( "%02s", $i );
00174                         foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
00175                                 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
00176                                 $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv6 address" );
00177                         }
00178                 }
00179                 // Have CIDR
00180                 $ipCIDRs = array(
00181                         '212.35.31.121/32',
00182                         '212.35.31.121/18',
00183                         '212.35.31.121/24',
00184                         '::ff:d:321:5/96',
00185                         'ff::d3:321:5/116',
00186                         'c:ff:12:1:ea:d:321:5/120',
00187                 );
00188                 foreach ( $ipCIDRs as $i ) {
00189                         $this->assertFalse( IP::isValid( $i ),
00190                                 "$i is an invalid IP address because it is a block" );
00191                 }
00192                 // Incomplete/garbage
00193                 $invalid = array(
00194                         'www.xn--var-xla.net',
00195                         '216.17.184.G',
00196                         '216.17.184.1.',
00197                         '216.17.184',
00198                         '216.17.184.',
00199                         '256.17.184.1'
00200                 );
00201                 foreach ( $invalid as $i ) {
00202                         $this->assertFalse( IP::isValid( $i ), "$i is an invalid IP address" );
00203                 }
00204         }
00205 
00209         public function testValidBlocks() {
00210                 $valid = array(
00211                         '116.17.184.5/32',
00212                         '0.17.184.5/30',
00213                         '16.17.184.1/24',
00214                         '30.242.52.14/1',
00215                         '10.232.52.13/8',
00216                         '30.242.52.14/0',
00217                         '::e:f:2001/96',
00218                         '::c:f:2001/128',
00219                         '::10:f:2001/70',
00220                         '::fe:f:2001/1',
00221                         '::6d:f:2001/8',
00222                         '::fe:f:2001/0',
00223                 );
00224                 foreach ( $valid as $i ) {
00225                         $this->assertTrue( IP::isValidBlock( $i ), "$i is a valid IP block" );
00226                 }
00227         }
00228 
00232         public function testInvalidBlocks() {
00233                 $invalid = array(
00234                         '116.17.184.5/33',
00235                         '0.17.184.5/130',
00236                         '16.17.184.1/-1',
00237                         '10.232.52.13/*',
00238                         '7.232.52.13/ab',
00239                         '11.232.52.13/',
00240                         '::e:f:2001/129',
00241                         '::c:f:2001/228',
00242                         '::10:f:2001/-1',
00243                         '::6d:f:2001/*',
00244                         '::86:f:2001/ab',
00245                         '::23:f:2001/',
00246                 );
00247                 foreach ( $invalid as $i ) {
00248                         $this->assertFalse( IP::isValidBlock( $i ), "$i is not a valid IP block" );
00249                 }
00250         }
00251 
00256         public function testSanitizeIP() {
00257                 $this->assertNull( IP::sanitizeIP( '' ) );
00258                 $this->assertNull( IP::sanitizeIP( ' ' ) );
00259         }
00260 
00265         public function testip2longWrapper() {
00266                 // @todo FIXME: Add more tests ?
00267                 $this->assertEquals( pow( 2, 32 ) - 1, IP::toUnsigned( '255.255.255.255' ) );
00268                 $i = 'IN.VA.LI.D';
00269                 $this->assertFalse( IP::toUnSigned( $i ) );
00270         }
00271 
00275         public function testPrivateIPs() {
00276                 $private = array( 'fc00::3', 'fc00::ff', '::1', '10.0.0.1', '172.16.0.1', '192.168.0.1' );
00277                 foreach ( $private as $p ) {
00278                         $this->assertFalse( IP::isPublic( $p ), "$p is not a public IP address" );
00279                 }
00280                 $public = array( '2001:5c0:1000:a::133', 'fc::3', '00FC::' );
00281                 foreach ( $public as $p ) {
00282                         $this->assertTrue( IP::isPublic( $p ), "$p is a public IP address" );
00283                 }
00284         }
00285 
00286         // Private wrapper used to test CIDR Parsing.
00287         private function assertFalseCIDR( $CIDR, $msg = '' ) {
00288                 $ff = array( false, false );
00289                 $this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
00290         }
00291 
00292         // Private wrapper to test network shifting using only dot notation
00293         private function assertNet( $expected, $CIDR ) {
00294                 $parse = IP::parseCIDR( $CIDR );
00295                 $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
00296         }
00297 
00301         public function testHexToQuad() {
00302                 $this->assertEquals( '0.0.0.1', IP::hexToQuad( '00000001' ) );
00303                 $this->assertEquals( '255.0.0.0', IP::hexToQuad( 'FF000000' ) );
00304                 $this->assertEquals( '255.255.255.255', IP::hexToQuad( 'FFFFFFFF' ) );
00305                 $this->assertEquals( '10.188.222.255', IP::hexToQuad( '0ABCDEFF' ) );
00306                 // hex not left-padded...
00307                 $this->assertEquals( '0.0.0.0', IP::hexToQuad( '0' ) );
00308                 $this->assertEquals( '0.0.0.1', IP::hexToQuad( '1' ) );
00309                 $this->assertEquals( '0.0.0.255', IP::hexToQuad( 'FF' ) );
00310                 $this->assertEquals( '0.0.255.0', IP::hexToQuad( 'FF00' ) );
00311         }
00312 
00316         public function testHexToOctet() {
00317                 $this->assertEquals( '0:0:0:0:0:0:0:1',
00318                         IP::hexToOctet( '00000000000000000000000000000001' ) );
00319                 $this->assertEquals( '0:0:0:0:0:0:FF:3',
00320                         IP::hexToOctet( '00000000000000000000000000FF0003' ) );
00321                 $this->assertEquals( '0:0:0:0:0:0:FF00:6',
00322                         IP::hexToOctet( '000000000000000000000000FF000006' ) );
00323                 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF',
00324                         IP::hexToOctet( '000000000000000000000000FCCFFAFF' ) );
00325                 $this->assertEquals( 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
00326                         IP::hexToOctet( 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ) );
00327                 // hex not left-padded...
00328                 $this->assertEquals( '0:0:0:0:0:0:0:0', IP::hexToOctet( '0' ) );
00329                 $this->assertEquals( '0:0:0:0:0:0:0:1', IP::hexToOctet( '1' ) );
00330                 $this->assertEquals( '0:0:0:0:0:0:0:FF', IP::hexToOctet( 'FF' ) );
00331                 $this->assertEquals( '0:0:0:0:0:0:0:FFD0', IP::hexToOctet( 'FFD0' ) );
00332                 $this->assertEquals( '0:0:0:0:0:0:FA00:0', IP::hexToOctet( 'FA000000' ) );
00333                 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF', IP::hexToOctet( 'FCCFFAFF' ) );
00334         }
00335 
00341         function testCIDRParsing() {
00342                 $this->assertFalseCIDR( '192.0.2.0', "missing mask" );
00343                 $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
00344 
00345                 // Verify if statement
00346                 $this->assertFalseCIDR( '256.0.0.0/32', "invalid net" );
00347                 $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
00348                 $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0" );
00349                 $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32" );
00350 
00351                 // Check internal logic
00352                 # 0 mask always result in array(0,0)
00353                 $this->assertEquals( array( 0, 0 ), IP::parseCIDR( '192.0.0.2/0' ) );
00354                 $this->assertEquals( array( 0, 0 ), IP::parseCIDR( '0.0.0.0/0' ) );
00355                 $this->assertEquals( array( 0, 0 ), IP::parseCIDR( '255.255.255.255/0' ) );
00356 
00357                 // @todo FIXME: Add more tests.
00358 
00359                 # This part test network shifting
00360                 $this->assertNet( '192.0.0.0', '192.0.0.2/24' );
00361                 $this->assertNet( '192.168.5.0', '192.168.5.13/24' );
00362                 $this->assertNet( '10.0.0.160', '10.0.0.161/28' );
00363                 $this->assertNet( '10.0.0.0', '10.0.0.3/28' );
00364                 $this->assertNet( '10.0.0.0', '10.0.0.3/30' );
00365                 $this->assertNet( '10.0.0.4', '10.0.0.4/30' );
00366                 $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
00367                 $this->assertNet( '10.128.0.0', '10.135.0.0/9' );
00368                 $this->assertNet( '134.0.0.0', '134.0.5.1/8' );
00369         }
00370 
00374         public function testIPCanonicalizeOnValidIp() {
00375                 $this->assertEquals( '192.0.2.152', IP::canonicalize( '192.0.2.152' ),
00376                         'Canonicalization of a valid IP returns it unchanged' );
00377         }
00378 
00382         public function testIPCanonicalizeMappedAddress() {
00383                 $this->assertEquals(
00384                         '192.0.2.152',
00385                         IP::canonicalize( '::ffff:192.0.2.152' )
00386                 );
00387                 $this->assertEquals(
00388                         '192.0.2.152',
00389                         IP::canonicalize( '::192.0.2.152' )
00390                 );
00391         }
00392 
00398         public function testIPIsInRange( $expected, $addr, $range, $message = '' ) {
00399                 $this->assertEquals(
00400                         $expected,
00401                         IP::isInRange( $addr, $range ),
00402                         $message
00403                 );
00404         }
00405 
00407         public static function provideIPsAndRanges() {
00408                 # Format: (expected boolean, address, range, optional message)
00409                 return array(
00410                         # IPv4
00411                         array( true, '192.0.2.0', '192.0.2.0/24', 'Network address' ),
00412                         array( true, '192.0.2.77', '192.0.2.0/24', 'Simple address' ),
00413                         array( true, '192.0.2.255', '192.0.2.0/24', 'Broadcast address' ),
00414 
00415                         array( false, '0.0.0.0', '192.0.2.0/24' ),
00416                         array( false, '255.255.255', '192.0.2.0/24' ),
00417 
00418                         # IPv6
00419                         array( false, '::1', '2001:DB8::/32' ),
00420                         array( false, '::', '2001:DB8::/32' ),
00421                         array( false, 'FE80::1', '2001:DB8::/32' ),
00422 
00423                         array( true, '2001:DB8::', '2001:DB8::/32' ),
00424                         array( true, '2001:0DB8::', '2001:DB8::/32' ),
00425                         array( true, '2001:DB8::1', '2001:DB8::/32' ),
00426                         array( true, '2001:0DB8::1', '2001:DB8::/32' ),
00427                         array( true, '2001:0DB8:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
00428                                 '2001:DB8::/32' ),
00429 
00430                         array( false, '2001:0DB8:F::', '2001:DB8::/96' ),
00431                 );
00432         }
00433 
00438         function testSplitHostAndPort( $expected, $input, $description ) {
00439                 $this->assertEquals( $expected, IP::splitHostAndPort( $input ), $description );
00440         }
00441 
00445         public static function provideSplitHostAndPort() {
00446                 return array(
00447                         array( false, '[', 'Unclosed square bracket' ),
00448                         array( false, '[::', 'Unclosed square bracket 2' ),
00449                         array( array( '::', false ), '::', 'Bare IPv6 0' ),
00450                         array( array( '::1', false ), '::1', 'Bare IPv6 1' ),
00451                         array( array( '::', false ), '[::]', 'Bracketed IPv6 0' ),
00452                         array( array( '::1', false ), '[::1]', 'Bracketed IPv6 1' ),
00453                         array( array( '::1', 80 ), '[::1]:80', 'Bracketed IPv6 with port' ),
00454                         array( false, '::x', 'Double colon but no IPv6' ),
00455                         array( array( 'x', 80 ), 'x:80', 'Hostname and port' ),
00456                         array( false, 'x:x', 'Hostname and invalid port' ),
00457                         array( array( 'x', false ), 'x', 'Plain hostname' )
00458                 );
00459         }
00460 
00465         function testCombineHostAndPort( $expected, $input, $description ) {
00466                 list( $host, $port, $defaultPort ) = $input;
00467                 $this->assertEquals(
00468                         $expected,
00469                         IP::combineHostAndPort( $host, $port, $defaultPort ),
00470                         $description );
00471         }
00472 
00476         public static function provideCombineHostAndPort() {
00477                 return array(
00478                         array( '[::1]', array( '::1', 2, 2 ), 'IPv6 default port' ),
00479                         array( '[::1]:2', array( '::1', 2, 3 ), 'IPv6 non-default port' ),
00480                         array( 'x', array( 'x', 2, 2 ), 'Normal default port' ),
00481                         array( 'x:2', array( 'x', 2, 3 ), 'Normal non-default port' ),
00482                 );
00483         }
00484 
00489         function testSanitizeRange( $input, $expected, $description ) {
00490                 $this->assertEquals( $expected, IP::sanitizeRange( $input ), $description );
00491         }
00492 
00496         public static function provideIPCIDRs() {
00497                 return array(
00498                         array( '35.56.31.252/16', '35.56.0.0/16', 'IPv4 range' ),
00499                         array( '135.16.21.252/24', '135.16.21.0/24', 'IPv4 range' ),
00500                         array( '5.36.71.252/32', '5.36.71.252/32', 'IPv4 silly range' ),
00501                         array( '5.36.71.252', '5.36.71.252', 'IPv4 non-range' ),
00502                         array( '0:1:2:3:4:c5:f6:7/96', '0:1:2:3:4:C5:0:0/96', 'IPv6 range' ),
00503                         array( '0:1:2:3:4:5:6:7/120', '0:1:2:3:4:5:6:0/120', 'IPv6 range' ),
00504                         array( '0:e1:2:3:4:5:e6:7/128', '0:E1:2:3:4:5:E6:7/128', 'IPv6 silly range' ),
00505                         array( '0:c1:A2:3:4:5:c6:7', '0:C1:A2:3:4:5:C6:7', 'IPv6 non range' ),
00506                 );
00507         }
00508 
00513         function testPrettifyIP( $ip, $prettified ) {
00514                 $this->assertEquals( $prettified, IP::prettifyIP( $ip ), "Prettify of $ip" );
00515         }
00516 
00520         public static function provideIPsToPrettify() {
00521                 return array(
00522                         array( '0:0:0:0:0:0:0:0', '::' ),
00523                         array( '0:0:0::0:0:0', '::' ),
00524                         array( '0:0:0:1:0:0:0:0', '0:0:0:1::' ),
00525                         array( '0:0::f', '::f' ),
00526                         array( '0::0:0:0:33:fef:b', '::33:fef:b' ),
00527                         array( '3f:535:0:0:0:0:e:fbb', '3f:535::e:fbb' ),
00528                         array( '0:0:fef:0:0:0:e:fbb', '0:0:fef::e:fbb' ),
00529                         array( 'abbc:2004::0:0:0:0', 'abbc:2004::' ),
00530                         array( 'cebc:2004:f:0:0:0:0:0', 'cebc:2004:f::' ),
00531                         array( '0:0:0:0:0:0:0:0/16', '::/16' ),
00532                         array( '0:0:0::0:0:0/64', '::/64' ),
00533                         array( '0:0::f/52', '::f/52' ),
00534                         array( '::0:0:33:fef:b/52', '::33:fef:b/52' ),
00535                         array( '3f:535:0:0:0:0:e:fbb/48', '3f:535::e:fbb/48' ),
00536                         array( '0:0:fef:0:0:0:e:fbb/96', '0:0:fef::e:fbb/96' ),
00537                         array( 'abbc:2004:0:0::0:0/40', 'abbc:2004::/40' ),
00538                         array( 'aebc:2004:f:0:0:0:0:0/80', 'aebc:2004:f::/80' ),
00539                 );
00540         }
00541 }