diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2015-09-16 00:11:41 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-09-16 00:11:41 +0200 |
commit | f6f7d8cf94f74ead433714a5170e1984886617a8 (patch) | |
tree | 2c5d59f7e7c6cb48323d76f7d09f7f9111fc3657 /tests | |
parent | ac1239220d4f73e18bca2ecceb7170ce6721b1f6 (diff) | |
parent | 9bd4f2d41e5a211ad60a83bfcdc60b8633076a54 (diff) | |
download | nextcloud-server-f6f7d8cf94f74ead433714a5170e1984886617a8.tar.gz nextcloud-server-f6f7d8cf94f74ead433714a5170e1984886617a8.zip |
Merge pull request #18938 from owncloud/occ_decrypt_all2
occ script to disable encryption and to decrypt all files again
Diffstat (limited to 'tests')
-rw-r--r-- | tests/core/command/encryption/decryptalltest.php | 215 | ||||
-rw-r--r-- | tests/lib/encryption/decryptalltest.php | 321 | ||||
-rw-r--r-- | tests/lib/files/storage/wrapper/encryption.php | 2 | ||||
-rw-r--r-- | tests/lib/files/stream/encryption.php | 2 |
4 files changed, 538 insertions, 2 deletions
diff --git a/tests/core/command/encryption/decryptalltest.php b/tests/core/command/encryption/decryptalltest.php new file mode 100644 index 00000000000..b7fd630e9b0 --- /dev/null +++ b/tests/core/command/encryption/decryptalltest.php @@ -0,0 +1,215 @@ +<?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 testConstructDesctruct() { + // 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->assertTrue( + $this->invokePrivate($instance, 'wasTrashbinEnabled') + ); + + $this->assertFalse( + $this->invokePrivate($instance, 'wasSingleUserModeEnabled') + ); + } + + /** + * @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(1)) + ->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/lib/encryption/decryptalltest.php b/tests/lib/encryption/decryptalltest.php new file mode 100644 index 00000000000..eb3bc721f86 --- /dev/null +++ b/tests/lib/encryption/decryptalltest.php @@ -0,0 +1,321 @@ +<?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 Test\Encryption; + + +use OC\Encryption\DecryptAll; +use OC\Encryption\Exceptions\DecryptionFailedException; +use OC\Encryption\Manager; +use OC\Files\View; +use OCP\IUserManager; +use Test\TestCase; + +class DecryptAllTest extends TestCase { + + /** @var \PHPUnit_Framework_MockObject_MockObject | IUserManager */ + protected $userManager; + + /** @var \PHPUnit_Framework_MockObject_MockObject | Manager */ + protected $encryptionManager; + + /** @var \PHPUnit_Framework_MockObject_MockObject | View */ + protected $view; + + /** @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 DecryptAll */ + protected $instance; + + public function setUp() { + parent::setUp(); + + $this->userManager = $this->getMockBuilder('OCP\IUserManager') + ->disableOriginalConstructor()->getMock(); + $this->encryptionManager = $this->getMockBuilder('OC\Encryption\Manager') + ->disableOriginalConstructor()->getMock(); + $this->view = $this->getMockBuilder('OC\Files\View') + ->disableOriginalConstructor()->getMock(); + $this->inputInterface = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface') + ->disableOriginalConstructor()->getMock(); + $this->outputInterface = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface') + ->disableOriginalConstructor()->getMock(); + $this->userInterface = $this->getMockBuilder('OCP\UserInterface') + ->disableOriginalConstructor()->getMock(); + + $this->outputInterface->expects($this->any())->method('getFormatter') + ->willReturn($this->getMock('\Symfony\Component\Console\Formatter\OutputFormatterInterface')); + + $this->instance = new DecryptAll($this->encryptionManager, $this->userManager, $this->view); + + $this->invokePrivate($this->instance, 'input', [$this->inputInterface]); + $this->invokePrivate($this->instance, 'output', [$this->outputInterface]); + } + + /** + * @dataProvider dataTrueFalse + */ + public function testDecryptAll($prepareResult) { + + $user = 'user1'; + + /** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject | $instance */ + $instance = $this->getMockBuilder('OC\Encryption\DecryptAll') + ->setConstructorArgs( + [ + $this->encryptionManager, + $this->userManager, + $this->view + ] + ) + ->setMethods(['prepareEncryptionModules', 'decryptAllUsersFiles']) + ->getMock(); + + $instance->expects($this->once()) + ->method('prepareEncryptionModules') + ->with($user) + ->willReturn($prepareResult); + + if ($prepareResult) { + $instance->expects($this->once()) + ->method('decryptAllUsersFiles') + ->with($user); + } else { + $instance->expects($this->never())->method('decryptAllUsersFiles'); + } + + $instance->decryptAll($this->inputInterface, $this->outputInterface, $user); + } + + public function dataTrueFalse() { + return [ + [true], + [false] + ]; + } + + /** + * @dataProvider dataTrueFalse + */ + public function testPrepareEncryptionModules($success) { + + $user = 'user1'; + + $dummyEncryptionModule = $this->getMockBuilder('OCP\Encryption\IEncryptionModule') + ->disableOriginalConstructor()->getMock(); + + $dummyEncryptionModule->expects($this->once()) + ->method('prepareDecryptAll') + ->with($this->inputInterface, $this->outputInterface, $user) + ->willReturn($success); + + $callback = function() use ($dummyEncryptionModule) {return $dummyEncryptionModule;}; + $moduleDescription = [ + 'id' => 'id', + 'displayName' => 'displayName', + 'callback' => $callback + ]; + + $this->encryptionManager->expects($this->once()) + ->method('getEncryptionModules') + ->willReturn([$moduleDescription]); + + $this->assertSame($success, + $this->invokePrivate($this->instance, 'prepareEncryptionModules', [$user]) + ); + } + + /** + * @dataProvider dataTestDecryptAllUsersFiles + */ + public function testDecryptAllUsersFiles($user) { + + /** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject | $instance */ + $instance = $this->getMockBuilder('OC\Encryption\DecryptAll') + ->setConstructorArgs( + [ + $this->encryptionManager, + $this->userManager, + $this->view + ] + ) + ->setMethods(['decryptUsersFiles']) + ->getMock(); + + $this->invokePrivate($instance, 'input', [$this->inputInterface]); + $this->invokePrivate($instance, 'output', [$this->outputInterface]); + + if (empty($user)) { + $this->userManager->expects($this->once()) + ->method('getBackends') + ->willReturn([$this->userInterface]); + $this->userInterface->expects($this->any()) + ->method('getUsers') + ->willReturn(['user1', 'user2']); + $instance->expects($this->at(0)) + ->method('decryptUsersFiles') + ->with('user1'); + $instance->expects($this->at(1)) + ->method('decryptUsersFiles') + ->with('user2'); + } else { + $instance->expects($this->once()) + ->method('decryptUsersFiles') + ->with($user); + } + + $this->invokePrivate($instance, 'decryptAllUsersFiles', [$user]); + } + + public function dataTestDecryptAllUsersFiles() { + return [ + ['user1'], + [''] + ]; + } + + public function testDecryptUsersFiles() { + /** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject $instance */ + $instance = $this->getMockBuilder('OC\Encryption\DecryptAll') + ->setConstructorArgs( + [ + $this->encryptionManager, + $this->userManager, + $this->view + ] + ) + ->setMethods(['decryptFile']) + ->getMock(); + + $this->view->expects($this->at(0))->method('getDirectoryContent') + ->with('/user1/files')->willReturn( + [ + ['name' => 'foo', 'type'=>'dir'], + ['name' => 'bar', 'type'=>'file'], + ] + ); + + $this->view->expects($this->at(3))->method('getDirectoryContent') + ->with('/user1/files/foo')->willReturn( + [ + ['name' => 'subfile', 'type'=>'file'] + ] + ); + + $this->view->expects($this->any())->method('is_dir') + ->willReturnCallback( + function($path) { + if ($path === '/user1/files/foo') { + return true; + } + return false; + } + ); + + $instance->expects($this->at(0)) + ->method('decryptFile') + ->with('/user1/files/bar'); + $instance->expects($this->at(1)) + ->method('decryptFile') + ->with('/user1/files/foo/subfile'); + + $progressBar = $this->getMockBuilder('Symfony\Component\Console\Helper\ProgressBar') + ->disableOriginalConstructor()->getMock(); + + $this->invokePrivate($instance, 'decryptUsersFiles', ['user1', $progressBar, '']); + + } + + public function testDecryptFile() { + + $path = 'test.txt'; + + /** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject $instance */ + $instance = $this->getMockBuilder('OC\Encryption\DecryptAll') + ->setConstructorArgs( + [ + $this->encryptionManager, + $this->userManager, + $this->view + ] + ) + ->setMethods(['getTimestamp']) + ->getMock(); + + $instance->expects($this->any())->method('getTimestamp')->willReturn(42); + + $this->view->expects($this->once()) + ->method('copy') + ->with($path, $path . '.decrypted.42'); + $this->view->expects($this->once()) + ->method('rename') + ->with($path . '.decrypted.42', $path); + + $this->assertTrue( + $this->invokePrivate($instance, 'decryptFile', [$path]) + ); + } + + public function testDecryptFileFailure() { + $path = 'test.txt'; + + /** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject $instance */ + $instance = $this->getMockBuilder('OC\Encryption\DecryptAll') + ->setConstructorArgs( + [ + $this->encryptionManager, + $this->userManager, + $this->view + ] + ) + ->setMethods(['getTimestamp']) + ->getMock(); + + $instance->expects($this->any())->method('getTimestamp')->willReturn(42); + + $this->view->expects($this->once()) + ->method('copy') + ->with($path, $path . '.decrypted.42') + ->willReturnCallback(function() { throw new DecryptionFailedException();}); + + $this->view->expects($this->never())->method('rename'); + $this->view->expects($this->once()) + ->method('file_exists') + ->with($path . '.decrypted.42') + ->willReturn(true); + $this->view->expects($this->once()) + ->method('unlink') + ->with($path . '.decrypted.42'); + + $this->assertFalse( + $this->invokePrivate($instance, 'decryptFile', [$path]) + ); + } + +} diff --git a/tests/lib/files/storage/wrapper/encryption.php b/tests/lib/files/storage/wrapper/encryption.php index 36a5b288c64..44e910b901f 100644 --- a/tests/lib/files/storage/wrapper/encryption.php +++ b/tests/lib/files/storage/wrapper/encryption.php @@ -194,7 +194,7 @@ class Encryption extends \Test\Files\Storage\Storage { protected function buildMockModule() { $this->encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') ->disableOriginalConstructor() - ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll']) + ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll', 'prepareDecryptAll']) ->getMock(); $this->encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE'); diff --git a/tests/lib/files/stream/encryption.php b/tests/lib/files/stream/encryption.php index ed3b5b1b156..f9d8f076b63 100644 --- a/tests/lib/files/stream/encryption.php +++ b/tests/lib/files/stream/encryption.php @@ -305,7 +305,7 @@ class Encryption extends \Test\TestCase { protected function buildMockModule() { $encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule') ->disableOriginalConstructor() - ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll']) + ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll', 'prepareDecryptAll']) ->getMock(); $encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE'); |