Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable14] Improve encrypt all / decrypt all #12045

Merged
merged 2 commits into from
Oct 25, 2018
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
6 changes: 6 additions & 0 deletions apps/encryption/lib/Crypto/EncryptAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,12 @@ protected function encryptUsersFiles($uid, ProgressBar $progress, $userCount) {
*/
protected function encryptFile($path) {

// skip already encrypted files
$fileInfo = $this->rootView->getFileInfo($path);
if ($fileInfo !== false && $fileInfo->isEncrypted()) {
return true;
}

$source = $path;
$target = $path . '.encrypted.' . time();

Expand Down
33 changes: 33 additions & 0 deletions apps/encryption/tests/Crypto/EncryptAllTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use OCA\Encryption\KeyManager;
use OCA\Encryption\Users\Setup;
use OCA\Encryption\Util;
use OCP\Files\FileInfo;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IUserManager;
Expand Down Expand Up @@ -354,4 +355,36 @@ public function testGenerateOneTimePassword() {
$this->assertSame($password, $userPasswords['user1']);
}

/**
* @dataProvider dataTestEncryptFile
* @param $isEncrypted
*/
public function testEncryptFile($isEncrypted) {
$fileInfo = $this->createMock(FileInfo::class);
$fileInfo->expects($this->any())->method('isEncrypted')
->willReturn($isEncrypted);
$this->view->expects($this->any())->method('getFileInfo')
->willReturn($fileInfo);


if($isEncrypted) {
$this->view->expects($this->never())->method('copy');
$this->view->expects($this->never())->method('rename');
} else {
$this->view->expects($this->once())->method('copy');
$this->view->expects($this->once())->method('rename');
}

$this->assertTrue(
$this->invokePrivate($this->encryptAll, 'encryptFile', ['foo.txt'])
);
}

public function dataTestEncryptFile() {
return [
[true],
[false],
];
}

}
6 changes: 6 additions & 0 deletions lib/private/Encryption/DecryptAll.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,12 @@ protected function decryptUsersFiles($uid, ProgressBar $progress, $userCount) {
*/
protected function decryptFile($path) {

// skip already decrypted files
$fileInfo = $this->rootView->getFileInfo($path);
if ($fileInfo !== false && !$fileInfo->isEncrypted()) {
return true;
}

$source = $path;
$target = $path . '.decrypted.' . $this->getTimestamp();

Expand Down
41 changes: 31 additions & 10 deletions tests/lib/Encryption/DecryptAllTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,10 @@ function($path) {

}

public function testDecryptFile() {
/**
* @dataProvider dataTrueFalse
*/
public function testDecryptFile($isEncrypted) {

$path = 'test.txt';

Expand All @@ -327,15 +330,26 @@ public function testDecryptFile() {
->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);

$fileInfo = $this->createMock(FileInfo::class);
$fileInfo->expects($this->any())->method('isEncrypted')
->willReturn($isEncrypted);
$this->view->expects($this->any())->method('getFileInfo')
->willReturn($fileInfo);

if ($isEncrypted) {
$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);
} else {
$instance->expects($this->never())->method('getTimestamp');
$this->view->expects($this->never())->method('copy');
$this->view->expects($this->never())->method('rename');
}
$this->assertTrue(
$this->invokePrivate($instance, 'decryptFile', [$path])
);
Expand All @@ -356,6 +370,13 @@ public function testDecryptFileFailure() {
->setMethods(['getTimestamp'])
->getMock();


$fileInfo = $this->createMock(FileInfo::class);
$fileInfo->expects($this->any())->method('isEncrypted')
->willReturn(true);
$this->view->expects($this->any())->method('getFileInfo')
->willReturn($fileInfo);

$instance->expects($this->any())->method('getTimestamp')->willReturn(42);

$this->view->expects($this->once())
Expand Down