Nenhuma Descrição

IpUtilsTest.php 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. array(false, 'an_invalid_ip', '192.168.1.0/24'),
  36. );
  37. }
  38. /**
  39. * @dataProvider testIpv6Provider
  40. */
  41. public function testIpv6($matches, $remoteAddr, $cidr)
  42. {
  43. if (!defined('AF_INET6')) {
  44. $this->markTestSkipped('Only works when PHP is compiled without the option "disable-ipv6".');
  45. }
  46. $this->assertSame($matches, IpUtils::checkIp($remoteAddr, $cidr));
  47. }
  48. public function testIpv6Provider()
  49. {
  50. return array(
  51. array(true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
  52. array(false, '2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'),
  53. array(false, '2a01:198:603:0:396e:4789:8e99:890f', '::1'),
  54. array(true, '0:0:0:0:0:0:0:1', '::1'),
  55. array(false, '0:0:603:0:396e:4789:8e99:0001', '::1'),
  56. array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '2a01:198:603:0::/65')),
  57. array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('2a01:198:603:0::/65', '::1')),
  58. array(false, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '1a01:198:603:0::/65')),
  59. array(false, '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2', '::1'),
  60. array(false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'),
  61. );
  62. }
  63. /**
  64. * @expectedException \RuntimeException
  65. * @requires extension sockets
  66. */
  67. public function testAnIpv6WithOptionDisabledIpv6()
  68. {
  69. if (defined('AF_INET6')) {
  70. $this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".');
  71. }
  72. IpUtils::checkIp('2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65');
  73. }
  74. }