pax_global_header00006660000000000000000000000064137365342430014524gustar00rootroot0000000000000052 comment=ff654b8fc7778b80279815d06a368f7b41249501 component-network-2.0.1/000077500000000000000000000000001373653424300152155ustar00rootroot00000000000000component-network-2.0.1/LICENSE000066400000000000000000000167441373653424300162360ustar00rootroot00000000000000 GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. component-network-2.0.1/README.md000066400000000000000000000026301373653424300164750ustar00rootroot00000000000000# Matomo/Network Component providing Network tools. [![Build Status](https://travis-ci.org/matomo-org/component-network.svg?branch=master)](https://travis-ci.org/matomo-org/component-network) ## Installation With Composer: ```json { "require": { "matomo/network": "*" } } ``` ## Usage ### IP To manipulate an IP address, you can use the `Matomo\Network\IP` class: ```php $ip = IP::fromStringIP('127.0.0.1'); // IPv6 $ip = IP::fromStringIP('::1'); // In binary format: $ip = IP::fromBinaryIP("\x7F\x00\x00\x01"); echo $ip->toString(); // 127.0.0.1 echo $ip->toBinary(); // IPv4 & IPv6 if ($ip instanceof IPv4) {} if ($ip instanceof IPv6) {} // Hostname reverse lookup echo $ip->getHostname(); if ($ip->isInRange('192.168.1.1/32')) {} if ($ip->isInRange('192.168.*.*')) {} // Anonymize an IP by setting X bytes to null bytes $ip->anonymize(2); ``` The `Matomo\Network\IPUtils` class provides utility methods: ```php echo IPUtils::binaryToStringIP("\x7F\x00\x00\x01"); echo IPUtils::stringToBinaryIP('127.0.0.1'); // Sanitization methods $sanitizedIp = IPUtils::sanitizeIp($_GET['ip']); $sanitizedIpRange = IPUtils::sanitizeIpRange($_GET['ipRange']); // IP range $bounds = IPUtils::getIPRangeBounds('192.168.1.*'); echo $bounds[0]; // 192.168.1.0 echo $bounds[1]; // 192.168.1.255 ``` ## License The Network component is released under the [LGPL v3.0](http://choosealicense.com/licenses/lgpl-3.0/). component-network-2.0.1/composer.json000066400000000000000000000006141373653424300177400ustar00rootroot00000000000000{ "name": "matomo/network", "type": "library", "license": "LGPL-3.0", "autoload": { "psr-4": { "Matomo\\Network\\": "src/" } }, "autoload-dev": { "psr-4": { "Tests\\Matomo\\Network\\": "tests/" } }, "require": { "php": ">=5.4" }, "require-dev": { "phpunit/phpunit": "^4.8.36" } } component-network-2.0.1/src/000077500000000000000000000000001373653424300160045ustar00rootroot00000000000000component-network-2.0.1/src/IP.php000066400000000000000000000127161373653424300170340ustar00rootroot00000000000000ip = $ip; } /** * Factory method to create an IP instance from an IP in binary format. * * @see fromStringIP * * @param string $ip IP address in a binary format. * @return IP */ public static function fromBinaryIP($ip) { if ($ip === null || $ip === '') { return new IPv4("\x00\x00\x00\x00"); } if (self::isIPv4($ip)) { return new IPv4($ip); } return new IPv6($ip); } /** * Factory method to create an IP instance from an IP represented as string. * * @see fromBinaryIP * * @param string $ip IP address in a string format (X.X.X.X). * @return IP */ public static function fromStringIP($ip) { return self::fromBinaryIP(IPUtils::stringToBinaryIP($ip)); } /** * Returns the IP address in a binary format. * * @return string */ public function toBinary() { return $this->ip; } /** * Returns the IP address in a string format (X.X.X.X). * * @return string */ public function toString() { return IPUtils::binaryToStringIP($this->ip); } /** * @return string */ public function __toString() { return $this->toString(); } /** * Tries to return the hostname associated to the IP. * * @return string|null The hostname or null if the hostname can't be resolved. */ public function getHostname() { $stringIp = $this->toString(); $host = strtolower(@gethostbyaddr($stringIp)); if ($host === '' || $host === $stringIp) { return null; } return $host; } /** * Determines if the IP address is in a specified IP address range. * * An IPv4-mapped address should be range checked with an IPv4-mapped address range. * * @param array|string $ipRange IP address range (string or array containing min and max IP addresses) * @return bool */ public function isInRange($ipRange) { $ipLen = strlen($this->ip); if (empty($this->ip) || empty($ipRange) || ($ipLen != 4 && $ipLen != 16)) { return false; } if (is_array($ipRange)) { // already split into low/high IP addresses $ipRange[0] = IPUtils::stringToBinaryIP($ipRange[0]); $ipRange[1] = IPUtils::stringToBinaryIP($ipRange[1]); } else { // expect CIDR format but handle some variations $ipRange = IPUtils::getIPRangeBounds($ipRange); } if ($ipRange === null) { return false; } $low = $ipRange[0]; $high = $ipRange[1]; if (strlen($low) != $ipLen) { return false; } // binary-safe string comparison if ($this->ip >= $low && $this->ip <= $high) { return true; } return false; } /** * Determines if the IP address is in a specified IP address range. * * An IPv4-mapped address should be range checked with IPv4-mapped address ranges. * * @param array $ipRanges List of IP address ranges (strings or arrays containing min and max IP addresses). * @return bool True if in any of the specified IP address ranges; false otherwise. */ public function isInRanges(array $ipRanges) { $ipLen = strlen($this->ip); if (empty($this->ip) || empty($ipRanges) || ($ipLen != 4 && $ipLen != 16)) { return false; } foreach ($ipRanges as $ipRange) { if ($this->isInRange($ipRange)) { return true; } } return false; } /** * Returns the IP address as an IPv4 string when possible. * * Some IPv6 can be transformed to IPv4 addresses, for example * IPv4-mapped IPv6 addresses: `::ffff:192.168.0.1` will return `192.168.0.1`. * * @return string|null IPv4 string address e.g. `'192.0.2.128'` or null if this is not an IPv4 address. */ public abstract function toIPv4String(); /** * Anonymize X bytes of the IP address by setting them to a null byte. * * This method returns a new IP instance, it does not modify the current object. * * @param int $byteCount Number of bytes to set to "\0". * * @return IP Returns a new modified instance. */ public abstract function anonymize($byteCount); /** * Returns true if this is an IPv4, IPv4-compat, or IPv4-mapped address, false otherwise. * * @param string $binaryIp * @return bool */ private static function isIPv4($binaryIp) { // in case mbstring overloads strlen function $strlen = function_exists('mb_orig_strlen') ? 'mb_orig_strlen' : 'strlen'; return $strlen($binaryIp) == 4; } } component-network-2.0.1/src/IPUtils.php000066400000000000000000000143151373653424300200520ustar00rootroot00000000000000 $posDot) { $ipString = substr($ipString, 0, $posColon); } // else: Dotted quad IPv6 address, A:B:C:D:E:F:G.H.I.J } else if (strpos($ipString, ':') === $posColon) { $ipString = substr($ipString, 0, $posColon); } // else: IPv6 address, A:B:C:D:E:F:G:H } // else: IPv4 address, A.B.C.D return $ipString; } /** * Sanitize human-readable (user-supplied) IP address range. * * Accepts the following formats for $ipRange: * - single IPv4 address, e.g., 127.0.0.1 * - single IPv6 address, e.g., ::1/128 * - IPv4 block using CIDR notation, e.g., 192.168.0.0/22 represents the IPv4 addresses from 192.168.0.0 to 192.168.3.255 * - IPv6 block using CIDR notation, e.g., 2001:DB8::/48 represents the IPv6 addresses from 2001:DB8:0:0:0:0:0:0 to 2001:DB8:0:FFFF:FFFF:FFFF:FFFF:FFFF * - wildcards, e.g., 192.168.0.* or 2001:DB8:*:*:*:*:*:* * * @param string $ipRangeString IP address range * @return string|null IP address range in CIDR notation OR null on failure */ public static function sanitizeIpRange($ipRangeString) { $ipRangeString = trim($ipRangeString); if (empty($ipRangeString)) { return null; } // IP address with wildcards '*' if (strpos($ipRangeString, '*') !== false) { // Disallow prefixed wildcards and anything other than wildcards // and separators (including IPv6 zero groups) after first wildcard if (preg_match('/[^.:]\*|\*.*([^.:*]|::)/', $ipRangeString)) { return null; } $numWildcards = substr_count($ipRangeString, '*'); $ipRangeString = str_replace('*', '0', $ipRangeString); // CIDR } elseif (($pos = strpos($ipRangeString, '/')) !== false) { $bits = substr($ipRangeString, $pos + 1); $ipRangeString = substr($ipRangeString, 0, $pos); if (!is_numeric($bits)) { return null; } } // single IP if (($ip = @inet_pton($ipRangeString)) === false) return null; $maxbits = strlen($ip) * 8; if (!isset($bits)) { $bits = $maxbits; if (isset($numWildcards)) { $bits -= ($maxbits === 32 ? 8 : 16) * $numWildcards; } } if ($bits < 0 || $bits > $maxbits) { return null; } return "$ipRangeString/$bits"; } /** * Converts an IP address in string/presentation format to binary/network address format. * * @param string $ipString IP address, either IPv4 or IPv6, e.g. `'127.0.0.1'`. * @return string Binary-safe string, e.g. `"\x7F\x00\x00\x01"`. */ public static function stringToBinaryIP($ipString) { // use @inet_pton() because it throws an exception and E_WARNING on invalid input $ip = @inet_pton($ipString); return $ip === false ? "\x00\x00\x00\x00" : $ip; } /** * Convert binary/network address format to string/presentation format. * * @param string $ip IP address in binary/network address format, e.g. `"\x7F\x00\x00\x01"`. * @return string IP address in string format, e.g. `'127.0.0.1'`. */ public static function binaryToStringIP($ip) { // use @inet_ntop() because it throws an exception and E_WARNING on invalid input $ipStr = @inet_ntop($ip); return $ipStr === false ? '0.0.0.0' : $ipStr; } /** * Get low and high IP addresses for a specified IP range. * * @param string $ipRange An IP address range in string format, e.g. `'192.168.1.1/24'`. * @return array|null Array `array($lowIp, $highIp)` in binary format, or null on failure. */ public static function getIPRangeBounds($ipRange) { $ipRange = self::sanitizeIpRange($ipRange); if ($ipRange === null || (($pos = strpos($ipRange, '/')) === false) || ($pos + 1 === strlen($ipRange)) ) { return null; } $range = substr($ipRange, 0, $pos); $high = $low = @inet_pton($range); if ($low === false) { return null; } $addrLen = strlen($low); $bits = (int) substr($ipRange, $pos + 1); if ($bits < 0 || $bits > $addrLen * 8) { return null; } $octet = (int) (($bits + 7) / 8); for ($i = $octet; $i < $addrLen; $i++) { $low[$i] = chr(0); $high[$i] = chr(255); } if (($n = $bits % 8)) { $mask = (1 << (8 - $n)) - 1; $value = ord($low[--$octet]) & ~$mask; $low[$octet] = chr($value); $high[$octet] = chr($value | $mask); } return array($low, $high); } } component-network-2.0.1/src/IPv4.php000066400000000000000000000015301373653424300172760ustar00rootroot00000000000000toString(); } /** * {@inheritdoc} */ public function anonymize($byteCount) { $newBinaryIp = $this->ip; $i = strlen($newBinaryIp); if ($byteCount > $i) { $byteCount = $i; } while ($byteCount-- > 0) { $newBinaryIp[--$i] = chr(0); } return self::fromBinaryIP($newBinaryIp); } } component-network-2.0.1/src/IPv6.php000066400000000000000000000034701373653424300173050ustar00rootroot00000000000000ip; if ($this->isMappedIPv4()) { $i = strlen($newBinaryIp); if ($byteCount > $i) { $byteCount = $i; } while ($byteCount-- > 0) { $newBinaryIp[--$i] = chr(0); } return self::fromBinaryIP($newBinaryIp); } $masks = array( 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', 'ffff:ffff:ffff:ffff::', 'ffff:ffff:ffff:0000::', 'ffff:ff00:0000:0000::', '0000::' ); $newBinaryIp = $newBinaryIp & pack('a16', inet_pton($masks[$byteCount])); return self::fromBinaryIP($newBinaryIp); } /** * {@inheritdoc} */ public function toIPv4String() { $str = $this->toString(); if ($this->isMappedIPv4()) { return substr($str, strlen(self::MAPPED_IPv4_START)); } return null; } /** * Returns true if this is a IPv4 mapped address, false otherwise. * * @return bool */ public function isMappedIPv4() { return substr_compare($this->ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff", 0, 12) === 0 || substr_compare($this->ip, "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 0, 12) === 0; } } component-network-2.0.1/tests/000077500000000000000000000000001373653424300163575ustar00rootroot00000000000000component-network-2.0.1/tests/IPTest.php000066400000000000000000000224451373653424300202470ustar00rootroot00000000000000assertInstanceOf('Matomo\Network\\' . $class, $ip); $this->assertEquals($binary, $ip->toBinary()); $this->assertEquals($str, $ip->toString()); $this->assertEquals($str, (string) $ip); } /** * @dataProvider emptyNullIpData */ public function testFromBinaryIPOnEmptyAndNull($ipAddress, $expectedBinary, $expectedStr) { $ip = IP::fromBinaryIP($ipAddress); $this->assertInstanceOf('Matomo\Network\\IPv4', $ip); $this->assertEquals($expectedBinary, $ip->toBinary()); $this->assertEquals($expectedStr, $ip->toString()); $this->assertEquals($expectedStr, (string) $ip); } /** * @dataProvider ipData */ public function testFromStringIP($str, $binary) { $ip = IP::fromStringIP($str); $this->assertEquals($binary, $ip->toBinary()); $this->assertEquals($str, $ip->toString()); $this->assertEquals($str, (string) $ip); } /** * @dataProvider emptyNullIpData */ public function testFromStringIPOnEmptyAndNull($ipAddress, $expectedBinary, $expectedStr) { $ip = IP::fromStringIP($ipAddress); $this->assertInstanceOf('Matomo\Network\\IPv4', $ip); $this->assertEquals($expectedBinary, $ip->toBinary()); $this->assertEquals($expectedStr, $ip->toString()); $this->assertEquals($expectedStr, (string) $ip); } public function testGetHostnameIPv4() { $hosts = array('localhost', 'localhost.localdomain', strtolower(@php_uname('n')), '127.0.0.1'); $ip = IP::fromStringIP('127.0.0.1'); $this->assertContains($ip->getHostname(), $hosts, '127.0.0.1 -> localhost'); } public function testGetHostnameIPv6() { $hosts = array('ip6-localhost', 'localhost', 'localhost.localdomain', strtolower(@php_uname('n')), '::1'); if(self::isTravisCI()) { // Reverse lookup does not work on Travis for ::1 ipv6 address $hosts[] = null; } $ip = IP::fromStringIP('::1'); $this->assertContains($ip->getHostname(), $hosts, '::1 -> ip6-localhost'); } /** * Returns true if continuous integration running this request * Useful to exclude tests which may fail only on this setup */ public static function isTravisCI() { $travis = getenv('TRAVIS'); return !empty($travis); } public function testGetHostnameFailure() { $ip = IP::fromStringIP('0.1.2.3'); $this->assertNull($ip->getHostname()); } public function getIpsInRangeData() { return array( array('192.168.1.10', array( '192.168.1.9' => false, '192.168.1.10' => true, '192.168.1.11' => false, // IPv6 addresses (including IPv4 mapped) have to be compared against IPv6 address ranges '::ffff:192.168.1.10' => false, )), array('::ffff:192.168.1.10', array( '::ffff:192.168.1.9' => false, '::ffff:192.168.1.10' => true, '::ffff:c0a8:010a' => true, '0000:0000:0000:0000:0000:ffff:c0a8:010a' => true, '::ffff:192.168.1.11' => false, // conversely, IPv4 addresses have to be compared against IPv4 address ranges '192.168.1.10' => false, )), array('192.168.1.10/32', array( '192.168.1.9' => false, '192.168.1.10' => true, '192.168.1.11' => false, )), array('192.168.1.10/31', array( '192.168.1.9' => false, '192.168.1.10' => true, '192.168.1.11' => true, '192.168.1.12' => false, )), array('192.168.1.128/25', array( '192.168.1.127' => false, '192.168.1.128' => true, '192.168.1.255' => true, '192.168.2.0' => false, )), array('192.168.1.10/24', array( '192.168.0.255' => false, '192.168.1.0' => true, '192.168.1.1' => true, '192.168.1.2' => true, '192.168.1.3' => true, '192.168.1.4' => true, '192.168.1.7' => true, '192.168.1.8' => true, '192.168.1.15' => true, '192.168.1.16' => true, '192.168.1.31' => true, '192.168.1.32' => true, '192.168.1.63' => true, '192.168.1.64' => true, '192.168.1.127' => true, '192.168.1.128' => true, '192.168.1.255' => true, '192.168.2.0' => false, )), array('192.168.1.*', array( '192.168.0.255' => false, '192.168.1.0' => true, '192.168.1.1' => true, '192.168.1.2' => true, '192.168.1.3' => true, '192.168.1.4' => true, '192.168.1.7' => true, '192.168.1.8' => true, '192.168.1.15' => true, '192.168.1.16' => true, '192.168.1.31' => true, '192.168.1.32' => true, '192.168.1.63' => true, '192.168.1.64' => true, '192.168.1.127' => true, '192.168.1.128' => true, '192.168.1.255' => true, '192.168.2.0' => false, )), ); } public function getEmptyIpRangeData() { return array( array(''), array(null) ); } /** * @dataProvider getIpsInRangeData */ public function testIsInRange($range, $test) { foreach ($test as $stringIp => $expected) { $ip = IP::fromStringIP($stringIp); // range as a string $this->assertEquals($expected, $ip->isInRange($range), "$ip in $range"); // range as an array(low, high) $arrayRange = IPUtils::getIPRangeBounds($range); $arrayRange[0] = IPUtils::binaryToStringIP($arrayRange[0]); $arrayRange[1] = IPUtils::binaryToStringIP($arrayRange[1]); $this->assertEquals($expected, $ip->isInRange($arrayRange), "$ip in $range"); } } /** * @dataProvider getEmptyIpRangeData */ public function testIsInRangeOnEmptyIPRange($emptyRange) { $ip = IP::fromStringIP('127.0.0.1'); $this->assertFalse($ip->isInRange($emptyRange)); } public function testIsInRangesOnEmptyIPRange() { $ip = IP::fromStringIP('127.0.0.1'); $this->assertFalse($ip->isInRanges(array())); } public function testIsInRangeWithInvalidRange() { $ip = IP::fromStringIP('127.0.0.1'); $this->assertFalse($ip->isInRange('foo-bar')); } /** * @dataProvider getIpsInRangeData */ public function testIsInRanges($range, $test) { foreach ($test as $stringIp => $expected) { $ip = IP::fromStringIP($stringIp); // range as a string $this->assertEquals($expected, $ip->isInRanges(array($range)), "$ip in $range"); // range as an array(low, high) $arrayRange = IPUtils::getIPRangeBounds($range); $arrayRange[0] = IPUtils::binaryToStringIP($arrayRange[0]); $arrayRange[1] = IPUtils::binaryToStringIP($arrayRange[1]); $this->assertEquals($expected, $ip->isInRanges(array($arrayRange)), "$ip in $range"); } } } component-network-2.0.1/tests/IPUtilsTest.php000066400000000000000000000416641373653424300212740ustar00rootroot00000000000000assertEquals($expected, IPUtils::sanitizeIp($ip)); } public function getIPRangeSanitizationData() { return array( array('', null), array(' 127.0.0.1 ', '127.0.0.1/32'), array('192.168.1.0', '192.168.1.0/32'), array('192.168.1.1/24', '192.168.1.1/24'), array('192.168.1.2/16', '192.168.1.2/16'), array('192.168.1.3/8', '192.168.1.3/8'), array('192.168.1.4/0', '192.168.1.4/0'), array('192.168.1.5/42', null), array('192.168.1.6/', null), array('192.168.1.7/a', null), array('192.168.2.*', '192.168.2.0/24'), array('192.169.*.*', '192.169.0.0/16'), array('193.*.*.*', '193.0.0.0/8'), array('*.*.*.*', '0.0.0.0/0'), array('*.*.*.1', null), array('*.*.1.1', null), array('*.1.1.1', null), array('1.*.1.1', null), array('1.1.*.1', null), array('1.*.*.1', null), array('1.1.1.**', null), array('1.1.1.1*', null), array('1.1.1.*1', null), array('1.1.1.1**', null), array('1.1.1.1*2', null), array('1.1.1.*/24', null), array('::1', '::1/128'), array('::ffff:127.0.0.1', '::ffff:127.0.0.1/128'), array('2001:5c0:1000:b::90f8', '2001:5c0:1000:b::90f8/128'), array('::1/64', '::1/64'), array('::1/129', null), array('::1/', null), array('::1/a', null), array('::ffff:127.0.0.1/64', '::ffff:127.0.0.1/64'), array('2001:5c0:1000:b::90f8/64', '2001:5c0:1000:b::90f8/64'), array('1:2:3:4:5:6:7:8', '1:2:3:4:5:6:7:8/128'), array('1:2:3:4:5:6:7:*', '1:2:3:4:5:6:7:0/112'), array('1:2:3:4:5:6:*:*', '1:2:3:4:5:6:0:0/96'), array('1:2:3:4:5:*:*:*', '1:2:3:4:5:0:0:0/80'), array('1:2:3:4:*:*:*:*', '1:2:3:4:0:0:0:0/64'), array('1:2:3:*:*:*:*:*', '1:2:3:0:0:0:0:0/48'), array('1:2:*:*:*:*:*:*', '1:2:0:0:0:0:0:0/32'), array('1:*:*:*:*:*:*:*', '1:0:0:0:0:0:0:0/16'), array('*:*:*:*:*:*:*:*', '0:0:0:0:0:0:0:0/0'), array('*:2:3:4:5:6:7:8', null), array('::*', '::0/112'), array('::*/112', null), array('::7:*', '::7:0/112'), array('1::*', '1::0/112'), array('::**', null), array('*::', null), array('*::8', null), array('*:2::8', null), array(':*:8', null), array('::*:8', null), array('::*8', null), ); } /** * @dataProvider getIPRangeSanitizationData */ public function testSanitizeIpRange($ip, $expected) { $this->assertSame($expected, IPUtils::sanitizeIpRange($ip)); } public function getIPData() { return array( // IPv4 array('0.0.0.0', "\x00\x00\x00\x00"), array('127.0.0.1', "\x7F\x00\x00\x01"), array('192.168.1.12', "\xc0\xa8\x01\x0c"), array('255.255.255.255', "\xff\xff\xff\xff"), // IPv6 array('::', "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), array('::1', "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"), array('::fffe:7f00:1', "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfe\x7f\x00\x00\x01"), array('::ffff:127.0.0.1', "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x7f\x00\x00\x01"), array('2001:5c0:1000:b::90f8', "\x20\x01\x05\xc0\x10\x00\x00\x0b\x00\x00\x00\x00\x00\x00\x90\xf8"), ); } /** * @dataProvider getIPData */ public function testStringToBinaryIP($string, $binary) { $this->assertEquals($binary, IPUtils::stringToBinaryIP($string)); } public function getInvalidIPData() { return array( // not a series of dotted numbers array(null), array(''), array('alpha'), array('...'), // missing an octet array('.0.0.0'), array('0..0.0'), array('0.0..0'), array('0.0.0.'), // octets must be 0-255 array('-1.0.0.0'), array('1.1.1.256'), // leading zeros not supported (i.e., can be ambiguous, e.g., octal) // array('07.07.07.07'), ); } /** * @dataProvider getInvalidIPData */ public function testStringToBinaryInvalidIP($stringIp) { $this->assertEquals("\x00\x00\x00\x00", IPUtils::stringToBinaryIP($stringIp)); } public function getBinaryIPData() { // a valid network address is either 4 or 16 bytes; those lines are intentionally left blank ;) return array( array(null), array(''), array("\x01"), array("\x01\x00"), array("\x01\x00\x00"), array("\x01\x00\x00\x00\x00"), array("\x01\x00\x00\x00\x00\x00"), array("\x01\x00\x00\x00\x00\x00\x00"), array("\x01\x00\x00\x00\x00\x00\x00\x00"), array("\x01\x00\x00\x00\x00\x00\x00\x00\x00"), array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00"), array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), array("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), ); } /** * @dataProvider getIPData */ public function testBinaryToStringIP($string, $binary) { $this->assertEquals($string, IPUtils::binaryToStringIP($binary)); } /** * @dataProvider getBinaryIPData */ public function testBinaryToStringInvalidIP($binary) { $this->assertEquals('0.0.0.0', IPUtils::binaryToStringIP($binary), bin2hex($binary)); } public function getBoundsForIPRangeTest() { return array( // invalid ranges array(null, null), array('', null), array('0', null), array('192.168.255.255/33', null), array('192.168.255.255/-1', null), array('192.168.1.1/', null), // single IPv4 array('127.0.0.1', array("\x7f\x00\x00\x01", "\x7f\x00\x00\x01")), // IPv4 with wildcards array('192.168.1.*', array("\xc0\xa8\x01\x00", "\xc0\xa8\x01\xff")), array('192.168.*.*', array("\xc0\xa8\x00\x00", "\xc0\xa8\xff\xff")), array('192.*.*.*', array("\xc0\x00\x00\x00", "\xc0\xff\xff\xff")), array('*.*.*.*', array("\x00\x00\x00\x00", "\xff\xff\xff\xff")), // single IPv4 in expected CIDR notation array('192.168.1.1/24', array("\xc0\xa8\x01\x00", "\xc0\xa8\x01\xff")), array('192.168.1.127/32', array("\xc0\xa8\x01\x7f", "\xc0\xa8\x01\x7f")), array('192.168.1.127/31', array("\xc0\xa8\x01\x7e", "\xc0\xa8\x01\x7f")), array('192.168.1.127/30', array("\xc0\xa8\x01\x7c", "\xc0\xa8\x01\x7f")), array('192.168.1.127/29', array("\xc0\xa8\x01\x78", "\xc0\xa8\x01\x7f")), array('192.168.1.127/28', array("\xc0\xa8\x01\x70", "\xc0\xa8\x01\x7f")), array('192.168.1.127/27', array("\xc0\xa8\x01\x60", "\xc0\xa8\x01\x7f")), array('192.168.1.127/26', array("\xc0\xa8\x01\x40", "\xc0\xa8\x01\x7f")), array('192.168.1.127/25', array("\xc0\xa8\x01\x00", "\xc0\xa8\x01\x7f")), array('192.168.1.255/32', array("\xc0\xa8\x01\xff", "\xc0\xa8\x01\xff")), array('192.168.1.255/31', array("\xc0\xa8\x01\xfe", "\xc0\xa8\x01\xff")), array('192.168.1.255/30', array("\xc0\xa8\x01\xfc", "\xc0\xa8\x01\xff")), array('192.168.1.255/29', array("\xc0\xa8\x01\xf8", "\xc0\xa8\x01\xff")), array('192.168.1.255/28', array("\xc0\xa8\x01\xf0", "\xc0\xa8\x01\xff")), array('192.168.1.255/27', array("\xc0\xa8\x01\xe0", "\xc0\xa8\x01\xff")), array('192.168.1.255/26', array("\xc0\xa8\x01\xc0", "\xc0\xa8\x01\xff")), array('192.168.1.255/25', array("\xc0\xa8\x01\x80", "\xc0\xa8\x01\xff")), array('192.168.255.255/24', array("\xc0\xa8\xff\x00", "\xc0\xa8\xff\xff")), array('192.168.255.255/23', array("\xc0\xa8\xfe\x00", "\xc0\xa8\xff\xff")), array('192.168.255.255/22', array("\xc0\xa8\xfc\x00", "\xc0\xa8\xff\xff")), array('192.168.255.255/21', array("\xc0\xa8\xf8\x00", "\xc0\xa8\xff\xff")), array('192.168.255.255/20', array("\xc0\xa8\xf0\x00", "\xc0\xa8\xff\xff")), array('192.168.255.255/19', array("\xc0\xa8\xe0\x00", "\xc0\xa8\xff\xff")), array('192.168.255.255/18', array("\xc0\xa8\xc0\x00", "\xc0\xa8\xff\xff")), array('192.168.255.255/17', array("\xc0\xa8\x80\x00", "\xc0\xa8\xff\xff")), array('192.168.255.255/16', array("\xc0\xa8\x00\x00", "\xc0\xa8\xff\xff")), array('192.168.255.255/15', array("\xc0\xa8\x00\x00", "\xc0\xa9\xff\xff")), array('192.168.255.255/14', array("\xc0\xa8\x00\x00", "\xc0\xab\xff\xff")), array('192.168.255.255/13', array("\xc0\xa8\x00\x00", "\xc0\xaf\xff\xff")), array('192.168.255.255/12', array("\xc0\xa0\x00\x00", "\xc0\xaf\xff\xff")), array('192.168.255.255/11', array("\xc0\xa0\x00\x00", "\xc0\xbf\xff\xff")), array('192.168.255.255/10', array("\xc0\x80\x00\x00", "\xc0\xbf\xff\xff")), array('192.168.255.255/9', array("\xc0\x80\x00\x00", "\xc0\xff\xff\xff")), array('192.168.255.255/8', array("\xc0\x00\x00\x00", "\xc0\xff\xff\xff")), array('192.168.255.255/7', array("\xc0\x00\x00\x00", "\xc1\xff\xff\xff")), array('192.168.255.255/6', array("\xc0\x00\x00\x00", "\xc3\xff\xff\xff")), array('192.168.255.255/5', array("\xc0\x00\x00\x00", "\xc7\xff\xff\xff")), array('192.168.255.255/4', array("\xc0\x00\x00\x00", "\xcf\xff\xff\xff")), array('192.168.255.255/3', array("\xc0\x00\x00\x00", "\xdf\xff\xff\xff")), array('192.168.255.255/2', array("\xc0\x00\x00\x00", "\xff\xff\xff\xff")), array('192.168.255.255/1', array("\x80\x00\x00\x00", "\xff\xff\xff\xff")), array('0.0.0.0/0', array("\x00\x00\x00\x00", "\xff\xff\xff\xff")), // single IPv6 array('::1', array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")), // single IPv6 in expected CIDR notation array('::1/128', array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")), array('::1/127', array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01")), array('::fffe:7f00:1/120', array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfe\x7f\x00\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xfe\x7f\x00\x00\xff")), array('::ffff:127.0.0.1/120', array("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x7f\x00\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\x7f\x00\x00\xff")), array('2001:ca11:911::b0b:15:dead/128', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xad", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xad")), array('2001:ca11:911::b0b:15:dead/127', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xac", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xad")), array('2001:ca11:911::b0b:15:dead/126', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xac", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xaf")), array('2001:ca11:911::b0b:15:dead/125', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xa8", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xaf")), array('2001:ca11:911::b0b:15:dead/124', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xa0", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xaf")), array('2001:ca11:911::b0b:15:dead/123', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xa0", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xbf")), array('2001:ca11:911::b0b:15:dead/122', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\x80", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xbf")), array('2001:ca11:911::b0b:15:dead/121', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\x80", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xff")), array('2001:ca11:911::b0b:15:dead/120', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\xff")), array('2001:ca11:911::b0b:15:dead/119', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xde\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xdf\xff")), array('2001:ca11:911::b0b:15:dead/118', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xdc\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xdf\xff")), array('2001:ca11:911::b0b:15:dead/117', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xd8\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xdf\xff")), array('2001:ca11:911::b0b:15:dead/116', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xd0\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xdf\xff")), array('2001:ca11:911::b0b:15:dead/115', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xc0\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xdf\xff")), array('2001:ca11:911::b0b:15:dead/114', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xc0\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xff\xff")), array('2001:ca11:911::b0b:15:dead/113', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\x80\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xff\xff")), array('2001:ca11:911::b0b:15:dead/112', array("\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\x00\x00", "\x20\x01\xca\x11\x09\x11\x00\x00\x00\x00\x0b\x0b\x00\x15\xff\xff")), ); } /** * @dataProvider getBoundsForIPRangeTest */ public function testGetIPRangeBounds($range, $expected) { $this->assertSame($expected, IPUtils::getIPRangeBounds($range)); } } component-network-2.0.1/tests/IPv4Test.php000066400000000000000000000066211373653424300205170ustar00rootroot00000000000000assertInstanceOf('Matomo\Network\IPv4', $ip); $this->assertEquals($expected, $ip->toIPv4String(), $stringIp); } public function getAddressesToAnonymize() { return array( // ip, array( expected0, expected1, expected2, expected3, expected4 ), array('0.0.0.0', array("\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.0.0.1', array("\x00\x00\x00\x01", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.0.0.255', array("\x00\x00\x00\xff", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.0.1.0', array("\x00\x00\x01\x00", "\x00\x00\x01\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.0.1.1', array("\x00\x00\x01\x01", "\x00\x00\x01\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.0.255.255', array("\x00\x00\xff\xff", "\x00\x00\xff\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.1.0.0', array("\x00\x01\x00\x00", "\x00\x01\x00\x00", "\x00\x01\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.1.1.1', array("\x00\x01\x01\x01", "\x00\x01\x01\x00", "\x00\x01\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.255.255.255', array("\x00\xff\xff\xff", "\x00\xff\xff\x00", "\x00\xff\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('1.0.0.0', array("\x01\x00\x00\x00", "\x01\x00\x00\x00", "\x01\x00\x00\x00", "\x01\x00\x00\x00", "\x00\x00\x00\x00")), array('127.255.255.255', array("\x7f\xff\xff\xff", "\x7f\xff\xff\x00", "\x7f\xff\x00\x00", "\x7f\x00\x00\x00", "\x00\x00\x00\x00")), array('128.0.0.0', array("\x80\x00\x00\x00", "\x80\x00\x00\x00", "\x80\x00\x00\x00", "\x80\x00\x00\x00", "\x00\x00\x00\x00")), array('255.255.255.255', array("\xff\xff\xff\xff", "\xff\xff\xff\x00", "\xff\xff\x00\x00", "\xff\x00\x00\x00", "\x00\x00\x00\x00")), ); } /** * @dataProvider getAddressesToAnonymize */ public function testAnonymize($ipString, $expected) { $ip = IP::fromStringIP($ipString); $this->assertInstanceOf('Matomo\Network\IPv4', $ip); // each IP is tested with 0 to 4 octets masked for ($byteCount = 0; $byteCount <= 4; $byteCount++) { $result = $ip->anonymize($byteCount); $this->assertEquals($expected[$byteCount], $result->toBinary(), "Got $result, Expected " . bin2hex($expected[$byteCount])); } // edge case (bounds check) $this->assertEquals("\x00\x00\x00\x00", $ip->anonymize(5)->toBinary()); } } component-network-2.0.1/tests/IPv6Test.php000066400000000000000000000145001373653424300205140ustar00rootroot00000000000000assertInstanceOf('Matomo\Network\IPv6', $ip); $this->assertEquals($expected, $ip->toIPv4String(), $stringIp); } public function getMappedIPv4Data() { return array( array(IP::fromStringIP('::ffff:192.168.0.1'), true), array(IP::fromStringIP('2001:5c0:1000:b::90f8'), false), // IPv4-mapped (RFC 4291, 2.5.5.2) array(IP::fromBinaryIP("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xc0\xa8\x01\x02"), true), // IPv4-compatible (this transitional format is deprecated in RFC 4291, section 2.5.5.1) array(IP::fromBinaryIP("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xa8\x01\x01"), true), // other IPv6 address array(IP::fromBinaryIP("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\xc0\xa8\x01\x03"), false), array(IP::fromBinaryIP("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xc0\xa8\x01\x04"), false), array(IP::fromBinaryIP("\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\xa8\x01\x05"), false), ); } /** * @dataProvider getMappedIPv4Data */ public function testIsMappedIPv4(IPv6 $ip, $isMapped) { $this->assertEquals($isMapped, $ip->isMappedIPv4(), $ip); } public function getAddressesToAnonymize() { return array( array('2001:db8:0:8d3:0:8a2e:70:7344', array( "\x20\x01\x0d\xb8\x00\x00\x08\xd3\x00\x00\x8a\x2e\x00\x70\x73\x44", "\x20\x01\x0d\xb8\x00\x00\x08\xd3\x00\x00\x00\x00\x00\x00\x00\x00", // mask 64 bits "\x20\x01\x0d\xb8\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", // mask 80 bits "\x20\x01\x0d\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", // mask 104 bits "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" // mask all bits )), array('2001:6f8:900:724::2', array( "\x20\x01\x06\xf8\x09\x00\x07\x24\x00\x00\x00\x00\x00\x00\x00\x02", "\x20\x01\x06\xf8\x09\x00\x07\x24\x00\x00\x00\x00\x00\x00\x00\x00", "\x20\x01\x06\xf8\x09\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", "\x20\x01\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", )) ); } /** * @dataProvider getAddressesToAnonymize */ public function testAnonymize($ipString, $expected) { $ip = IP::fromStringIP($ipString); $this->assertInstanceOf('Matomo\Network\IPv6', $ip); // each IP is tested with 0 to 4 octets masked for ($byteCount = 0; $byteCount <= 4; $byteCount++) { $result = $ip->anonymize($byteCount); $this->assertEquals($expected[$byteCount], $result->toBinary(), "Got $result, Expected " . bin2hex($expected[$byteCount]) . ", Mask: " . $byteCount); } } public function getIPv4AddressesToAnonymize() { return array( // ip, array( expected0, expected1, expected2, expected3, expected4 ), array('0.0.0.0', array("\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.0.0.1', array("\x00\x00\x00\x01", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.0.0.255', array("\x00\x00\x00\xff", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.0.1.0', array("\x00\x00\x01\x00", "\x00\x00\x01\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.0.1.1', array("\x00\x00\x01\x01", "\x00\x00\x01\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.0.255.255', array("\x00\x00\xff\xff", "\x00\x00\xff\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.1.0.0', array("\x00\x01\x00\x00", "\x00\x01\x00\x00", "\x00\x01\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.1.1.1', array("\x00\x01\x01\x01", "\x00\x01\x01\x00", "\x00\x01\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('0.255.255.255', array("\x00\xff\xff\xff", "\x00\xff\xff\x00", "\x00\xff\x00\x00", "\x00\x00\x00\x00", "\x00\x00\x00\x00")), array('1.0.0.0', array("\x01\x00\x00\x00", "\x01\x00\x00\x00", "\x01\x00\x00\x00", "\x01\x00\x00\x00", "\x00\x00\x00\x00")), array('127.255.255.255', array("\x7f\xff\xff\xff", "\x7f\xff\xff\x00", "\x7f\xff\x00\x00", "\x7f\x00\x00\x00", "\x00\x00\x00\x00")), array('128.0.0.0', array("\x80\x00\x00\x00", "\x80\x00\x00\x00", "\x80\x00\x00\x00", "\x80\x00\x00\x00", "\x00\x00\x00\x00")), array('255.255.255.255', array("\xff\xff\xff\xff", "\xff\xff\xff\x00", "\xff\xff\x00\x00", "\xff\x00\x00\x00", "\x00\x00\x00\x00")), ); } /** * @dataProvider getIPv4AddressesToAnonymize */ public function testAnonymizeIPv4MappedAdresses($ipString, $expected) { $ip = IP::fromStringIP('::ffff:' . $ipString); $this->assertInstanceOf('Matomo\Network\IPv6', $ip); // mask IPv4 mapped addresses for ($byteCount = 0; $byteCount <= 4; $byteCount++) { $result = $ip->anonymize($byteCount); $expectedIp = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff" . $expected[$byteCount]; $this->assertEquals($expectedIp, $result->toBinary(), "Got $result, Expected " . bin2hex($expectedIp)); } $this->assertEquals("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\x00\x00\x00\x00\x00", $ip->anonymize(5)->toBinary()); } }