diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2015-04-07 16:46:45 +0200 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2015-04-07 16:46:45 +0200 |
commit | 1fbf5d86df7ba4001ca826d9dfb8fad073924fde (patch) | |
tree | 9260b35011fabbbf69747419282d193fa7a9089c /apps/encryption/tests | |
parent | 2182ae0d278f466e7f117b03bf4ebca0e6e9fe9b (diff) | |
parent | 2d2cb09715554926945de29b80f033905a219abd (diff) | |
download | nextcloud-server-1fbf5d86df7ba4001ca826d9dfb8fad073924fde.tar.gz nextcloud-server-1fbf5d86df7ba4001ca826d9dfb8fad073924fde.zip |
Merge pull request #14472 from owncloud/feature/wipencryptionapp
encryption 2.0 app
Diffstat (limited to 'apps/encryption/tests')
-rw-r--r-- | apps/encryption/tests/lib/HookManagerTest.php | 74 | ||||
-rw-r--r-- | apps/encryption/tests/lib/KeyManagerTest.php | 286 | ||||
-rw-r--r-- | apps/encryption/tests/lib/RecoveryTest.php | 265 | ||||
-rw-r--r-- | apps/encryption/tests/lib/SessionTest.php | 140 | ||||
-rw-r--r-- | apps/encryption/tests/lib/UtilTest.php | 128 | ||||
-rw-r--r-- | apps/encryption/tests/lib/crypto/encryptionTest.php | 78 | ||||
-rw-r--r-- | apps/encryption/tests/lib/users/SetupTest.php | 81 |
7 files changed, 1052 insertions, 0 deletions
diff --git a/apps/encryption/tests/lib/HookManagerTest.php b/apps/encryption/tests/lib/HookManagerTest.php new file mode 100644 index 00000000000..3c360ff3504 --- /dev/null +++ b/apps/encryption/tests/lib/HookManagerTest.php @@ -0,0 +1,74 @@ +<?php +/** + * @author Clark Tomlinson <clark@owncloud.com> + * @since 3/31/15, 1:54 PM + * @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 OCA\Encryption\Tests; + + +use OCA\Encryption\HookManager; +use Test\TestCase; + +class HookManagerTest extends TestCase { + + /** + * @var HookManager + */ + private static $instance; + + /** + * + */ + public function testRegisterHookWithArray() { + self::$instance->registerHook([ + $this->getMockBuilder('OCA\Encryption\Hooks\Contracts\IHook')->disableOriginalConstructor()->getMock(), + $this->getMockBuilder('OCA\Encryption\Hooks\Contracts\IHook')->disableOriginalConstructor()->getMock(), + $this->getMock('NotIHook') + ]); + + $hookInstances = \Test_Helper::invokePrivate(self::$instance, 'hookInstances'); + // Make sure our type checking works + $this->assertCount(2, $hookInstances); + } + + + /** + * + */ + public static function setUpBeforeClass() { + parent::setUpBeforeClass(); + // have to make instance static to preserve data between tests + self::$instance = new HookManager(); + + } + + /** + * + */ + public function testRegisterHooksWithInstance() { + $mock = $this->getMockBuilder('OCA\Encryption\Hooks\Contracts\IHook')->disableOriginalConstructor()->getMock(); + self::$instance->registerHook($mock); + + $hookInstances = \Test_Helper::invokePrivate(self::$instance, 'hookInstances'); + $this->assertCount(3, $hookInstances); + + } + +} diff --git a/apps/encryption/tests/lib/KeyManagerTest.php b/apps/encryption/tests/lib/KeyManagerTest.php new file mode 100644 index 00000000000..d12578bb8d2 --- /dev/null +++ b/apps/encryption/tests/lib/KeyManagerTest.php @@ -0,0 +1,286 @@ +<?php +/** + * @author Clark Tomlinson <fallen013@gmail.com> + * @since 3/5/15, 10:53 AM + * @link http:/www.clarkt.com + * @copyright Clark Tomlinson © 2015 + * + */ + +namespace OCA\Encryption\Tests; + + +use OCA\Encryption\KeyManager; +use Test\TestCase; + +class KeyManagerTest extends TestCase { + /** + * @var KeyManager + */ + private $instance; + /** + * @var string + */ + private $userId; + + /** @var string */ + private $systemKeyId; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $keyStorageMock; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $cryptMock; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $userMock; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $sessionMock; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $logMock; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $utilMock; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $configMock; + + public function setUp() { + parent::setUp(); + $this->userId = 'user1'; + $this->systemKeyId = 'systemKeyId'; + $this->keyStorageMock = $this->getMock('OCP\Encryption\Keys\IStorage'); + $this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt') + ->disableOriginalConstructor() + ->getMock(); + $this->configMock = $this->getMock('OCP\IConfig'); + $this->configMock->expects($this->any()) + ->method('getAppValue') + ->willReturn($this->systemKeyId); + $this->userMock = $this->getMock('OCP\IUserSession'); + $this->sessionMock = $this->getMockBuilder('OCA\Encryption\Session') + ->disableOriginalConstructor() + ->getMock(); + $this->logMock = $this->getMock('OCP\ILogger'); + $this->utilMock = $this->getMockBuilder('OCA\Encryption\Util') + ->disableOriginalConstructor() + ->getMock(); + + $this->instance = new KeyManager( + $this->keyStorageMock, + $this->cryptMock, + $this->configMock, + $this->userMock, + $this->sessionMock, + $this->logMock, + $this->utilMock); + } + + public function testDeleteShareKey() { + $this->keyStorageMock->expects($this->any()) + ->method('deleteFileKey') + ->with($this->equalTo('/path'), $this->equalTo('keyId.shareKey')) + ->willReturn(true); + + $this->assertTrue( + $this->instance->deleteShareKey('/path', 'keyId') + ); + } + + public function testGetPrivateKey() { + $this->keyStorageMock->expects($this->any()) + ->method('getUserKey') + ->with($this->equalTo($this->userId), $this->equalTo('privateKey')) + ->willReturn('privateKey'); + + + $this->assertSame('privateKey', + $this->instance->getPrivateKey($this->userId) + ); + } + + public function testGetPublicKey() { + $this->keyStorageMock->expects($this->any()) + ->method('getUserKey') + ->with($this->equalTo($this->userId), $this->equalTo('publicKey')) + ->willReturn('publicKey'); + + + $this->assertSame('publicKey', + $this->instance->getPublicKey($this->userId) + ); + } + + public function testRecoveryKeyExists() { + $this->keyStorageMock->expects($this->any()) + ->method('getSystemUserKey') + ->with($this->equalTo($this->systemKeyId . '.publicKey')) + ->willReturn('recoveryKey'); + + + $this->assertTrue($this->instance->recoveryKeyExists()); + } + + public function testCheckRecoveryKeyPassword() { + $this->keyStorageMock->expects($this->any()) + ->method('getSystemUserKey') + ->with($this->equalTo($this->systemKeyId . '.privateKey')) + ->willReturn('recoveryKey'); + $this->cryptMock->expects($this->any()) + ->method('decryptPrivateKey') + ->with($this->equalTo('recoveryKey'), $this->equalTo('pass')) + ->willReturn('decryptedRecoveryKey'); + + $this->assertTrue($this->instance->checkRecoveryPassword('pass')); + } + + public function testSetPublicKey() { + $this->keyStorageMock->expects($this->any()) + ->method('setUserKey') + ->with( + $this->equalTo($this->userId), + $this->equalTo('publicKey'), + $this->equalTo('key')) + ->willReturn(true); + + + $this->assertTrue( + $this->instance->setPublicKey($this->userId, 'key') + ); + } + + public function testSetPrivateKey() { + $this->keyStorageMock->expects($this->any()) + ->method('setUserKey') + ->with( + $this->equalTo($this->userId), + $this->equalTo('privateKey'), + $this->equalTo('key')) + ->willReturn(true); + + + $this->assertTrue( + $this->instance->setPrivateKey($this->userId, 'key') + ); + } + + public function testUserHasKeys() { + $this->keyStorageMock->expects($this->exactly(2)) + ->method('getUserKey') + ->with($this->equalTo($this->userId), $this->anything()) + ->willReturn('key'); + + + $this->assertTrue( + $this->instance->userHasKeys($this->userId) + ); + } + + public function testInit() { + $this->keyStorageMock->expects($this->any()) + ->method('getUserKey') + ->with($this->equalTo($this->userId), $this->equalTo('privateKey')) + ->willReturn('privateKey'); + $this->cryptMock->expects($this->any()) + ->method('decryptPrivateKey') + ->with($this->equalTo('privateKey'), $this->equalTo('pass')) + ->willReturn('decryptedPrivateKey'); + + + $this->assertTrue( + $this->instance->init($this->userId, 'pass') + ); + + } + + public function testSetRecoveryKey() { + $this->keyStorageMock->expects($this->exactly(2)) + ->method('setSystemUserKey') + ->willReturn(true); + $this->cryptMock->expects($this->any()) + ->method('symmetricEncryptFileContent') + ->with($this->equalTo('privateKey'), $this->equalTo('pass')) + ->willReturn('decryptedPrivateKey'); + + + $this->assertTrue( + $this->instance->setRecoveryKey('pass', + array('publicKey' => 'publicKey', 'privateKey' => 'privateKey')) + ); + } + + public function testSetSystemPrivateKey() { + $this->keyStorageMock->expects($this->exactly(1)) + ->method('setSystemUserKey') + ->with($this->equalTo('keyId.privateKey'), $this->equalTo('key')) + ->willReturn(true); + + + $this->assertTrue( + $this->instance->setSystemPrivateKey('keyId', 'key') + ); + } + + public function testGetSystemPrivateKey() { + $this->keyStorageMock->expects($this->exactly(1)) + ->method('getSystemUserKey') + ->with($this->equalTo('keyId.privateKey')) + ->willReturn('systemPrivateKey'); + + + $this->assertSame('systemPrivateKey', + $this->instance->getSystemPrivateKey('keyId') + ); + } + + public function testGetEncryptedFileKey() { + $this->keyStorageMock->expects($this->once()) + ->method('getFileKey') + ->with('/', 'fileKey') + ->willReturn(true); + + $this->assertTrue($this->instance->getEncryptedFileKey('/')); + } + + public function testGetFileKey() { + $this->keyStorageMock->expects($this->exactly(4)) + ->method('getFileKey') + ->willReturn(true); + + $this->keyStorageMock->expects($this->once()) + ->method('getSystemUserKey') + ->willReturn(true); + + $this->cryptMock->expects($this->once()) + ->method('symmetricDecryptFileContent') + ->willReturn(true); + + $this->cryptMock->expects($this->once()) + ->method('multiKeyDecrypt') + ->willReturn(true); + + $this->assertTrue($this->instance->getFileKey('/', null)); + $this->assertEmpty($this->instance->getFileKey('/', $this->userId)); + } + + public function testDeletePrivateKey() { + $this->keyStorageMock->expects($this->once()) + ->method('deleteUserKey') + ->with('user1', 'privateKey') + ->willReturn(true); + + $this->assertTrue(\Test_Helper::invokePrivate($this->instance, + 'deletePrivateKey', + [$this->userId])); + } + + public function testDeleteAllFileKeys() { + $this->keyStorageMock->expects($this->once()) + ->method('deleteAllFileKeys') + ->willReturn(true); + + $this->assertTrue($this->instance->deleteAllFileKeys('/')); + } +} diff --git a/apps/encryption/tests/lib/RecoveryTest.php b/apps/encryption/tests/lib/RecoveryTest.php new file mode 100644 index 00000000000..701762b56d6 --- /dev/null +++ b/apps/encryption/tests/lib/RecoveryTest.php @@ -0,0 +1,265 @@ +<?php +/** + * @author Clark Tomlinson <clark@owncloud.com> + * @since 4/3/15, 9:57 AM + * @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 OCA\Encryption\Tests; + + +use OCA\Encryption\Recovery; +use Test\TestCase; + +class RecoveryTest extends TestCase { + private static $tempStorage = []; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $fileMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $viewMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $userSessionMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $keyManagerMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $configMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $cryptMock; + /** + * @var Recovery + */ + private $instance; + + public function testEnableAdminRecovery() { + $this->keyManagerMock->expects($this->exactly(2)) + ->method('recoveryKeyExists') + ->willReturnOnConsecutiveCalls(false, true); + + $this->cryptMock->expects($this->once()) + ->method('createKeyPair') + ->willReturn(true); + + $this->keyManagerMock->expects($this->once()) + ->method('setRecoveryKey') + ->willReturn(false); + + $this->keyManagerMock->expects($this->exactly(2)) + ->method('checkRecoveryPassword') + ->willReturnOnConsecutiveCalls(true, false); + + $this->assertTrue($this->instance->enableAdminRecovery('password')); + $this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage); + $this->assertEquals(1, self::$tempStorage['recoveryAdminEnabled']); + + $this->assertFalse($this->instance->enableAdminRecovery('password')); + } + + public function testChangeRecoveryKeyPassword() { + $this->assertFalse($this->instance->changeRecoveryKeyPassword('password', + 'passwordOld')); + + $this->keyManagerMock->expects($this->once()) + ->method('getSystemPrivateKey'); + + $this->cryptMock->expects($this->once()) + ->method('decryptPrivateKey'); + + $this->cryptMock->expects($this->once()) + ->method('symmetricEncryptFileContent') + ->willReturn(true); + + $this->assertTrue($this->instance->changeRecoveryKeyPassword('password', + 'passwordOld')); + } + + public function testDisableAdminRecovery() { + + $this->keyManagerMock->expects($this->exactly(2)) + ->method('checkRecoveryPassword') + ->willReturnOnConsecutiveCalls(true, false); + + $this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage); + $this->assertTrue($this->instance->disableAdminRecovery('password')); + $this->assertEquals(0, self::$tempStorage['recoveryAdminEnabled']); + + $this->assertFalse($this->instance->disableAdminRecovery('password')); + } + + public function testIsRecoveryEnabledForUser() { + + $this->configMock->expects($this->exactly(2)) + ->method('getUserValue') + ->willReturnOnConsecutiveCalls('1', '0'); + + $this->assertTrue($this->instance->isRecoveryEnabledForUser()); + $this->assertFalse($this->instance->isRecoveryEnabledForUser('admin')); + } + + public function testIsRecoveryKeyEnabled() { + $this->assertFalse($this->instance->isRecoveryKeyEnabled()); + self::$tempStorage['recoveryAdminEnabled'] = '1'; + $this->assertTrue($this->instance->isRecoveryKeyEnabled()); + } + + public function testSetRecoveryFolderForUser() { + $this->viewMock->expects($this->exactly(2)) + ->method('getDirectoryContent') + ->willReturn([]); + $this->assertTrue($this->instance->setRecoveryForUser(0)); + $this->assertTrue($this->instance->setRecoveryForUser('1')); + } + + public function testRecoverUserFiles() { + $this->viewMock->expects($this->once()) + ->method('getDirectoryContent') + ->willReturn([]); + + $this->cryptMock->expects($this->once()) + ->method('decryptPrivateKey'); + $this->assertNull($this->instance->recoverUsersFiles('password', + 'admin')); + } + + public function testRecoverFile() { + $this->keyManagerMock->expects($this->once()) + ->method('getEncryptedFileKey') + ->willReturn(true); + + $this->keyManagerMock->expects($this->once()) + ->method('getShareKey') + ->willReturn(true); + + $this->cryptMock->expects($this->once()) + ->method('multiKeyDecrypt') + ->willReturn(true); + + $this->fileMock->expects($this->once()) + ->method('getAccessList') + ->willReturn(['users' => ['admin']]); + + $this->keyManagerMock->expects($this->once()) + ->method('getPublicKey') + ->willReturn('publicKey'); + + $this->keyManagerMock->expects($this->once()) + ->method('addSystemKeys') + ->willReturn(['admin' => 'publicKey']); + + + $this->cryptMock->expects($this->once()) + ->method('multiKeyEncrypt'); + + $this->keyManagerMock->expects($this->once()) + ->method('setAllFileKeys'); + + $this->assertNull(\Test_Helper::invokePrivate($this->instance, + 'recoverFile', + ['/', 'testkey'])); + } + + protected function setUp() { + parent::setUp(); + + + $this->userSessionMock = $this->getMockBuilder('OCP\IUserSession') + ->disableOriginalConstructor() + ->setMethods([ + 'isLoggedIn', + 'getUID', + 'login', + 'logout', + 'setUser', + 'getUser' + ]) + ->getMock(); + + $this->userSessionMock->expects($this->any())->method('getUID')->will($this->returnValue('admin')); + + $this->userSessionMock->expects($this->any()) + ->method($this->anything()) + ->will($this->returnSelf()); + + $this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt')->disableOriginalConstructor()->getMock(); + $randomMock = $this->getMock('OCP\Security\ISecureRandom'); + $this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager')->disableOriginalConstructor()->getMock(); + $this->configMock = $this->getMock('OCP\IConfig'); + $keyStorageMock = $this->getMock('OCP\Encryption\Keys\IStorage'); + $this->fileMock = $this->getMock('OCP\Encryption\IFile'); + $this->viewMock = $this->getMock('OC\Files\View'); + + $this->configMock->expects($this->any()) + ->method('setAppValue') + ->will($this->returnCallback([$this, 'setValueTester'])); + + $this->configMock->expects($this->any()) + ->method('getAppValue') + ->will($this->returnCallback([$this, 'getValueTester'])); + + $this->instance = new Recovery($this->userSessionMock, + $this->cryptMock, + $randomMock, + $this->keyManagerMock, + $this->configMock, + $keyStorageMock, + $this->fileMock, + $this->viewMock); + } + + + /** + * @param $app + * @param $key + * @param $value + */ + public function setValueTester($app, $key, $value) { + self::$tempStorage[$key] = $value; + } + + /** + * @param $key + */ + public function removeValueTester($key) { + unset(self::$tempStorage[$key]); + } + + /** + * @param $app + * @param $key + * @return mixed + */ + public function getValueTester($app, $key) { + if (!empty(self::$tempStorage[$key])) { + return self::$tempStorage[$key]; + } + return null; + } + + +} diff --git a/apps/encryption/tests/lib/SessionTest.php b/apps/encryption/tests/lib/SessionTest.php new file mode 100644 index 00000000000..f7e026808f0 --- /dev/null +++ b/apps/encryption/tests/lib/SessionTest.php @@ -0,0 +1,140 @@ +<?php +/** + * @author Clark Tomlinson <clark@owncloud.com> + * @since 3/31/15, 10:19 AM + * @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 OCA\Encryption\Tests; + + +use OCA\Encryption\Session; +use Test\TestCase; + +class SessionTest extends TestCase { + private static $tempStorage = []; + /** + * @var Session + */ + private $instance; + private $sessionMock; + + /** + * @expectedException \OCA\Encryption\Exceptions\PrivateKeyMissingException + * @expectedExceptionMessage Private Key missing for user: please try to log-out and log-in again + */ + public function testThatGetPrivateKeyThrowsExceptionWhenNotSet() { + $this->instance->getPrivateKey(); + } + + /** + * @depends testThatGetPrivateKeyThrowsExceptionWhenNotSet + */ + public function testSetAndGetPrivateKey() { + $this->instance->setPrivateKey('dummyPrivateKey'); + $this->assertEquals('dummyPrivateKey', $this->instance->getPrivateKey()); + + } + + /** + * @depends testSetAndGetPrivateKey + */ + public function testIsPrivateKeySet() { + $this->assertTrue($this->instance->isPrivateKeySet()); + + unset(self::$tempStorage['privateKey']); + $this->assertFalse($this->instance->isPrivateKeySet()); + + // Set private key back so we can test clear method + self::$tempStorage['privateKey'] = 'dummyPrivateKey'; + } + + /** + * + */ + public function testSetAndGetStatusWillSetAndReturn() { + // Check if get status will return 0 if it has not been set before + $this->assertEquals(0, $this->instance->getStatus()); + + $this->instance->setStatus(Session::NOT_INITIALIZED); + $this->assertEquals(0, $this->instance->getStatus()); + + $this->instance->setStatus(Session::INIT_EXECUTED); + $this->assertEquals(1, $this->instance->getStatus()); + + $this->instance->setStatus(Session::INIT_SUCCESSFUL); + $this->assertEquals(2, $this->instance->getStatus()); + } + + /** + * @param $key + * @param $value + */ + public function setValueTester($key, $value) { + self::$tempStorage[$key] = $value; + } + + /** + * @param $key + */ + public function removeValueTester($key) { + unset(self::$tempStorage[$key]); + } + + /** + * @param $key + * @return mixed + */ + public function getValueTester($key) { + if (!empty(self::$tempStorage[$key])) { + return self::$tempStorage[$key]; + } + return null; + } + + /** + * + */ + public function testClearWillRemoveValues() { + $this->instance->clear(); + $this->assertEmpty(self::$tempStorage); + } + + /** + * + */ + protected function setUp() { + parent::setUp(); + $this->sessionMock = $this->getMock('OCP\ISession'); + + $this->sessionMock->expects($this->any()) + ->method('set') + ->will($this->returnCallback([$this, "setValueTester"])); + + $this->sessionMock->expects($this->any()) + ->method('get') + ->will($this->returnCallback([$this, "getValueTester"])); + + $this->sessionMock->expects($this->any()) + ->method('remove') + ->will($this->returnCallback([$this, "removeValueTester"])); + + + $this->instance = new Session($this->sessionMock); + } +} diff --git a/apps/encryption/tests/lib/UtilTest.php b/apps/encryption/tests/lib/UtilTest.php new file mode 100644 index 00000000000..fa87a629f2f --- /dev/null +++ b/apps/encryption/tests/lib/UtilTest.php @@ -0,0 +1,128 @@ +<?php +/** + * @author Clark Tomlinson <clark@owncloud.com> + * @since 3/31/15, 3:49 PM + * @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 OCA\Encryption\Tests; + + +use OCA\Encryption\Util; +use Test\TestCase; + +class UtilTest extends TestCase { + private static $tempStorage = []; + private $configMock; + private $filesMock; + /** + * @var Util + */ + private $instance; + + public function testSetRecoveryForUser() { + $this->instance->setRecoveryForUser('1'); + $this->assertArrayHasKey('recoveryEnabled', self::$tempStorage); + } + + /** + * + */ + public function testIsRecoveryEnabledForUser() { + $this->assertTrue($this->instance->isRecoveryEnabledForUser()); + + // Assert recovery will return default value if not set + unset(self::$tempStorage['recoveryEnabled']); + $this->assertEquals(0, $this->instance->isRecoveryEnabledForUser()); + } + + public function testUserHasFiles() { + $this->filesMock->expects($this->once()) + ->method('file_exists') + ->will($this->returnValue(true)); + + $this->assertTrue($this->instance->userHasFiles('admin')); + } + + protected function setUp() { + parent::setUp(); + $this->filesMock = $this->getMock('OC\Files\View'); + + $cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt') + ->disableOriginalConstructor() + ->getMock(); + $loggerMock = $this->getMock('OCP\ILogger'); + $userSessionMock = $this->getMockBuilder('OCP\IUserSession') + ->disableOriginalConstructor() + ->setMethods([ + 'isLoggedIn', + 'getUID', + 'login', + 'logout', + 'setUser', + 'getUser' + ]) + ->getMock(); + + $userSessionMock->method('isLoggedIn')->will($this->returnValue(true)); + + $userSessionMock->method('getUID')->will($this->returnValue('admin')); + + $userSessionMock->expects($this->any()) + ->method($this->anything()) + ->will($this->returnSelf()); + + + $this->configMock = $configMock = $this->getMock('OCP\IConfig'); + + $this->configMock->expects($this->any()) + ->method('getUserValue') + ->will($this->returnCallback([$this, 'getValueTester'])); + + $this->configMock->expects($this->any()) + ->method('setUserValue') + ->will($this->returnCallback([$this, 'setValueTester'])); + + $this->instance = new Util($this->filesMock, $cryptMock, $loggerMock, $userSessionMock, $configMock); + } + + /** + * @param $userId + * @param $app + * @param $key + * @param $value + */ + public function setValueTester($userId, $app, $key, $value) { + self::$tempStorage[$key] = $value; + } + + /** + * @param $userId + * @param $app + * @param $key + * @param $default + * @return mixed + */ + public function getValueTester($userId, $app, $key, $default) { + if (!empty(self::$tempStorage[$key])) { + return self::$tempStorage[$key]; + } + return $default ?: null; + } + +} diff --git a/apps/encryption/tests/lib/crypto/encryptionTest.php b/apps/encryption/tests/lib/crypto/encryptionTest.php new file mode 100644 index 00000000000..52a322463a9 --- /dev/null +++ b/apps/encryption/tests/lib/crypto/encryptionTest.php @@ -0,0 +1,78 @@ +<?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 OCA\Encryption\Tests\Crypto; + +use Test\TestCase; +use OCA\Encryption\Crypto\Encryption; + +class EncryptionTest extends TestCase { + + /** @var Encryption */ + private $instance; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $keyManagerMock; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $cryptMock; + + /** @var \PHPUnit_Framework_MockObject_MockObject */ + private $utilMock; + + public function setUp() { + parent::setUp(); + + $this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt') + ->disableOriginalConstructor() + ->getMock(); + $this->utilMock = $this->getMockBuilder('OCA\Encryption\Util') + ->disableOriginalConstructor() + ->getMock(); + $this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager') + ->disableOriginalConstructor() + ->getMock(); + + $this->instance = new Encryption($this->cryptMock, $this->keyManagerMock, $this->utilMock); + } + + /** + * @dataProvider dataProviderForTestGetPathToRealFile + */ + public function testGetPathToRealFile($path, $expected) { + $this->assertSame($expected, + \Test_Helper::invokePrivate($this->instance, 'getPathToRealFile', array($path)) + ); + } + + public function dataProviderForTestGetPathToRealFile() { + return array( + array('/user/files/foo/bar.txt', '/user/files/foo/bar.txt'), + array('/user/files/foo.txt', '/user/files/foo.txt'), + array('/user/files_versions/foo.txt.v543534', '/user/files/foo.txt'), + array('/user/files_versions/foo/bar.txt.v5454', '/user/files/foo/bar.txt'), + ); + } + + +}
\ No newline at end of file diff --git a/apps/encryption/tests/lib/users/SetupTest.php b/apps/encryption/tests/lib/users/SetupTest.php new file mode 100644 index 00000000000..6a66df3674b --- /dev/null +++ b/apps/encryption/tests/lib/users/SetupTest.php @@ -0,0 +1,81 @@ +<?php +/** + * @author Clark Tomlinson <clark@owncloud.com> + * @since 4/6/15, 11:50 AM + * @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 OCA\Encryption\Tests\Users; + + +use OCA\Encryption\Users\Setup; +use Test\TestCase; + +class SetupTest extends TestCase { + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $keyManagerMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $cryptMock; + /** + * @var Setup + */ + private $instance; + + public function testSetupServerSide() { + $this->keyManagerMock->expects($this->exactly(2)) + ->method('userHasKeys') + ->with('admin') + ->willReturnOnConsecutiveCalls(true, false); + + $this->assertTrue($this->instance->setupServerSide('admin', + 'password')); + + $this->keyManagerMock->expects($this->once()) + ->method('storeKeyPair') + ->with('admin', 'password') + ->willReturn(false); + + $this->assertFalse($this->instance->setupServerSide('admin', + 'password')); + } + + protected function setUp() { + parent::setUp(); + $logMock = $this->getMock('OCP\ILogger'); + $userSessionMock = $this->getMockBuilder('OCP\IUserSession') + ->disableOriginalConstructor() + ->getMock(); + $this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt') + ->disableOriginalConstructor() + ->getMock(); + + $this->keyManagerMock = $this->getMockBuilder('OCA\Encryption\KeyManager') + ->disableOriginalConstructor() + ->getMock(); + + $this->instance = new Setup($logMock, + $userSessionMock, + $this->cryptMock, + $this->keyManagerMock); + } + +} |