IpUtilsTest.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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, '0:0:603:0:396e:4789:8e99:0001', '::/0'),
  58. array(true, '0:0:603:0:396e:4789:8e99:0001', '2a01:198:603:0::/0'),
  59. array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '2a01:198:603:0::/65')),
  60. array(true, '2a01:198:603:0:396e:4789:8e99:890f', array('2a01:198:603:0::/65', '::1')),
  61. array(false, '2a01:198:603:0:396e:4789:8e99:890f', array('::1', '1a01:198:603:0::/65')),
  62. array(false, '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2', '::1'),
  63. array(false, '2a01:198:603:0:396e:4789:8e99:890f', 'unknown'),
  64. );
  65. }
  66. /**
  67. * @expectedException \RuntimeException
  68. * @requires extension sockets
  69. */
  70. public function testAnIpv6WithOptionDisabledIpv6()
  71. {
  72. if (defined('AF_INET6')) {
  73. $this->markTestSkipped('Only works when PHP is compiled with the option "disable-ipv6".');
  74. }
  75. IpUtils::checkIp('2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65');
  76. }
  77. /**
  78. * @dataProvider invalidIpAddressData
  79. */
  80. public function testInvalidIpAddressesDoNotMatch($requestIp, $proxyIp)
  81. {
  82. $this->assertFalse(IpUtils::checkIp4($requestIp, $proxyIp));
  83. }
  84. public function invalidIpAddressData()
  85. {
  86. return array(
  87. 'invalid proxy wildcard' => array('192.168.20.13', '*'),
  88. 'invalid proxy missing netmask' => array('192.168.20.13', '0.0.0.0'),
  89. 'invalid request IP with invalid proxy wildcard' => array('0.0.0.0', '*'),
  90. );
  91. }
  92. }