IpAddressClassifierTest.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. ['fe80::1fc4:15d8:78db:2319%enp4s0'], // v6 zone ID
  36. ['0:0:0:0:0:ffff:10.0.0.1'],
  37. ['0:0:0:0:0:ffff:127.0.0.0'],
  38. ['10.0.0.1'],
  39. ['::'],
  40. ['::1'],
  41. ['100.100.100.200'],
  42. ['192.0.0.1'],
  43. ];
  44. }
  45. /**
  46. * @dataProvider localIpAddressData
  47. */
  48. public function testLocalAddress(string $ip): void {
  49. $isLocal = $this->classifier->isLocalAddress($ip);
  50. self::assertTrue($isLocal);
  51. }
  52. }