From 1e8098471c50e2482158ede4209944cee4d69ad5 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 24 Oct 2018 16:15:17 +0200 Subject: [PATCH 1/2] skip already encrypted files on encrypt all command Signed-off-by: Bjoern Schiessle --- apps/encryption/lib/Crypto/EncryptAll.php | 6 ++++ .../tests/Crypto/EncryptAllTest.php | 33 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/apps/encryption/lib/Crypto/EncryptAll.php b/apps/encryption/lib/Crypto/EncryptAll.php index c2619dc8ef1ff..ee13fee9eef73 100644 --- a/apps/encryption/lib/Crypto/EncryptAll.php +++ b/apps/encryption/lib/Crypto/EncryptAll.php @@ -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(); diff --git a/apps/encryption/tests/Crypto/EncryptAllTest.php b/apps/encryption/tests/Crypto/EncryptAllTest.php index a39bf7befb6a3..647b951a0a67e 100644 --- a/apps/encryption/tests/Crypto/EncryptAllTest.php +++ b/apps/encryption/tests/Crypto/EncryptAllTest.php @@ -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; @@ -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], + ]; + } + } From 6f3328a9cd37c9bb6ff2bc0e85135f4955fb851a Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 24 Oct 2018 16:49:39 +0200 Subject: [PATCH 2/2] skip already decrypted files on decrypt all command Signed-off-by: Bjoern Schiessle --- lib/private/Encryption/DecryptAll.php | 6 ++++ tests/lib/Encryption/DecryptAllTest.php | 41 +++++++++++++++++++------ 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/lib/private/Encryption/DecryptAll.php b/lib/private/Encryption/DecryptAll.php index 12bda54a52e9e..16eee34733453 100644 --- a/lib/private/Encryption/DecryptAll.php +++ b/lib/private/Encryption/DecryptAll.php @@ -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(); diff --git a/tests/lib/Encryption/DecryptAllTest.php b/tests/lib/Encryption/DecryptAllTest.php index 59c24fb3c1d42..4e4b2438a25cd 100644 --- a/tests/lib/Encryption/DecryptAllTest.php +++ b/tests/lib/Encryption/DecryptAllTest.php @@ -311,7 +311,10 @@ function($path) { } - public function testDecryptFile() { + /** + * @dataProvider dataTrueFalse + */ + public function testDecryptFile($isEncrypted) { $path = 'test.txt'; @@ -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]) ); @@ -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())