1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 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: 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/33'),
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: );
38: }
39:
40: 41: 42:
43: public function testIpv6($matches, $remoteAddr, $cidr)
44: {
45: if (!defined('AF_INET6')) {
46: $this->markTestSkipped('Only works when PHP is compiled without the option "disable-ipv6".');
47: }
48:
49: $this->assertSame($matches, IpUtils::checkIp($remoteAddr, $cidr));
50: }
51:
52: public function testIpv6Provider()
53: {
54: return array(
55: array(true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
56: array(false, '2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
57: array(false, '2a01:198:603:0:396e:4789:8e99:890f', '::1'),
58: array(true, '0:0:0:0:0:0:0:1', '::1'),
59: array(false, '0:0:603:0:396e:4789:8e99:0001', '::1'),
60: array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '2a01:198:603:0::/65')),
61: array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('2a01:198:603:0::/65', '::1')),
62: array(false, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '1a01:198:603:0::/65')),
63: );
64: }
65:
66: 67: 68:
69: public function testAnIpv6WithOptionDisabledIpv6()
70: {
71: if (!extension_loaded('sockets')) {
72: $this->markTestSkipped('Only works when the socket extension is enabled');
73: }
74:
75: if (defined('AF_INET6')) {
76: $this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".');
77: }
78:
79: IpUtils::checkIp('2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65');
80: }
81: }
82: