IpUtilsTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpFoundation\Tests;
  11. use Symfony\Component\HttpFoundation\IpUtils;
  12. class IpUtilsTest extends \PHPUnit_Framework_TestCase
  13. {
  14. /**
  15. * @dataProvider testIpv4Provider
  16. */
  17. public function testIpv4($matches, $remoteAddr, $cidr)
  18. {
  19. $this->assertSame($matches, IpUtils::checkIp($remoteAddr, $cidr));
  20. }
  21. public function testIpv4Provider()
  22. {
  23. return array(
  24. array(true, '192.168.1.1', '192.168.1.1'),
  25. array(true, '192.168.1.1', '192.168.1.1/1'),
  26. array(true, '192.168.1.1', '192.168.1.0/24'),
  27. array(false, '192.168.1.1', '1.2.3.4/1'),
  28. array(false, '192.168.1.1', '192.168.1.1/33'), // invalid subnet
  29. array(true, '192.168.1.1', array('1.2.3.4/1', '192.168.1.0/24')),
  30. array(true, '192.168.1.1', array('192.168.1.0/24', '1.2.3.4/1')),
  31. array(false, '192.168.1.1', array('1.2.3.4/1', '4.3.2.1/1')),
  32. array(true, '1.2.3.4', '0.0.0.0/0'),
  33. array(true, '1.2.3.4', '192.168.1.0/0'),
  34. array(false, '1.2.3.4', '256.256.256/0'), // invalid CIDR notation
  35. );
  36. }
  37. /**
  38. * @dataProvider testIpv6Provider
  39. */
  40. public function testIpv6($matches, $remoteAddr, $cidr)
  41. {
  42. if (!defined('AF_INET6')) {
  43. $this->markTestSkipped('Only works when PHP is compiled without the option "disable-ipv6".');
  44. }
  45. $this->assertSame($matches, IpUtils::checkIp($remoteAddr, $cidr));
  46. }
  47. public function testIpv6Provider()
  48. {
  49. return array(
  50. array(true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
  51. array(false, '2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
  52. array(false, '2a01:198:603:0:396e:4789:8e99:890f', '::1'),
  53. array(true, '0:0:0:0:0:0:0:1', '::1'),
  54. array(false, '0:0:603:0:396e:4789:8e99:0001', '::1'),
  55. array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '2a01:198:603:0::/65')),
  56. array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('2a01:198:603:0::/65', '::1')),
  57. array(false, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '1a01:198:603:0::/65')),
  58. array(false, '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2', '::1'),
  59. array(false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'),
  60. );
  61. }
  62. /**
  63. * @expectedException \RuntimeException
  64. * @requires extension sockets
  65. */
  66. public function testAnIpv6WithOptionDisabledIpv6()
  67. {
  68. if (defined('AF_INET6')) {
  69. $this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".');
  70. }
  71. IpUtils::checkIp('2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65');
  72. }
  73. }