summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorBjoern Schiessle <schiessle@owncloud.com>2015-01-14 20:39:23 +0100
committerBjoern Schiessle <schiessle@owncloud.com>2015-03-26 20:56:51 +0100
commitff9c85ce60aac1098c741b7ea630d9fc545e3d96 (patch)
treeb51ab4917630680beb0499fae4a1d7c0ae100e34 /tests
parenta9b4f0d8429dbeb612e80b168b6146890bb7843e (diff)
downloadnextcloud-server-ff9c85ce60aac1098c741b7ea630d9fc545e3d96.tar.gz
nextcloud-server-ff9c85ce60aac1098c741b7ea630d9fc545e3d96.zip
implement basic encryption functionallity in core to enable multiple encryption modules
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/encryption/keys/storage.php280
-rw-r--r--tests/lib/encryption/managertest.php114
-rw-r--r--tests/lib/encryption/utiltest.php101
3 files changed, 495 insertions, 0 deletions
diff --git a/tests/lib/encryption/keys/storage.php b/tests/lib/encryption/keys/storage.php
new file mode 100644
index 00000000000..c2e5bdbd3d1
--- /dev/null
+++ b/tests/lib/encryption/keys/storage.php
@@ -0,0 +1,280 @@
+<?php
+
+/**
+ * ownCloud
+ *
+ * @copyright (C) 2015 ownCloud, Inc.
+ *
+ * @author Bjoern Schiessle <schiessle@owncloud.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+namespace Test\Encryption\Keys;
+
+use OC\Encryption\Keys\Storage;
+use Test\TestCase;
+
+class StorageTest extends TestCase {
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ protected $util;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ protected $view;
+
+ public function setUp() {
+ parent::setUp();
+
+ $this->util = $this->getMockBuilder('OC\Encryption\Util')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->view = $this->getMockBuilder('OC\Files\View')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ }
+
+ public function testSetFileKey() {
+ $this->util->expects($this->any())
+ ->method('getUidAndFilename')
+ ->willReturn(array('user1', '/files/foo.txt'));
+ $this->util->expects($this->any())
+ ->method('stripPartialFileExtension')
+ ->willReturnArgument(0);
+ $this->util->expects($this->any())
+ ->method('isSystemWideMountPoint')
+ ->willReturn(false);
+ $this->view->expects($this->once())
+ ->method('file_put_contents')
+ ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'),
+ $this->equalTo('key'))
+ ->willReturn(strlen('key'));
+
+ $storage = new Storage('encModule', $this->view, $this->util);
+
+ $this->assertTrue(
+ $storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key')
+ );
+ }
+
+ public function testGetFileKey() {
+ $this->util->expects($this->any())
+ ->method('getUidAndFilename')
+ ->willReturn(array('user1', '/files/foo.txt'));
+ $this->util->expects($this->any())
+ ->method('stripPartialFileExtension')
+ ->willReturnArgument(0);
+ $this->util->expects($this->any())
+ ->method('isSystemWideMountPoint')
+ ->willReturn(false);
+ $this->view->expects($this->once())
+ ->method('file_get_contents')
+ ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
+ ->willReturn('key');
+ $this->view->expects($this->once())
+ ->method('file_exists')
+ ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
+ ->willReturn(true);
+
+ $storage = new Storage('encModule', $this->view, $this->util);
+
+ $this->assertSame('key',
+ $storage->getFileKey('user1/files/foo.txt', 'fileKey')
+ );
+ }
+
+ public function testSetFileKeySystemWide() {
+ $this->util->expects($this->any())
+ ->method('getUidAndFilename')
+ ->willReturn(array('user1', '/files/foo.txt'));
+ $this->util->expects($this->any())
+ ->method('isSystemWideMountPoint')
+ ->willReturn(true);
+ $this->util->expects($this->any())
+ ->method('stripPartialFileExtension')
+ ->willReturnArgument(0);
+ $this->view->expects($this->once())
+ ->method('file_put_contents')
+ ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'),
+ $this->equalTo('key'))
+ ->willReturn(strlen('key'));
+
+ $storage = new Storage('encModule', $this->view, $this->util);
+
+ $this->assertTrue(
+ $storage->setFileKey('user1/files/foo.txt', 'fileKey', 'key')
+ );
+ }
+
+ public function testGetFileKeySystemWide() {
+ $this->util->expects($this->any())
+ ->method('getUidAndFilename')
+ ->willReturn(array('user1', '/files/foo.txt'));
+ $this->util->expects($this->any())
+ ->method('stripPartialFileExtension')
+ ->willReturnArgument(0);
+ $this->util->expects($this->any())
+ ->method('isSystemWideMountPoint')
+ ->willReturn(true);
+ $this->view->expects($this->once())
+ ->method('file_get_contents')
+ ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
+ ->willReturn('key');
+ $this->view->expects($this->once())
+ ->method('file_exists')
+ ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
+ ->willReturn(true);
+
+ $storage = new Storage('encModule', $this->view, $this->util);
+
+ $this->assertSame('key',
+ $storage->getFileKey('user1/files/foo.txt', 'fileKey')
+ );
+ }
+
+ public function testSetSystemUserKey() {
+ $this->view->expects($this->once())
+ ->method('file_put_contents')
+ ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'),
+ $this->equalTo('key'))
+ ->willReturn(strlen('key'));
+
+ $storage = new Storage('encModule', $this->view, $this->util);
+
+ $this->assertTrue(
+ $storage->setSystemUserKey('shareKey_56884', 'key')
+ );
+ }
+
+ public function testSetUserKey() {
+ $this->view->expects($this->once())
+ ->method('file_put_contents')
+ ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'),
+ $this->equalTo('key'))
+ ->willReturn(strlen('key'));
+
+ $storage = new Storage('encModule', $this->view, $this->util);
+
+ $this->assertTrue(
+ $storage->setUserKey('user1', 'publicKey', 'key')
+ );
+ }
+
+ public function testGetSystemUserKey() {
+ $this->view->expects($this->once())
+ ->method('file_get_contents')
+ ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
+ ->willReturn('key');
+ $this->view->expects($this->once())
+ ->method('file_exists')
+ ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
+ ->willReturn(true);
+
+ $storage = new Storage('encModule', $this->view, $this->util);
+
+ $this->assertSame('key',
+ $storage->getSystemUserKey('shareKey_56884')
+ );
+ }
+
+ public function testGetUserKey() {
+ $this->view->expects($this->once())
+ ->method('file_get_contents')
+ ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
+ ->willReturn('key');
+ $this->view->expects($this->once())
+ ->method('file_exists')
+ ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
+ ->willReturn(true);
+
+ $storage = new Storage('encModule', $this->view, $this->util);
+
+ $this->assertSame('key',
+ $storage->getUserKey('user1', 'publicKey')
+ );
+ }
+
+ public function testDeleteUserKey() {
+ $this->view->expects($this->once())
+ ->method('unlink')
+ ->with($this->equalTo('/user1/files_encryption/encModule/user1.publicKey'))
+ ->willReturn(true);
+
+ $storage = new Storage('encModule', $this->view, $this->util);
+
+ $this->assertTrue(
+ $storage->deleteUserKey('user1', 'publicKey')
+ );
+ }
+
+ public function testDeleteSystemUserKey() {
+ $this->view->expects($this->once())
+ ->method('unlink')
+ ->with($this->equalTo('/files_encryption/encModule/shareKey_56884'))
+ ->willReturn(true);
+
+ $storage = new Storage('encModule', $this->view, $this->util);
+
+ $this->assertTrue(
+ $storage->deleteSystemUserKey('shareKey_56884')
+ );
+ }
+
+ public function testDeleteFileKeySystemWide() {
+ $this->util->expects($this->any())
+ ->method('getUidAndFilename')
+ ->willReturn(array('user1', '/files/foo.txt'));
+ $this->util->expects($this->any())
+ ->method('stripPartialFileExtension')
+ ->willReturnArgument(0);
+ $this->util->expects($this->any())
+ ->method('isSystemWideMountPoint')
+ ->willReturn(true);
+ $this->view->expects($this->once())
+ ->method('unlink')
+ ->with($this->equalTo('/files_encryption/keys/files/foo.txt/encModule/fileKey'))
+ ->willReturn(true);
+
+ $storage = new Storage('encModule', $this->view, $this->util);
+
+ $this->assertTrue(
+ $storage->deleteFileKey('user1/files/foo.txt', 'fileKey')
+ );
+ }
+
+ public function testDeleteFileKey() {
+ $this->util->expects($this->any())
+ ->method('getUidAndFilename')
+ ->willReturn(array('user1', '/files/foo.txt'));
+ $this->util->expects($this->any())
+ ->method('stripPartialFileExtension')
+ ->willReturnArgument(0);
+ $this->util->expects($this->any())
+ ->method('isSystemWideMountPoint')
+ ->willReturn(false);
+ $this->view->expects($this->once())
+ ->method('unlink')
+ ->with($this->equalTo('/user1/files_encryption/keys/files/foo.txt/encModule/fileKey'))
+ ->willReturn(true);
+
+ $storage = new Storage('encModule', $this->view, $this->util);
+
+ $this->assertTrue(
+ $storage->deleteFileKey('user1/files/foo.txt', 'fileKey')
+ );
+ }
+
+}
diff --git a/tests/lib/encryption/managertest.php b/tests/lib/encryption/managertest.php
new file mode 100644
index 00000000000..ab297bae0cb
--- /dev/null
+++ b/tests/lib/encryption/managertest.php
@@ -0,0 +1,114 @@
+<?php
+
+namespace Test\Encryption;
+
+use OC\Encryption\Manager;
+use Test\TestCase;
+
+class ManagerTest extends TestCase {
+
+ public function testManagerIsDisabled() {
+ $config = $this->getMock('\OCP\IConfig');
+ $m = new Manager($config);
+ $this->assertFalse($m->isEnabled());
+ }
+
+ public function testManagerIsDisabledIfEnabledButNoModules() {
+ $config = $this->getMock('\OCP\IConfig');
+ $config->expects($this->any())->method('getAppValue')->willReturn(true);
+ $m = new Manager($config);
+ $this->assertFalse($m->isEnabled());
+ }
+
+ public function testManagerIsDisabledIfDisabledButModules() {
+ $config = $this->getMock('\OCP\IConfig');
+ $config->expects($this->any())->method('getAppValue')->willReturn(false);
+ $em = $this->getMock('\OCP\Encryption\IEncryptionModule');
+ $em->expects($this->any())->method('getId')->willReturn(0);
+ $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
+ $m = new Manager($config);
+ $m->registerEncryptionModule($em);
+ $this->assertFalse($m->isEnabled());
+ }
+
+ public function testManagerIsEnabled() {
+ $config = $this->getMock('\OCP\IConfig');
+ $config->expects($this->any())->method('getSystemValue')->willReturn(true);
+ $config->expects($this->any())->method('getAppValue')->willReturn('yes');
+ $m = new Manager($config);
+ $this->assertTrue($m->isEnabled());
+ }
+
+ /**
+ * @expectedException \OC\Encryption\Exceptions\ModuleAlreadyExistsException
+ * @expectedExceptionMessage Id "0" already used by encryption module "TestDummyModule0"
+ */
+ public function testModuleRegistration() {
+ $config = $this->getMock('\OCP\IConfig');
+ $config->expects($this->any())->method('getAppValue')->willReturn('yes');
+ $em = $this->getMock('\OCP\Encryption\IEncryptionModule');
+ $em->expects($this->any())->method('getId')->willReturn(0);
+ $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
+ $m = new Manager($config);
+ $m->registerEncryptionModule($em);
+ $this->assertSame(1, count($m->getEncryptionModules()));
+ $m->registerEncryptionModule($em);
+ }
+
+ public function testModuleUnRegistration() {
+ $config = $this->getMock('\OCP\IConfig');
+ $config->expects($this->any())->method('getAppValue')->willReturn(true);
+ $em = $this->getMock('\OCP\Encryption\IEncryptionModule');
+ $em->expects($this->any())->method('getId')->willReturn(0);
+ $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
+ $m = new Manager($config);
+ $m->registerEncryptionModule($em);
+ $this->assertSame(1,
+ count($m->getEncryptionModules())
+ );
+ $m->unregisterEncryptionModule($em);
+ $this->assertEmpty($m->getEncryptionModules());
+ }
+
+ /**
+ * @expectedException \OC\Encryption\Exceptions\ModuleDoesNotExistsException
+ * @expectedExceptionMessage Module with id: unknown does not exists.
+ */
+ public function testGetEncryptionModuleUnknown() {
+ $config = $this->getMock('\OCP\IConfig');
+ $config->expects($this->any())->method('getAppValue')->willReturn(true);
+ $em = $this->getMock('\OCP\Encryption\IEncryptionModule');
+ $em->expects($this->any())->method('getId')->willReturn(0);
+ $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
+ $m = new Manager($config);
+ $m->registerEncryptionModule($em);
+ $this->assertSame(1, count($m->getEncryptionModules()));
+ $m->getEncryptionModule('unknown');
+ }
+
+ public function testGetEncryptionModule() {
+ $config = $this->getMock('\OCP\IConfig');
+ $config->expects($this->any())->method('getAppValue')->willReturn(true);
+ $em = $this->getMock('\OCP\Encryption\IEncryptionModule');
+ $em->expects($this->any())->method('getId')->willReturn(0);
+ $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
+ $m = new Manager($config);
+ $m->registerEncryptionModule($em);
+ $this->assertSame(1, count($m->getEncryptionModules()));
+ $en0 = $m->getEncryptionModule(0);
+ $this->assertEquals(0, $en0->getId());
+ }
+
+ public function testGetDefaultEncryptionModule() {
+ $config = $this->getMock('\OCP\IConfig');
+ $config->expects($this->any())->method('getAppValue')->willReturn(true);
+ $em = $this->getMock('\OCP\Encryption\IEncryptionModule');
+ $em->expects($this->any())->method('getId')->willReturn(0);
+ $em->expects($this->any())->method('getDisplayName')->willReturn('TestDummyModule0');
+ $m = new Manager($config);
+ $m->registerEncryptionModule($em);
+ $this->assertSame(1, count($m->getEncryptionModules()));
+ $en0 = $m->getEncryptionModule(0);
+ $this->assertEquals(0, $en0->getId());
+ }
+}
diff --git a/tests/lib/encryption/utiltest.php b/tests/lib/encryption/utiltest.php
new file mode 100644
index 00000000000..00a9ab9c578
--- /dev/null
+++ b/tests/lib/encryption/utiltest.php
@@ -0,0 +1,101 @@
+<?php
+
+namespace Test\Encryption;
+
+use OC\Encryption\Util;
+use Test\TestCase;
+
+class UtilTest extends TestCase {
+
+ /**
+ * block size will always be 8192 for a PHP stream
+ * @see https://bugs.php.net/bug.php?id=21641
+ * @var integer
+ */
+ protected $headerSize = 8192;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ protected $view;
+
+ /** @var \PHPUnit_Framework_MockObject_MockObject */
+ protected $userManager;
+
+ public function setUp() {
+ parent::setUp();
+ $this->view = $this->getMockBuilder('OC\Files\View')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $this->userManager = $this->getMockBuilder('OC\User\Manager')
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ /**
+ * @dataProvider providesHeadersForEncryptionModule
+ */
+ public function testGetEncryptionModuleId($expected, $header) {
+ $u = new Util($this->view, $this->userManager);
+ $id = $u->getEncryptionModuleId($header);
+ $this->assertEquals($expected, $id);
+ }
+
+ public function providesHeadersForEncryptionModule() {
+ return [
+ ['', []],
+ ['', ['1']],
+ [2, ['oc_encryption_module' => 2]],
+ ];
+ }
+
+ /**
+ * @dataProvider providesHeaders
+ */
+ public function testReadHeader($header, $expected, $moduleId) {
+ $expected['oc_encryption_module'] = $moduleId;
+ $u = new Util($this->view, $this->userManager);
+ $result = $u->readHeader($header);
+ $this->assertSameSize($expected, $result);
+ foreach ($expected as $key => $value) {
+ $this->assertArrayHasKey($key, $result);
+ $this->assertSame($value, $result[$key]);
+ }
+ }
+
+ /**
+ * @dataProvider providesHeaders
+ */
+ public function testCreateHeader($expected, $header, $moduleId) {
+
+ $em = $this->getMock('\OCP\Encryption\IEncryptionModule');
+ $em->expects($this->any())->method('getId')->willReturn($moduleId);
+
+ $u = new Util($this->view, $this->userManager);
+ $result = $u->createHeader($header, $em);
+ $this->assertEquals($expected, $result);
+ }
+
+ public function providesHeaders() {
+ return [
+ [str_pad('HBEGIN:oc_encryption_module:0:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
+ , [], '0'],
+ [str_pad('HBEGIN:oc_encryption_module:0:custom_header:foo:HEND', $this->headerSize, '-', STR_PAD_RIGHT)
+ , ['custom_header' => 'foo'], '0'],
+ ];
+ }
+
+ /**
+ * @expectedException \OC\Encryption\Exceptions\EncryptionHeaderKeyExistsException
+ */
+ public function testCreateHeaderFailed() {
+
+ $header = array('header1' => 1, 'header2' => 2, 'oc_encryption_module' => 'foo');
+
+ $em = $this->getMock('\OCP\Encryption\IEncryptionModule');
+ $em->expects($this->any())->method('getId')->willReturn('moduleId');
+
+ $u = new Util($this->view, $this->userManager);
+ $u->createHeader($header, $em);
+ }
+
+}