You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

105 lines
3.5 KiB

  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. * @requires extension sockets
  68. */
  69. public function testAnIpv6WithOptionDisabledIpv6()
  70. {
  71. $this->expectException('RuntimeException');
  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. }