* 001.001.001.001 and other addresses with leading zeros are valid
authorÆvar Arnfjörð Bjarmason <avar@users.mediawiki.org>
Sun, 31 Dec 2006 09:42:59 +0000 (09:42 +0000)
committerÆvar Arnfjörð Bjarmason <avar@users.mediawiki.org>
Sun, 31 Dec 2006 09:42:59 +0000 (09:42 +0000)
includes/IP.php
t/inc/IP.t

index 16fae9c..edf4af7 100644 (file)
@@ -10,7 +10,7 @@
 // Some regex definition to "play" with IP address and IP address blocks
 
 // An IP is made of 4 bytes from x00 to xFF which is d0 to d255
-define( 'RE_IP_BYTE', '(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)');
+define( 'RE_IP_BYTE', '(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|0?[0-9]?[0-9])');
 define( 'RE_IP_ADD' , RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE . '\.' . RE_IP_BYTE );
 // An IP block is an IP address and a prefix (d1 to d32)
 define( 'RE_IP_PREFIX', '(3[0-2]|[12]?\d)');
@@ -78,7 +78,7 @@ class IP {
 
        /**
         * Split out an IP block as an array of 4 bytes and a mask,
-        * return false if it cant be determined
+        * return false if it can't be determined
         *
         * @parameter $ip string A quad dotted IP address
         * @return array
@@ -223,8 +223,8 @@ class IP {
         $unsignedIP = IP::toUnsigned($addr);
         list( $start, $end ) = IP::parseRange($range);
 
-       $start = hexdec($start);
-       $end   = hexdec($end);
+               $start = hexdec($start);
+               $end   = hexdec($end);
 
         return (($unsignedIP >= $start) && ($unsignedIP <= $end));
     }
index 85fe8cb..eb4978b 100644 (file)
@@ -3,7 +3,7 @@
 
 require 'Test.php';
 
-plan( 'no_plan' );
+plan( 1120 );
 
 require_ok( 'includes/IP.php' );
 
@@ -23,10 +23,15 @@ foreach ( range( 0, 255 ) as $i ) {
        }
 }
 
-$valid = array( '216.17.184.1', '0.0.0.0', '000.000.000.000' );
-
-foreach ( $valid as $v ) {
-       ok( IP::isValid( $v ), "$v is a valid IPv4 address" );
+# A bit excessive perhaps? meh..
+foreach ( range( 256, 999 ) as $i ) {
+       $a = sprintf( "%03d", $i );
+       $b = sprintf( "%02d", $i );
+       $c = sprintf( "%01d", $i );
+       foreach ( array_unique( array( $a, $b, $c ) ) as $f ) {
+               $ip = "$f.$f.$f.$f";
+               ok( ! IP::isValid( $ip ), "$ip is not a valid IPv4 address" );
+       }
 }
 
 $invalid = array(
@@ -52,9 +57,4 @@ foreach ( $private as $p ) {
        ok( ! IP::isPublic( $p ), "$p is not a public IP address" ); 
 }
 
-#is   ('127.0.0.1',    is_loopback_ipv4('127.0.0.1'),          'is_loopback_ipv4 127.0.0.1');
-#is   ('192.0.2.9',    is_testnet_ipv4('192.0.2.9'),           'is_testnet_ipv4 192.0.2.9');
-#is   ('216.17.184.1', is_public_ipv4('216.17.184.1'),         'is_public_ipv4 216.17.184.1');
-#isnt ('192.168.0.1',  is_public_ipv4('192.168.0.1'),          'is_public_ipv4 192.168.0.1');
-#
-?>
\ No newline at end of file
+?>