[BUGFIX] cmpIPv4: prevent E_NOTICE, cleanup compare, testcases
authorStefan Neufeind <typo3.neufeind@speedpartner.de>
Mon, 6 Jun 2011 00:17:33 +0000 (02:17 +0200)
committerTolleiv Nietsch <info@tolleiv.de>
Tue, 6 Mar 2012 19:40:24 +0000 (20:40 +0100)
Cleanup for an E_NOTICE on exploding the bitmask.
Replaced (correct working, but "unreadable") strcmp().
Add testcases.

Change-Id: I99c7007e11cd3c9740ab6acfd770dd2ea8cd1545
Resolves: #27230
Releases: 4.3, 4.4, 4.5, 4.6
Reviewed-on: http://review.typo3.org/7129
Reviewed-by: Markus Klein
Tested-by: Markus Klein
Reviewed-by: Stefan Neufeind
Tested-by: Stefan Neufeind
Reviewed-by: Tolleiv Nietsch
Tested-by: Tolleiv Nietsch
t3lib/class.t3lib_div.php
tests/t3lib/t3lib_divTest.php

index 1fd4fb1..576e82d 100644 (file)
@@ -739,9 +739,9 @@ final class t3lib_div {
        /**
         * Match IPv4 number with list of numbers with wildcard
         *
-        * @param       string          $baseIP is the current remote IP address for instance, typ. REMOTE_ADDR
-        * @param       string          $list is a comma-list of IP-addresses to match with. *-wildcard allowed instead of number, plus leaving out parts in the IP number is accepted as wildcard (eg. 192.168.*.* equals 192.168)
-        * @return      boolean         True if an IP-mask from $list matches $baseIP
+        * @param string $baseIP is the current remote IP address for instance, typ. REMOTE_ADDR
+        * @param string $list is a comma-list of IP-addresses to match with. *-wildcard allowed instead of number, plus leaving out parts in the IP number is accepted as wildcard (eg. 192.168.*.* equals 192.168), could also contain IPv6 addresses
+        * @return boolean  True if an IP-mask from $list matches $baseIP
         */
        public static function cmpIPv4($baseIP, $list) {
                $IPpartsReq = explode('.', $baseIP);
@@ -749,7 +749,12 @@ final class t3lib_div {
                        $values = self::trimExplode(',', $list, 1);
 
                        foreach ($values as $test) {
-                               list($test, $mask) = explode('/', $test);
+                               $testList = explode('/', $test);
+                               if (count($testList) == 2) {
+                                       list($test, $mask) = $testList;
+                               } else {
+                                       $mask = FALSE;
+                               }
 
                                if (intval($mask)) {
                                                // "192.168.3.0/24"
@@ -766,7 +771,7 @@ final class t3lib_div {
                                        $yes = 1;
                                        foreach ($IPparts as $index => $val) {
                                                $val = trim($val);
-                                               if (strcmp($val, '*') && strcmp($IPpartsReq[$index], $val)) {
+                                               if (($val !== '*') && ($IPpartsReq[$index] !== $val)) {
                                                        $yes = 0;
                                                }
                                        }
index 2880b91..0299b72 100644 (file)
@@ -183,6 +183,60 @@ class t3lib_divTest extends tx_phpunit_testcase {
        }
 
        ///////////////////////////
+       // Tests concerning cmpIPv4
+       ///////////////////////////
+
+       /**
+        * Data provider for cmpIPv4ReturnsTrueForMatchingAddress
+        *
+        * @return array Data sets
+        */
+       public static function cmpIPv4DataProviderMatching() {
+               return array(
+                       'host with full IP address' => array('127.0.0.1', '127.0.0.1'),
+                       'host with two wildcards at the end' => array('127.0.0.1', '127.0.*.*'),
+                       'host with wildcard at third octet' => array('127.0.0.1', '127.0.*.1'),
+                       'host with wildcard at second octet' => array('127.0.0.1', '127.*.0.1'),
+                       '/8 subnet' => array('127.0.0.1', '127.1.1.1/8'),
+                       '/32 subnet (match only name)' => array('127.0.0.1', '127.0.0.1/32'),
+                       '/30 subnet' => array('10.10.3.1', '10.10.3.3/30'),
+                       'host with wildcard in list with IPv4/IPv6 addresses' => array('192.168.1.1', '127.0.0.1, 1234:5678::/126, 192.168.*'),
+                       'host in list with IPv4/IPv6 addresses' => array('192.168.1.1', '::1, 1234:5678::/126, 192.168.1.1'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider cmpIPv4DataProviderMatching
+        */
+       public function cmpIPv4ReturnsTrueForMatchingAddress($ip, $list) {
+               $this->assertTrue(t3lib_div::cmpIPv4($ip, $list));
+       }
+
+       /**
+        * Data provider for cmpIPv4ReturnsFalseForNotMatchingAddress
+        *
+        * @return array Data sets
+        */
+       public static function cmpIPv4DataProviderNotMatching() {
+               return array(
+                       'single host' => array('127.0.0.1', '127.0.0.2'),
+                       'single host with wildcard' => array('127.0.0.1', '127.*.1.1'),
+                       'single host with /32 subnet mask' => array('127.0.0.1', '127.0.0.2/32'),
+                       '/31 subnet' => array('127.0.0.1', '127.0.0.2/31'),
+                       'list with IPv4/IPv6 addresses' => array('127.0.0.1', '10.0.2.3, 192.168.1.1, ::1'),
+                       'list with only IPv6 addresses' => array('10.20.30.40', '::1, 1234:5678::/127'),
+               );
+       }
+
+       /**
+        * @test
+        * @dataProvider cmpIPv4DataProviderNotMatching
+        */
+       public function cmpIPv4ReturnsFalseForNotMatchingAddress($ip, $list) {
+               $this->assertFalse(t3lib_div::cmpIPv4($ip, $list));
+       }
+       ///////////////////////////
        // Tests concerning cmpIPv6
        ///////////////////////////