Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/register_command.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
\OC::$server->getEncryptionManager(),
\OC::$server->getAppManager(),
\OC::$server->getConfig(),
new \OC\Encryption\DecryptAll(\OC::$server->getEncryptionManager(), \OC::$server->getUserManager(), new \OC\Files\View()),
new \OC\Encryption\DecryptAll(\OC::$server->getEncryptionManager(), \OC::$server->getUserManager(), new \OC\Files\View(), \OC::$server->getLogger()),
new \Symfony\Component\Console\Helper\QuestionHelper())
);

Expand Down
16 changes: 14 additions & 2 deletions lib/private/Encryption/DecryptAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OC\Encryption\Exceptions\DecryptionFailedException;
use OC\Files\View;
use \OCP\Encryption\IEncryptionModule;
use OCP\ILogger;
use OCP\IUserManager;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -54,20 +55,26 @@ class DecryptAll {
/** @var array files which couldn't be decrypted */
protected $failed;

/** @var ILogger */
protected $logger;

/**
* @param Manager $encryptionManager
* @param IUserManager $userManager
* @param View $rootView
* @param ILogger $logger
*/
public function __construct(
Manager $encryptionManager,
IUserManager $userManager,
View $rootView
View $rootView,
ILogger $logger
) {
$this->encryptionManager = $encryptionManager;
$this->userManager = $userManager;
$this->rootView = $rootView;
$this->failed = [];
$this->logger = $logger;
}

/**
Expand Down Expand Up @@ -141,6 +148,7 @@ protected function prepareEncryptionModules($user) {
* iterate over all user and encrypt their files
*
* @param string $user which users files should be decrypted, default = all users
* @return bool
*/
protected function decryptAllUsersFiles($user = '') {

Expand Down Expand Up @@ -198,7 +206,7 @@ protected function decryptAllUsersFiles($user = '') {
$progress->finish();

$this->output->writeln("\n\n");

return true;
}

/**
Expand Down Expand Up @@ -239,6 +247,10 @@ protected function decryptUsersFiles($uid, ProgressBar $progress, $userCount) {
}
}
} catch (\Exception $e) {
$this->logger->logException($e, [
'message' => "Exception trying to decrypt file <$path> for user <$uid>",
'app' => __CLASS__
]);
if (isset($this->failed[$uid])) {
$this->failed[$uid][] = $path;
} else {
Expand Down
96 changes: 64 additions & 32 deletions tests/lib/Encryption/DecryptAllTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@
use OC\Encryption\Manager;
use OC\Files\FileInfo;
use OC\Files\View;
use OCA\Files_Sharing\SharedStorage;
use OCP\Encryption\IEncryptionModule;
use OCP\ILogger;
use OCP\IUserManager;
use OCP\UserInterface;
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Test\TestCase;

/**
Expand All @@ -49,6 +58,9 @@ class DecryptAllTest extends TestCase {
/** @var \PHPUnit_Framework_MockObject_MockObject | View */
protected $view;

/** @var \PHPUnit_Framework_MockObject_MockObject | ILogger */
protected $logger;

/** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Input\InputInterface */
protected $inputInterface;

Expand All @@ -64,23 +76,25 @@ class DecryptAllTest extends TestCase {
public function setUp() {
parent::setUp();

$this->userManager = $this->getMockBuilder('OCP\IUserManager')
$this->userManager = $this->getMockBuilder(IUserManager::class)
->disableOriginalConstructor()->getMock();
$this->encryptionManager = $this->getMockBuilder(Manager::class)
->disableOriginalConstructor()->getMock();
$this->encryptionManager = $this->getMockBuilder('OC\Encryption\Manager')
$this->view = $this->getMockBuilder(View::class)
->disableOriginalConstructor()->getMock();
$this->view = $this->getMockBuilder('OC\Files\View')
$this->logger = $this->getMockBuilder(ILogger::class)
->disableOriginalConstructor()->getMock();
$this->inputInterface = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')
$this->inputInterface = $this->getMockBuilder(InputInterface::class)
->disableOriginalConstructor()->getMock();
$this->outputInterface = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')
$this->outputInterface = $this->getMockBuilder(OutputInterface::class)
->disableOriginalConstructor()->getMock();
$this->userInterface = $this->getMockBuilder('OCP\UserInterface')
$this->userInterface = $this->getMockBuilder(UserInterface::class)
->disableOriginalConstructor()->getMock();

$this->outputInterface->expects($this->any())->method('getFormatter')
->willReturn($this->createMock('\Symfony\Component\Console\Formatter\OutputFormatterInterface'));
->willReturn($this->createMock(OutputFormatterInterface::class));

$this->instance = new DecryptAll($this->encryptionManager, $this->userManager, $this->view);
$this->instance = new DecryptAll($this->encryptionManager, $this->userManager, $this->view, $this->logger);

$this->invokePrivate($this->instance, 'input', [$this->inputInterface]);
$this->invokePrivate($this->instance, 'output', [$this->outputInterface]);
Expand Down Expand Up @@ -110,12 +124,13 @@ public function testDecryptAll($prepareResult, $user, $userExistsChecked) {
$this->userManager->expects($this->never())->method('userExists');
}
/** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject | $instance */
$instance = $this->getMockBuilder('OC\Encryption\DecryptAll')
$instance = $this->getMockBuilder(DecryptAll::class)
->setConstructorArgs(
[
$this->encryptionManager,
$this->userManager,
$this->view
$this->view,
$this->logger
]
)
->setMethods(['prepareEncryptionModules', 'decryptAllUsersFiles'])
Expand Down Expand Up @@ -169,7 +184,7 @@ public function testPrepareEncryptionModules($success) {

$user = 'user1';

$dummyEncryptionModule = $this->getMockBuilder('OCP\Encryption\IEncryptionModule')
$dummyEncryptionModule = $this->getMockBuilder(IEncryptionModule::class)
->disableOriginalConstructor()->getMock();

$dummyEncryptionModule->expects($this->once())
Expand Down Expand Up @@ -199,12 +214,13 @@ public function testPrepareEncryptionModules($success) {
public function testDecryptAllUsersFiles($user) {

/** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject | $instance */
$instance = $this->getMockBuilder('OC\Encryption\DecryptAll')
$instance = $this->getMockBuilder(DecryptAll::class)
->setConstructorArgs(
[
$this->encryptionManager,
$this->userManager,
$this->view
$this->view,
$this->logger
]
)
->setMethods(['decryptUsersFiles'])
Expand Down Expand Up @@ -242,18 +258,27 @@ public function dataTestDecryptAllUsersFiles() {
];
}

public function testDecryptUsersFiles() {
$storage = $this->getMockBuilder('OCA\Files_Sharing\SharedStorage')
->disableOriginalConstructor()
->getMock();
public function providesData() {
return[
[true],
[false]
];
}
/**
* @param $throwsExceptionInDecrypt
* @dataProvider providesData
*/
public function testDecryptUsersFiles($throwsExceptionInDecrypt) {
$storage = $this->createMock(SharedStorage::class);

/** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject $instance */
$instance = $this->getMockBuilder('OC\Encryption\DecryptAll')
$instance = $this->getMockBuilder(DecryptAll::class)
->setConstructorArgs(
[
$this->encryptionManager,
$this->userManager,
$this->view
$this->view,
$this->logger
]
)
->setMethods(['decryptFile'])
Expand Down Expand Up @@ -284,31 +309,37 @@ function($path) {
}
);

$instance->expects($this->at(0))
->method('decryptFile')
->with('/user1/files/bar');
$instance->expects($this->at(1))
->method('decryptFile')
->with('/user1/files/foo/subfile');
if ($throwsExceptionInDecrypt) {
$instance->expects($this->at(0))
->method('decryptFile')
->with('/user1/files/bar')
->willThrowException(new \Exception());
} else {
$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();
$progressBar = new ProgressBar(new NullOutput());

$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')
$instance = $this->getMockBuilder(DecryptAll::class)
->setConstructorArgs(
[
$this->encryptionManager,
$this->userManager,
$this->view
$this->view,
$this->logger
]
)
->setMethods(['getTimestamp'])
Expand All @@ -332,12 +363,13 @@ public function testDecryptFileFailure() {
$path = 'test.txt';

/** @var DecryptAll | \PHPUnit_Framework_MockObject_MockObject $instance */
$instance = $this->getMockBuilder('OC\Encryption\DecryptAll')
$instance = $this->getMockBuilder(DecryptAll::class)
->setConstructorArgs(
[
$this->encryptionManager,
$this->userManager,
$this->view
$this->view,
$this->logger
]
)
->setMethods(['getTimestamp'])
Expand Down