RemoteHostValidatorTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\Security;
  8. use OC\Net\HostnameClassifier;
  9. use OC\Net\IpAddressClassifier;
  10. use OC\Security\RemoteHostValidator;
  11. use OCP\IConfig;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. use Psr\Log\LoggerInterface;
  14. use Test\TestCase;
  15. class RemoteHostValidatorTest extends TestCase {
  16. /** @var IConfig|IConfig&MockObject|MockObject */
  17. private IConfig $config;
  18. /** @var HostnameClassifier|HostnameClassifier&MockObject|MockObject */
  19. private HostnameClassifier $hostnameClassifier;
  20. /** @var IpAddressClassifier|IpAddressClassifier&MockObject|MockObject */
  21. private IpAddressClassifier $ipAddressClassifier;
  22. /** @var MockObject|LoggerInterface|LoggerInterface&MockObject */
  23. private LoggerInterface $logger;
  24. private RemoteHostValidator $validator;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. $this->config = $this->createMock(IConfig::class);
  28. $this->hostnameClassifier = $this->createMock(HostnameClassifier::class);
  29. $this->ipAddressClassifier = $this->createMock(IpAddressClassifier::class);
  30. $this->logger = $this->createMock(LoggerInterface::class);
  31. $this->validator = new RemoteHostValidator(
  32. $this->config,
  33. $this->hostnameClassifier,
  34. $this->ipAddressClassifier,
  35. $this->logger,
  36. );
  37. }
  38. public function dataValid(): array {
  39. return [
  40. ['nextcloud.com', true],
  41. ['com.one-.nextcloud-one.com', false],
  42. ];
  43. }
  44. /**
  45. * @dataProvider dataValid
  46. */
  47. public function testValid(string $host, bool $expected): void {
  48. $this->hostnameClassifier
  49. ->method('isLocalHostname')
  50. ->with($host)
  51. ->willReturn(false);
  52. $this->ipAddressClassifier
  53. ->method('isLocalAddress')
  54. ->with($host)
  55. ->willReturn(false);
  56. $valid = $this->validator->isValid($host);
  57. self::assertSame($expected, $valid);
  58. }
  59. public function testLocalHostname(): void {
  60. $host = 'localhost';
  61. $this->hostnameClassifier
  62. ->method('isLocalHostname')
  63. ->with($host)
  64. ->willReturn(true);
  65. $this->ipAddressClassifier
  66. ->method('isLocalAddress')
  67. ->with($host)
  68. ->willReturn(false);
  69. $valid = $this->validator->isValid($host);
  70. self::assertFalse($valid);
  71. }
  72. public function testLocalAddress(): void {
  73. $host = '10.0.0.10';
  74. $this->hostnameClassifier
  75. ->method('isLocalHostname')
  76. ->with($host)
  77. ->willReturn(false);
  78. $this->ipAddressClassifier
  79. ->method('isLocalAddress')
  80. ->with($host)
  81. ->willReturn(true);
  82. $valid = $this->validator->isValid($host);
  83. self::assertFalse($valid);
  84. }
  85. }