123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- <?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\FileInfo;
- use OC\Files\View;
- use OCP\IUserManager;
- use Symfony\Component\Console\Formatter\OutputFormatterInterface;
- use Test\TestCase;
- /**
- * Class DecryptAllTest
- *
- * @group DB
- *
- * @package Test\Encryption
- */
- 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->createMock(OutputFormatterInterface::class));
- $this->instance = new DecryptAll($this->encryptionManager, $this->userManager, $this->view);
- $this->invokePrivate($this->instance, 'input', [$this->inputInterface]);
- $this->invokePrivate($this->instance, 'output', [$this->outputInterface]);
- }
- public function dataDecryptAll() {
- return [
- [true, 'user1', true],
- [false, 'user1', true],
- [true, '0', true],
- [false, '0', true],
- [true, '', false],
- ];
- }
- /**
- * @dataProvider dataDecryptAll
- * @param bool $prepareResult
- * @param string $user
- * @param bool $userExistsChecked
- */
- public function testDecryptAll($prepareResult, $user, $userExistsChecked) {
- if ($userExistsChecked) {
- $this->userManager->expects($this->once())->method('userExists')->willReturn(true);
- } else {
- $this->userManager->expects($this->never())->method('userExists');
- }
- /** @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);
- }
- /**
- * test decrypt all call with a user who doesn't exists
- */
- public function testDecryptAllWrongUser() {
- $this->userManager->expects($this->once())->method('userExists')->willReturn(false);
- $this->outputInterface->expects($this->once())->method('writeln')
- ->with('User "user1" does not exist. Please check the username and try again');
- $this->assertFalse(
- $this->instance->decryptAll($this->inputInterface, $this->outputInterface, 'user1')
- );
- }
- public function dataTrueFalse() {
- return [
- [true],
- [false],
- ];
- }
- /**
- * @dataProvider dataTrueFalse
- * @param bool $success
- */
- 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();
- $storage = $this->getMockBuilder('OCP\Files\Storage')
- ->disableOriginalConstructor()->getMock();
- $sharedStorage = $this->getMockBuilder('OCP\Files\Storage')
- ->disableOriginalConstructor()->getMock();
- $sharedStorage->expects($this->once())->method('instanceOfStorage')
- ->with('OCA\Files_Sharing\SharedStorage')->willReturn(true);
- $this->view->expects($this->at(0))->method('getDirectoryContent')
- ->with('/user1/files')->willReturn(
- [
- new FileInfo('path', $storage, 'intPath', ['name' => 'foo', 'type'=>'dir'], null),
- new FileInfo('path', $storage, 'intPath', ['name' => 'bar', 'type'=>'file', 'encrypted'=>true], null),
- new FileInfo('path', $sharedStorage, 'intPath', ['name' => 'shared', 'type'=>'file', 'encrypted'=>true], null),
- ]
- );
- $this->view->expects($this->at(3))->method('getDirectoryContent')
- ->with('/user1/files/foo')->willReturn(
- [
- new FileInfo('path', $storage, 'intPath', ['name' => 'subfile', 'type'=>'file', 'encrypted'=>true], null)
- ]
- );
- $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])
- );
- }
- }
|