MediaWiki  REL1_19
IPTest.php
Go to the documentation of this file.
00001 <?php
00006 class IPTest extends MediaWikiTestCase {
00011         public function testisIPAddress() {
00012                 $this->assertFalse( IP::isIPAddress( false ), 'Boolean false is not an IP' );
00013                 $this->assertFalse( IP::isIPAddress( true  ), 'Boolean true is not an IP' );
00014                 $this->assertFalse( IP::isIPAddress( "" ), 'Empty string is not an IP' );
00015                 $this->assertFalse( IP::isIPAddress( 'abc' ), 'Garbage IP string' );
00016                 $this->assertFalse( IP::isIPAddress( ':' ), 'Single ":" is not an IP' );
00017                 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::1'), 'IPv6 with a double :: occurence' );
00018                 $this->assertFalse( IP::isIPAddress( '2001:0DB8::A:1::'), 'IPv6 with a double :: occurence, last at end' );
00019                 $this->assertFalse( IP::isIPAddress( '::2001:0DB8::5:1'), 'IPv6 with a double :: occurence, firt at beginning' );
00020                 $this->assertFalse( IP::isIPAddress( '124.24.52' ), 'IPv4 not enough quads' );
00021                 $this->assertFalse( IP::isIPAddress( '24.324.52.13' ), 'IPv4 out of range' );
00022                 $this->assertFalse( IP::isIPAddress( '.24.52.13' ), 'IPv4 starts with period' );
00023                 $this->assertFalse( IP::isIPAddress( 'fc:100:300' ), 'IPv6 with only 3 words' );
00024 
00025                 $this->assertTrue( IP::isIPAddress( '::' ), 'RFC 4291 IPv6 Unspecified Address' );
00026                 $this->assertTrue( IP::isIPAddress( '::1' ), 'RFC 4291 IPv6 Loopback Address' );
00027                 $this->assertTrue( IP::isIPAddress( '74.24.52.13/20', 'IPv4 range' ) );
00028                 $this->assertTrue( IP::isIPAddress( 'fc:100:a:d:1:e:ac:0/24' ), 'IPv6 range' );
00029                 $this->assertTrue( IP::isIPAddress( 'fc::100:a:d:1:e:ac/96' ), 'IPv6 range with "::"' );
00030 
00031                 $validIPs = array( 'fc:100::', 'fc:100:a:d:1:e:ac::', 'fc::100', '::fc:100:a:d:1:e:ac',
00032                         '::fc', 'fc::100:a:d:1:e:ac', 'fc:100:a:d:1:e:ac:0', '124.24.52.13', '1.24.52.13' );
00033                 foreach ( $validIPs as $ip ) {
00034                         $this->assertTrue( IP::isIPAddress( $ip ), "$ip is a valid IP address" );
00035                 }
00036         }
00037 
00041         public function testisIPv6() {
00042                 $this->assertFalse( IP::isIPv6( ':fc:100::' ), 'IPv6 starting with lone ":"' );
00043                 $this->assertFalse( IP::isIPv6( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
00044                 $this->assertFalse( IP::isIPv6( 'fc:300' ), 'IPv6 with only 2 words' );
00045                 $this->assertFalse( IP::isIPv6( 'fc:100:300' ), 'IPv6 with only 3 words' );
00046 
00047                 $this->assertTrue( IP::isIPv6( 'fc:100::' ) );
00048                 $this->assertTrue( IP::isIPv6( 'fc:100:a::' ) );
00049                 $this->assertTrue( IP::isIPv6( 'fc:100:a:d::' ) );
00050                 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1::' ) );
00051                 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e::' ) );
00052                 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac::' ) );
00053 
00054                 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
00055                 $this->assertFalse( IP::isIPv6( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
00056 
00057                 $this->assertFalse( IP::isIPv6( ':::' ) );
00058                 $this->assertFalse( IP::isIPv6( '::0:' ), 'IPv6 ending in a lone ":"' );
00059 
00060                 $this->assertTrue( IP::isIPv6( '::' ), 'IPv6 zero address' );
00061                 $this->assertTrue( IP::isIPv6( '::0' ) );
00062                 $this->assertTrue( IP::isIPv6( '::fc' ) );
00063                 $this->assertTrue( IP::isIPv6( '::fc:100' ) );
00064                 $this->assertTrue( IP::isIPv6( '::fc:100:a' ) );
00065                 $this->assertTrue( IP::isIPv6( '::fc:100:a:d' ) );
00066                 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1' ) );
00067                 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e' ) );
00068                 $this->assertTrue( IP::isIPv6( '::fc:100:a:d:1:e:ac' ) );
00069 
00070                 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
00071                 $this->assertFalse( IP::isIPv6( '::fc:100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
00072 
00073                 $this->assertFalse( IP::isIPv6( ':fc::100' ), 'IPv6 starting with lone ":"' );
00074                 $this->assertFalse( IP::isIPv6( 'fc::100:' ), 'IPv6 ending with lone ":"' );
00075                 $this->assertFalse( IP::isIPv6( 'fc:::100' ), 'IPv6 with ":::" in the middle' );
00076 
00077                 $this->assertTrue( IP::isIPv6( 'fc::100' ), 'IPv6 with "::" and 2 words' );
00078                 $this->assertTrue( IP::isIPv6( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
00079                 $this->assertTrue( IP::isIPv6( 'fc::100:a:d', 'IPv6 with "::" and 4 words' ) );
00080                 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
00081                 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e' ), 'IPv6 with "::" and 6 words' );
00082                 $this->assertTrue( IP::isIPv6( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
00083                 $this->assertTrue( IP::isIPv6( '2001::df'), 'IPv6 with "::" and 2 words' );
00084                 $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df'), 'IPv6 with "::" and 5 words' );
00085                 $this->assertTrue( IP::isIPv6( '2001:5c0:1400:a::df:2'), 'IPv6 with "::" and 6 words' );
00086 
00087                 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0' ), 'IPv6 with "::" and 8 words' );
00088                 $this->assertFalse( IP::isIPv6( 'fc::100:a:d:1:e:ac:0:1' ), 'IPv6 with 9 words' );
00089 
00090                 $this->assertTrue( IP::isIPv6( 'fc:100:a:d:1:e:ac:0' ) );
00091         }
00092 
00096         public function testisIPv4() {
00097                 $this->assertFalse( IP::isIPv4( false ), 'Boolean false is not an IP' );
00098                 $this->assertFalse( IP::isIPv4( true  ), 'Boolean true is not an IP' );
00099                 $this->assertFalse( IP::isIPv4( "" ), 'Empty string is not an IP' );
00100                 $this->assertFalse( IP::isIPv4( 'abc' ) );
00101                 $this->assertFalse( IP::isIPv4( ':' ) );
00102                 $this->assertFalse( IP::isIPv4( '124.24.52' ), 'IPv4 not enough quads' );
00103                 $this->assertFalse( IP::isIPv4( '24.324.52.13' ), 'IPv4 out of range' );
00104                 $this->assertFalse( IP::isIPv4( '.24.52.13' ), 'IPv4 starts with period' );
00105 
00106                 $this->assertTrue( IP::isIPv4( '124.24.52.13' ) );
00107                 $this->assertTrue( IP::isIPv4( '1.24.52.13' ) );
00108                 $this->assertTrue( IP::isIPv4( '74.24.52.13/20', 'IPv4 range' ) );
00109         }
00110 
00114         public function testValidIPs() {
00115                 foreach ( range( 0, 255 ) as $i ) {
00116                         $a = sprintf( "%03d", $i );
00117                         $b = sprintf( "%02d", $i );
00118                         $c = sprintf( "%01d", $i );
00119                         foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
00120                                 $ip = "$f.$f.$f.$f";
00121                                 $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv4 address" );
00122                         }
00123                 }
00124                 foreach ( range( 0x0, 0xFFFF, 0xF ) as $i ) {
00125                         $a = sprintf( "%04x", $i );
00126                         $b = sprintf( "%03x", $i );
00127                         $c = sprintf( "%02x", $i );
00128                         foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
00129                                 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
00130                                 $this->assertTrue( IP::isValid( $ip ) , "$ip is a valid IPv6 address" );
00131                         }
00132                 }
00133                 // test with some abbreviations
00134                 $this->assertFalse( IP::isValid( ':fc:100::' ), 'IPv6 starting with lone ":"' );
00135                 $this->assertFalse( IP::isValid( 'fc:100:::' ), 'IPv6 ending with a ":::"' );
00136                 $this->assertFalse( IP::isValid( 'fc:300' ), 'IPv6 with only 2 words' );
00137                 $this->assertFalse( IP::isValid( 'fc:100:300' ), 'IPv6 with only 3 words' );
00138 
00139                 $this->assertTrue( IP::isValid( 'fc:100::' ) );
00140                 $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e::' ) );
00141                 $this->assertTrue( IP::isValid( 'fc:100:a:d:1:e:ac::' ) );
00142 
00143                 $this->assertTrue( IP::isValid( 'fc::100' ), 'IPv6 with "::" and 2 words' );
00144                 $this->assertTrue( IP::isValid( 'fc::100:a' ), 'IPv6 with "::" and 3 words' );
00145                 $this->assertTrue( IP::isValid( '2001::df'), 'IPv6 with "::" and 2 words' );
00146                 $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df'), 'IPv6 with "::" and 5 words' );
00147                 $this->assertTrue( IP::isValid( '2001:5c0:1400:a::df:2'), 'IPv6 with "::" and 6 words' );
00148                 $this->assertTrue( IP::isValid( 'fc::100:a:d:1' ), 'IPv6 with "::" and 5 words' );
00149                 $this->assertTrue( IP::isValid( 'fc::100:a:d:1:e:ac' ), 'IPv6 with "::" and 7 words' );
00150 
00151                 $this->assertFalse( IP::isValid( 'fc:100:a:d:1:e:ac:0::' ), 'IPv6 with 8 words ending with "::"' );
00152                 $this->assertFalse( IP::isValid( 'fc:100:a:d:1:e:ac:0:1::' ), 'IPv6 with 9 words ending with "::"' );
00153         }
00154 
00158         public function testInvalidIPs() {
00159                 // Out of range...
00160                 foreach ( range( 256, 999 ) as $i ) {
00161                         $a = sprintf( "%03d", $i );
00162                         $b = sprintf( "%02d", $i );
00163                         $c = sprintf( "%01d", $i );
00164                         foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
00165                                 $ip = "$f.$f.$f.$f";
00166                                 $this->assertFalse( IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
00167                         }
00168                 }
00169                 foreach ( range( 'g', 'z' ) as $i ) {
00170                         $a = sprintf( "%04s", $i );
00171                         $b = sprintf( "%03s", $i );
00172                         $c = sprintf( "%02s", $i );
00173                         foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
00174                                 $ip = "$f:$f:$f:$f:$f:$f:$f:$f";
00175                                 $this->assertFalse( IP::isValid( $ip ) , "$ip is not a valid IPv6 address" );
00176                         }
00177                 }
00178                 // Have CIDR
00179                 $ipCIDRs = array(
00180                         '212.35.31.121/32',
00181                         '212.35.31.121/18',
00182                         '212.35.31.121/24',
00183                         '::ff:d:321:5/96',
00184                         'ff::d3:321:5/116',
00185                         'c:ff:12:1:ea:d:321:5/120',
00186                 );
00187                 foreach ( $ipCIDRs as $i ) {
00188                         $this->assertFalse( IP::isValid( $i ),
00189                                 "$i is an invalid IP address because it is a block" );
00190                 }
00191                 // Incomplete/garbage
00192                 $invalid = array(
00193                         'www.xn--var-xla.net',
00194                         '216.17.184.G',
00195                         '216.17.184.1.',
00196                         '216.17.184',
00197                         '216.17.184.',
00198                         '256.17.184.1'
00199                 );
00200                 foreach ( $invalid as $i ) {
00201                         $this->assertFalse( IP::isValid( $i ), "$i is an invalid IP address" );
00202                 }
00203         }
00204 
00208         public function testValidBlocks() {
00209                 $valid = array(
00210                         '116.17.184.5/32',
00211                         '0.17.184.5/30',
00212                         '16.17.184.1/24',
00213                         '30.242.52.14/1',
00214                         '10.232.52.13/8',
00215                         '30.242.52.14/0',
00216                         '::e:f:2001/96',
00217                         '::c:f:2001/128',
00218                         '::10:f:2001/70',
00219                         '::fe:f:2001/1',
00220                         '::6d:f:2001/8',
00221                         '::fe:f:2001/0',
00222                 );
00223                 foreach ( $valid as $i ) {
00224                         $this->assertTrue( IP::isValidBlock( $i ), "$i is a valid IP block" );
00225                 }
00226         }
00227 
00231         public function testInvalidBlocks() {
00232                 $invalid = array(
00233                         '116.17.184.5/33',
00234                         '0.17.184.5/130',
00235                         '16.17.184.1/-1',
00236                         '10.232.52.13/*',
00237                         '7.232.52.13/ab',
00238                         '11.232.52.13/',
00239                         '::e:f:2001/129',
00240                         '::c:f:2001/228',
00241                         '::10:f:2001/-1',
00242                         '::6d:f:2001/*',
00243                         '::86:f:2001/ab',
00244                         '::23:f:2001/',
00245                 );
00246                 foreach ( $invalid as $i ) {
00247                         $this->assertFalse( IP::isValidBlock( $i ), "$i is not a valid IP block" );
00248                 }
00249         }
00250 
00255         public function testSanitizeIP() {
00256                 $this->assertNull( IP::sanitizeIP('')  );
00257                 $this->assertNull( IP::sanitizeIP(' ') );
00258         }
00259 
00264         public function testip2longWrapper() {
00265                 // @todo FIXME: Add more tests ?
00266                 $this->assertEquals( pow(2,32) - 1, IP::toUnsigned( '255.255.255.255' ));
00267                 $i = 'IN.VA.LI.D';
00268                 $this->assertFalse( IP::toUnSigned( $i ) );
00269         }
00270 
00274         public function testPrivateIPs() {
00275                 $private = array( 'fc00::3', 'fc00::ff', '::1', '10.0.0.1', '172.16.0.1', '192.168.0.1' );
00276                 foreach ( $private as $p ) {
00277                         $this->assertFalse( IP::isPublic( $p ), "$p is not a public IP address" );
00278                 }
00279                 $public = array( '2001:5c0:1000:a::133', 'fc::3', '00FC::' );
00280                 foreach ( $public as $p ) {
00281                         $this->assertTrue( IP::isPublic( $p ), "$p is a public IP address" );
00282                 }
00283         }
00284 
00285         // Private wrapper used to test CIDR Parsing.
00286         private function assertFalseCIDR( $CIDR, $msg='' ) {
00287                 $ff = array( false, false );
00288                 $this->assertEquals( $ff, IP::parseCIDR( $CIDR ), $msg );
00289         }
00290 
00291         // Private wrapper to test network shifting using only dot notation
00292         private function assertNet( $expected, $CIDR ) {
00293                 $parse = IP::parseCIDR( $CIDR );
00294                 $this->assertEquals( $expected, long2ip( $parse[0] ), "network shifting $CIDR" );
00295         }
00296 
00300         public function testHexToQuad() {
00301                 $this->assertEquals( '0.0.0.1'        , IP::hexToQuad( '00000001' ) );
00302                 $this->assertEquals( '255.0.0.0'      , IP::hexToQuad( 'FF000000' ) );
00303                 $this->assertEquals( '255.255.255.255', IP::hexToQuad( 'FFFFFFFF' ) );
00304                 $this->assertEquals( '10.188.222.255' , IP::hexToQuad( '0ABCDEFF' ) );
00305                 // hex not left-padded...
00306                 $this->assertEquals( '0.0.0.0'     , IP::hexToQuad( '0' ) );
00307                 $this->assertEquals( '0.0.0.1'     , IP::hexToQuad( '1' ) );
00308                 $this->assertEquals( '0.0.0.255'   , IP::hexToQuad( 'FF' ) );
00309                 $this->assertEquals( '0.0.255.0'   , IP::hexToQuad( 'FF00' ) );
00310         }
00311 
00315         public function testHexToOctet() {
00316                 $this->assertEquals( '0:0:0:0:0:0:0:1',
00317                         IP::hexToOctet( '00000000000000000000000000000001' ) );
00318                 $this->assertEquals( '0:0:0:0:0:0:FF:3',
00319                         IP::hexToOctet( '00000000000000000000000000FF0003' ) );
00320                 $this->assertEquals( '0:0:0:0:0:0:FF00:6',
00321                         IP::hexToOctet( '000000000000000000000000FF000006' ) );
00322                 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF',
00323                         IP::hexToOctet( '000000000000000000000000FCCFFAFF' ) );
00324                 $this->assertEquals( 'FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF',
00325                         IP::hexToOctet( 'FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF' ) );
00326                 // hex not left-padded...
00327                 $this->assertEquals( '0:0:0:0:0:0:0:0'          , IP::hexToOctet( '0' ) );
00328                 $this->assertEquals( '0:0:0:0:0:0:0:1'          , IP::hexToOctet( '1' ) );
00329                 $this->assertEquals( '0:0:0:0:0:0:0:FF'         , IP::hexToOctet( 'FF' ) );
00330                 $this->assertEquals( '0:0:0:0:0:0:0:FFD0'       , IP::hexToOctet( 'FFD0' ) );
00331                 $this->assertEquals( '0:0:0:0:0:0:FA00:0'       , IP::hexToOctet( 'FA000000' ) );
00332                 $this->assertEquals( '0:0:0:0:0:0:FCCF:FAFF', IP::hexToOctet( 'FCCFFAFF' ) );
00333         }
00334 
00340         function testCIDRParsing() {
00341                 $this->assertFalseCIDR( '192.0.2.0' , "missing mask"    );
00342                 $this->assertFalseCIDR( '192.0.2.0/', "missing bitmask" );
00343 
00344                 // Verify if statement
00345                 $this->assertFalseCIDR( '256.0.0.0/32', "invalid net"      );
00346                 $this->assertFalseCIDR( '192.0.2.0/AA', "mask not numeric" );
00347                 $this->assertFalseCIDR( '192.0.2.0/-1', "mask < 0"         );
00348                 $this->assertFalseCIDR( '192.0.2.0/33', "mask > 32"        );
00349 
00350                 // Check internal logic
00351                 # 0 mask always result in array(0,0)
00352                 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('192.0.0.2/0') );
00353                 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('0.0.0.0/0') );
00354                 $this->assertEquals( array( 0, 0 ), IP::parseCIDR('255.255.255.255/0') );
00355 
00356                 // @todo FIXME: Add more tests.
00357 
00358                 # This part test network shifting
00359                 $this->assertNet( '192.0.0.0'  , '192.0.0.2/24'   );
00360                 $this->assertNet( '192.168.5.0', '192.168.5.13/24');
00361                 $this->assertNet( '10.0.0.160' , '10.0.0.161/28'  );
00362                 $this->assertNet( '10.0.0.0'   , '10.0.0.3/28'  );
00363                 $this->assertNet( '10.0.0.0'   , '10.0.0.3/30'  );
00364                 $this->assertNet( '10.0.0.4'   , '10.0.0.4/30'  );
00365                 $this->assertNet( '172.17.32.0', '172.17.35.48/21' );
00366                 $this->assertNet( '10.128.0.0' , '10.135.0.0/9' );
00367                 $this->assertNet( '134.0.0.0'  , '134.0.5.1/8'  );
00368         }
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         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         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         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         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 }