1
0

RemoteHostValidatorTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 testValid(): void {
  55. $host = 'nextcloud.com';
  56. $this->hostnameClassifier
  57. ->method('isLocalHostname')
  58. ->with($host)
  59. ->willReturn(false);
  60. $this->ipAddressClassifier
  61. ->method('isLocalAddress')
  62. ->with($host)
  63. ->willReturn(false);
  64. $valid = $this->validator->isValid($host);
  65. self::assertTrue($valid);
  66. }
  67. public function testLocalHostname(): void {
  68. $host = 'localhost';
  69. $this->hostnameClassifier
  70. ->method('isLocalHostname')
  71. ->with($host)
  72. ->willReturn(true);
  73. $this->ipAddressClassifier
  74. ->method('isLocalAddress')
  75. ->with($host)
  76. ->willReturn(false);
  77. $valid = $this->validator->isValid($host);
  78. self::assertFalse($valid);
  79. }
  80. public function testLocalAddress(): void {
  81. $host = '10.0.0.10';
  82. $this->hostnameClassifier
  83. ->method('isLocalHostname')
  84. ->with($host)
  85. ->willReturn(false);
  86. $this->ipAddressClassifier
  87. ->method('isLocalAddress')
  88. ->with($host)
  89. ->willReturn(true);
  90. $valid = $this->validator->isValid($host);
  91. self::assertFalse($valid);
  92. }
  93. }