IpUtilsTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 [
  25. [true, '192.168.1.1', '192.168.1.1'],
  26. [true, '192.168.1.1', '192.168.1.1/1'],
  27. [true, '192.168.1.1', '192.168.1.0/24'],
  28. [false, '192.168.1.1', '1.2.3.4/1'],
  29. [false, '192.168.1.1', '192.168.1.1/33'], // invalid subnet
  30. [true, '192.168.1.1', ['1.2.3.4/1', '192.168.1.0/24']],
  31. [true, '192.168.1.1', ['192.168.1.0/24', '1.2.3.4/1']],
  32. [false, '192.168.1.1', ['1.2.3.4/1', '4.3.2.1/1']],
  33. [true, '1.2.3.4', '0.0.0.0/0'],
  34. [true, '1.2.3.4', '192.168.1.0/0'],
  35. [false, '1.2.3.4', '256.256.256/0'], // invalid CIDR notation
  36. [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 [
  52. [true, '2a01:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'],
  53. [false, '2a00:198:603:0:396e:4789:8e99:890f', '2a01:198:603:0::/65'],
  54. [false, '2a01:198:603:0:396e:4789:8e99:890f', '::1'],
  55. [true, '0:0:0:0:0:0:0:1', '::1'],
  56. [false, '0:0:603:0:396e:4789:8e99:0001', '::1'],
  57. [true, '0:0:603:0:396e:4789:8e99:0001', '::/0'],
  58. [true, '0:0:603:0:396e:4789:8e99:0001', '2a01:198:603:0::/0'],
  59. [true, '2a01:198:603:0:396e:4789:8e99:890f', ['::1', '2a01:198:603:0::/65']],
  60. [true, '2a01:198:603:0:396e:4789:8e99:890f', ['2a01:198:603:0::/65', '::1']],
  61. [false, '2a01:198:603:0:396e:4789:8e99:890f', ['::1', '1a01:198:603:0::/65']],
  62. [false, '}__test|O:21:&quot;JDatabaseDriverMysqli&quot;:3:{s:2', '::1'],
  63. [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 [
  87. 'invalid proxy wildcard' => ['192.168.20.13', '*'],
  88. 'invalid proxy missing netmask' => ['192.168.20.13', '0.0.0.0'],
  89. 'invalid request IP with invalid proxy wildcard' => ['0.0.0.0', '*'],
  90. ];
  91. }
  92. }