aboutsummaryrefslogtreecommitdiffstats
path: root/tests/Core/Command/Encryption
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Core/Command/Encryption')
-rw-r--r--tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php360
-rw-r--r--tests/Core/Command/Encryption/DecryptAllTest.php216
-rw-r--r--tests/Core/Command/Encryption/DisableTest.php74
-rw-r--r--tests/Core/Command/Encryption/EnableTest.php101
-rw-r--r--tests/Core/Command/Encryption/EncryptAllTest.php94
-rw-r--r--tests/Core/Command/Encryption/SetDefaultModuleTest.php130
6 files changed, 975 insertions, 0 deletions
diff --git a/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php b/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php
new file mode 100644
index 00000000000..0bc6cbb64cf
--- /dev/null
+++ b/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php
@@ -0,0 +1,360 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+namespace Tests\Core\Command\Encryption;
+
+use OC\Core\Command\Encryption\ChangeKeyStorageRoot;
+use OC\Encryption\Keys\Storage;
+use OC\Encryption\Util;
+use OC\Files\View;
+use OCP\IConfig;
+use OCP\IUserManager;
+use OCP\UserInterface;
+use Symfony\Component\Console\Formatter\OutputFormatterInterface;
+use Symfony\Component\Console\Helper\QuestionHelper;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Test\TestCase;
+
+class ChangeKeyStorageRootTest extends TestCase {
+ /** @var ChangeKeyStorageRoot */
+ protected $changeKeyStorageRoot;
+
+ /** @var View | \PHPUnit\Framework\MockObject\MockObject */
+ protected $view;
+
+ /** @var IUserManager | \PHPUnit\Framework\MockObject\MockObject */
+ protected $userManager;
+
+ /** @var IConfig | \PHPUnit\Framework\MockObject\MockObject */
+ protected $config;
+
+ /** @var Util | \PHPUnit\Framework\MockObject\MockObject */
+ protected $util;
+
+ /** @var QuestionHelper | \PHPUnit\Framework\MockObject\MockObject */
+ protected $questionHelper;
+
+ /** @var InputInterface | \PHPUnit\Framework\MockObject\MockObject */
+ protected $inputInterface;
+
+ /** @var OutputInterface | \PHPUnit\Framework\MockObject\MockObject */
+ protected $outputInterface;
+
+ /** @var UserInterface|\PHPUnit\Framework\MockObject\MockObject */
+ protected $userInterface;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->view = $this->getMockBuilder(View::class)->getMock();
+ $this->userManager = $this->getMockBuilder(IUserManager::class)->getMock();
+ $this->config = $this->getMockBuilder(IConfig::class)->getMock();
+ $this->util = $this->getMockBuilder('OC\Encryption\Util')->disableOriginalConstructor()->getMock();
+ $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)->getMock();
+ $this->inputInterface = $this->getMockBuilder(InputInterface::class)->getMock();
+ $this->outputInterface = $this->getMockBuilder(OutputInterface::class)->getMock();
+ $this->userInterface = $this->getMockBuilder(UserInterface::class)->getMock();
+
+ /* We need format method to return a string */
+ $outputFormatter = $this->createMock(OutputFormatterInterface::class);
+ $outputFormatter->method('isDecorated')->willReturn(false);
+ $outputFormatter->method('format')->willReturnArgument(0);
+
+ $this->outputInterface->expects($this->any())->method('getFormatter')
+ ->willReturn($outputFormatter);
+
+ $this->changeKeyStorageRoot = new ChangeKeyStorageRoot(
+ $this->view,
+ $this->userManager,
+ $this->config,
+ $this->util,
+ $this->questionHelper
+ );
+ }
+
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestExecute')]
+ public function testExecute($newRoot, $answer, $successMoveKey): void {
+ $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
+ ->setConstructorArgs(
+ [
+ $this->view,
+ $this->userManager,
+ $this->config,
+ $this->util,
+ $this->questionHelper
+ ]
+ )->onlyMethods(['moveAllKeys'])->getMock();
+
+ $this->util->expects($this->once())->method('getKeyStorageRoot')
+ ->willReturn('');
+ $this->inputInterface->expects($this->once())->method('getArgument')
+ ->with('newRoot')->willReturn($newRoot);
+
+ if ($answer === true || $newRoot !== null) {
+ $changeKeyStorageRoot->expects($this->once())->method('moveAllKeys')
+ ->willReturn($successMoveKey);
+ } else {
+ $changeKeyStorageRoot->expects($this->never())->method('moveAllKeys');
+ }
+
+ if ($successMoveKey === true) {
+ $this->util->expects($this->once())->method('setKeyStorageRoot');
+ } else {
+ $this->util->expects($this->never())->method('setKeyStorageRoot');
+ }
+
+ if ($newRoot === null) {
+ $this->questionHelper->expects($this->once())->method('ask')->willReturn($answer);
+ } else {
+ $this->questionHelper->expects($this->never())->method('ask');
+ }
+
+ $this->invokePrivate(
+ $changeKeyStorageRoot,
+ 'execute',
+ [$this->inputInterface, $this->outputInterface]
+ );
+ }
+
+ public static function dataTestExecute(): array {
+ return [
+ [null, true, true],
+ [null, true, false],
+ [null, false, null],
+ ['/newRoot', null, true],
+ ['/newRoot', null, false]
+ ];
+ }
+
+ public function testMoveAllKeys(): void {
+ /** @var ChangeKeyStorageRoot $changeKeyStorageRoot */
+ $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
+ ->setConstructorArgs(
+ [
+ $this->view,
+ $this->userManager,
+ $this->config,
+ $this->util,
+ $this->questionHelper
+ ]
+ )->onlyMethods(['prepareNewRoot', 'moveSystemKeys', 'moveUserKeys'])->getMock();
+
+ $changeKeyStorageRoot->expects($this->once())->method('prepareNewRoot')->with('newRoot');
+ $changeKeyStorageRoot->expects($this->once())->method('moveSystemKeys')->with('oldRoot', 'newRoot');
+ $changeKeyStorageRoot->expects($this->once())->method('moveUserKeys')->with('oldRoot', 'newRoot', $this->outputInterface);
+
+ $this->invokePrivate($changeKeyStorageRoot, 'moveAllKeys', ['oldRoot', 'newRoot', $this->outputInterface]);
+ }
+
+ public function testPrepareNewRoot(): void {
+ $this->view->expects($this->once())->method('is_dir')->with('newRoot')
+ ->willReturn(true);
+
+ $this->view->expects($this->once())->method('file_put_contents')
+ ->with('newRoot/' . Storage::KEY_STORAGE_MARKER,
+ 'Nextcloud will detect this folder as key storage root only if this file exists')->willReturn(true);
+
+ $this->invokePrivate($this->changeKeyStorageRoot, 'prepareNewRoot', ['newRoot']);
+ }
+
+ /**
+ *
+ * @param bool $dirExists
+ * @param bool $couldCreateFile
+ */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestPrepareNewRootException')]
+ public function testPrepareNewRootException($dirExists, $couldCreateFile): void {
+ $this->expectException(\Exception::class);
+
+ $this->view->expects($this->once())->method('is_dir')->with('newRoot')
+ ->willReturn($dirExists);
+ $this->view->expects($this->any())->method('file_put_contents')->willReturn($couldCreateFile);
+
+ $this->invokePrivate($this->changeKeyStorageRoot, 'prepareNewRoot', ['newRoot']);
+ }
+
+ public static function dataTestPrepareNewRootException(): array {
+ return [
+ [true, false],
+ [true, null],
+ [false, true]
+ ];
+ }
+
+ /**
+ *
+ * @param bool $dirExists
+ * @param bool $targetExists
+ * @param bool $executeRename
+ */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestMoveSystemKeys')]
+ public function testMoveSystemKeys($dirExists, $targetExists, $executeRename): void {
+ $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
+ ->setConstructorArgs(
+ [
+ $this->view,
+ $this->userManager,
+ $this->config,
+ $this->util,
+ $this->questionHelper
+ ]
+ )->onlyMethods(['targetExists'])->getMock();
+
+ $this->view->expects($this->once())->method('is_dir')
+ ->with('oldRoot/files_encryption')->willReturn($dirExists);
+ $changeKeyStorageRoot->expects($this->any())->method('targetExists')
+ ->with('newRoot/files_encryption')->willReturn($targetExists);
+
+ if ($executeRename) {
+ $this->view->expects($this->once())->method('rename')
+ ->with('oldRoot/files_encryption', 'newRoot/files_encryption');
+ } else {
+ $this->view->expects($this->never())->method('rename');
+ }
+
+ $this->invokePrivate($changeKeyStorageRoot, 'moveSystemKeys', ['oldRoot', 'newRoot']);
+ }
+
+ public static function dataTestMoveSystemKeys(): array {
+ return [
+ [true, false, true],
+ [false, true, false],
+ [true, true, false],
+ [false, false, false]
+ ];
+ }
+
+
+ public function testMoveUserKeys(): void {
+ $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
+ ->setConstructorArgs(
+ [
+ $this->view,
+ $this->userManager,
+ $this->config,
+ $this->util,
+ $this->questionHelper
+ ]
+ )->onlyMethods(['setupUserFS', 'moveUserEncryptionFolder'])->getMock();
+
+ $this->userManager->expects($this->once())->method('getBackends')
+ ->willReturn([$this->userInterface]);
+ $this->userInterface->expects($this->once())->method('getUsers')
+ ->willReturn(['user1', 'user2']);
+ $changeKeyStorageRoot->expects($this->exactly(2))->method('setupUserFS');
+ $changeKeyStorageRoot->expects($this->exactly(2))->method('moveUserEncryptionFolder');
+
+ $this->invokePrivate($changeKeyStorageRoot, 'moveUserKeys', ['oldRoot', 'newRoot', $this->outputInterface]);
+ }
+
+ /**
+ *
+ * @param bool $userExists
+ * @param bool $isDir
+ * @param bool $targetExists
+ * @param bool $shouldRename
+ */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestMoveUserEncryptionFolder')]
+ public function testMoveUserEncryptionFolder($userExists, $isDir, $targetExists, $shouldRename): void {
+ $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot')
+ ->setConstructorArgs(
+ [
+ $this->view,
+ $this->userManager,
+ $this->config,
+ $this->util,
+ $this->questionHelper
+ ]
+ )->onlyMethods(['targetExists', 'prepareParentFolder'])->getMock();
+
+ $this->userManager->expects($this->once())->method('userExists')
+ ->willReturn($userExists);
+ $this->view->expects($this->any())->method('is_dir')
+ ->willReturn($isDir);
+ $changeKeyStorageRoot->expects($this->any())->method('targetExists')
+ ->willReturn($targetExists);
+
+ if ($shouldRename) {
+ $changeKeyStorageRoot->expects($this->once())->method('prepareParentFolder')
+ ->with('newRoot/user1');
+ $this->view->expects($this->once())->method('rename')
+ ->with('oldRoot/user1/files_encryption', 'newRoot/user1/files_encryption');
+ } else {
+ $changeKeyStorageRoot->expects($this->never())->method('prepareParentFolder');
+ $this->view->expects($this->never())->method('rename');
+ }
+
+ $this->invokePrivate($changeKeyStorageRoot, 'moveUserEncryptionFolder', ['user1', 'oldRoot', 'newRoot']);
+ }
+
+ public static function dataTestMoveUserEncryptionFolder(): array {
+ return [
+ [true, true, false, true],
+ [true, false, true, false],
+ [false, true, true, false],
+ [false, false, true, false],
+ [false, true, false, false],
+ [false, true, true, false],
+ [false, false, false, false]
+ ];
+ }
+
+
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestPrepareParentFolder')]
+ public function testPrepareParentFolder($path, $pathExists): void {
+ $this->view->expects($this->any())->method('file_exists')
+ ->willReturnCallback(
+ function ($fileExistsPath) use ($path, $pathExists) {
+ if ($path === $fileExistsPath) {
+ return $pathExists;
+ }
+ return false;
+ }
+ );
+
+ if ($pathExists === false) {
+ $subDirs = explode('/', ltrim($path, '/'));
+ $this->view->expects($this->exactly(count($subDirs)))->method('mkdir');
+ } else {
+ $this->view->expects($this->never())->method('mkdir');
+ }
+
+ $this->invokePrivate(
+ $this->changeKeyStorageRoot,
+ 'prepareParentFolder',
+ [$path]
+ );
+ }
+
+ public static function dataTestPrepareParentFolder(): array {
+ return [
+ ['/user/folder/sub_folder/keystorage', true],
+ ['/user/folder/sub_folder/keystorage', false]
+ ];
+ }
+
+ public function testTargetExists(): void {
+ $this->view->expects($this->once())->method('file_exists')->with('path')
+ ->willReturn(false);
+
+ $this->assertFalse(
+ $this->invokePrivate($this->changeKeyStorageRoot, 'targetExists', ['path'])
+ );
+ }
+
+
+ public function testTargetExistsException(): void {
+ $this->expectException(\Exception::class);
+
+ $this->view->expects($this->once())->method('file_exists')->with('path')
+ ->willReturn(true);
+
+ $this->invokePrivate($this->changeKeyStorageRoot, 'targetExists', ['path']);
+ }
+}
diff --git a/tests/Core/Command/Encryption/DecryptAllTest.php b/tests/Core/Command/Encryption/DecryptAllTest.php
new file mode 100644
index 00000000000..41d9e4c713f
--- /dev/null
+++ b/tests/Core/Command/Encryption/DecryptAllTest.php
@@ -0,0 +1,216 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+namespace Tests\Core\Command\Encryption;
+
+use OC\Core\Command\Encryption\DecryptAll;
+use OCP\App\IAppManager;
+use OCP\Encryption\IManager;
+use OCP\IConfig;
+use Symfony\Component\Console\Helper\QuestionHelper;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Test\TestCase;
+
+class DecryptAllTest extends TestCase {
+ /** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */
+ protected $config;
+
+ /** @var \PHPUnit\Framework\MockObject\MockObject | \OCP\Encryption\IManager */
+ protected $encryptionManager;
+
+ /** @var \PHPUnit\Framework\MockObject\MockObject|IAppManager */
+ protected $appManager;
+
+ /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Input\InputInterface */
+ protected $consoleInput;
+
+ /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Output\OutputInterface */
+ protected $consoleOutput;
+
+ /** @var \PHPUnit\Framework\MockObject\MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
+ protected $questionHelper;
+
+ /** @var \PHPUnit\Framework\MockObject\MockObject | \OC\Encryption\DecryptAll */
+ protected $decryptAll;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->config = $this->getMockBuilder(IConfig::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->encryptionManager = $this->getMockBuilder(IManager::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->appManager = $this->getMockBuilder(IAppManager::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->questionHelper = $this->getMockBuilder(QuestionHelper::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->decryptAll = $this->getMockBuilder(\OC\Encryption\DecryptAll::class)
+ ->disableOriginalConstructor()->getMock();
+ $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
+ $this->consoleInput->expects($this->any())
+ ->method('isInteractive')
+ ->willReturn(true);
+ $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
+
+ $this->config->expects($this->any())
+ ->method('getSystemValue')
+ ->with('maintenance', false)
+ ->willReturn(false);
+ $this->appManager->expects($this->any())
+ ->method('isEnabledForUser')
+ ->with('files_trashbin')->willReturn(true);
+ }
+
+ public function testMaintenanceAndTrashbin(): void {
+ // on construct we enable single-user-mode and disable the trash bin
+ // on destruct we disable single-user-mode again and enable the trash bin
+ $calls = [
+ ['maintenance', true],
+ ['maintenance', false],
+ ];
+ $this->config->expects($this->exactly(2))
+ ->method('setSystemValue')
+ ->willReturnCallback(function () use (&$calls): void {
+ $expected = array_shift($calls);
+ $this->assertEquals($expected, func_get_args());
+ });
+ $this->appManager->expects($this->once())
+ ->method('disableApp')
+ ->with('files_trashbin');
+ $this->appManager->expects($this->once())
+ ->method('enableApp')
+ ->with('files_trashbin');
+
+ $instance = new DecryptAll(
+ $this->encryptionManager,
+ $this->appManager,
+ $this->config,
+ $this->decryptAll,
+ $this->questionHelper
+ );
+ $this->invokePrivate($instance, 'forceMaintenanceAndTrashbin');
+
+ $this->assertTrue(
+ $this->invokePrivate($instance, 'wasTrashbinEnabled')
+ );
+
+ $this->assertFalse(
+ $this->invokePrivate($instance, 'wasMaintenanceModeEnabled')
+ );
+ $this->invokePrivate($instance, 'resetMaintenanceAndTrashbin');
+ }
+
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestExecute')]
+ public function testExecute($encryptionEnabled, $continue): void {
+ $instance = new DecryptAll(
+ $this->encryptionManager,
+ $this->appManager,
+ $this->config,
+ $this->decryptAll,
+ $this->questionHelper
+ );
+
+ $this->encryptionManager->expects($this->once())
+ ->method('isEnabled')
+ ->willReturn($encryptionEnabled);
+
+ $this->consoleInput->expects($this->any())
+ ->method('getArgument')
+ ->with('user')
+ ->willReturn('user1');
+
+ if ($encryptionEnabled) {
+ $calls = [
+ ['core', 'encryption_enabled', 'no'],
+ ['core', 'encryption_enabled', 'yes'],
+ ];
+ $this->config->expects($this->exactly(2))
+ ->method('setAppValue')
+ ->willReturnCallback(function () use (&$calls): void {
+ $expected = array_shift($calls);
+ $this->assertEquals($expected, func_get_args());
+ });
+ $this->questionHelper->expects($this->once())
+ ->method('ask')
+ ->willReturn($continue);
+ if ($continue) {
+ $this->decryptAll->expects($this->once())
+ ->method('decryptAll')
+ ->with($this->consoleInput, $this->consoleOutput, 'user1');
+ } else {
+ $this->decryptAll->expects($this->never())->method('decryptAll');
+ }
+ } else {
+ $this->config->expects($this->never())->method('setAppValue');
+ $this->decryptAll->expects($this->never())->method('decryptAll');
+ $this->questionHelper->expects($this->never())->method('ask');
+ }
+
+ $this->invokePrivate($instance, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+
+ public static function dataTestExecute(): array {
+ return [
+ [true, true],
+ [true, false],
+ [false, true],
+ [false, false]
+ ];
+ }
+
+
+ public function testExecuteFailure(): void {
+ $this->expectException(\Exception::class);
+
+ $instance = new DecryptAll(
+ $this->encryptionManager,
+ $this->appManager,
+ $this->config,
+ $this->decryptAll,
+ $this->questionHelper
+ );
+
+ // make sure that we enable encryption again after a exception was thrown
+ $calls = [
+ ['core', 'encryption_enabled', 'no'],
+ ['core', 'encryption_enabled', 'yes'],
+ ];
+ $this->config->expects($this->exactly(2))
+ ->method('setAppValue')
+ ->willReturnCallback(function () use (&$calls): void {
+ $expected = array_shift($calls);
+ $this->assertEquals($expected, func_get_args());
+ });
+ $this->encryptionManager->expects($this->once())
+ ->method('isEnabled')
+ ->willReturn(true);
+
+ $this->consoleInput->expects($this->any())
+ ->method('getArgument')
+ ->with('user')
+ ->willReturn('user1');
+
+ $this->questionHelper->expects($this->once())
+ ->method('ask')
+ ->willReturn(true);
+
+ $this->decryptAll->expects($this->once())
+ ->method('decryptAll')
+ ->with($this->consoleInput, $this->consoleOutput, 'user1')
+ ->willReturnCallback(function (): void {
+ throw new \Exception();
+ });
+
+ $this->invokePrivate($instance, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+}
diff --git a/tests/Core/Command/Encryption/DisableTest.php b/tests/Core/Command/Encryption/DisableTest.php
new file mode 100644
index 00000000000..a89fd636e47
--- /dev/null
+++ b/tests/Core/Command/Encryption/DisableTest.php
@@ -0,0 +1,74 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+namespace Tests\Core\Command\Encryption;
+
+use OC\Core\Command\Encryption\Disable;
+use OCP\IConfig;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Test\TestCase;
+
+class DisableTest extends TestCase {
+ /** @var \PHPUnit\Framework\MockObject\MockObject */
+ protected $config;
+ /** @var \PHPUnit\Framework\MockObject\MockObject */
+ protected $consoleInput;
+ /** @var \PHPUnit\Framework\MockObject\MockObject */
+ protected $consoleOutput;
+
+ /** @var \Symfony\Component\Console\Command\Command */
+ protected $command;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $config = $this->config = $this->getMockBuilder(IConfig::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
+ $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
+
+ /** @var IConfig $config */
+ $this->command = new Disable($config);
+ }
+
+
+ public static function dataDisable(): array {
+ return [
+ ['yes', true, 'Encryption disabled'],
+ ['no', false, 'Encryption is already disabled'],
+ ];
+ }
+
+ /**
+ *
+ * @param string $oldStatus
+ * @param bool $isUpdating
+ * @param string $expectedString
+ */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataDisable')]
+ public function testDisable($oldStatus, $isUpdating, $expectedString): void {
+ $this->config->expects($this->once())
+ ->method('getAppValue')
+ ->with('core', 'encryption_enabled', $this->anything())
+ ->willReturn($oldStatus);
+
+ $this->consoleOutput->expects($this->once())
+ ->method('writeln')
+ ->with($this->stringContains($expectedString));
+
+ if ($isUpdating) {
+ $this->config->expects($this->once())
+ ->method('setAppValue')
+ ->with('core', 'encryption_enabled', 'no');
+ }
+
+ self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+}
diff --git a/tests/Core/Command/Encryption/EnableTest.php b/tests/Core/Command/Encryption/EnableTest.php
new file mode 100644
index 00000000000..32d1a7576f5
--- /dev/null
+++ b/tests/Core/Command/Encryption/EnableTest.php
@@ -0,0 +1,101 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+namespace Tests\Core\Command\Encryption;
+
+use OC\Core\Command\Encryption\Enable;
+use OCP\Encryption\IManager;
+use OCP\IConfig;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Test\TestCase;
+
+class EnableTest extends TestCase {
+ /** @var \PHPUnit\Framework\MockObject\MockObject */
+ protected $config;
+ /** @var \PHPUnit\Framework\MockObject\MockObject */
+ protected $manager;
+ /** @var \PHPUnit\Framework\MockObject\MockObject */
+ protected $consoleInput;
+ /** @var \PHPUnit\Framework\MockObject\MockObject */
+ protected $consoleOutput;
+
+ /** @var \Symfony\Component\Console\Command\Command */
+ protected $command;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $config = $this->config = $this->getMockBuilder(IConfig::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $manager = $this->manager = $this->getMockBuilder(IManager::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
+ $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
+
+ /** @var \OCP\IConfig $config */
+ /** @var \OCP\Encryption\IManager $manager */
+ $this->command = new Enable($config, $manager);
+ }
+
+
+ public static function dataEnable(): array {
+ return [
+ ['no', null, [], true, 'Encryption enabled', 'No encryption module is loaded'],
+ ['yes', null, [], false, 'Encryption is already enabled', 'No encryption module is loaded'],
+ ['no', null, ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'No default module is set'],
+ ['no', 'OC_NO_MODULE', ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'The current default module does not exist: OC_NO_MODULE'],
+ ['no', 'OC_TEST_MODULE', ['OC_TEST_MODULE' => []], true, 'Encryption enabled', 'Default module: OC_TEST_MODULE'],
+ ];
+ }
+
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataEnable')]
+ public function testEnable(string $oldStatus, ?string $defaultModule, array $availableModules, bool $isUpdating, string $expectedString, string $expectedDefaultModuleString): void {
+ if ($isUpdating) {
+ $this->config->expects($this->once())
+ ->method('setAppValue')
+ ->with('core', 'encryption_enabled', 'yes');
+ }
+
+ $this->manager->expects($this->atLeastOnce())
+ ->method('getEncryptionModules')
+ ->willReturn($availableModules);
+
+ if (empty($availableModules)) {
+ $this->config->expects($this->once())
+ ->method('getAppValue')
+ ->willReturnMap([
+ ['core', 'encryption_enabled', 'no', $oldStatus],
+ ]);
+ } else {
+ $this->config->expects($this->exactly(2))
+ ->method('getAppValue')
+ ->willReturnMap([
+ ['core', 'encryption_enabled', 'no', $oldStatus],
+ ['core', 'default_encryption_module', null, $defaultModule],
+ ]);
+ }
+
+ $calls = [
+ [$expectedString, 0],
+ ['', 0],
+ [$expectedDefaultModuleString, 0],
+ ];
+ $this->consoleOutput->expects($this->exactly(3))
+ ->method('writeln')
+ ->willReturnCallback(function (string $message, int $level) use (&$calls): void {
+ $call = array_shift($calls);
+ $this->assertStringContainsString($call[0], $message);
+ $this->assertSame($call[1], $level);
+ });
+
+ self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+}
diff --git a/tests/Core/Command/Encryption/EncryptAllTest.php b/tests/Core/Command/Encryption/EncryptAllTest.php
new file mode 100644
index 00000000000..15cbe83737d
--- /dev/null
+++ b/tests/Core/Command/Encryption/EncryptAllTest.php
@@ -0,0 +1,94 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+namespace Tests\Core\Command\Encryption;
+
+use OC\Core\Command\Encryption\EncryptAll;
+use OCP\App\IAppManager;
+use OCP\Encryption\IEncryptionModule;
+use OCP\Encryption\IManager;
+use OCP\IConfig;
+use PHPUnit\Framework\MockObject\MockObject;
+use Symfony\Component\Console\Helper\QuestionHelper;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Test\TestCase;
+
+class EncryptAllTest extends TestCase {
+ private IConfig&MockObject $config;
+ private IManager&MockObject $encryptionManager;
+ private IAppManager&MockObject $appManager;
+ private InputInterface&MockObject $consoleInput;
+ private OutputInterface&MockObject $consoleOutput;
+ private QuestionHelper&MockObject $questionHelper;
+ private IEncryptionModule&MockObject $encryptionModule;
+
+ private EncryptAll $command;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->config = $this->createMock(IConfig::class);
+ $this->encryptionManager = $this->createMock(IManager::class);
+ $this->appManager = $this->createMock(IAppManager::class);
+ $this->encryptionModule = $this->createMock(IEncryptionModule::class);
+ $this->questionHelper = $this->createMock(QuestionHelper::class);
+ $this->consoleInput = $this->createMock(InputInterface::class);
+ $this->consoleInput->expects($this->any())
+ ->method('isInteractive')
+ ->willReturn(true);
+ $this->consoleOutput = $this->createMock(OutputInterface::class);
+ }
+
+ public function testEncryptAll(): void {
+ // trash bin needs to be disabled in order to avoid adding dummy files to the users
+ // trash bin which gets deleted during the encryption process
+ $this->appManager->expects($this->once())->method('disableApp')->with('files_trashbin');
+
+ $instance = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
+ $this->invokePrivate($instance, 'forceMaintenanceAndTrashbin');
+ $this->invokePrivate($instance, 'resetMaintenanceAndTrashbin');
+ }
+
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataTestExecute')]
+ public function testExecute($answer, $askResult): void {
+ $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
+
+ $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(true);
+ $this->questionHelper->expects($this->once())->method('ask')->willReturn($askResult);
+
+ if ($answer === 'Y' || $answer === 'y') {
+ $this->encryptionManager->expects($this->once())
+ ->method('getEncryptionModule')->willReturn($this->encryptionModule);
+ $this->encryptionModule->expects($this->once())
+ ->method('encryptAll')->with($this->consoleInput, $this->consoleOutput);
+ } else {
+ $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
+ $this->encryptionModule->expects($this->never())->method('encryptAll');
+ }
+
+ $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+
+ public static function dataTestExecute(): array {
+ return [
+ ['y', true], ['Y', true], ['n', false], ['N', false], ['', false]
+ ];
+ }
+
+
+ public function testExecuteException(): void {
+ $this->expectException(\Exception::class);
+
+ $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
+ $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(false);
+ $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
+ $this->encryptionModule->expects($this->never())->method('encryptAll');
+ $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+}
diff --git a/tests/Core/Command/Encryption/SetDefaultModuleTest.php b/tests/Core/Command/Encryption/SetDefaultModuleTest.php
new file mode 100644
index 00000000000..df38d730db3
--- /dev/null
+++ b/tests/Core/Command/Encryption/SetDefaultModuleTest.php
@@ -0,0 +1,130 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+namespace Tests\Core\Command\Encryption;
+
+use OC\Core\Command\Encryption\SetDefaultModule;
+use OCP\Encryption\IManager;
+use OCP\IConfig;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+use Test\TestCase;
+
+class SetDefaultModuleTest extends TestCase {
+ /** @var \PHPUnit\Framework\MockObject\MockObject|IManager */
+ protected $manager;
+ /** @var \PHPUnit\Framework\MockObject\MockObject|IConfig */
+ protected $config;
+ /** @var \PHPUnit\Framework\MockObject\MockObject */
+ protected $consoleInput;
+ /** @var \PHPUnit\Framework\MockObject\MockObject */
+ protected $consoleOutput;
+
+ /** @var \Symfony\Component\Console\Command\Command */
+ protected $command;
+
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->manager = $this->getMockBuilder(IManager::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->config = $this->getMockBuilder(IConfig::class)
+ ->getMock();
+
+ $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
+ $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
+
+ $this->command = new SetDefaultModule($this->manager, $this->config);
+ }
+
+
+ public static function dataSetDefaultModule(): array {
+ return [
+ ['ID0', 'ID0', null, null, 'already'],
+ ['ID0', 'ID1', 'ID1', true, 'info'],
+ ['ID0', 'ID1', 'ID1', false, 'error'],
+ ];
+ }
+
+ /**
+ *
+ * @param string $oldModule
+ * @param string $newModule
+ * @param string $updateModule
+ * @param bool $updateSuccess
+ * @param string $expectedString
+ */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataSetDefaultModule')]
+ public function testSetDefaultModule($oldModule, $newModule, $updateModule, $updateSuccess, $expectedString): void {
+ $this->consoleInput->expects($this->once())
+ ->method('getArgument')
+ ->with('module')
+ ->willReturn($newModule);
+
+ $this->manager->expects($this->once())
+ ->method('getDefaultEncryptionModuleId')
+ ->willReturn($oldModule);
+
+ $this->config->expects($this->once())
+ ->method('getSystemValue')
+ ->with('maintenance', false)
+ ->willReturn(false);
+
+ if ($updateModule) {
+ $this->manager->expects($this->once())
+ ->method('setDefaultEncryptionModule')
+ ->with($updateModule)
+ ->willReturn($updateSuccess);
+ }
+
+ $this->consoleOutput->expects($this->once())
+ ->method('writeln')
+ ->with($this->stringContains($expectedString));
+
+ self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+
+ /**
+ *
+ * @param string $oldModule
+ * @param string $newModule
+ * @param string $updateModule
+ * @param bool $updateSuccess
+ * @param string $expectedString
+ */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataSetDefaultModule')]
+ public function testMaintenanceMode($oldModule, $newModule, $updateModule, $updateSuccess, $expectedString): void {
+ $this->consoleInput->expects($this->never())
+ ->method('getArgument')
+ ->with('module')
+ ->willReturn($newModule);
+
+ $this->manager->expects($this->never())
+ ->method('getDefaultEncryptionModuleId')
+ ->willReturn($oldModule);
+
+ $this->config->expects($this->once())
+ ->method('getSystemValue')
+ ->with('maintenance', false)
+ ->willReturn(true);
+
+ $calls = [
+ 'Maintenance mode must be disabled when setting default module,',
+ 'in order to load the relevant encryption modules correctly.',
+ ];
+ $this->consoleOutput->expects($this->exactly(2))
+ ->method('writeln')
+ ->willReturnCallback(function ($message) use (&$calls): void {
+ $expected = array_shift($calls);
+ $this->assertStringContainsString($expected, $message);
+ });
+
+ self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+}