1
0

IpAddressClassifierTest.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace lib\Net;
  8. use OC\Net\IpAddressClassifier;
  9. use Test\TestCase;
  10. class IpAddressClassifierTest extends TestCase {
  11. private IpAddressClassifier $classifier;
  12. protected function setUp(): void {
  13. parent::setUp();
  14. $this->classifier = new IpAddressClassifier();
  15. }
  16. public function publicIpAddressData(): array {
  17. return [
  18. ['8.8.8.8'],
  19. ['8.8.4.4'],
  20. ['2001:4860:4860::8888'],
  21. ['2001:4860:4860::8844'],
  22. ];
  23. }
  24. /**
  25. * @dataProvider publicIpAddressData
  26. */
  27. public function testPublicAddress(string $ip): void {
  28. $isLocal = $this->classifier->isLocalAddress($ip);
  29. self::assertFalse($isLocal);
  30. }
  31. public function localIpAddressData(): array {
  32. return [
  33. ['192.168.0.1'],
  34. ['fe80::200:5aee:feaa:20a2'],
  35. ['0:0:0:0:0:ffff:10.0.0.1'],
  36. ['0:0:0:0:0:ffff:127.0.0.0'],
  37. ['10.0.0.1'],
  38. ['::'],
  39. ['::1'],
  40. ['100.100.100.200'],
  41. ['192.0.0.1'],
  42. ];
  43. }
  44. /**
  45. * @dataProvider localIpAddressData
  46. */
  47. public function testLocalAddress(string $ip): void {
  48. $isLocal = $this->classifier->isLocalAddress($ip);
  49. self::assertTrue($isLocal);
  50. }
  51. }