diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2016-05-19 10:43:49 +0200 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2016-05-19 11:18:25 +0200 |
commit | 392bc0c6b9967d3e33570ada51a391159d4d69aa (patch) | |
tree | 26bf91653e6aa726a12c0748d2b225ff31b77668 /tests/Core/Command/Encryption | |
parent | 7ca5b35379144d868a36f10c237c78de56f4ed5a (diff) | |
download | nextcloud-server-392bc0c6b9967d3e33570ada51a391159d4d69aa.tar.gz nextcloud-server-392bc0c6b9967d3e33570ada51a391159d4d69aa.zip |
Move tests/core/ to PSR-4
Diffstat (limited to 'tests/Core/Command/Encryption')
-rw-r--r-- | tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php | 381 | ||||
-rw-r--r-- | tests/Core/Command/Encryption/DecryptAllTest.php | 218 | ||||
-rw-r--r-- | tests/Core/Command/Encryption/DisableTest.php | 85 | ||||
-rw-r--r-- | tests/Core/Command/Encryption/EnableTest.php | 119 | ||||
-rw-r--r-- | tests/Core/Command/Encryption/EncryptAllTest.php | 133 | ||||
-rw-r--r-- | tests/Core/Command/Encryption/SetDefaultModuleTest.php | 92 |
6 files changed, 1028 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..2a1f48983f1 --- /dev/null +++ b/tests/Core/Command/Encryption/ChangeKeyStorageRootTest.php @@ -0,0 +1,381 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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/> + * + */ + + +namespace Tests\Core\Command\Encryption; + + +use OC\Core\Command\Encryption\ChangeKeyStorageRoot; +use OC\Encryption\Util; +use OC\Files\View; +use OCP\IConfig; +use OCP\IUserManager; +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 \OCP\UserInterface | \PHPUnit_Framework_MockObject_MockObject */ + protected $userInterface; + + public function setUp() { + parent::setUp(); + + $this->view = $this->getMock('\OC\Files\View'); + $this->userManager = $this->getMock('\OCP\IUserManager'); + $this->config = $this->getMock('\OCP\IConfig'); + $this->util = $this->getMockBuilder('OC\Encryption\Util')->disableOriginalConstructor()->getMock(); + $this->questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper'); + $this->inputInterface = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->outputInterface = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $this->userInterface = $this->getMock('\OCP\UserInterface'); + + $outputFormatterInterface = $this->getMock('Symfony\Component\Console\Formatter\OutputFormatterInterface'); + $this->outputInterface->expects($this->any())->method('getFormatter') + ->willReturn($outputFormatterInterface); + + $this->changeKeyStorageRoot = new ChangeKeyStorageRoot( + $this->view, + $this->userManager, + $this->config, + $this->util, + $this->questionHelper + ); + + } + + /** + * @dataProvider dataTestExecute + */ + public function testExecute($newRoot, $answer, $successMoveKey) { + + $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') + ->setConstructorArgs( + [ + $this->view, + $this->userManager, + $this->config, + $this->util, + $this->questionHelper + ] + )->setMethods(['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 function dataTestExecute() { + return [ + [null, true, true], + [null, true, false], + [null, false, null], + ['/newRoot', null, true], + ['/newRoot', null, false] + ]; + } + + public function testMoveAllKeys() { + + /** @var \OC\Core\Command\Encryption\ChangeKeyStorageRoot $changeKeyStorageRoot */ + $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') + ->setConstructorArgs( + [ + $this->view, + $this->userManager, + $this->config, + $this->util, + $this->questionHelper + ] + )->setMethods(['prepareNewRoot', 'moveSystemKeys', 'moveUserKeys'])->getMock(); + + $changeKeyStorageRoot->expects($this->at(0))->method('prepareNewRoot')->with('newRoot'); + $changeKeyStorageRoot->expects($this->at(1))->method('moveSystemKeys')->with('oldRoot', 'newRoot'); + $changeKeyStorageRoot->expects($this->at(2))->method('moveUserKeys')->with('oldRoot', 'newRoot', $this->outputInterface); + + $this->invokePrivate($changeKeyStorageRoot, 'moveAllKeys', ['oldRoot', 'newRoot', $this->outputInterface]); + + } + + public function testPrepareNewRoot() { + $this->view->expects($this->once())->method('is_dir')->with('newRoot') + ->willReturn(true); + + $this->view->expects($this->once())->method('file_put_contents') + ->with('newRoot/' . \OC\Encryption\Keys\Storage::KEY_STORAGE_MARKER, + 'ownCloud will detect this folder as key storage root only if this file exists'); + + $this->invokePrivate($this->changeKeyStorageRoot, 'prepareNewRoot', ['newRoot']); + } + + /** + * @dataProvider dataTestPrepareNewRootException + * @expectedException \Exception + * + * @param bool $dirExists + * @param bool $couldCreateFile + */ + public function testPrepareNewRootException($dirExists, $couldCreateFile) { + $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 function dataTestPrepareNewRootException() { + return [ + [true, false], + [false, true] + ]; + } + + /** + * @dataProvider dataTestMoveSystemKeys + * + * @param bool $dirExists + * @param bool $targetExists + * @param bool $executeRename + */ + public function testMoveSystemKeys($dirExists, $targetExists, $executeRename) { + + $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') + ->setConstructorArgs( + [ + $this->view, + $this->userManager, + $this->config, + $this->util, + $this->questionHelper + ] + )->setMethods(['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 function dataTestMoveSystemKeys() { + return [ + [true, false, true], + [false, true, false], + [true, true, false], + [false, false, false] + ]; + } + + + public function testMoveUserKeys() { + + $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') + ->setConstructorArgs( + [ + $this->view, + $this->userManager, + $this->config, + $this->util, + $this->questionHelper + ] + )->setMethods(['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]); + } + + /** + * @dataProvider dataTestMoveUserEncryptionFolder + * + * @param bool $userExists + * @param bool $isDir + * @param bool $targetExists + * @param bool $shouldRename + */ + public function testMoveUserEncryptionFolder($userExists, $isDir, $targetExists, $shouldRename) { + + $changeKeyStorageRoot = $this->getMockBuilder('OC\Core\Command\Encryption\ChangeKeyStorageRoot') + ->setConstructorArgs( + [ + $this->view, + $this->userManager, + $this->config, + $this->util, + $this->questionHelper + ] + )->setMethods(['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 function dataTestMoveUserEncryptionFolder() { + 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] + ]; + } + + + /** + * @dataProvider dataTestPrepareParentFolder + */ + public function testPrepareParentFolder($path, $pathExists) { + $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 function dataTestPrepareParentFolder() { + return [ + ['/user/folder/sub_folder/keystorage', true], + ['/user/folder/sub_folder/keystorage', false] + ]; + } + + public function testTargetExists() { + $this->view->expects($this->once())->method('file_exists')->with('path') + ->willReturn(false); + + $this->assertFalse( + $this->invokePrivate($this->changeKeyStorageRoot, 'targetExists', ['path']) + ); + } + + /** + * @expectedException \Exception + */ + public function testTargetExistsException() { + $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..972ea03150c --- /dev/null +++ b/tests/Core/Command/Encryption/DecryptAllTest.php @@ -0,0 +1,218 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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/> + * + */ + + +namespace Tests\Core\Command\Encryption; + + +use OC\Core\Command\Encryption\DecryptAll; +use Test\TestCase; + +class DecryptAllTest extends TestCase { + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IConfig */ + protected $config; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\Encryption\IManager */ + protected $encryptionManager; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\App\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; + + public function setUp() { + parent::setUp(); + + $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->encryptionManager = $this->getMockBuilder('OCP\Encryption\IManager') + ->disableOriginalConstructor() + ->getMock(); + $this->appManager = $this->getMockBuilder('OCP\App\IAppManager') + ->disableOriginalConstructor() + ->getMock(); + $this->questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') + ->disableOriginalConstructor() + ->getMock(); + $this->decryptAll = $this->getMockBuilder('OC\Encryption\DecryptAll') + ->disableOriginalConstructor()->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + $this->config->expects($this->any()) + ->method('getSystemValue') + ->with('singleuser', false) + ->willReturn(false); + $this->appManager->expects($this->any()) + ->method('isEnabledForUser') + ->with('files_trashbin')->willReturn(true); + + } + + public function testSingleUserAndTrashbin() { + + // on construct we enable single-user-mode and disable the trash bin + $this->config->expects($this->at(1)) + ->method('setSystemValue') + ->with('singleuser', true); + $this->appManager->expects($this->once()) + ->method('disableApp') + ->with('files_trashbin'); + + // on destruct wi disable single-user-mode again and enable the trash bin + $this->config->expects($this->at(2)) + ->method('setSystemValue') + ->with('singleuser', false); + $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, 'forceSingleUserAndTrashbin'); + + $this->assertTrue( + $this->invokePrivate($instance, 'wasTrashbinEnabled') + ); + + $this->assertFalse( + $this->invokePrivate($instance, 'wasSingleUserModeEnabled') + ); + $this->invokePrivate($instance, 'resetSingleUserAndTrashbin'); + } + + /** + * @dataProvider dataTestExecute + */ + public function testExecute($encryptionEnabled, $continue) { + + $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) { + $this->config->expects($this->at(0)) + ->method('setAppValue') + ->with('core', 'encryption_enabled', 'no'); + $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'); + $this->config->expects($this->at(1)) + ->method('setAppValue') + ->with('core', 'encryption_enabled', 'yes'); + } + } 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 function dataTestExecute() { + return [ + [true, true], + [true, false], + [false, true], + [false, false] + ]; + } + + /** + * @expectedException \Exception + */ + public function testExecuteFailure() { + $instance = new DecryptAll( + $this->encryptionManager, + $this->appManager, + $this->config, + $this->decryptAll, + $this->questionHelper + ); + + $this->config->expects($this->at(0)) + ->method('setAppValue') + ->with('core', 'encryption_enabled', 'no'); + + // make sure that we enable encryption again after a exception was thrown + $this->config->expects($this->at(3)) + ->method('setAppValue') + ->with('core', 'encryption_enabled', 'yes'); + + $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() { 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..dfd06e2e26e --- /dev/null +++ b/tests/Core/Command/Encryption/DisableTest.php @@ -0,0 +1,85 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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/> + * + */ + +namespace Tests\Core\Command\Encryption; + + +use OC\Core\Command\Encryption\Disable; +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() { + parent::setUp(); + + $config = $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\IConfig $config */ + $this->command = new Disable($config); + } + + + public function dataDisable() { + return [ + ['yes', true, 'Encryption disabled'], + ['no', false, 'Encryption is already disabled'], + ]; + } + + /** + * @dataProvider dataDisable + * + * @param string $oldStatus + * @param bool $isUpdating + * @param string $expectedString + */ + public function testDisable($oldStatus, $isUpdating, $expectedString) { + $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..e2357464aa1 --- /dev/null +++ b/tests/Core/Command/Encryption/EnableTest.php @@ -0,0 +1,119 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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/> + * + */ + +namespace Tests\Core\Command\Encryption; + + +use OC\Core\Command\Encryption\Enable; +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() { + parent::setUp(); + + $config = $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $manager = $this->manager = $this->getMockBuilder('OCP\Encryption\IManager') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\IConfig $config */ + /** @var \OCP\Encryption\IManager $manager */ + $this->command = new Enable($config, $manager); + } + + + public function dataEnable() { + 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'], + ]; + } + + /** + * @dataProvider dataEnable + * + * @param string $oldStatus + * @param string $defaultModule + * @param array $availableModules + * @param bool $isUpdating + * @param string $expectedString + * @param string $expectedDefaultModuleString + */ + public function testEnable($oldStatus, $defaultModule, $availableModules, $isUpdating, $expectedString, $expectedDefaultModuleString) { + $invokeCount = 0; + $this->config->expects($this->at($invokeCount)) + ->method('getAppValue') + ->with('core', 'encryption_enabled', $this->anything()) + ->willReturn($oldStatus); + $invokeCount++; + + if ($isUpdating) { + $this->config->expects($this->once()) + ->method('setAppValue') + ->with('core', 'encryption_enabled', 'yes'); + $invokeCount++; + } + + $this->manager->expects($this->atLeastOnce()) + ->method('getEncryptionModules') + ->willReturn($availableModules); + + if (!empty($availableModules)) { + $this->config->expects($this->at($invokeCount)) + ->method('getAppValue') + ->with('core', 'default_encryption_module', $this->anything()) + ->willReturn($defaultModule); + } + + $this->consoleOutput->expects($this->at(0)) + ->method('writeln') + ->with($this->stringContains($expectedString)); + + $this->consoleOutput->expects($this->at(1)) + ->method('writeln') + ->with(''); + + $this->consoleOutput->expects($this->at(2)) + ->method('writeln') + ->with($this->stringContains($expectedDefaultModuleString)); + + 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..128b4caa148 --- /dev/null +++ b/tests/Core/Command/Encryption/EncryptAllTest.php @@ -0,0 +1,133 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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/> + * + */ + + +namespace Tests\Core\Command\Encryption; + + +use OC\Core\Command\Encryption\EncryptAll; +use Test\TestCase; + +class EncryptAllTest extends TestCase { + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IConfig */ + protected $config; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\Encryption\IManager */ + protected $encryptionManager; + + /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\App\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 | \OCP\Encryption\IEncryptionModule */ + protected $encryptionModule; + + /** @var EncryptAll */ + protected $command; + + protected function setUp() { + parent::setUp(); + + $this->config = $this->getMockBuilder('OCP\IConfig') + ->disableOriginalConstructor() + ->getMock(); + $this->encryptionManager = $this->getMockBuilder('OCP\Encryption\IManager') + ->disableOriginalConstructor() + ->getMock(); + $this->appManager = $this->getMockBuilder('OCP\App\IAppManager') + ->disableOriginalConstructor() + ->getMock(); + $this->encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') + ->disableOriginalConstructor() + ->getMock(); + $this->questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + } + + public function testEncryptAll() { + // 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'); + // enable single user mode to avoid that other user login during encryption + // destructor should disable the single user mode again + $this->config->expects($this->once())->method('getSystemValue')->with('singleuser', false)->willReturn(false); + $this->config->expects($this->at(1))->method('setSystemValue')->with('singleuser', true); + $this->config->expects($this->at(2))->method('setSystemValue')->with('singleuser', false); + + $instance = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper); + $this->invokePrivate($instance, 'forceSingleUserAndTrashbin'); + $this->invokePrivate($instance, 'resetSingleUserAndTrashbin'); + } + + /** + * @dataProvider dataTestExecute + */ + public function testExecute($answer, $askResult) { + + $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 function dataTestExecute() { + return [ + ['y', true], ['Y', true], ['n', false], ['N', false], ['', false] + ]; + } + + /** + * @expectedException \Exception + */ + public function testExecuteException() { + $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..3230a57db07 --- /dev/null +++ b/tests/Core/Command/Encryption/SetDefaultModuleTest.php @@ -0,0 +1,92 @@ +<?php +/** + * @author Joas Schilling <nickvergessen@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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/> + * + */ + +namespace Tests\Core\Command\Encryption; + + +use OC\Core\Command\Encryption\SetDefaultModule; +use Test\TestCase; + +class SetDefaultModuleTest extends TestCase { + /** @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() { + parent::setUp(); + + $manager = $this->manager = $this->getMockBuilder('OCP\Encryption\IManager') + ->disableOriginalConstructor() + ->getMock(); + $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + + /** @var \OCP\Encryption\IManager $manager */ + $this->command = new SetDefaultModule($manager); + } + + + public function dataSetDefaultModule() { + return [ + ['ID0', 'ID0', null, null, 'already'], + ['ID0', 'ID1', 'ID1', true, 'info'], + ['ID0', 'ID1', 'ID1', false, 'error'], + ]; + } + + /** + * @dataProvider dataSetDefaultModule + * + * @param string $oldModule + * @param string $newModule + * @param string $updateModule + * @param bool $updateSuccess + * @param string $expectedString + */ + public function testSetDefaultModule($oldModule, $newModule, $updateModule, $updateSuccess, $expectedString) { + $this->consoleInput->expects($this->once()) + ->method('getArgument') + ->with('module') + ->willReturn($newModule); + + $this->manager->expects($this->once()) + ->method('getDefaultEncryptionModuleId') + ->willReturn($oldModule); + 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]); + } +} |