RemoteHostValidatorTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * @copyright 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace lib\Security;
  24. use OC\Net\HostnameClassifier;
  25. use OC\Net\IpAddressClassifier;
  26. use OC\Security\RemoteHostValidator;
  27. use OCP\IConfig;
  28. use PHPUnit\Framework\MockObject\MockObject;
  29. use Psr\Log\LoggerInterface;
  30. use Test\TestCase;
  31. class RemoteHostValidatorTest extends TestCase {
  32. /** @var IConfig|IConfig&MockObject|MockObject */
  33. private IConfig $config;
  34. /** @var HostnameClassifier|HostnameClassifier&MockObject|MockObject */
  35. private HostnameClassifier $hostnameClassifier;
  36. /** @var IpAddressClassifier|IpAddressClassifier&MockObject|MockObject */
  37. private IpAddressClassifier $ipAddressClassifier;
  38. /** @var MockObject|LoggerInterface|LoggerInterface&MockObject */
  39. private LoggerInterface $logger;
  40. private RemoteHostValidator $validator;
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->config = $this->createMock(IConfig::class);
  44. $this->hostnameClassifier = $this->createMock(HostnameClassifier::class);
  45. $this->ipAddressClassifier = $this->createMock(IpAddressClassifier::class);
  46. $this->logger = $this->createMock(LoggerInterface::class);
  47. $this->validator = new RemoteHostValidator(
  48. $this->config,
  49. $this->hostnameClassifier,
  50. $this->ipAddressClassifier,
  51. $this->logger,
  52. );
  53. }
  54. public function dataValid(): array {
  55. return [
  56. ['nextcloud.com', true],
  57. ['com.one-.nextcloud-one.com', false],
  58. ];
  59. }
  60. /**
  61. * @dataProvider dataValid
  62. */
  63. public function testValid(string $host, bool $expected): void {
  64. $this->hostnameClassifier
  65. ->method('isLocalHostname')
  66. ->with($host)
  67. ->willReturn(false);
  68. $this->ipAddressClassifier
  69. ->method('isLocalAddress')
  70. ->with($host)
  71. ->willReturn(false);
  72. $valid = $this->validator->isValid($host);
  73. self::assertSame($expected, $valid);
  74. }
  75. public function testLocalHostname(): void {
  76. $host = 'localhost';
  77. $this->hostnameClassifier
  78. ->method('isLocalHostname')
  79. ->with($host)
  80. ->willReturn(true);
  81. $this->ipAddressClassifier
  82. ->method('isLocalAddress')
  83. ->with($host)
  84. ->willReturn(false);
  85. $valid = $this->validator->isValid($host);
  86. self::assertFalse($valid);
  87. }
  88. public function testLocalAddress(): void {
  89. $host = '10.0.0.10';
  90. $this->hostnameClassifier
  91. ->method('isLocalHostname')
  92. ->with($host)
  93. ->willReturn(false);
  94. $this->ipAddressClassifier
  95. ->method('isLocalAddress')
  96. ->with($host)
  97. ->willReturn(true);
  98. $valid = $this->validator->isValid($host);
  99. self::assertFalse($valid);
  100. }
  101. }