aboutsummaryrefslogtreecommitdiffstats
path: root/apps/encryption/tests/Crypto/EncryptAllTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/encryption/tests/Crypto/EncryptAllTest.php')
-rw-r--r--apps/encryption/tests/Crypto/EncryptAllTest.php252
1 files changed, 126 insertions, 126 deletions
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],