菜谱项目

IpUtilsTest.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /**
  76. * @dataProvider invalidIpAddressData
  77. */
  78. public function testInvalidIpAddressesDoNotMatch($requestIp, $proxyIp)
  79. {
  80. $this->assertFalse(IpUtils::checkIp4($requestIp, $proxyIp));
  81. }
  82. public function invalidIpAddressData()
  83. {
  84. return array(
  85. 'invalid proxy wildcard' => array('192.168.20.13', '*'),
  86. 'invalid proxy missing netmask' => array('192.168.20.13', '0.0.0.0'),
  87. 'invalid request IP with invalid proxy wildcard' => array('0.0.0.0', '*'),
  88. );
  89. }
  90. }