aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Security
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/Security')
-rw-r--r--tests/lib/Security/Bruteforce/Backend/MemoryCacheBackendTest.php8
-rw-r--r--tests/lib/Security/CSP/ContentSecurityPolicyManagerTest.php25
-rw-r--r--tests/lib/Security/CSRF/CsrfTokenGeneratorTest.php7
-rw-r--r--tests/lib/Security/CSRF/CsrfTokenManagerTest.php19
-rw-r--r--tests/lib/Security/CSRF/CsrfTokenTest.php8
-rw-r--r--tests/lib/Security/CSRF/TokenStorage/SessionStorageTest.php9
-rw-r--r--tests/lib/Security/CertificateManagerTest.php30
-rw-r--r--tests/lib/Security/CertificateTest.php2
-rw-r--r--tests/lib/Security/CredentialsManagerTest.php10
-rw-r--r--tests/lib/Security/CryptoTest.php10
-rw-r--r--tests/lib/Security/FeaturePolicy/FeaturePolicyManagerTest.php9
-rw-r--r--tests/lib/Security/HasherTest.php27
-rw-r--r--tests/lib/Security/Ip/BruteforceAllowListTest.php5
-rw-r--r--tests/lib/Security/Ip/RemoteAddressTest.php4
-rw-r--r--tests/lib/Security/Normalizer/IpAddressTest.php4
-rw-r--r--tests/lib/Security/RateLimiting/LimiterTest.php28
-rw-r--r--tests/lib/Security/RemoteHostValidatorIntegrationTest.php16
-rw-r--r--tests/lib/Security/RemoteHostValidatorTest.php6
-rw-r--r--tests/lib/Security/SecureRandomTest.php28
-rw-r--r--tests/lib/Security/TrustedDomainHelperTest.php6
20 files changed, 127 insertions, 134 deletions
diff --git a/tests/lib/Security/Bruteforce/Backend/MemoryCacheBackendTest.php b/tests/lib/Security/Bruteforce/Backend/MemoryCacheBackendTest.php
index 82bffb74e01..e0289fa7ca9 100644
--- a/tests/lib/Security/Bruteforce/Backend/MemoryCacheBackendTest.php
+++ b/tests/lib/Security/Bruteforce/Backend/MemoryCacheBackendTest.php
@@ -36,7 +36,7 @@ class MemoryCacheBackendTest extends TestCase {
$this->cacheFactory
->expects($this->once())
->method('createDistributed')
- ->with('OC\Security\Bruteforce\Backend\MemoryCacheBackend')
+ ->with(MemoryCacheBackend::class)
->willReturn($this->cache);
$this->backend = new MemoryCacheBackend(
@@ -55,7 +55,7 @@ class MemoryCacheBackendTest extends TestCase {
$this->assertSame(0, $this->backend->getAttempts('10.10.10.10/32', 0));
}
- public function dataGetAttempts(): array {
+ public static function dataGetAttempts(): array {
return [
[0, null, null, 4],
[100, null, null, 2],
@@ -67,9 +67,7 @@ class MemoryCacheBackendTest extends TestCase {
];
}
- /**
- * @dataProvider dataGetAttempts
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataGetAttempts')]
public function testGetAttempts(int $maxAge, ?string $action, ?array $metadata, int $expected): void {
$this->cache
->expects($this->once())
diff --git a/tests/lib/Security/CSP/ContentSecurityPolicyManagerTest.php b/tests/lib/Security/CSP/ContentSecurityPolicyManagerTest.php
index 63a5565e7fa..a32a4132287 100644
--- a/tests/lib/Security/CSP/ContentSecurityPolicyManagerTest.php
+++ b/tests/lib/Security/CSP/ContentSecurityPolicyManagerTest.php
@@ -11,8 +11,11 @@ declare(strict_types=1);
namespace Test\Security\CSP;
use OC\Security\CSP\ContentSecurityPolicyManager;
+use OCP\AppFramework\Http\ContentSecurityPolicy;
+use OCP\AppFramework\Http\EmptyContentSecurityPolicy;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Security\CSP\AddContentSecurityPolicyEvent;
+use OCP\Server;
use Test\TestCase;
class ContentSecurityPolicyManagerTest extends TestCase {
@@ -24,26 +27,26 @@ class ContentSecurityPolicyManagerTest extends TestCase {
protected function setUp(): void {
parent::setUp();
- $this->dispatcher = \OC::$server->query(IEventDispatcher::class);
+ $this->dispatcher = Server::get(IEventDispatcher::class);
$this->contentSecurityPolicyManager = new ContentSecurityPolicyManager($this->dispatcher);
}
public function testAddDefaultPolicy(): void {
- $this->contentSecurityPolicyManager->addDefaultPolicy(new \OCP\AppFramework\Http\ContentSecurityPolicy());
+ $this->contentSecurityPolicyManager->addDefaultPolicy(new ContentSecurityPolicy());
$this->addToAssertionCount(1);
}
public function testGetDefaultPolicyWithPolicies(): void {
- $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
+ $policy = new ContentSecurityPolicy();
$policy->addAllowedFontDomain('mydomain.com');
$policy->addAllowedImageDomain('anotherdomain.de');
$this->contentSecurityPolicyManager->addDefaultPolicy($policy);
- $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
+ $policy = new ContentSecurityPolicy();
$policy->addAllowedFontDomain('example.com');
$policy->addAllowedImageDomain('example.org');
$policy->allowEvalScript(true);
$this->contentSecurityPolicyManager->addDefaultPolicy($policy);
- $policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy();
+ $policy = new EmptyContentSecurityPolicy();
$policy->addAllowedChildSrcDomain('childdomain');
$policy->addAllowedFontDomain('anotherFontDomain');
$policy->addAllowedFormActionDomain('thirdDomain');
@@ -65,8 +68,8 @@ class ContentSecurityPolicyManagerTest extends TestCase {
}
public function testGetDefaultPolicyWithPoliciesViaEvent(): void {
- $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e) {
- $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
+ $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e): void {
+ $policy = new ContentSecurityPolicy();
$policy->addAllowedFontDomain('mydomain.com');
$policy->addAllowedImageDomain('anotherdomain.de');
$policy->useStrictDynamic(true);
@@ -75,16 +78,16 @@ class ContentSecurityPolicyManagerTest extends TestCase {
$e->addPolicy($policy);
});
- $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e) {
- $policy = new \OCP\AppFramework\Http\ContentSecurityPolicy();
+ $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e): void {
+ $policy = new ContentSecurityPolicy();
$policy->addAllowedFontDomain('example.com');
$policy->addAllowedImageDomain('example.org');
$policy->allowEvalScript(false);
$e->addPolicy($policy);
});
- $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e) {
- $policy = new \OCP\AppFramework\Http\EmptyContentSecurityPolicy();
+ $this->dispatcher->addListener(AddContentSecurityPolicyEvent::class, function (AddContentSecurityPolicyEvent $e): void {
+ $policy = new EmptyContentSecurityPolicy();
$policy->addAllowedChildSrcDomain('childdomain');
$policy->addAllowedFontDomain('anotherFontDomain');
$policy->addAllowedFormActionDomain('thirdDomain');
diff --git a/tests/lib/Security/CSRF/CsrfTokenGeneratorTest.php b/tests/lib/Security/CSRF/CsrfTokenGeneratorTest.php
index 98eddf602ee..86f458d8ea8 100644
--- a/tests/lib/Security/CSRF/CsrfTokenGeneratorTest.php
+++ b/tests/lib/Security/CSRF/CsrfTokenGeneratorTest.php
@@ -10,8 +10,11 @@ declare(strict_types=1);
namespace Test\Security\CSRF;
+use OC\Security\CSRF\CsrfTokenGenerator;
+use OCP\Security\ISecureRandom;
+
class CsrfTokenGeneratorTest extends \Test\TestCase {
- /** @var \OCP\Security\ISecureRandom */
+ /** @var ISecureRandom */
private $random;
/** @var \OC\Security\CSRF\CsrfTokenGenerator */
private $csrfTokenGenerator;
@@ -20,7 +23,7 @@ class CsrfTokenGeneratorTest extends \Test\TestCase {
parent::setUp();
$this->random = $this->getMockBuilder('\OCP\Security\ISecureRandom')
->disableOriginalConstructor()->getMock();
- $this->csrfTokenGenerator = new \OC\Security\CSRF\CsrfTokenGenerator($this->random);
+ $this->csrfTokenGenerator = new CsrfTokenGenerator($this->random);
}
public function testGenerateTokenWithCustomNumber(): void {
diff --git a/tests/lib/Security/CSRF/CsrfTokenManagerTest.php b/tests/lib/Security/CSRF/CsrfTokenManagerTest.php
index 47f873bfe13..66ee18475a4 100644
--- a/tests/lib/Security/CSRF/CsrfTokenManagerTest.php
+++ b/tests/lib/Security/CSRF/CsrfTokenManagerTest.php
@@ -10,6 +10,9 @@ declare(strict_types=1);
namespace Test\Security\CSRF;
+use OC\Security\CSRF\CsrfToken;
+use OC\Security\CSRF\CsrfTokenManager;
+
class CsrfTokenManagerTest extends \Test\TestCase {
/** @var \OC\Security\CSRF\CsrfTokenManager */
private $csrfTokenManager;
@@ -25,7 +28,7 @@ class CsrfTokenManagerTest extends \Test\TestCase {
$this->storageInterface = $this->getMockBuilder('\OC\Security\CSRF\TokenStorage\SessionStorage')
->disableOriginalConstructor()->getMock();
- $this->csrfTokenManager = new \OC\Security\CSRF\CsrfTokenManager(
+ $this->csrfTokenManager = new CsrfTokenManager(
$this->tokenGenerator,
$this->storageInterface
);
@@ -41,7 +44,7 @@ class CsrfTokenManagerTest extends \Test\TestCase {
->method('getToken')
->willReturn('MyExistingToken');
- $expected = new \OC\Security\CSRF\CsrfToken('MyExistingToken');
+ $expected = new CsrfToken('MyExistingToken');
$this->assertEquals($expected, $this->csrfTokenManager->getToken());
}
@@ -55,7 +58,7 @@ class CsrfTokenManagerTest extends \Test\TestCase {
->method('getToken')
->willReturn('MyExistingToken');
- $expected = new \OC\Security\CSRF\CsrfToken('MyExistingToken');
+ $expected = new CsrfToken('MyExistingToken');
$token = $this->csrfTokenManager->getToken();
$this->assertSame($token, $this->csrfTokenManager->getToken());
$this->assertSame($token, $this->csrfTokenManager->getToken());
@@ -75,7 +78,7 @@ class CsrfTokenManagerTest extends \Test\TestCase {
->method('setToken')
->with('MyNewToken');
- $expected = new \OC\Security\CSRF\CsrfToken('MyNewToken');
+ $expected = new CsrfToken('MyNewToken');
$this->assertEquals($expected, $this->csrfTokenManager->getToken());
}
@@ -89,7 +92,7 @@ class CsrfTokenManagerTest extends \Test\TestCase {
->method('setToken')
->with('MyNewToken');
- $expected = new \OC\Security\CSRF\CsrfToken('MyNewToken');
+ $expected = new CsrfToken('MyNewToken');
$this->assertEquals($expected, $this->csrfTokenManager->refreshToken());
}
@@ -106,7 +109,7 @@ class CsrfTokenManagerTest extends \Test\TestCase {
->expects($this->once())
->method('hasToken')
->willReturn(false);
- $token = new \OC\Security\CSRF\CsrfToken('Token');
+ $token = new CsrfToken('Token');
$this->assertSame(false, $this->csrfTokenManager->isTokenValid($token));
}
@@ -116,7 +119,7 @@ class CsrfTokenManagerTest extends \Test\TestCase {
->expects($this->once())
->method('hasToken')
->willReturn(true);
- $token = new \OC\Security\CSRF\CsrfToken('Token');
+ $token = new CsrfToken('Token');
$this->storageInterface
->expects($this->once())
->method('getToken')
@@ -134,7 +137,7 @@ class CsrfTokenManagerTest extends \Test\TestCase {
->expects($this->once())
->method('hasToken')
->willReturn(true);
- $token = new \OC\Security\CSRF\CsrfToken($tokenVal);
+ $token = new CsrfToken($tokenVal);
$this->storageInterface
->expects($this->once())
->method('getToken')
diff --git a/tests/lib/Security/CSRF/CsrfTokenTest.php b/tests/lib/Security/CSRF/CsrfTokenTest.php
index 9ecbbe9f23a..5b5ba5ae54f 100644
--- a/tests/lib/Security/CSRF/CsrfTokenTest.php
+++ b/tests/lib/Security/CSRF/CsrfTokenTest.php
@@ -10,15 +10,17 @@ declare(strict_types=1);
namespace Test\Security\CSRF;
+use OC\Security\CSRF\CsrfToken;
+
class CsrfTokenTest extends \Test\TestCase {
public function testGetEncryptedValue(): void {
- $csrfToken = new \OC\Security\CSRF\CsrfToken('MyCsrfToken');
+ $csrfToken = new CsrfToken('MyCsrfToken');
$this->assertSame(33, strlen($csrfToken->getEncryptedValue()));
$this->assertSame(':', $csrfToken->getEncryptedValue()[16]);
}
public function testGetEncryptedValueStaysSameOnSecondRequest(): void {
- $csrfToken = new \OC\Security\CSRF\CsrfToken('MyCsrfToken');
+ $csrfToken = new CsrfToken('MyCsrfToken');
$tokenValue = $csrfToken->getEncryptedValue();
$this->assertSame($tokenValue, $csrfToken->getEncryptedValue());
$this->assertSame($tokenValue, $csrfToken->getEncryptedValue());
@@ -29,7 +31,7 @@ class CsrfTokenTest extends \Test\TestCase {
$b = 'def';
$xorB64 = 'BQcF';
$tokenVal = sprintf('%s:%s', $xorB64, base64_encode($a));
- $csrfToken = new \OC\Security\CSRF\CsrfToken($tokenVal);
+ $csrfToken = new CsrfToken($tokenVal);
$this->assertSame($b, $csrfToken->getDecryptedValue());
}
}
diff --git a/tests/lib/Security/CSRF/TokenStorage/SessionStorageTest.php b/tests/lib/Security/CSRF/TokenStorage/SessionStorageTest.php
index f8386124626..2b2c4af0444 100644
--- a/tests/lib/Security/CSRF/TokenStorage/SessionStorageTest.php
+++ b/tests/lib/Security/CSRF/TokenStorage/SessionStorageTest.php
@@ -10,10 +10,11 @@ declare(strict_types=1);
namespace Test\Security\CSRF\TokenStorage;
+use OC\Security\CSRF\TokenStorage\SessionStorage;
use OCP\ISession;
class SessionStorageTest extends \Test\TestCase {
- /** @var \OCP\ISession */
+ /** @var ISession */
private $session;
/** @var \OC\Security\CSRF\TokenStorage\SessionStorage */
private $sessionStorage;
@@ -22,13 +23,13 @@ class SessionStorageTest extends \Test\TestCase {
parent::setUp();
$this->session = $this->getMockBuilder(ISession::class)
->disableOriginalConstructor()->getMock();
- $this->sessionStorage = new \OC\Security\CSRF\TokenStorage\SessionStorage($this->session);
+ $this->sessionStorage = new SessionStorage($this->session);
}
/**
* @return array
*/
- public function getTokenDataProvider() {
+ public static function getTokenDataProvider(): array {
return [
[
'',
@@ -41,9 +42,9 @@ class SessionStorageTest extends \Test\TestCase {
/**
* @param string $token
- * @dataProvider getTokenDataProvider
*
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('getTokenDataProvider')]
public function testGetTokenWithEmptyToken($token): void {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Session does not contain a requesttoken');
diff --git a/tests/lib/Security/CertificateManagerTest.php b/tests/lib/Security/CertificateManagerTest.php
index 1c168228b6a..4dadc824ef6 100644
--- a/tests/lib/Security/CertificateManagerTest.php
+++ b/tests/lib/Security/CertificateManagerTest.php
@@ -10,11 +10,16 @@ declare(strict_types=1);
namespace Test\Security;
+use OC\Files\Filesystem;
+use OC\Files\Storage\Temporary;
use OC\Files\View;
+use OC\Security\Certificate;
use OC\Security\CertificateManager;
use OCP\Files\InvalidPathException;
use OCP\IConfig;
+use OCP\IUserManager;
use OCP\Security\ISecureRandom;
+use OCP\Server;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
@@ -37,12 +42,12 @@ class CertificateManagerTest extends \Test\TestCase {
$this->username = $this->getUniqueID('', 20);
$this->createUser($this->username, '');
- $storage = new \OC\Files\Storage\Temporary();
+ $storage = new Temporary();
$this->registerMount($this->username, $storage, '/' . $this->username . '/');
\OC_Util::tearDownFS();
\OC_User::setUserId($this->username);
- \OC\Files\Filesystem::tearDown();
+ Filesystem::tearDown();
\OC_Util::setupFS($this->username);
$config = $this->createMock(IConfig::class);
@@ -54,7 +59,7 @@ class CertificateManagerTest extends \Test\TestCase {
->willReturn('random');
$this->certificateManager = new CertificateManager(
- new \OC\Files\View(),
+ new View(),
$config,
$this->createMock(LoggerInterface::class),
$this->random
@@ -62,7 +67,7 @@ class CertificateManagerTest extends \Test\TestCase {
}
protected function tearDown(): void {
- $user = \OC::$server->getUserManager()->get($this->username);
+ $user = Server::get(IUserManager::class)->get($this->username);
if ($user !== null) {
$user->delete();
}
@@ -83,12 +88,12 @@ class CertificateManagerTest extends \Test\TestCase {
// Add some certificates
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
$certificateStore = [];
- $certificateStore[] = new \OC\Security\Certificate(file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
+ $certificateStore[] = new Certificate(file_get_contents(__DIR__ . '/../../data/certificates/goodCertificate.crt'), 'GoodCertificate');
$this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
// Add another certificates
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt'), 'ExpiredCertificate');
- $certificateStore[] = new \OC\Security\Certificate(file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt'), 'ExpiredCertificate');
+ $certificateStore[] = new Certificate(file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt'), 'ExpiredCertificate');
$this->assertEqualsArrays($certificateStore, $this->certificateManager->listCertificates());
}
@@ -100,10 +105,7 @@ class CertificateManagerTest extends \Test\TestCase {
$this->certificateManager->addCertificate('InvalidCertificate', 'invalidCertificate');
}
- /**
- * @return array
- */
- public function dangerousFileProvider() {
+ public static function dangerousFileProvider(): array {
return [
['.htaccess'],
['../../foo.txt'],
@@ -112,9 +114,9 @@ class CertificateManagerTest extends \Test\TestCase {
}
/**
- * @dataProvider dangerousFileProvider
* @param string $filename
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('dangerousFileProvider')]
public function testAddDangerousFile($filename): void {
$this->expectException(InvalidPathException::class);
$this->certificateManager->addCertificate(file_get_contents(__DIR__ . '/../../data/certificates/expiredCertificate.crt'), $filename);
@@ -134,13 +136,13 @@ class CertificateManagerTest extends \Test\TestCase {
}
/**
- * @dataProvider dataTestNeedRebundling
*
* @param int $CaBundleMtime
* @param int $targetBundleMtime
* @param int $targetBundleExists
* @param bool $expected
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestNeedRebundling')]
public function testNeedRebundling($CaBundleMtime,
$targetBundleMtime,
$targetBundleExists,
@@ -153,7 +155,7 @@ class CertificateManagerTest extends \Test\TestCase {
/** @var CertificateManager | \PHPUnit\Framework\MockObject\MockObject $certificateManager */
$certificateManager = $this->getMockBuilder('OC\Security\CertificateManager')
->setConstructorArgs([$view, $config, $this->createMock(LoggerInterface::class), $this->random])
- ->setMethods(['getFilemtimeOfCaBundle', 'getCertificateBundle'])
+ ->onlyMethods(['getFilemtimeOfCaBundle', 'getCertificateBundle'])
->getMock();
$certificateManager->expects($this->any())->method('getFilemtimeOfCaBundle')
@@ -181,7 +183,7 @@ class CertificateManagerTest extends \Test\TestCase {
);
}
- public function dataTestNeedRebundling() {
+ public static function dataTestNeedRebundling(): array {
return [
//values: CaBundleMtime, targetBundleMtime, targetBundleExists, expected
diff --git a/tests/lib/Security/CertificateTest.php b/tests/lib/Security/CertificateTest.php
index 3b833974321..732b431d73e 100644
--- a/tests/lib/Security/CertificateTest.php
+++ b/tests/lib/Security/CertificateTest.php
@@ -31,7 +31,7 @@ class CertificateTest extends \Test\TestCase {
$this->expiredCertificate = new Certificate($expiredCertificate, 'ExpiredCertificate');
}
-
+
public function testBogusData(): void {
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Certificate could not get parsed.');
diff --git a/tests/lib/Security/CredentialsManagerTest.php b/tests/lib/Security/CredentialsManagerTest.php
index ce60a2f62a2..4dfe8c5681d 100644
--- a/tests/lib/Security/CredentialsManagerTest.php
+++ b/tests/lib/Security/CredentialsManagerTest.php
@@ -17,9 +17,7 @@ use OCP\Server;
* @group DB
*/
class CredentialsManagerTest extends \Test\TestCase {
- /**
- * @dataProvider credentialsProvider
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('credentialsProvider')]
public function testWithDB($userId, $identifier): void {
$credentialsManager = Server::get(ICredentialsManager::class);
@@ -34,9 +32,7 @@ class CredentialsManagerTest extends \Test\TestCase {
$this->assertSame(1, $removedRows);
}
- /**
- * @dataProvider credentialsProvider
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('credentialsProvider')]
public function testUpdate($userId, $identifier): void {
$credentialsManager = Server::get(ICredentialsManager::class);
@@ -50,7 +46,7 @@ class CredentialsManagerTest extends \Test\TestCase {
$this->assertSame($secretsRev, $received);
}
- public function credentialsProvider(): array {
+ public static function credentialsProvider(): array {
return [
[
'alice',
diff --git a/tests/lib/Security/CryptoTest.php b/tests/lib/Security/CryptoTest.php
index 79140b34456..0f8575ab0b5 100644
--- a/tests/lib/Security/CryptoTest.php
+++ b/tests/lib/Security/CryptoTest.php
@@ -11,9 +11,11 @@ declare(strict_types=1);
namespace Test\Security;
use OC\Security\Crypto;
+use OCP\IConfig;
+use OCP\Server;
class CryptoTest extends \Test\TestCase {
- public function defaultEncryptionProvider() {
+ public static function defaultEncryptionProvider(): array {
return [
['Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt.'],
[''],
@@ -26,12 +28,10 @@ class CryptoTest extends \Test\TestCase {
protected function setUp(): void {
parent::setUp();
- $this->crypto = new Crypto(\OC::$server->getConfig());
+ $this->crypto = new Crypto(Server::get(IConfig::class));
}
- /**
- * @dataProvider defaultEncryptionProvider
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('defaultEncryptionProvider')]
public function testDefaultEncrypt($stringToEncrypt): void {
$ciphertext = $this->crypto->encrypt($stringToEncrypt);
$this->assertEquals($stringToEncrypt, $this->crypto->decrypt($ciphertext));
diff --git a/tests/lib/Security/FeaturePolicy/FeaturePolicyManagerTest.php b/tests/lib/Security/FeaturePolicy/FeaturePolicyManagerTest.php
index 7386aa023a9..01624cb92d3 100644
--- a/tests/lib/Security/FeaturePolicy/FeaturePolicyManagerTest.php
+++ b/tests/lib/Security/FeaturePolicy/FeaturePolicyManagerTest.php
@@ -12,6 +12,7 @@ use OC\Security\FeaturePolicy\FeaturePolicyManager;
use OCP\AppFramework\Http\FeaturePolicy;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Security\FeaturePolicy\AddFeaturePolicyEvent;
+use OCP\Server;
use Test\TestCase;
class FeaturePolicyManagerTest extends TestCase {
@@ -23,7 +24,7 @@ class FeaturePolicyManagerTest extends TestCase {
protected function setUp(): void {
parent::setUp();
- $this->dispatcher = \OC::$server->query(IEventDispatcher::class);
+ $this->dispatcher = Server::get(IEventDispatcher::class);
$this->manager = new FeaturePolicyManager($this->dispatcher);
}
@@ -33,7 +34,7 @@ class FeaturePolicyManagerTest extends TestCase {
}
public function testGetDefaultPolicyWithPoliciesViaEvent(): void {
- $this->dispatcher->addListener(AddFeaturePolicyEvent::class, function (AddFeaturePolicyEvent $e) {
+ $this->dispatcher->addListener(AddFeaturePolicyEvent::class, function (AddFeaturePolicyEvent $e): void {
$policy = new FeaturePolicy();
$policy->addAllowedMicrophoneDomain('mydomain.com');
$policy->addAllowedPaymentDomain('mypaymentdomain.com');
@@ -41,7 +42,7 @@ class FeaturePolicyManagerTest extends TestCase {
$e->addPolicy($policy);
});
- $this->dispatcher->addListener(AddFeaturePolicyEvent::class, function (AddFeaturePolicyEvent $e) {
+ $this->dispatcher->addListener(AddFeaturePolicyEvent::class, function (AddFeaturePolicyEvent $e): void {
$policy = new FeaturePolicy();
$policy->addAllowedPaymentDomain('mydomainother.com');
$policy->addAllowedGeoLocationDomain('mylocation.here');
@@ -49,7 +50,7 @@ class FeaturePolicyManagerTest extends TestCase {
$e->addPolicy($policy);
});
- $this->dispatcher->addListener(AddFeaturePolicyEvent::class, function (AddFeaturePolicyEvent $e) {
+ $this->dispatcher->addListener(AddFeaturePolicyEvent::class, function (AddFeaturePolicyEvent $e): void {
$policy = new FeaturePolicy();
$policy->addAllowedAutoplayDomain('youtube.com');
diff --git a/tests/lib/Security/HasherTest.php b/tests/lib/Security/HasherTest.php
index 41cad7f323f..33130f86a73 100644
--- a/tests/lib/Security/HasherTest.php
+++ b/tests/lib/Security/HasherTest.php
@@ -17,10 +17,7 @@ use OCP\IConfig;
* Class HasherTest
*/
class HasherTest extends \Test\TestCase {
- /**
- * @return array
- */
- public function versionHashProvider() {
+ public static function versionHashProvider(): array {
return [
['asf32äà$$a.|3', null],
['asf32äà$$a.|3|5', null],
@@ -30,7 +27,7 @@ class HasherTest extends \Test\TestCase {
];
}
- public function hashProviders70_71(): array {
+ public static function hashProviders70_71(): array {
return [
// Valid SHA1 strings
['password', '5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8', true],
@@ -66,7 +63,7 @@ class HasherTest extends \Test\TestCase {
];
}
- public function hashProviders72(): array {
+ public static function hashProviders72(): array {
return [
// Valid ARGON2 hashes
['password', '2|$argon2i$v=19$m=1024,t=2,p=2$T3JGcEkxVFNOVktNSjZUcg$4/hyLtSejxNgAuzSFFV/HLM3qRQKBwEtKw61qPN4zWA', true],
@@ -83,7 +80,7 @@ class HasherTest extends \Test\TestCase {
];
}
- public function hashProviders73(): array {
+ public static function hashProviders73(): array {
return [
// Valid ARGON2ID hashes
['password', '2|$argon2id$v=19$m=65536,t=4,p=1$TEtIMnhUczliQzI0Y01WeA$BpMUDrApy25iagIogUAnlc0rNTPJmGs8lOEeVHujJ9Q', true],
@@ -127,18 +124,14 @@ class HasherTest extends \Test\TestCase {
$this->assertNotNull($hash);
}
- /**
- * @dataProvider versionHashProvider
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('versionHashProvider')]
public function testSplitHash($hash, $expected): void {
$relativePath = self::invokePrivate($this->hasher, 'splitHash', [$hash]);
$this->assertSame($expected, $relativePath);
}
- /**
- * @dataProvider hashProviders70_71
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('hashProviders70_71')]
public function testVerify($password, $hash, $expected): void {
$this->config
->expects($this->any())
@@ -154,9 +147,7 @@ class HasherTest extends \Test\TestCase {
$this->assertSame($expected, $result);
}
- /**
- * @dataProvider hashProviders72
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('hashProviders72')]
public function testVerifyArgon2i($password, $hash, $expected): void {
if (!\defined('PASSWORD_ARGON2I')) {
$this->markTestSkipped('Need ARGON2 support to test ARGON2 hashes');
@@ -166,9 +157,7 @@ class HasherTest extends \Test\TestCase {
$this->assertSame($expected, $result);
}
- /**
- * @dataProvider hashProviders73
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('hashProviders73')]
public function testVerifyArgon2id(string $password, string $hash, bool $expected): void {
if (!\defined('PASSWORD_ARGON2ID')) {
$this->markTestSkipped('Need ARGON2ID support to test ARGON2ID hashes');
diff --git a/tests/lib/Security/Ip/BruteforceAllowListTest.php b/tests/lib/Security/Ip/BruteforceAllowListTest.php
index f7ef9e3df72..0cffc128d64 100644
--- a/tests/lib/Security/Ip/BruteforceAllowListTest.php
+++ b/tests/lib/Security/Ip/BruteforceAllowListTest.php
@@ -42,7 +42,7 @@ class BruteforceAllowListTest extends TestCase {
);
}
- public function dataIsBypassListed(): array {
+ public static function dataIsBypassListed(): array {
return [
[
'10.10.10.10',
@@ -130,10 +130,9 @@ class BruteforceAllowListTest extends TestCase {
}
/**
- * @dataProvider dataIsBypassListed
- *
* @param string[] $allowList
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataIsBypassListed')]
public function testIsBypassListed(
string $ip,
array $allowList,
diff --git a/tests/lib/Security/Ip/RemoteAddressTest.php b/tests/lib/Security/Ip/RemoteAddressTest.php
index d1f621796fe..a6619cffe8e 100644
--- a/tests/lib/Security/Ip/RemoteAddressTest.php
+++ b/tests/lib/Security/Ip/RemoteAddressTest.php
@@ -25,8 +25,8 @@ class RemoteAddressTest extends \Test\TestCase {
/**
* @param mixed $allowedRanges
- * @dataProvider dataProvider
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataProvider')]
public function testAllowedIps(string $remoteIp, $allowedRanges, bool $expected): void {
$this->request
->method('getRemoteAddress')
@@ -44,7 +44,7 @@ class RemoteAddressTest extends \Test\TestCase {
/**
* @return array<string, mixed, bool>
*/
- public function dataProvider(): array {
+ public static function dataProvider(): array {
return [
// No IP (ie. CLI)
['', ['192.168.1.2/24'], true],
diff --git a/tests/lib/Security/Normalizer/IpAddressTest.php b/tests/lib/Security/Normalizer/IpAddressTest.php
index 33a8b4d28f1..f7adfb4a0dd 100644
--- a/tests/lib/Security/Normalizer/IpAddressTest.php
+++ b/tests/lib/Security/Normalizer/IpAddressTest.php
@@ -13,7 +13,7 @@ use OC\Security\Normalizer\IpAddress;
use Test\TestCase;
class IpAddressTest extends TestCase {
- public function subnetDataProvider() {
+ public static function subnetDataProvider(): array {
return [
[
'64.233.191.254',
@@ -55,11 +55,11 @@ class IpAddressTest extends TestCase {
}
/**
- * @dataProvider subnetDataProvider
*
* @param string $input
* @param string $expected
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('subnetDataProvider')]
public function testGetSubnet($input, $expected): void {
$this->assertSame($expected, (new IpAddress($input))->getSubnet());
}
diff --git a/tests/lib/Security/RateLimiting/LimiterTest.php b/tests/lib/Security/RateLimiting/LimiterTest.php
index 92600e22de8..b19d5c6feba 100644
--- a/tests/lib/Security/RateLimiting/LimiterTest.php
+++ b/tests/lib/Security/RateLimiting/LimiterTest.php
@@ -10,29 +10,35 @@ declare(strict_types=1);
namespace Test\Security\RateLimiting;
use OC\Security\RateLimiting\Backend\IBackend;
+use OC\Security\RateLimiting\Exception\RateLimitExceededException;
use OC\Security\RateLimiting\Limiter;
use OCP\IUser;
+use OCP\Security\RateLimiting\ILimiter;
+use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Log\LoggerInterface;
use Test\TestCase;
class LimiterTest extends TestCase {
- /** @var IBackend|\PHPUnit\Framework\MockObject\MockObject */
- private $backend;
- /** @var Limiter */
- private $limiter;
+
+ private IBackend&MockObject $backend;
+ private ILimiter $limiter;
+ private LoggerInterface $logger;
protected function setUp(): void {
parent::setUp();
$this->backend = $this->createMock(IBackend::class);
+ $this->logger = $this->createMock(LoggerInterface::class);
$this->limiter = new Limiter(
- $this->backend
+ $this->backend,
+ $this->logger,
);
}
public function testRegisterAnonRequestExceeded(): void {
- $this->expectException(\OC\Security\RateLimiting\Exception\RateLimitExceededException::class);
+ $this->expectException(RateLimitExceededException::class);
$this->expectExceptionMessage('Rate limit exceeded');
$this->backend
@@ -43,6 +49,8 @@ class LimiterTest extends TestCase {
'4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47'
)
->willReturn(101);
+ $this->logger->expects($this->once())
+ ->method('info');
$this->limiter->registerAnonRequest('MyIdentifier', 100, 100, '127.0.0.1');
}
@@ -64,13 +72,15 @@ class LimiterTest extends TestCase {
'4664f0d9c88dcb7552be47b37bb52ce35977b2e60e1ac13757cf625f31f87050a41f3da064887fa87d49fd042e4c8eb20de8f10464877d3959677ab011b73a47',
100
);
+ $this->logger->expects($this->never())
+ ->method('info');
$this->limiter->registerAnonRequest('MyIdentifier', 100, 100, '127.0.0.1');
}
public function testRegisterUserRequestExceeded(): void {
- $this->expectException(\OC\Security\RateLimiting\Exception\RateLimitExceededException::class);
+ $this->expectException(RateLimitExceededException::class);
$this->expectExceptionMessage('Rate limit exceeded');
/** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */
@@ -87,6 +97,8 @@ class LimiterTest extends TestCase {
'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805'
)
->willReturn(101);
+ $this->logger->expects($this->once())
+ ->method('info');
$this->limiter->registerUserRequest('MyIdentifier', 100, 100, $user);
}
@@ -115,6 +127,8 @@ class LimiterTest extends TestCase {
'ddb2ec50fa973fd49ecf3d816f677c8095143e944ad10485f30fb3dac85c13a346dace4dae2d0a15af91867320957bfd38a43d9eefbb74fe6919e15119b6d805',
100
);
+ $this->logger->expects($this->never())
+ ->method('info');
$this->limiter->registerUserRequest('MyIdentifier', 100, 100, $user);
}
diff --git a/tests/lib/Security/RemoteHostValidatorIntegrationTest.php b/tests/lib/Security/RemoteHostValidatorIntegrationTest.php
index 6cddb8b4ce9..913acfa054d 100644
--- a/tests/lib/Security/RemoteHostValidatorIntegrationTest.php
+++ b/tests/lib/Security/RemoteHostValidatorIntegrationTest.php
@@ -37,7 +37,7 @@ class RemoteHostValidatorIntegrationTest extends TestCase {
);
}
- public function localHostsData(): array {
+ public static function localHostsData(): array {
return [
['[::1]'],
['[::]'],
@@ -73,9 +73,7 @@ class RemoteHostValidatorIntegrationTest extends TestCase {
];
}
- /**
- * @dataProvider localHostsData
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('localHostsData')]
public function testLocalHostsWhenNotAllowed(string $host): void {
$this->config
->method('getSystemValueBool')
@@ -87,9 +85,7 @@ class RemoteHostValidatorIntegrationTest extends TestCase {
self::assertFalse($isValid);
}
- /**
- * @dataProvider localHostsData
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('localHostsData')]
public function testLocalHostsWhenAllowed(string $host): void {
$this->config
->method('getSystemValueBool')
@@ -101,7 +97,7 @@ class RemoteHostValidatorIntegrationTest extends TestCase {
self::assertTrue($isValid);
}
- public function externalAddressesData():array {
+ public static function externalAddressesData():array {
return [
['8.8.8.8'],
['8.8.4.4'],
@@ -111,9 +107,7 @@ class RemoteHostValidatorIntegrationTest extends TestCase {
];
}
- /**
- * @dataProvider externalAddressesData
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('externalAddressesData')]
public function testExternalHost(string $host): void {
$this->config
->method('getSystemValueBool')
diff --git a/tests/lib/Security/RemoteHostValidatorTest.php b/tests/lib/Security/RemoteHostValidatorTest.php
index c698ebbc3c6..b048b9dafd1 100644
--- a/tests/lib/Security/RemoteHostValidatorTest.php
+++ b/tests/lib/Security/RemoteHostValidatorTest.php
@@ -44,16 +44,14 @@ class RemoteHostValidatorTest extends TestCase {
);
}
- public function dataValid(): array {
+ public static function dataValid(): array {
return [
['nextcloud.com', true],
['com.one-.nextcloud-one.com', false],
];
}
- /**
- * @dataProvider dataValid
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataValid')]
public function testValid(string $host, bool $expected): void {
$this->hostnameClassifier
->method('isLocalHostname')
diff --git a/tests/lib/Security/SecureRandomTest.php b/tests/lib/Security/SecureRandomTest.php
index 8000917579b..954fd85eaf1 100644
--- a/tests/lib/Security/SecureRandomTest.php
+++ b/tests/lib/Security/SecureRandomTest.php
@@ -13,7 +13,7 @@ namespace Test\Security;
use OC\Security\SecureRandom;
class SecureRandomTest extends \Test\TestCase {
- public function stringGenerationProvider() {
+ public static function stringGenerationProvider(): array {
return [
[1, 1],
[128, 128],
@@ -24,7 +24,7 @@ class SecureRandomTest extends \Test\TestCase {
];
}
- public static function charCombinations() {
+ public static function charCombinations(): array {
return [
['CHAR_LOWER', '[a-z]'],
['CHAR_UPPER', '[A-Z]'],
@@ -37,37 +37,29 @@ class SecureRandomTest extends \Test\TestCase {
protected function setUp(): void {
parent::setUp();
- $this->rng = new \OC\Security\SecureRandom();
+ $this->rng = new SecureRandom();
}
- /**
- * @dataProvider stringGenerationProvider
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('stringGenerationProvider')]
public function testGetLowStrengthGeneratorLength($length, $expectedLength): void {
$generator = $this->rng;
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
}
- /**
- * @dataProvider stringGenerationProvider
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('stringGenerationProvider')]
public function testMediumLowStrengthGeneratorLength($length, $expectedLength): void {
$generator = $this->rng;
$this->assertEquals($expectedLength, strlen($generator->generate($length)));
}
- /**
- * @dataProvider stringGenerationProvider
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('stringGenerationProvider')]
public function testUninitializedGenerate($length, $expectedLength): void {
$this->assertEquals($expectedLength, strlen($this->rng->generate($length)));
}
- /**
- * @dataProvider charCombinations
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('charCombinations')]
public function testScheme($charName, $chars): void {
$generator = $this->rng;
$scheme = constant('OCP\Security\ISecureRandom::' . $charName);
@@ -76,16 +68,14 @@ class SecureRandomTest extends \Test\TestCase {
$this->assertSame(1, $matchesRegex);
}
- public static function invalidLengths() {
+ public static function invalidLengths(): array {
return [
[0],
[-1],
];
}
- /**
- * @dataProvider invalidLengths
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('invalidLengths')]
public function testInvalidLengths($length): void {
$this->expectException(\LengthException::class);
$generator = $this->rng;
diff --git a/tests/lib/Security/TrustedDomainHelperTest.php b/tests/lib/Security/TrustedDomainHelperTest.php
index 656b7a77206..8b671a93d06 100644
--- a/tests/lib/Security/TrustedDomainHelperTest.php
+++ b/tests/lib/Security/TrustedDomainHelperTest.php
@@ -27,11 +27,11 @@ class TrustedDomainHelperTest extends \Test\TestCase {
}
/**
- * @dataProvider trustedDomainDataProvider
* @param string $trustedDomains
* @param string $testDomain
* @param bool $result
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('trustedDomainDataProvider')]
public function testIsTrustedUrl($trustedDomains, $testDomain, $result): void {
$this->config->method('getSystemValue')
->willReturnMap([
@@ -44,11 +44,11 @@ class TrustedDomainHelperTest extends \Test\TestCase {
}
/**
- * @dataProvider trustedDomainDataProvider
* @param string $trustedDomains
* @param string $testDomain
* @param bool $result
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('trustedDomainDataProvider')]
public function testIsTrustedDomain($trustedDomains, $testDomain, $result): void {
$this->config->method('getSystemValue')
->willReturnMap([
@@ -63,7 +63,7 @@ class TrustedDomainHelperTest extends \Test\TestCase {
/**
* @return array
*/
- public function trustedDomainDataProvider() {
+ public static function trustedDomainDataProvider(): array {
$trustedHostTestList = [
'host.one.test',
'host.two.test',