Files
nextcloud-server-mirror/tests/lib/Security/RemoteHostValidatorTest.php
Côme Chilliet 611180d728 chore: Replace $this->mocks[… by $this->getAutoMock(… in tests
This is clearer, safer and better typed.

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
2026-10-01 15:45:42 +02:00

82 lines
1.9 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace lib\Security;
use OC\Net\HostnameClassifier;
use OC\Net\IpAddressClassifier;
use OC\Security\RemoteHostValidator;
use Test\TestCase;
class RemoteHostValidatorTest extends TestCase {
private RemoteHostValidator $validator;
#[\Override]
protected function setUp(): void {
parent::setUp();
$this->validator = $this->createInstanceWithMocks(RemoteHostValidator::class);
}
public static function dataValid(): array {
return [
['nextcloud.com', true],
['com.one-.nextcloud-one.com', false],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('dataValid')]
public function testValid(string $host, bool $expected): void {
$this->getAutoMock(HostnameClassifier::class)
->method('isLocalHostname')
->with($host)
->willReturn(false);
$this->getAutoMock(IpAddressClassifier::class)
->method('isLocalAddress')
->with($host)
->willReturn(false);
$valid = $this->validator->isValid($host);
self::assertSame($expected, $valid);
}
public function testLocalHostname(): void {
$host = 'localhost';
$this->getAutoMock(HostnameClassifier::class)
->method('isLocalHostname')
->with($host)
->willReturn(true);
$this->getAutoMock(IpAddressClassifier::class)
->method('isLocalAddress')
->with($host)
->willReturn(false);
$valid = $this->validator->isValid($host);
self::assertFalse($valid);
}
public function testLocalAddress(): void {
$host = '10.0.0.10';
$this->getAutoMock(HostnameClassifier::class)
->method('isLocalHostname')
->with($host)
->willReturn(false);
$this->getAutoMock(IpAddressClassifier::class)
->method('isLocalAddress')
->with($host)
->willReturn(true);
$valid = $this->validator->isValid($host);
self::assertFalse($valid);
}
}