summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorBjörn Schießle <bjoern@schiessle.org>2015-08-28 20:44:55 +0200
committerBjörn Schießle <bjoern@schiessle.org>2015-08-28 20:44:55 +0200
commit6e210d960c23c3ab9bb28ba6b1fa3a77c8b951c8 (patch)
tree420049bcc149225465c4eabe03c09fb6d382bf35 /tests
parentdc04b618bcd9ed795a81b7a88bc2a97725c629a8 (diff)
parente51fe617d89fd515e9e6836f205f67c613939b41 (diff)
downloadnextcloud-server-6e210d960c23c3ab9bb28ba6b1fa3a77c8b951c8.tar.gz
nextcloud-server-6e210d960c23c3ab9bb28ba6b1fa3a77c8b951c8.zip
Merge pull request #18423 from owncloud/occ_encrypt_all
occ command line tool to encrypt all files
Diffstat (limited to 'tests')
-rw-r--r--tests/core/command/encryption/encryptalltest.php131
-rw-r--r--tests/lib/files/storage/wrapper/encryption.php63
-rw-r--r--tests/lib/files/stream/encryption.php2
3 files changed, 141 insertions, 55 deletions
diff --git a/tests/core/command/encryption/encryptalltest.php b/tests/core/command/encryption/encryptalltest.php
new file mode 100644
index 00000000000..41edee6987c
--- /dev/null
+++ b/tests/core/command/encryption/encryptalltest.php
@@ -0,0 +1,131 @@
+<?php
+/**
+ * @author Björn Schießle <schiessle@owncloud.com>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+
+namespace Tests\Core\Command\Encryption;
+
+
+use OC\Core\Command\Encryption\EncryptAll;
+use Test\TestCase;
+
+class EncryptAllTest extends TestCase {
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\IConfig */
+ protected $config;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\Encryption\IManager */
+ protected $encryptionManager;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\App\IAppManager */
+ protected $appManager;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Input\InputInterface */
+ protected $consoleInput;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Output\OutputInterface */
+ protected $consoleOutput;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject | \Symfony\Component\Console\Helper\QuestionHelper */
+ protected $questionHelper;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\Encryption\IEncryptionModule */
+ protected $encryptionModule;
+
+ /** @var EncryptAll */
+ protected $command;
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->config = $this->getMockBuilder('OCP\IConfig')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->encryptionManager = $this->getMockBuilder('OCP\Encryption\IManager')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->appManager = $this->getMockBuilder('OCP\App\IAppManager')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')
+ ->disableOriginalConstructor()
+ ->getMock();
+ $this->consoleInput = $this->getMock('Symfony\Component\Console\Input\InputInterface');
+ $this->consoleOutput = $this->getMock('Symfony\Component\Console\Output\OutputInterface');
+
+ }
+
+ public function testEncryptAll() {
+ // trash bin needs to be disabled in order to avoid adding dummy files to the users
+ // trash bin which gets deleted during the encryption process
+ $this->appManager->expects($this->once())->method('disableApp')->with('files_trashbin');
+ // enable single user mode to avoid that other user login during encryption
+ // destructor should disable the single user mode again
+ $this->config->expects($this->once())->method('getSystemValue')->with('singleUser', false)->willReturn(false);
+ $this->config->expects($this->at(1))->method('setSystemValue')->with('singleUser', true);
+ $this->config->expects($this->at(2))->method('setSystemValue')->with('singleUser', false);
+
+ new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
+ }
+
+ /**
+ * @dataProvider dataTestExecute
+ */
+ public function testExecute($answer, $askResult) {
+
+ $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
+
+ $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(true);
+ $this->questionHelper->expects($this->once())->method('ask')->willReturn($askResult);
+
+ if ($answer === 'Y' || $answer === 'y') {
+ $this->encryptionManager->expects($this->once())
+ ->method('getEncryptionModule')->willReturn($this->encryptionModule);
+ $this->encryptionModule->expects($this->once())
+ ->method('encryptAll')->with($this->consoleInput, $this->consoleOutput);
+ } else {
+ $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
+ $this->encryptionModule->expects($this->never())->method('encryptAll');
+ }
+
+ $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+
+ public function dataTestExecute() {
+ return [
+ ['y', true], ['Y', true], ['n', false], ['N', false], ['', false]
+ ];
+ }
+
+ /**
+ * @expectedException \Exception
+ */
+ public function testExecuteException() {
+ $command = new EncryptAll($this->encryptionManager, $this->appManager, $this->config, $this->questionHelper);
+ $this->encryptionManager->expects($this->once())->method('isEnabled')->willReturn(false);
+ $this->encryptionManager->expects($this->never())->method('getEncryptionModule');
+ $this->encryptionModule->expects($this->never())->method('encryptAll');
+ $this->invokePrivate($command, 'execute', [$this->consoleInput, $this->consoleOutput]);
+ }
+
+}
diff --git a/tests/lib/files/storage/wrapper/encryption.php b/tests/lib/files/storage/wrapper/encryption.php
index c49e6bb0d1f..36a5b288c64 100644
--- a/tests/lib/files/storage/wrapper/encryption.php
+++ b/tests/lib/files/storage/wrapper/encryption.php
@@ -194,7 +194,7 @@ class Encryption extends \Test\Files\Storage\Storage {
protected function buildMockModule() {
$this->encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule')
->disableOriginalConstructor()
- ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable'])
+ ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll'])
->getMock();
$this->encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE');
@@ -241,59 +241,14 @@ class Encryption extends \Test\Files\Storage\Storage {
$this->instance->rename($source, $target);
}
- /**
- * @dataProvider dataTestCopyAndRename
- *
- * @param string $source
- * @param string $target
- * @param $encryptionEnabled
- * @param boolean $copyKeysReturn
- * @param boolean $shouldUpdate
- */
- public function testCopyEncryption($source,
- $target,
- $encryptionEnabled,
- $copyKeysReturn,
- $shouldUpdate) {
-
- if ($encryptionEnabled) {
- $this->keyStore
- ->expects($this->once())
- ->method('copyKeys')
- ->willReturn($copyKeysReturn);
- $this->cache->expects($this->atLeastOnce())
- ->method('put')
- ->willReturnCallback(function($path, $data) {
- $this->assertArrayHasKey('encrypted', $data);
- $this->assertTrue($data['encrypted']);
- });
- } else {
- $this->cache->expects($this->never())->method('put');
- $this->keyStore->expects($this->never())->method('copyKeys');
- }
- $this->util->expects($this->any())
- ->method('isFile')->willReturn(true);
- $this->util->expects($this->any())
- ->method('isExcluded')->willReturn(false);
- $this->encryptionManager->expects($this->once())
- ->method('isEnabled')->willReturn($encryptionEnabled);
- if ($shouldUpdate) {
- $this->update->expects($this->once())
- ->method('update');
- } else {
- $this->update->expects($this->never())
- ->method('update');
- }
-
- $this->instance->mkdir($source);
- $this->instance->mkdir(dirname($target));
- $this->instance->copy($source, $target);
-
- if ($encryptionEnabled) {
- $this->assertSame($this->dummySize,
- $this->instance->filesize($target)
- );
- }
+ public function testCopyEncryption() {
+ $this->instance->file_put_contents('source.txt', 'bar');
+ $this->instance->copy('source.txt', 'target.txt');
+ $this->assertSame('bar', $this->instance->file_get_contents('target.txt'));
+ $targetMeta = $this->instance->getMetaData('target.txt');
+ $sourceMeta = $this->instance->getMetaData('source.txt');
+ $this->assertSame($sourceMeta['encrypted'], $targetMeta['encrypted']);
+ $this->assertSame($sourceMeta['size'], $targetMeta['size']);
}
/**
diff --git a/tests/lib/files/stream/encryption.php b/tests/lib/files/stream/encryption.php
index 281ec0a14a0..ed3b5b1b156 100644
--- a/tests/lib/files/stream/encryption.php
+++ b/tests/lib/files/stream/encryption.php
@@ -305,7 +305,7 @@ class Encryption extends \Test\TestCase {
protected function buildMockModule() {
$encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule')
->disableOriginalConstructor()
- ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable'])
+ ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'getUnencryptedBlockSize', 'isReadable', 'encryptAll'])
->getMock();
$encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE');