Skip to content

Commit

Permalink
Merge pull request #12018 from nextcloud/improve-encrypt-all
Browse files Browse the repository at this point in the history
Improve encrypt all / decrypt all
  • Loading branch information
MorrisJobke authored Oct 24, 2018
2 parents 37782b1 + d76a87f commit 410bd9d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 10 deletions.
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

0 comments on commit 410bd9d

Please sign in to comment.