aboutsummaryrefslogtreecommitdiffstats
path: root/apps/encryption/tests/Crypto
diff options
context:
space:
mode:
Diffstat (limited to 'apps/encryption/tests/Crypto')
-rw-r--r--apps/encryption/tests/Crypto/CryptTest.php163
-rw-r--r--apps/encryption/tests/Crypto/DecryptAllTest.php34
-rw-r--r--apps/encryption/tests/Crypto/EncryptAllTest.php202
-rw-r--r--apps/encryption/tests/Crypto/EncryptionTest.php101
4 files changed, 222 insertions, 278 deletions
diff --git a/apps/encryption/tests/Crypto/CryptTest.php b/apps/encryption/tests/Crypto/CryptTest.php
index 738dfe0a2a0..1355e2c855d 100644
--- a/apps/encryption/tests/Crypto/CryptTest.php
+++ b/apps/encryption/tests/Crypto/CryptTest.php
@@ -1,5 +1,7 @@
<?php
+declare(strict_types=1);
+
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -8,27 +10,21 @@
namespace OCA\Encryption\Tests\Crypto;
use OCA\Encryption\Crypto\Crypt;
+use OCP\Encryption\Exceptions\GenericEncryptionException;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IUserSession;
+use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class CryptTest extends TestCase {
- /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
- private $logger;
-
- /** @var \OCP\IUserSession|\PHPUnit\Framework\MockObject\MockObject */
- private $userSession;
-
- /** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
- private $config;
-
- /** @var \OCP\IL10N|\PHPUnit\Framework\MockObject\MockObject */
- private $l;
+ protected LoggerInterface&MockObject $logger;
+ protected IUserSession&MockObject $userSession;
+ protected IConfig&MockObject $config;
+ protected IL10N&MockObject $l;
- /** @var Crypt */
- private $crypt;
+ protected Crypt $crypt;
protected function setUp(): void {
parent::setUp();
@@ -37,8 +33,7 @@ class CryptTest extends TestCase {
->disableOriginalConstructor()
->getMock();
$this->logger->expects($this->any())
- ->method('warning')
- ->willReturn(true);
+ ->method('warning');
$this->userSession = $this->getMockBuilder(IUserSession::class)
->disableOriginalConstructor()
->getMock();
@@ -53,7 +48,7 @@ class CryptTest extends TestCase {
/**
* test getOpenSSLConfig without any additional parameters
*/
- public function testGetOpenSSLConfigBasic() {
+ public function testGetOpenSSLConfigBasic(): void {
$this->config->expects($this->once())
->method('getSystemValue')
->with($this->equalTo('openssl'), $this->equalTo([]))
@@ -68,7 +63,7 @@ class CryptTest extends TestCase {
/**
* test getOpenSSLConfig with additional parameters defined in config.php
*/
- public function testGetOpenSSLConfig() {
+ public function testGetOpenSSLConfig(): void {
$this->config->expects($this->once())
->method('getSystemValue')
->with($this->equalTo('openssl'), $this->equalTo([]))
@@ -85,10 +80,9 @@ class CryptTest extends TestCase {
/**
* test generateHeader with valid key formats
- *
- * @dataProvider dataTestGenerateHeader
*/
- public function testGenerateHeader($keyFormat, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGenerateHeader')]
+ public function testGenerateHeader($keyFormat, $expected): void {
$this->config->expects($this->once())
->method('getSystemValueString')
->with($this->equalTo('cipher'), $this->equalTo('AES-256-CTR'))
@@ -107,16 +101,13 @@ class CryptTest extends TestCase {
* test generateHeader with invalid key format
*
*/
- public function testGenerateHeaderInvalid() {
+ public function testGenerateHeaderInvalid(): void {
$this->expectException(\InvalidArgumentException::class);
$this->crypt->generateHeader('unknown');
}
- /**
- * @return array
- */
- public function dataTestGenerateHeader() {
+ public static function dataTestGenerateHeader(): array {
return [
[null, 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash2:encoding:binary:HEND'],
['password', 'HBEGIN:cipher:AES-128-CFB:keyFormat:password:encoding:binary:HEND'],
@@ -124,11 +115,11 @@ class CryptTest extends TestCase {
];
}
- public function testGetCipherWithInvalidCipher() {
+ public function testGetCipherWithInvalidCipher(): void {
$this->config->expects($this->once())
- ->method('getSystemValueString')
- ->with($this->equalTo('cipher'), $this->equalTo('AES-256-CTR'))
- ->willReturn('Not-Existing-Cipher');
+ ->method('getSystemValueString')
+ ->with($this->equalTo('cipher'), $this->equalTo('AES-256-CTR'))
+ ->willReturn('Not-Existing-Cipher');
$this->logger
->expects($this->once())
->method('warning')
@@ -138,11 +129,11 @@ class CryptTest extends TestCase {
}
/**
- * @dataProvider dataProviderGetCipher
* @param string $configValue
* @param string $expected
*/
- public function testGetCipher($configValue, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderGetCipher')]
+ public function testGetCipher($configValue, $expected): void {
$this->config->expects($this->once())
->method('getSystemValueString')
->with($this->equalTo('cipher'), $this->equalTo('AES-256-CTR'))
@@ -155,10 +146,8 @@ class CryptTest extends TestCase {
/**
* data provider for testGetCipher
- *
- * @return array
*/
- public function dataProviderGetCipher() {
+ public static function dataProviderGetCipher(): array {
return [
['AES-128-CFB', 'AES-128-CFB'],
['AES-256-CFB', 'AES-256-CFB'],
@@ -172,7 +161,7 @@ class CryptTest extends TestCase {
/**
* test concatIV()
*/
- public function testConcatIV() {
+ public function testConcatIV(): void {
$result = self::invokePrivate(
$this->crypt,
'concatIV',
@@ -183,10 +172,8 @@ class CryptTest extends TestCase {
);
}
- /**
- * @dataProvider dataTestSplitMetaData
- */
- public function testSplitMetaData($data, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestSplitMetaData')]
+ public function testSplitMetaData($data, $expected): void {
$this->config->method('getSystemValueBool')
->with('encryption_skip_signature_check', false)
->willReturn(true);
@@ -201,7 +188,7 @@ class CryptTest extends TestCase {
$this->assertSame($expected['signature'], $result['signature']);
}
- public function dataTestSplitMetaData() {
+ public static function dataTestSplitMetaData(): array {
return [
['encryptedContent00iv001234567890123456xx',
['encrypted' => 'encryptedContent', 'iv' => '1234567890123456', 'signature' => false]],
@@ -210,10 +197,8 @@ class CryptTest extends TestCase {
];
}
- /**
- * @dataProvider dataTestHasSignature
- */
- public function testHasSignature($data, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestHasSignature')]
+ public function testHasSignature($data, $expected): void {
$this->config->method('getSystemValueBool')
->with('encryption_skip_signature_check', false)
->willReturn(true);
@@ -222,24 +207,22 @@ class CryptTest extends TestCase {
);
}
- public function dataTestHasSignature() {
+ public static function dataTestHasSignature(): array {
return [
['encryptedContent00iv001234567890123456xx', false],
['encryptedContent00iv00123456789012345600sig00e1992521e437f6915f9173b190a512cfc38a00ac24502db44e0ba10c2bb0cc86xxx', true]
];
}
- /**
- * @dataProvider dataTestHasSignatureFail
- */
- public function testHasSignatureFail($cipher) {
- $this->expectException(\OCP\Encryption\Exceptions\GenericEncryptionException::class);
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestHasSignatureFail')]
+ public function testHasSignatureFail($cipher): void {
+ $this->expectException(GenericEncryptionException::class);
$data = 'encryptedContent00iv001234567890123456xx';
$this->invokePrivate($this->crypt, 'hasSignature', [$data, $cipher]);
}
- public function dataTestHasSignatureFail() {
+ public static function dataTestHasSignatureFail(): array {
return [
['AES-256-CTR'],
['aes-256-ctr'],
@@ -251,7 +234,7 @@ class CryptTest extends TestCase {
/**
* test addPadding()
*/
- public function testAddPadding() {
+ public function testAddPadding(): void {
$result = self::invokePrivate($this->crypt, 'addPadding', ['data']);
$this->assertSame('dataxxx', $result);
}
@@ -259,21 +242,19 @@ class CryptTest extends TestCase {
/**
* test removePadding()
*
- * @dataProvider dataProviderRemovePadding
* @param $data
* @param $expected
*/
- public function testRemovePadding($data, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderRemovePadding')]
+ public function testRemovePadding($data, $expected): void {
$result = self::invokePrivate($this->crypt, 'removePadding', [$data]);
$this->assertSame($expected, $result);
}
/**
* data provider for testRemovePadding
- *
- * @return array
*/
- public function dataProviderRemovePadding() {
+ public static function dataProviderRemovePadding(): array {
return [
['dataxx', 'data'],
['data', false]
@@ -283,7 +264,7 @@ class CryptTest extends TestCase {
/**
* test parseHeader()
*/
- public function testParseHeader() {
+ public function testParseHeader(): void {
$header = 'HBEGIN:foo:bar:cipher:AES-256-CFB:encoding:binary:HEND';
$result = self::invokePrivate($this->crypt, 'parseHeader', [$header]);
@@ -328,7 +309,7 @@ class CryptTest extends TestCase {
*
* @depends testEncrypt
*/
- public function testDecrypt($data) {
+ public function testDecrypt($data): void {
$result = self::invokePrivate(
$this->crypt,
'decrypt',
@@ -339,10 +320,9 @@ class CryptTest extends TestCase {
/**
* test return values of valid ciphers
- *
- * @dataProvider dataTestGetKeySize
*/
- public function testGetKeySize($cipher, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetKeySize')]
+ public function testGetKeySize($cipher, $expected): void {
$result = $this->invokePrivate($this->crypt, 'getKeySize', [$cipher]);
$this->assertSame($expected, $result);
}
@@ -351,16 +331,13 @@ class CryptTest extends TestCase {
* test exception if cipher is unknown
*
*/
- public function testGetKeySizeFailure() {
+ public function testGetKeySizeFailure(): void {
$this->expectException(\InvalidArgumentException::class);
$this->invokePrivate($this->crypt, 'getKeySize', ['foo']);
}
- /**
- * @return array
- */
- public function dataTestGetKeySize() {
+ public static function dataTestGetKeySize(): array {
return [
['AES-256-CFB', 32],
['AES-128-CFB', 16],
@@ -369,33 +346,28 @@ class CryptTest extends TestCase {
];
}
- /**
- * @dataProvider dataTestDecryptPrivateKey
- */
- public function testDecryptPrivateKey($header, $privateKey, $expectedCipher, $isValidKey, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestDecryptPrivateKey')]
+ public function testDecryptPrivateKey($header, $privateKey, $expectedCipher, $isValidKey, $expected): void {
$this->config->method('getSystemValueBool')
- ->withConsecutive(['encryption.legacy_format_support', false],
- ['encryption.use_legacy_base64_encoding', false])
- ->willReturnOnConsecutiveCalls(true, false);
+ ->willReturnMap([
+ ['encryption.legacy_format_support', false, true],
+ ['encryption.use_legacy_base64_encoding', false, false],
+ ]);
- /** @var \OCA\Encryption\Crypto\Crypt | \PHPUnit\Framework\MockObject\MockObject $crypt */
+ /** @var Crypt|\PHPUnit\Framework\MockObject\MockObject $crypt */
$crypt = $this->getMockBuilder(Crypt::class)
- ->setConstructorArgs(
- [
- $this->logger,
- $this->userSession,
- $this->config,
- $this->l
- ]
- )
- ->setMethods(
- [
- 'parseHeader',
- 'generatePasswordHash',
- 'symmetricDecryptFileContent',
- 'isValidPrivateKey'
- ]
- )
+ ->setConstructorArgs([
+ $this->logger,
+ $this->userSession,
+ $this->config,
+ $this->l
+ ])
+ ->onlyMethods([
+ 'parseHeader',
+ 'generatePasswordHash',
+ 'symmetricDecryptFileContent',
+ 'isValidPrivateKey'
+ ])
->getMock();
$crypt->expects($this->once())->method('parseHeader')->willReturn($header);
@@ -416,10 +388,7 @@ class CryptTest extends TestCase {
$this->assertSame($expected, $result);
}
- /**
- * @return array
- */
- public function dataTestDecryptPrivateKey() {
+ public static function dataTestDecryptPrivateKey(): array {
return [
[['cipher' => 'AES-128-CFB', 'keyFormat' => 'password'], 'HBEGIN:HENDprivateKey', 'AES-128-CFB', true, 'key'],
[['cipher' => 'AES-256-CFB', 'keyFormat' => 'password'], 'HBEGIN:HENDprivateKey', 'AES-256-CFB', true, 'key'],
@@ -430,7 +399,7 @@ class CryptTest extends TestCase {
];
}
- public function testIsValidPrivateKey() {
+ public function testIsValidPrivateKey(): void {
$res = openssl_pkey_new();
openssl_pkey_export($res, $privateKey);
@@ -445,7 +414,7 @@ class CryptTest extends TestCase {
);
}
- public function testMultiKeyEncrypt() {
+ public function testMultiKeyEncrypt(): void {
$res = openssl_pkey_new();
openssl_pkey_export($res, $privateKey);
$publicKeyPem = openssl_pkey_get_details($res)['key'];
diff --git a/apps/encryption/tests/Crypto/DecryptAllTest.php b/apps/encryption/tests/Crypto/DecryptAllTest.php
index 7723fd5ce4f..82e6100bce5 100644
--- a/apps/encryption/tests/Crypto/DecryptAllTest.php
+++ b/apps/encryption/tests/Crypto/DecryptAllTest.php
@@ -1,5 +1,7 @@
<?php
+declare(strict_types=1);
+
/**
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -12,28 +14,19 @@ use OCA\Encryption\Crypto\DecryptAll;
use OCA\Encryption\KeyManager;
use OCA\Encryption\Session;
use OCA\Encryption\Util;
+use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Console\Helper\QuestionHelper;
use Test\TestCase;
class DecryptAllTest extends TestCase {
- /** @var DecryptAll */
- protected $instance;
-
- /** @var Util | \PHPUnit\Framework\MockObject\MockObject */
- protected $util;
-
- /** @var KeyManager | \PHPUnit\Framework\MockObject\MockObject */
- protected $keyManager;
-
- /** @var Crypt | \PHPUnit\Framework\MockObject\MockObject */
- protected $crypt;
-
- /** @var Session | \PHPUnit\Framework\MockObject\MockObject */
- protected $session;
+ protected DecryptAll $instance;
- /** @var QuestionHelper | \PHPUnit\Framework\MockObject\MockObject */
- protected $questionHelper;
+ protected Util&MockObject $util;
+ protected KeyManager&MockObject $keyManager;
+ protected Crypt&MockObject $crypt;
+ protected Session&MockObject $session;
+ protected QuestionHelper&MockObject $questionHelper;
protected function setUp(): void {
parent::setUp();
@@ -58,7 +51,7 @@ class DecryptAllTest extends TestCase {
);
}
- public function testUpdateSession() {
+ public function testUpdateSession(): void {
$this->session->expects($this->once())->method('prepareDecryptAll')
->with('user1', 'key1');
@@ -66,15 +59,16 @@ class DecryptAllTest extends TestCase {
}
/**
- * @dataProvider dataTestGetPrivateKey
*
* @param string $user
* @param string $recoveryKeyId
*/
- public function testGetPrivateKey($user, $recoveryKeyId, $masterKeyId) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestGetPrivateKey')]
+ public function testGetPrivateKey($user, $recoveryKeyId, $masterKeyId): void {
$password = 'passwd';
$recoveryKey = 'recoveryKey';
$userKey = 'userKey';
+ $masterKey = 'userKey';
$unencryptedKey = 'unencryptedKey';
$this->keyManager->expects($this->any())->method('getRecoveryKeyId')
@@ -105,7 +99,7 @@ class DecryptAllTest extends TestCase {
);
}
- public function dataTestGetPrivateKey() {
+ public static function dataTestGetPrivateKey() {
return [
['user1', 'recoveryKey', 'masterKeyId'],
['recoveryKeyId', 'recoveryKeyId', 'masterKeyId'],
diff --git a/apps/encryption/tests/Crypto/EncryptAllTest.php b/apps/encryption/tests/Crypto/EncryptAllTest.php
index ec05c422dd4..c56e3375a73 100644
--- a/apps/encryption/tests/Crypto/EncryptAllTest.php
+++ b/apps/encryption/tests/Crypto/EncryptAllTest.php
@@ -1,5 +1,7 @@
<?php
+declare(strict_types=1);
+
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -20,6 +22,8 @@ use OCP\L10N\IFactory;
use OCP\Mail\IMailer;
use OCP\Security\ISecureRandom;
use OCP\UserInterface;
+use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\QuestionHelper;
@@ -29,50 +33,23 @@ use Test\TestCase;
class EncryptAllTest extends TestCase {
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Encryption\KeyManager */
- protected $keyManager;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Encryption\Util */
- protected $util;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IUserManager */
- protected $userManager;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OCA\Encryption\Users\Setup */
- protected $setupUser;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OC\Files\View */
- protected $view;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IConfig */
- protected $config;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Mail\IMailer */
- protected $mailer;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\IL10N */
- protected $l;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | IFactory */
- protected $l10nFactory;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
- protected $questionHelper;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */
- protected $inputInterface;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */
- protected $outputInterface;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\UserInterface */
- protected $userInterface;
-
- /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Security\ISecureRandom */
- protected $secureRandom;
-
- /** @var EncryptAll */
- protected $encryptAll;
+ protected KeyManager&MockObject $keyManager;
+ protected Util&MockObject $util;
+ protected IUserManager&MockObject $userManager;
+ protected Setup&MockObject $setupUser;
+ protected View&MockObject $view;
+ protected IConfig&MockObject $config;
+ protected IMailer&MockObject $mailer;
+ protected IL10N&MockObject $l;
+ protected IFactory&MockObject $l10nFactory;
+ protected \Symfony\Component\Console\Helper\QuestionHelper&MockObject $questionHelper;
+ protected \Symfony\Component\Console\Input\InputInterface&MockObject $inputInterface;
+ protected \Symfony\Component\Console\Output\OutputInterface&MockObject $outputInterface;
+ protected UserInterface&MockObject $userInterface;
+ protected ISecureRandom&MockObject $secureRandom;
+ protected LoggerInterface&MockObject $logger;
+
+ protected EncryptAll $encryptAll;
protected function setUp(): void {
parent::setUp();
@@ -101,10 +78,11 @@ class EncryptAllTest extends TestCase {
->disableOriginalConstructor()->getMock();
$this->userInterface = $this->getMockBuilder(UserInterface::class)
->disableOriginalConstructor()->getMock();
+ $this->logger = $this->createMock(LoggerInterface::class);
/**
* We need format method to return a string
- * @var OutputFormatterInterface|\PHPUnit\Framework\MockObject\MockObject
+ * @var OutputFormatterInterface&MockObject
*/
$outputFormatter = $this->createMock(OutputFormatterInterface::class);
$outputFormatter->method('isDecorated')->willReturn(false);
@@ -131,12 +109,20 @@ class EncryptAllTest extends TestCase {
$this->l,
$this->l10nFactory,
$this->questionHelper,
- $this->secureRandom
+ $this->secureRandom,
+ $this->logger,
);
}
- public function testEncryptAll() {
- /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
+ protected function createFileInfoMock($type, string $name): FileInfo&MockObject {
+ $fileInfo = $this->createMock(FileInfo::class);
+ $fileInfo->method('getType')->willReturn($type);
+ $fileInfo->method('getName')->willReturn($name);
+ return $fileInfo;
+ }
+
+ public function testEncryptAll(): void {
+ /** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class)
->setConstructorArgs(
[
@@ -150,10 +136,11 @@ class EncryptAllTest extends TestCase {
$this->l,
$this->l10nFactory,
$this->questionHelper,
- $this->secureRandom
+ $this->secureRandom,
+ $this->logger,
]
)
- ->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
+ ->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
->getMock();
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
@@ -164,8 +151,8 @@ class EncryptAllTest extends TestCase {
$encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
}
- public function testEncryptAllWithMasterKey() {
- /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
+ public function testEncryptAllWithMasterKey(): void {
+ /** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class)
->setConstructorArgs(
[
@@ -179,10 +166,11 @@ class EncryptAllTest extends TestCase {
$this->l,
$this->l10nFactory,
$this->questionHelper,
- $this->secureRandom
+ $this->secureRandom,
+ $this->logger,
]
)
- ->setMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
+ ->onlyMethods(['createKeyPairs', 'encryptAllUsersFiles', 'outputPasswords'])
->getMock();
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(true);
@@ -194,8 +182,8 @@ class EncryptAllTest extends TestCase {
$encryptAll->encryptAll($this->inputInterface, $this->outputInterface);
}
- public function testCreateKeyPairs() {
- /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
+ public function testCreateKeyPairs(): void {
+ /** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class)
->setConstructorArgs(
[
@@ -209,10 +197,11 @@ class EncryptAllTest extends TestCase {
$this->l,
$this->l10nFactory,
$this->questionHelper,
- $this->secureRandom
+ $this->secureRandom,
+ $this->logger,
]
)
- ->setMethods(['setupUserFS', 'generateOneTimePassword'])
+ ->onlyMethods(['setupUserFS', 'generateOneTimePassword'])
->getMock();
@@ -244,8 +233,8 @@ class EncryptAllTest extends TestCase {
$this->assertSame('', $userPasswords['user2']);
}
- public function testEncryptAllUsersFiles() {
- /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
+ public function testEncryptAllUsersFiles(): void {
+ /** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class)
->setConstructorArgs(
[
@@ -259,10 +248,11 @@ class EncryptAllTest extends TestCase {
$this->l,
$this->l10nFactory,
$this->questionHelper,
- $this->secureRandom
+ $this->secureRandom,
+ $this->logger,
]
)
- ->setMethods(['encryptUsersFiles'])
+ ->onlyMethods(['encryptUsersFiles'])
->getMock();
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
@@ -271,17 +261,22 @@ class EncryptAllTest extends TestCase {
$this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
$this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]);
- $encryptAll->expects($this->exactly(2))->method('encryptUsersFiles')
- ->withConsecutive(
- ['user1'],
- ['user2'],
- );
+ $encryptAllCalls = [];
+ $encryptAll->expects($this->exactly(2))
+ ->method('encryptUsersFiles')
+ ->willReturnCallback(function ($uid) use (&$encryptAllCalls): void {
+ $encryptAllCalls[] = $uid;
+ });
$this->invokePrivate($encryptAll, 'encryptAllUsersFiles');
+ self::assertEquals([
+ 'user1',
+ 'user2',
+ ], $encryptAllCalls);
}
- public function testEncryptUsersFiles() {
- /** @var EncryptAll | \PHPUnit\Framework\MockObject\MockObject $encryptAll */
+ public function testEncryptUsersFiles(): void {
+ /** @var EncryptAll&MockObject $encryptAll */
$encryptAll = $this->getMockBuilder(EncryptAll::class)
->setConstructorArgs(
[
@@ -295,43 +290,43 @@ class EncryptAllTest extends TestCase {
$this->l,
$this->l10nFactory,
$this->questionHelper,
- $this->secureRandom
+ $this->secureRandom,
+ $this->logger,
]
)
- ->setMethods(['encryptFile', 'setupUserFS'])
+ ->onlyMethods(['encryptFile', 'setupUserFS'])
->getMock();
$this->util->expects($this->any())->method('isMasterKeyEnabled')->willReturn(false);
$this->view->expects($this->exactly(2))->method('getDirectoryContent')
- ->withConsecutive(
- ['/user1/files'],
- ['/user1/files/foo'],
- )->willReturnOnConsecutiveCalls(
+ ->willReturnMap([
[
- ['name' => 'foo', 'type' => 'dir'],
- ['name' => 'bar', 'type' => 'file'],
+ '/user1/files',
+ '',
+ null,
+ [
+ $this->createFileInfoMock(FileInfo::TYPE_FOLDER, 'foo'),
+ $this->createFileInfoMock(FileInfo::TYPE_FILE, 'bar'),
+ ],
],
[
- ['name' => 'subfile', 'type' => 'file']
- ]
- );
-
- $this->view->expects($this->any())->method('is_dir')
- ->willReturnCallback(
- function ($path) {
- if ($path === '/user1/files/foo') {
- return true;
- }
- return false;
- }
- );
+ '/user1/files/foo',
+ '',
+ null,
+ [
+ $this->createFileInfoMock(FileInfo::TYPE_FILE, 'subfile'),
+ ],
+ ],
+ ]);
- $encryptAll->expects($this->exactly(2))->method('encryptFile')
- ->withConsecutive(
- ['/user1/files/bar'],
- ['/user1/files/foo/subfile'],
- );
+ $encryptAllCalls = [];
+ $encryptAll->expects($this->exactly(2))
+ ->method('encryptFile')
+ ->willReturnCallback(function (FileInfo $file, string $path) use (&$encryptAllCalls): bool {
+ $encryptAllCalls[] = $path;
+ return true;
+ });
$outputFormatter = $this->createMock(OutputFormatterInterface::class);
$outputFormatter->method('isDecorated')->willReturn(false);
@@ -341,9 +336,13 @@ class EncryptAllTest extends TestCase {
$progressBar = new ProgressBar($this->outputInterface);
$this->invokePrivate($encryptAll, 'encryptUsersFiles', ['user1', $progressBar, '']);
+ self::assertEquals([
+ '/user1/files/bar',
+ '/user1/files/foo/subfile',
+ ], $encryptAllCalls);
}
- public function testGenerateOneTimePassword() {
+ public function testGenerateOneTimePassword(): void {
$password = $this->invokePrivate($this->encryptAll, 'generateOneTimePassword', ['user1']);
$this->assertTrue(is_string($password));
$this->assertSame(8, strlen($password));
@@ -354,15 +353,14 @@ class EncryptAllTest extends TestCase {
}
/**
- * @dataProvider dataTestEncryptFile
* @param $isEncrypted
*/
- public function testEncryptFile($isEncrypted) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestEncryptFile')]
+ public function testEncryptFile($isEncrypted): void {
$fileInfo = $this->createMock(FileInfo::class);
$fileInfo->expects($this->any())->method('isEncrypted')
->willReturn($isEncrypted);
- $this->view->expects($this->any())->method('getFileInfo')
- ->willReturn($fileInfo);
+ $this->view->expects($this->never())->method('getFileInfo');
if ($isEncrypted) {
@@ -374,11 +372,11 @@ class EncryptAllTest extends TestCase {
}
$this->assertTrue(
- $this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt'])
+ $this->invokePrivate($this->encryptAll, 'encryptFile', [$fileInfo, 'foo.txt'])
);
}
- public function dataTestEncryptFile() {
+ public static function dataTestEncryptFile(): array {
return [
[true],
[false],
diff --git a/apps/encryption/tests/Crypto/EncryptionTest.php b/apps/encryption/tests/Crypto/EncryptionTest.php
index 4767d22339d..37e484550ef 100644
--- a/apps/encryption/tests/Crypto/EncryptionTest.php
+++ b/apps/encryption/tests/Crypto/EncryptionTest.php
@@ -1,5 +1,7 @@
<?php
+declare(strict_types=1);
+
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -7,6 +9,8 @@
*/
namespace OCA\Encryption\Tests\Crypto;
+use OC\Encryption\Exceptions\DecryptionFailedException;
+use OC\Files\View;
use OCA\Encryption\Crypto\Crypt;
use OCA\Encryption\Crypto\DecryptAll;
use OCA\Encryption\Crypto\EncryptAll;
@@ -15,48 +19,32 @@ use OCA\Encryption\Exceptions\PublicKeyMissingException;
use OCA\Encryption\KeyManager;
use OCA\Encryption\Session;
use OCA\Encryption\Util;
-use OCP\Files\Storage;
+use OCP\Files\Storage\IStorage;
use OCP\IL10N;
+use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Test\TestCase;
class EncryptionTest extends TestCase {
- /** @var Encryption */
- private $instance;
-
- /** @var \OCA\Encryption\KeyManager|\PHPUnit\Framework\MockObject\MockObject */
- private $keyManagerMock;
-
- /** @var \OCA\Encryption\Crypto\EncryptAll|\PHPUnit\Framework\MockObject\MockObject */
- private $encryptAllMock;
-
- /** @var \OCA\Encryption\Crypto\DecryptAll|\PHPUnit\Framework\MockObject\MockObject */
- private $decryptAllMock;
-
- /** @var \OCA\Encryption\Session|\PHPUnit\Framework\MockObject\MockObject */
- private $sessionMock;
-
- /** @var \OCA\Encryption\Crypto\Crypt|\PHPUnit\Framework\MockObject\MockObject */
- private $cryptMock;
-
- /** @var \OCA\Encryption\Util|\PHPUnit\Framework\MockObject\MockObject */
- private $utilMock;
- /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
- private $loggerMock;
+ protected Encryption $instance;
- /** @var \OCP\IL10N|\PHPUnit\Framework\MockObject\MockObject */
- private $l10nMock;
-
- /** @var \OCP\Files\Storage|\PHPUnit\Framework\MockObject\MockObject */
- private $storageMock;
+ protected KeyManager&MockObject $keyManagerMock;
+ protected EncryptAll&MockObject $encryptAllMock;
+ protected DecryptAll&MockObject $decryptAllMock;
+ protected Session&MockObject $sessionMock;
+ protected Crypt&MockObject $cryptMock;
+ protected Util&MockObject $utilMock;
+ protected LoggerInterface&MockObject $loggerMock;
+ protected IL10N&MockObject $l10nMock;
+ protected IStorage&MockObject $storageMock;
protected function setUp(): void {
parent::setUp();
- $this->storageMock = $this->getMockBuilder(Storage::class)
+ $this->storageMock = $this->getMockBuilder(IStorage::class)
->disableOriginalConstructor()->getMock();
$this->cryptMock = $this->getMockBuilder(Crypt::class)
->disableOriginalConstructor()
@@ -102,7 +90,7 @@ class EncryptionTest extends TestCase {
/**
* test if public key from one of the recipients is missing
*/
- public function testEndUser1() {
+ public function testEndUser1(): void {
$this->sessionMock->expects($this->once())
->method('decryptAllModeActivated')
->willReturn(false);
@@ -115,12 +103,12 @@ class EncryptionTest extends TestCase {
* test if public key from owner is missing
*
*/
- public function testEndUser2() {
+ public function testEndUser2(): void {
$this->sessionMock->expects($this->once())
->method('decryptAllModeActivated')
->willReturn(false);
- $this->expectException(\OCA\Encryption\Exceptions\PublicKeyMissingException::class);
+ $this->expectException(PublicKeyMissingException::class);
$this->instance->begin('/foo/bar', 'user2', 'r', [], ['users' => ['user1', 'user2', 'user3']]);
$this->endTest();
@@ -164,16 +152,14 @@ class EncryptionTest extends TestCase {
return $publicKeys;
}
- /**
- * @dataProvider dataProviderForTestGetPathToRealFile
- */
- public function testGetPathToRealFile($path, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataProviderForTestGetPathToRealFile')]
+ public function testGetPathToRealFile($path, $expected): void {
$this->assertSame($expected,
self::invokePrivate($this->instance, 'getPathToRealFile', [$path])
);
}
- public function dataProviderForTestGetPathToRealFile() {
+ public static function dataProviderForTestGetPathToRealFile(): array {
return [
['/user/files/foo/bar.txt', '/user/files/foo/bar.txt'],
['/user/files/foo.txt', '/user/files/foo.txt'],
@@ -182,10 +168,8 @@ class EncryptionTest extends TestCase {
];
}
- /**
- * @dataProvider dataTestBegin
- */
- public function testBegin($mode, $header, $legacyCipher, $defaultCipher, $fileKey, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestBegin')]
+ public function testBegin($mode, $header, $legacyCipher, $defaultCipher, $fileKey, $expected): void {
$this->sessionMock->expects($this->once())
->method('decryptAllModeActivated')
->willReturn(false);
@@ -226,7 +210,7 @@ class EncryptionTest extends TestCase {
}
}
- public function dataTestBegin() {
+ public static function dataTestBegin(): array {
return [
['w', ['cipher' => 'myCipher'], 'legacyCipher', 'defaultCipher', 'fileKey', 'defaultCipher'],
['r', ['cipher' => 'myCipher'], 'legacyCipher', 'defaultCipher', 'fileKey', 'myCipher'],
@@ -239,7 +223,7 @@ class EncryptionTest extends TestCase {
/**
* test begin() if decryptAll mode was activated
*/
- public function testBeginDecryptAll() {
+ public function testBeginDecryptAll(): void {
$path = '/user/files/foo.txt';
$fileKey = 'fileKey';
@@ -263,7 +247,7 @@ class EncryptionTest extends TestCase {
* in this case we can initialize the encryption without a username/password
* and continue
*/
- public function testBeginInitMasterKey() {
+ public function testBeginInitMasterKey(): void {
$this->sessionMock->expects($this->once())
->method('decryptAllModeActivated')
->willReturn(false);
@@ -277,12 +261,12 @@ class EncryptionTest extends TestCase {
}
/**
- * @dataProvider dataTestUpdate
*
* @param string $fileKey
* @param boolean $expected
*/
- public function testUpdate($fileKey, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestUpdate')]
+ public function testUpdate($fileKey, $expected): void {
$this->keyManagerMock->expects($this->once())
->method('getFileKey')->willReturn($fileKey);
@@ -303,24 +287,24 @@ class EncryptionTest extends TestCase {
);
}
- public function dataTestUpdate() {
+ public static function dataTestUpdate(): array {
return [
['', false],
['fileKey', true]
];
}
- public function testUpdateNoUsers() {
+ public function testUpdateNoUsers(): void {
$this->invokePrivate($this->instance, 'rememberVersion', [['path' => 2]]);
$this->keyManagerMock->expects($this->never())->method('getFileKey');
$this->keyManagerMock->expects($this->never())->method('getPublicKey');
$this->keyManagerMock->expects($this->never())->method('addSystemKeys');
$this->keyManagerMock->expects($this->once())->method('setVersion')
- ->willReturnCallback(function ($path, $version, $view) {
+ ->willReturnCallback(function ($path, $version, $view): void {
$this->assertSame('path', $path);
$this->assertSame(2, $version);
- $this->assertTrue($view instanceof \OC\Files\View);
+ $this->assertTrue($view instanceof View);
});
$this->instance->update('path', 'user1', []);
}
@@ -329,13 +313,13 @@ class EncryptionTest extends TestCase {
* Test case if the public key is missing. Nextcloud should still encrypt
* the file for the remaining users
*/
- public function testUpdateMissingPublicKey() {
+ public function testUpdateMissingPublicKey(): void {
$this->keyManagerMock->expects($this->once())
->method('getFileKey')->willReturn('fileKey');
$this->keyManagerMock->expects($this->any())
->method('getPublicKey')->willReturnCallback(
- function ($user) {
+ function ($user): void {
throw new PublicKeyMissingException($user);
}
);
@@ -366,10 +350,9 @@ class EncryptionTest extends TestCase {
/**
* by default the encryption module should encrypt regular files, files in
* files_versions and files in files_trashbin
- *
- * @dataProvider dataTestShouldEncrypt
*/
- public function testShouldEncrypt($path, $shouldEncryptHomeStorage, $isHomeStorage, $expected) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestShouldEncrypt')]
+ public function testShouldEncrypt($path, $shouldEncryptHomeStorage, $isHomeStorage, $expected): void {
$this->utilMock->expects($this->once())->method('shouldEncryptHomeStorage')
->willReturn($shouldEncryptHomeStorage);
@@ -385,7 +368,7 @@ class EncryptionTest extends TestCase {
);
}
- public function dataTestShouldEncrypt() {
+ public static function dataTestShouldEncrypt(): array {
return [
['/user1/files/foo.txt', true, true, true],
['/user1/files_versions/foo.txt', true, true, true],
@@ -402,14 +385,14 @@ class EncryptionTest extends TestCase {
}
- public function testDecrypt() {
- $this->expectException(\OC\Encryption\Exceptions\DecryptionFailedException::class);
+ public function testDecrypt(): void {
+ $this->expectException(DecryptionFailedException::class);
$this->expectExceptionMessage('Cannot decrypt this file, probably this is a shared file. Please ask the file owner to reshare the file with you.');
$this->instance->decrypt('abc');
}
- public function testPrepareDecryptAll() {
+ public function testPrepareDecryptAll(): void {
/** @var \Symfony\Component\Console\Input\InputInterface $input */
$input = $this->createMock(InputInterface::class);
/** @var \Symfony\Component\Console\Output\OutputInterface $output */