Merge pull request #12018 from nextcloud/improve-encrypt-all

Improve encrypt all / decrypt all
This commit is contained in:
Morris Jobke 2018-10-24 18:27:03 +02:00 committed by GitHub
commit 410bd9d784
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 8 deletions

View File

@ -295,6 +295,12 @@ class EncryptAll {
*/ */
protected function encryptFile($path) { protected function encryptFile($path) {
// skip already encrypted files
$fileInfo = $this->rootView->getFileInfo($path);
if ($fileInfo !== false && $fileInfo->isEncrypted()) {
return true;
}
$source = $path; $source = $path;
$target = $path . '.encrypted.' . time(); $target = $path . '.encrypted.' . time();

View File

@ -33,6 +33,7 @@ use OCA\Encryption\Crypto\EncryptAll;
use OCA\Encryption\KeyManager; use OCA\Encryption\KeyManager;
use OCA\Encryption\Users\Setup; use OCA\Encryption\Users\Setup;
use OCA\Encryption\Util; use OCA\Encryption\Util;
use OCP\Files\FileInfo;
use OCP\IConfig; use OCP\IConfig;
use OCP\IL10N; use OCP\IL10N;
use OCP\IUserManager; use OCP\IUserManager;
@ -354,4 +355,36 @@ class EncryptAllTest extends TestCase {
$this->assertSame($password, $userPasswords['user1']); $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],
];
}
} }

View File

@ -252,6 +252,12 @@ class DecryptAll {
*/ */
protected function decryptFile($path) { protected function decryptFile($path) {
// skip already decrypted files
$fileInfo = $this->rootView->getFileInfo($path);
if ($fileInfo !== false && !$fileInfo->isEncrypted()) {
return true;
}
$source = $path; $source = $path;
$target = $path . '.decrypted.' . $this->getTimestamp(); $target = $path . '.decrypted.' . $this->getTimestamp();

View File

@ -311,7 +311,10 @@ class DecryptAllTest extends TestCase {
} }
public function testDecryptFile() { /**
* @dataProvider dataTrueFalse
*/
public function testDecryptFile($isEncrypted) {
$path = 'test.txt'; $path = 'test.txt';
@ -327,15 +330,26 @@ class DecryptAllTest extends TestCase {
->setMethods(['getTimestamp']) ->setMethods(['getTimestamp'])
->getMock(); ->getMock();
$instance->expects($this->any())->method('getTimestamp')->willReturn(42); $fileInfo = $this->createMock(FileInfo::class);
$fileInfo->expects($this->any())->method('isEncrypted')
->willReturn($isEncrypted);
$this->view->expects($this->any())->method('getFileInfo')
->willReturn($fileInfo);
$this->view->expects($this->once()) if ($isEncrypted) {
->method('copy') $instance->expects($this->any())->method('getTimestamp')->willReturn(42);
->with($path, $path . '.decrypted.42');
$this->view->expects($this->once())
->method('rename')
->with($path . '.decrypted.42', $path);
$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->assertTrue(
$this->invokePrivate($instance, 'decryptFile', [$path]) $this->invokePrivate($instance, 'decryptFile', [$path])
); );
@ -356,6 +370,13 @@ class DecryptAllTest extends TestCase {
->setMethods(['getTimestamp']) ->setMethods(['getTimestamp'])
->getMock(); ->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); $instance->expects($this->any())->method('getTimestamp')->willReturn(42);
$this->view->expects($this->once()) $this->view->expects($this->once())