Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 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 Symfony\Component\HttpFoundation\IpUtils;
15
 
16
class IpUtilsTest extends \PHPUnit_Framework_TestCase
17
{
18
    /**
19
     * @dataProvider testIpv4Provider
20
     */
21
    public function testIpv4($matches, $remoteAddr, $cidr)
22
    {
23
        $this->assertSame($matches, IpUtils::checkIp($remoteAddr, $cidr));
24
    }
25
 
26
    public function testIpv4Provider()
27
    {
28
        return array(
29
            array(true, '192.168.1.1', '192.168.1.1'),
30
            array(true, '192.168.1.1', '192.168.1.1/1'),
31
            array(true, '192.168.1.1', '192.168.1.0/24'),
32
            array(false, '192.168.1.1', '1.2.3.4/1'),
33
            array(false, '192.168.1.1', '192.168.1.1/33'), // invalid subnet
34
            array(true, '192.168.1.1', array('1.2.3.4/1', '192.168.1.0/24')),
35
            array(true, '192.168.1.1', array('192.168.1.0/24', '1.2.3.4/1')),
36
            array(false, '192.168.1.1', array('1.2.3.4/1', '4.3.2.1/1')),
37
            array(true, '1.2.3.4', '0.0.0.0/0'),
38
            array(true, '1.2.3.4', '192.168.1.0/0'),
39
            array(false, '1.2.3.4', '256.256.256/0'), // invalid CIDR notation
40
        );
41
    }
42
 
43
    /**
44
     * @dataProvider testIpv6Provider
45
     */
46
    public function testIpv6($matches, $remoteAddr, $cidr)
47
    {
48
        if (!defined('AF_INET6')) {
49
            $this->markTestSkipped('Only works when PHP is compiled without the option "disable-ipv6".');
50
        }
51
 
52
        $this->assertSame($matches, IpUtils::checkIp($remoteAddr, $cidr));
53
    }
54
 
55
    public function testIpv6Provider()
56
    {
57
        return array(
58
            array(true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
59
            array(false, '2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
60
            array(false, '2a01:198:603:0:396e:4789:8e99:890f', '::1'),
61
            array(true, '0:0:0:0:0:0:0:1', '::1'),
62
            array(false, '0:0:603:0:396e:4789:8e99:0001', '::1'),
63
            array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '2a01:198:603:0::/65')),
64
            array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('2a01:198:603:0::/65', '::1')),
65
            array(false, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '1a01:198:603:0::/65')),
66
            array(false, '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2', '::1'),
67
            array(false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'),
68
        );
69
    }
70
 
71
    /**
72
     * @expectedException \RuntimeException
73
     * @requires extension sockets
74
     */
75
    public function testAnIpv6WithOptionDisabledIpv6()
76
    {
77
        if (defined('AF_INET6')) {
78
            $this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".');
79
        }
80
 
81
        IpUtils::checkIp('2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65');
82
    }
83
}