RemoteAddressTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Security\Ip;
  8. use OC\Security\Ip\RemoteAddress;
  9. use OCP\IConfig;
  10. use OCP\IRequest;
  11. class RemoteAddressTest extends \Test\TestCase {
  12. private IConfig $config;
  13. private IRequest $request;
  14. protected function setUp(): void {
  15. parent::setUp();
  16. $this->config = $this->createMock(IConfig::class);
  17. $this->request = $this->createMock(IRequest::class);
  18. }
  19. /**
  20. * @param mixed $allowedRanges
  21. * @dataProvider dataProvider
  22. */
  23. public function testAllowedIps(string $remoteIp, $allowedRanges, bool $expected): void {
  24. $this->request
  25. ->method('getRemoteAddress')
  26. ->willReturn($remoteIp);
  27. $this->config
  28. ->method('getSystemValue')
  29. ->with('allowed_admin_ranges', false)
  30. ->willReturn($allowedRanges);
  31. $remoteAddress = new RemoteAddress($this->config, $this->request);
  32. $this->assertEquals($expected, $remoteAddress->allowsAdminActions());
  33. }
  34. /**
  35. * @return array<string, mixed, bool>
  36. */
  37. public function dataProvider(): array {
  38. return [
  39. // No IP (ie. CLI)
  40. ['', ['192.168.1.2/24'], true],
  41. ['', ['fe80/8'], true],
  42. // No configuration
  43. ['1.2.3.4', false, true],
  44. ['1234:4567:8910::', false, true],
  45. // Empty configuration
  46. ['1.2.3.4', [], true],
  47. ['1234:4567:8910::', [], true],
  48. // Invalid configuration
  49. ['1.2.3.4', 'hello', true],
  50. ['1234:4567:8910::', 'world', true],
  51. // Mixed configuration
  52. ['192.168.1.5', ['1.2.3.*', '1234::/8'], false],
  53. ['::1', ['127.0.0.1', '1234::/8'], false],
  54. ['192.168.1.5', ['192.168.1.0/24', '1234::/8'], true],
  55. // Allowed IP
  56. ['1.2.3.4', ['1.2.3.*'], true],
  57. ['fc00:1:2:3::1', ['fc00::/7'], true],
  58. ['1.2.3.4', ['192.168.1.2/24', '1.2.3.0/24'], true],
  59. ['1234:4567:8910::1', ['fe80::/8','1234:4567::/16'], true],
  60. // Blocked IP
  61. ['192.168.1.5', ['1.2.3.*'], false],
  62. ['9234:4567:8910::', ['1234:4567::1'], false],
  63. ['192.168.2.1', ['192.168.1.2/24', '1.2.3.0/24'], false],
  64. ['9234:4567:8910::', ['fe80::/8','1234:4567::/16'], false],
  65. ];
  66. }
  67. }