Updating Media dependent modules to versions compatible with core Media.
[yaffs-website] / vendor / symfony / http-foundation / Tests / IpUtilsTest.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpFoundation\Tests;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\HttpFoundation\IpUtils;
16
17 class IpUtilsTest extends TestCase
18 {
19     /**
20      * @dataProvider getIpv4Data
21      */
22     public function testIpv4($matches, $remoteAddr, $cidr)
23     {
24         $this->assertSame($matches, IpUtils::checkIp($remoteAddr, $cidr));
25     }
26
27     public function getIpv4Data()
28     {
29         return array(
30             array(true, '192.168.1.1', '192.168.1.1'),
31             array(true, '192.168.1.1', '192.168.1.1/1'),
32             array(true, '192.168.1.1', '192.168.1.0/24'),
33             array(false, '192.168.1.1', '1.2.3.4/1'),
34             array(false, '192.168.1.1', '192.168.1.1/33'), // invalid subnet
35             array(true, '192.168.1.1', array('1.2.3.4/1', '192.168.1.0/24')),
36             array(true, '192.168.1.1', array('192.168.1.0/24', '1.2.3.4/1')),
37             array(false, '192.168.1.1', array('1.2.3.4/1', '4.3.2.1/1')),
38             array(true, '1.2.3.4', '0.0.0.0/0'),
39             array(true, '1.2.3.4', '192.168.1.0/0'),
40             array(false, '1.2.3.4', '256.256.256/0'), // invalid CIDR notation
41             array(false, 'an_invalid_ip', '192.168.1.0/24'),
42         );
43     }
44
45     /**
46      * @dataProvider getIpv6Data
47      */
48     public function testIpv6($matches, $remoteAddr, $cidr)
49     {
50         if (!defined('AF_INET6')) {
51             $this->markTestSkipped('Only works when PHP is compiled without the option "disable-ipv6".');
52         }
53
54         $this->assertSame($matches, IpUtils::checkIp($remoteAddr, $cidr));
55     }
56
57     public function getIpv6Data()
58     {
59         return array(
60             array(true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
61             array(false, '2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
62             array(false, '2a01:198:603:0:396e:4789:8e99:890f', '::1'),
63             array(true, '0:0:0:0:0:0:0:1', '::1'),
64             array(false, '0:0:603:0:396e:4789:8e99:0001', '::1'),
65             array(true, '0:0:603:0:396e:4789:8e99:0001', '::/0'),
66             array(true, '0:0:603:0:396e:4789:8e99:0001', '2a01:198:603:0::/0'),
67             array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '2a01:198:603:0::/65')),
68             array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('2a01:198:603:0::/65', '::1')),
69             array(false, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '1a01:198:603:0::/65')),
70             array(false, '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2', '::1'),
71             array(false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'),
72         );
73     }
74
75     /**
76      * @expectedException \RuntimeException
77      * @requires extension sockets
78      */
79     public function testAnIpv6WithOptionDisabledIpv6()
80     {
81         if (defined('AF_INET6')) {
82             $this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".');
83         }
84
85         IpUtils::checkIp('2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65');
86     }
87
88     /**
89      * @dataProvider invalidIpAddressData
90      */
91     public function testInvalidIpAddressesDoNotMatch($requestIp, $proxyIp)
92     {
93         $this->assertFalse(IpUtils::checkIp4($requestIp, $proxyIp));
94     }
95
96     public function invalidIpAddressData()
97     {
98         return array(
99             'invalid proxy wildcard' => array('192.168.20.13', '*'),
100             'invalid proxy missing netmask' => array('192.168.20.13', '0.0.0.0'),
101             'invalid request IP with invalid proxy wildcard' => array('0.0.0.0', '*'),
102         );
103     }
104 }