IpUtilsTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. );
  59. }
  60. /**
  61. * @expectedException \RuntimeException
  62. * @requires extension sockets
  63. */
  64. public function testAnIpv6WithOptionDisabledIpv6()
  65. {
  66. if (defined('AF_INET6')) {
  67. $this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".');
  68. }
  69. IpUtils::checkIp('2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65');
  70. }
  71. }