IpUtilsTest.php 3.0 KB

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