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.php234
-rw-r--r--apps/encryption/tests/Crypto/DecryptAllTest.php59
-rw-r--r--apps/encryption/tests/Crypto/EncryptAllTest.php252
-rw-r--r--apps/encryption/tests/Crypto/EncryptionTest.php171
4 files changed, 308 insertions, 408 deletions
diff --git a/apps/encryption/tests/Crypto/CryptTest.php b/apps/encryption/tests/Crypto/CryptTest.php
index 60548a0f1dc..1355e2c855d 100644
--- a/apps/encryption/tests/Crypto/CryptTest.php
+++ b/apps/encryption/tests/Crypto/CryptTest.php
@@ -1,65 +1,39 @@
<?php
+
+declare(strict_types=1);
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Encryption\Tests\Crypto;
use OCA\Encryption\Crypto\Crypt;
+use OCP\Encryption\Exceptions\GenericEncryptionException;
use OCP\IConfig;
use OCP\IL10N;
-use OCP\ILogger;
use OCP\IUserSession;
+use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Log\LoggerInterface;
use Test\TestCase;
class CryptTest extends TestCase {
+ protected LoggerInterface&MockObject $logger;
+ protected IUserSession&MockObject $userSession;
+ protected IConfig&MockObject $config;
+ protected IL10N&MockObject $l;
-
- /** @var \OCP\ILogger|\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;
-
- /** @var Crypt */
- private $crypt;
+ protected Crypt $crypt;
protected function setUp(): void {
parent::setUp();
- $this->logger = $this->getMockBuilder(ILogger::class)
+ $this->logger = $this->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->logger->expects($this->any())
- ->method('warning')
- ->willReturn(true);
+ ->method('warning');
$this->userSession = $this->getMockBuilder(IUserSession::class)
->disableOriginalConstructor()
->getMock();
@@ -74,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([]))
@@ -89,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([]))
@@ -106,12 +80,11 @@ 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('getSystemValue')
+ ->method('getSystemValueString')
->with($this->equalTo('cipher'), $this->equalTo('AES-256-CTR'))
->willReturn('AES-128-CFB');
@@ -128,44 +101,41 @@ 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:hash:HEND'],
- ['password', 'HBEGIN:cipher:AES-128-CFB:keyFormat:password:HEND'],
- ['hash', 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash:HEND']
+ [null, 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash2:encoding:binary:HEND'],
+ ['password', 'HBEGIN:cipher:AES-128-CFB:keyFormat:password:encoding:binary:HEND'],
+ ['hash', 'HBEGIN:cipher:AES-128-CFB:keyFormat:hash:encoding:binary:HEND']
];
}
- public function testGetCipherWithInvalidCipher() {
+ public function testGetCipherWithInvalidCipher(): void {
$this->config->expects($this->once())
- ->method('getSystemValue')
- ->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')
->with('Unsupported cipher (Not-Existing-Cipher) defined in config.php supported. Falling back to AES-256-CTR');
- $this->assertSame('AES-256-CTR', $this->crypt->getCipher());
+ $this->assertSame('AES-256-CTR', $this->crypt->getCipher());
}
/**
- * @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('getSystemValue')
+ ->method('getSystemValueString')
->with($this->equalTo('cipher'), $this->equalTo('AES-256-CTR'))
->willReturn($configValue);
@@ -176,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'],
@@ -193,7 +161,7 @@ class CryptTest extends TestCase {
/**
* test concatIV()
*/
- public function testConcatIV() {
+ public function testConcatIV(): void {
$result = self::invokePrivate(
$this->crypt,
'concatIV',
@@ -204,11 +172,9 @@ class CryptTest extends TestCase {
);
}
- /**
- * @dataProvider dataTestSplitMetaData
- */
- public function testSplitMetaData($data, $expected) {
- $this->config->method('getSystemValue')
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestSplitMetaData')]
+ public function testSplitMetaData($data, $expected): void {
+ $this->config->method('getSystemValueBool')
->with('encryption_skip_signature_check', false)
->willReturn(true);
$result = self::invokePrivate($this->crypt, 'splitMetaData', [$data, 'AES-256-CFB']);
@@ -222,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]],
@@ -231,11 +197,9 @@ class CryptTest extends TestCase {
];
}
- /**
- * @dataProvider dataTestHasSignature
- */
- public function testHasSignature($data, $expected) {
- $this->config->method('getSystemValue')
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestHasSignature')]
+ public function testHasSignature($data, $expected): void {
+ $this->config->method('getSystemValueBool')
->with('encryption_skip_signature_check', false)
->willReturn(true);
$this->assertSame($expected,
@@ -243,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'],
@@ -272,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);
}
@@ -280,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]
@@ -304,16 +264,18 @@ class CryptTest extends TestCase {
/**
* test parseHeader()
*/
- public function testParseHeader() {
- $header = 'HBEGIN:foo:bar:cipher:AES-256-CFB:HEND';
+ public function testParseHeader(): void {
+ $header = 'HBEGIN:foo:bar:cipher:AES-256-CFB:encoding:binary:HEND';
$result = self::invokePrivate($this->crypt, 'parseHeader', [$header]);
$this->assertTrue(is_array($result));
- $this->assertSame(2, count($result));
+ $this->assertSame(3, count($result));
$this->assertArrayHasKey('foo', $result);
$this->assertArrayHasKey('cipher', $result);
+ $this->assertArrayHasKey('encoding', $result);
$this->assertSame('bar', $result['foo']);
$this->assertSame('AES-256-CFB', $result['cipher']);
+ $this->assertSame('binary', $result['encoding']);
}
/**
@@ -324,18 +286,20 @@ class CryptTest extends TestCase {
public function testEncrypt() {
$decrypted = 'content';
$password = 'password';
+ $cipher = 'AES-256-CTR';
$iv = self::invokePrivate($this->crypt, 'generateIv');
$this->assertTrue(is_string($iv));
$this->assertSame(16, strlen($iv));
- $result = self::invokePrivate($this->crypt, 'encrypt', [$decrypted, $iv, $password]);
+ $result = self::invokePrivate($this->crypt, 'encrypt', [$decrypted, $iv, $password, $cipher]);
$this->assertTrue(is_string($result));
return [
'password' => $password,
'iv' => $iv,
+ 'cipher' => $cipher,
'encrypted' => $result,
'decrypted' => $decrypted];
}
@@ -345,21 +309,20 @@ class CryptTest extends TestCase {
*
* @depends testEncrypt
*/
- public function testDecrypt($data) {
+ public function testDecrypt($data): void {
$result = self::invokePrivate(
$this->crypt,
'decrypt',
- [$data['encrypted'], $data['iv'], $data['password']]);
+ [$data['encrypted'], $data['iv'], $data['password'], $data['cipher'], true]);
$this->assertSame($data['decrypted'], $result);
}
/**
* 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);
}
@@ -368,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],
@@ -386,32 +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')
- ->with('encryption.legacy_format_support', false)
- ->willReturn(true);
+ ->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);
@@ -432,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'],
@@ -446,7 +399,7 @@ class CryptTest extends TestCase {
];
}
- public function testIsValidPrivateKey() {
+ public function testIsValidPrivateKey(): void {
$res = openssl_pkey_new();
openssl_pkey_export($res, $privateKey);
@@ -460,4 +413,17 @@ class CryptTest extends TestCase {
$this->invokePrivate($this->crypt, 'isValidPrivateKey', ['foo'])
);
}
+
+ public function testMultiKeyEncrypt(): void {
+ $res = openssl_pkey_new();
+ openssl_pkey_export($res, $privateKey);
+ $publicKeyPem = openssl_pkey_get_details($res)['key'];
+ $publicKey = openssl_pkey_get_public($publicKeyPem);
+
+ $shareKeys = $this->crypt->multiKeyEncrypt('content', ['user1' => $publicKey]);
+ $this->assertEquals(
+ 'content',
+ $this->crypt->multiKeyDecrypt($shareKeys['user1'], $privateKey)
+ );
+ }
}
diff --git a/apps/encryption/tests/Crypto/DecryptAllTest.php b/apps/encryption/tests/Crypto/DecryptAllTest.php
index 68b49307f31..82e6100bce5 100644
--- a/apps/encryption/tests/Crypto/DecryptAllTest.php
+++ b/apps/encryption/tests/Crypto/DecryptAllTest.php
@@ -1,26 +1,11 @@
<?php
+
+declare(strict_types=1);
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
+ * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Encryption\Tests\Crypto;
@@ -29,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();
@@ -75,7 +51,7 @@ class DecryptAllTest extends TestCase {
);
}
- public function testUpdateSession() {
+ public function testUpdateSession(): void {
$this->session->expects($this->once())->method('prepareDecryptAll')
->with('user1', 'key1');
@@ -83,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')
@@ -122,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 e5c10dd67e8..c56e3375a73 100644
--- a/apps/encryption/tests/Crypto/EncryptAllTest.php
+++ b/apps/encryption/tests/Crypto/EncryptAllTest.php
@@ -1,29 +1,11 @@
<?php
+
+declare(strict_types=1);
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Kenneth Newwood <kenneth@newwood.name>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Encryption\Tests\Crypto;
@@ -36,9 +18,12 @@ use OCP\Files\FileInfo;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IUserManager;
+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;
@@ -48,47 +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 | \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();
@@ -106,6 +67,7 @@ class EncryptAllTest extends TestCase {
->disableOriginalConstructor()->getMock();
$this->mailer = $this->getMockBuilder(IMailer::class)
->disableOriginalConstructor()->getMock();
+ $this->l10nFactory = $this->createMock(IFactory::class);
$this->l = $this->getMockBuilder(IL10N::class)
->disableOriginalConstructor()->getMock();
$this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
@@ -116,9 +78,14 @@ 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 */
+ /**
+ * We need format method to return a string
+ * @var OutputFormatterInterface&MockObject
+ */
$outputFormatter = $this->createMock(OutputFormatterInterface::class);
+ $outputFormatter->method('isDecorated')->willReturn(false);
$outputFormatter->method('format')->willReturnArgument(0);
$this->outputInterface->expects($this->any())->method('getFormatter')
@@ -140,13 +107,22 @@ class EncryptAllTest extends TestCase {
$this->config,
$this->mailer,
$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(
[
@@ -158,23 +134,25 @@ class EncryptAllTest extends TestCase {
$this->config,
$this->mailer,
$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);
- $encryptAll->expects($this->at(0))->method('createKeyPairs')->with();
- $encryptAll->expects($this->at(1))->method('outputPasswords')->with();
- $encryptAll->expects($this->at(2))->method('encryptAllUsersFiles')->with();
+ $encryptAll->expects($this->once())->method('createKeyPairs')->with();
+ $encryptAll->expects($this->once())->method('outputPasswords')->with();
+ $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with();
$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(
[
@@ -186,24 +164,26 @@ class EncryptAllTest extends TestCase {
$this->config,
$this->mailer,
$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);
$encryptAll->expects($this->never())->method('createKeyPairs');
$this->keyManager->expects($this->once())->method('validateMasterKey');
- $encryptAll->expects($this->at(0))->method('encryptAllUsersFiles')->with();
+ $encryptAll->expects($this->once())->method('encryptAllUsersFiles')->with();
$encryptAll->expects($this->never())->method('outputPasswords');
$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(
[
@@ -215,11 +195,13 @@ class EncryptAllTest extends TestCase {
$this->config,
$this->mailer,
$this->l,
+ $this->l10nFactory,
$this->questionHelper,
- $this->secureRandom
+ $this->secureRandom,
+ $this->logger,
]
)
- ->setMethods(['setupUserFS', 'generateOneTimePassword'])
+ ->onlyMethods(['setupUserFS', 'generateOneTimePassword'])
->getMock();
@@ -251,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(
[
@@ -264,11 +246,13 @@ class EncryptAllTest extends TestCase {
$this->config,
$this->mailer,
$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);
@@ -277,14 +261,22 @@ class EncryptAllTest extends TestCase {
$this->invokePrivate($encryptAll, 'output', [$this->outputInterface]);
$this->invokePrivate($encryptAll, 'userPasswords', [['user1' => 'pwd1', 'user2' => 'pwd2']]);
- $encryptAll->expects($this->at(0))->method('encryptUsersFiles')->with('user1');
- $encryptAll->expects($this->at(1))->method('encryptUsersFiles')->with('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(
[
@@ -296,52 +288,61 @@ class EncryptAllTest extends TestCase {
$this->config,
$this->mailer,
$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->at(0))->method('getDirectoryContent')
- ->with('/user1/files')->willReturn(
+ $this->view->expects($this->exactly(2))->method('getDirectoryContent')
+ ->willReturnMap([
[
- ['name' => 'foo', 'type' => 'dir'],
- ['name' => 'bar', 'type' => 'file'],
- ]
- );
-
- $this->view->expects($this->at(3))->method('getDirectoryContent')
- ->with('/user1/files/foo')->willReturn(
+ '/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;
- }
- );
-
- $encryptAll->expects($this->at(1))->method('encryptFile')->with('/user1/files/bar');
- $encryptAll->expects($this->at(2))->method('encryptFile')->with('/user1/files/foo/subfile');
+ '/user1/files/foo',
+ '',
+ null,
+ [
+ $this->createFileInfoMock(FileInfo::TYPE_FILE, '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);
$this->outputInterface->expects($this->any())
->method('getFormatter')
- ->willReturn($this->createMock(OutputFormatterInterface::class));
+ ->willReturn($outputFormatter);
$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));
@@ -352,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) {
@@ -372,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 63698dcdc63..37e484550ef 100644
--- a/apps/encryption/tests/Crypto/EncryptionTest.php
+++ b/apps/encryption/tests/Crypto/EncryptionTest.php
@@ -1,31 +1,16 @@
<?php
+
+declare(strict_types=1);
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Bjoern Schiessle <bjoern@schiessle.org>
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license AGPL-3.0
- *
- * This code is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License, version 3,
- * as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License, version 3,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
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;
@@ -34,49 +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 OCP\ILogger;
+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 \OCP\ILogger|\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()
@@ -96,7 +64,7 @@ class EncryptionTest extends TestCase {
$this->decryptAllMock = $this->getMockBuilder(DecryptAll::class)
->disableOriginalConstructor()
->getMock();
- $this->loggerMock = $this->getMockBuilder(ILogger::class)
+ $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->l10nMock = $this->getMockBuilder(IL10N::class)
@@ -122,7 +90,11 @@ 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);
+
$this->instance->begin('/foo/bar', 'user1', 'r', [], ['users' => ['user1', 'user2', 'user3']]);
$this->endTest();
}
@@ -131,8 +103,12 @@ class EncryptionTest extends TestCase {
* test if public key from owner is missing
*
*/
- public function testEndUser2() {
- $this->expectException(\OCA\Encryption\Exceptions\PublicKeyMissingException::class);
+ public function testEndUser2(): void {
+ $this->sessionMock->expects($this->once())
+ ->method('decryptAllModeActivated')
+ ->willReturn(false);
+
+ $this->expectException(PublicKeyMissingException::class);
$this->instance->begin('/foo/bar', 'user2', 'r', [], ['users' => ['user1', 'user2', 'user3']]);
$this->endTest();
@@ -156,7 +132,7 @@ class EncryptionTest extends TestCase {
->willReturnCallback([$this, 'addSystemKeysCallback']);
$this->cryptMock->expects($this->any())
->method('multiKeyEncrypt')
- ->willReturn(true);
+ ->willReturn([]);
$this->instance->end('/foo/bar');
}
@@ -176,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'],
@@ -194,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);
@@ -238,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'],
@@ -251,37 +223,18 @@ class EncryptionTest extends TestCase {
/**
* test begin() if decryptAll mode was activated
*/
- public function testBeginDecryptAll() {
+ public function testBeginDecryptAll(): void {
$path = '/user/files/foo.txt';
- $recoveryKeyId = 'recoveryKeyId';
- $recoveryShareKey = 'recoveryShareKey';
- $decryptAllKey = 'decryptAllKey';
$fileKey = 'fileKey';
$this->sessionMock->expects($this->once())
->method('decryptAllModeActivated')
->willReturn(true);
- $this->sessionMock->expects($this->once())
- ->method('getDecryptAllUid')
- ->willReturn($recoveryKeyId);
- $this->sessionMock->expects($this->once())
- ->method('getDecryptAllKey')
- ->willReturn($decryptAllKey);
-
- $this->keyManagerMock->expects($this->once())
- ->method('getEncryptedFileKey')
- ->willReturn('encryptedFileKey');
$this->keyManagerMock->expects($this->once())
- ->method('getShareKey')
- ->with($path, $recoveryKeyId)
- ->willReturn($recoveryShareKey);
- $this->cryptMock->expects($this->once())
- ->method('multiKeyDecrypt')
- ->with('encryptedFileKey', $recoveryShareKey, $decryptAllKey)
+ ->method('getFileKey')
+ ->with($path, 'user', null, true)
->willReturn($fileKey);
- $this->keyManagerMock->expects($this->never())->method('getFileKey');
-
$this->instance->begin($path, 'user', 'r', [], []);
$this->assertSame($fileKey,
@@ -294,7 +247,11 @@ 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);
+
$this->sessionMock->expects($this->once())->method('isReady')->willReturn(false);
$this->utilMock->expects($this->once())->method('isMasterKeyEnabled')
->willReturn(true);
@@ -304,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);
@@ -330,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', []);
}
@@ -356,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);
}
);
@@ -378,6 +335,7 @@ class EncryptionTest extends TestCase {
function ($fileKey, $publicKeys) {
$this->assertEmpty($publicKeys);
$this->assertSame('fileKey', $fileKey);
+ return [];
}
);
@@ -392,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);
@@ -411,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],
@@ -428,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 */