]> source.dussan.org Git - nextcloud-server.git/commitdiff
cleanup keymanager test and add some additional tests
authorBjoern Schiessle <schiessle@owncloud.com>
Tue, 31 Mar 2015 18:30:54 +0000 (20:30 +0200)
committerThomas Müller <thomas.mueller@tmit.eu>
Tue, 7 Apr 2015 11:30:29 +0000 (13:30 +0200)
apps/encryption/lib/keymanager.php
apps/encryption/lib/recovery.php
apps/encryption/tests/lib/KeyManagerTest.php

index e8e92cd549aafabe9faca80d68505da6d48ab786..f96c426a72550ab0d3114b186a2a403088476350 100644 (file)
@@ -300,6 +300,7 @@ class KeyManager {
         *
         * @param string $uid userid
         * @param string $passPhrase users password
+        * @return boolean
         */
        public function init($uid, $passPhrase) {
                try {
@@ -314,6 +315,8 @@ class KeyManager {
 
                $this->session->setPrivateKey($privateKey);
                $this->session->setStatus(Session::INIT_SUCCESSFUL);
+
+               return true;
        }
 
        /**
@@ -353,14 +356,14 @@ class KeyManager {
        }
 
        /**
-        * delete file key
+        * delete share key
         *
         * @param string $path
         * @param string $keyId
         * @return boolean
         */
-       public function deleteFileKey($path, $keyId) {
-               return $this->keyStorage->deleteFileKey($path, $keyId);
+       public function deleteShareKey($path, $keyId) {
+               return $this->keyStorage->deleteFileKey($path, $keyId . '.' . $this->shareKeyId);
        }
 
 
index 00711e3bfab70d29cfbb9175165443d974b88778..4201b829ec924ce0fc1aebfacb0f02b09c5234c4 100644 (file)
@@ -228,7 +228,7 @@ class Recovery {
                        if ($item['type'] === 'dir') {
                                $this->removeRecoveryKeys($filePath . '/');
                        } else {
-                               $this->keyManager->deleteFileKey($filePath, $this->keyManager->getRecoveryKeyId() . '.shareKey');
+                               $this->keyManager->deleteShareKey($filePath, $this->keyManager->getRecoveryKeyId());
                        }
                }
        }
index 510ab1798883f74730ff9e389702cbde1fa09bfc..98ffe85c3788194428b94820832b23b04cce0768 100644 (file)
@@ -48,6 +48,30 @@ class KeyManagerTest extends TestCase {
         */
        private $dataDir;
 
+       /** @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;
+
        /**
         *
         */
@@ -70,124 +94,290 @@ class KeyManagerTest extends TestCase {
 
        public function setUp() {
                parent::setUp();
-               $keyStorageMock = $this->getMock('OCP\Encryption\Keys\IStorage');
+               $this->userId = 'user1';
+               $this->systemKeyId = 'systemKeyId';
+               $this->keyStorageMock = $this->getMock('OCP\Encryption\Keys\IStorage');
+
+               /*
                $keyStorageMock->method('getUserKey')
                        ->will($this->returnValue(false));
                $keyStorageMock->method('setUserKey')
                        ->will($this->returnValue(true));
-               $cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt')
+                */
+
+               $this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt')
                        ->disableOriginalConstructor()
                        ->getMock();
-               $configMock = $this->getMock('OCP\IConfig');
-               $userMock = $this->getMock('OCP\IUserSession');
+               $this->configMock = $this->getMock('OCP\IConfig');
+               $this->configMock->expects($this->any())
+                       ->method('getAppValue')
+                       ->willReturn($this->systemKeyId);
+               $this->userMock = $this->getMock('OCP\IUserSession');
+
+               /*
                $userMock
                        ->method('getUID')
                        ->will($this->returnValue('admin'));
-               $sessionMock = $this->getMock('OCP\ISession');
-               $logMock = $this->getMock('OCP\ILogger');
-               $recoveryMock = $this->getMockBuilder('OCA\Encryption\Recovery')
+                */
+
+               $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($keyStorageMock,
-                       $cryptMock,
-                       $configMock,
-                       $userMock,
-                       $sessionMock,
-                       $logMock,
-                       $recoveryMock);
-
-               self::loginAsUser(self::$testUser);
-               $this->userId = self::$testUser;
-               $this->userPassword = self::$testUser;
-               $this->view = new View('/');
-
-               $this->dummyKeys = [
-                       'privateKey' => 'superinsecureprivatekey',
-                       'publicKey' => 'superinsecurepublickey'
-               ];
-
-
-               $userManager = \OC::$server->getUserManager();
-
-               $userHome = $userManager->get($this->userId)->getHome();
-
-               $this->dataDir = str_replace('/' . $this->userId, '', $userHome);
        }
 
-       protected function tearDown() {
-               parent::tearDown();
-               $this->view->deleteAll('/' . self::$testUser . '/files_encryption/keys');
-       }
+       public function testDeleteShareKey() {
+               $this->keyStorageMock->expects($this->any())
+                       ->method('deleteFileKey')
+                       ->with($this->equalTo('/path'), $this->equalTo('keyId.shareKey'))
+                       ->willReturn(true);
+               $keymanager = new KeyManager(
+                       $this->keyStorageMock,
+                       $this->cryptMock,
+                       $this->configMock,
+                       $this->userMock,
+                       $this->sessionMock,
+                       $this->logMock,
+                       $this->utilMock);
 
-       public static function tearDownAfterClass() {
-               parent::tearDownAfterClass();
-               // Cleanup Test user
-               \OC::$server->getUserManager()->get(self::$testUser)->delete();
-               // Reset app files_trashbin
-               if (self::$trashbinState) {
-                       \OC_App::enable('files_trashbin');
-               }
+               $this->assertTrue(
+                       $keymanager->deleteShareKey('/path', 'keyId')
+               );
        }
 
 
-       /**
-        * @expectedException \OC\Encryption\Exceptions\PrivateKeyMissingException
-        */
        public function testGetPrivateKey() {
-               $this->assertFalse($this->instance->getPrivateKey($this->userId));
+               $this->keyStorageMock->expects($this->any())
+                       ->method('getUserKey')
+                       ->with($this->equalTo($this->userId), $this->equalTo('privateKey'))
+                       ->willReturn('privateKey');
+               $keymanager = new KeyManager(
+                       $this->keyStorageMock,
+                       $this->cryptMock,
+                       $this->configMock,
+                       $this->userMock,
+                       $this->sessionMock,
+                       $this->logMock,
+                       $this->utilMock);
+
+               $this->assertSame('privateKey',
+                       $keymanager->getPrivateKey($this->userId)
+               );
        }
 
-       /**
-        * @expectedException \OC\Encryption\Exceptions\PublicKeyMissingException
-        */
        public function testGetPublicKey() {
-               $this->assertFalse($this->instance->getPublicKey($this->userId));
+               $this->keyStorageMock->expects($this->any())
+                       ->method('getUserKey')
+                       ->with($this->equalTo($this->userId), $this->equalTo('publicKey'))
+                       ->willReturn('publicKey');
+               $keymanager = new KeyManager(
+                       $this->keyStorageMock,
+                       $this->cryptMock,
+                       $this->configMock,
+                       $this->userMock,
+                       $this->sessionMock,
+                       $this->logMock,
+                       $this->utilMock);
+
+               $this->assertSame('publicKey',
+                       $keymanager->getPublicKey($this->userId)
+               );
        }
 
-       /**
-        *
-        */
        public function testRecoveryKeyExists() {
-               $this->assertFalse($this->instance->recoveryKeyExists());
+               $this->keyStorageMock->expects($this->any())
+                       ->method('getSystemUserKey')
+                       ->with($this->equalTo($this->systemKeyId . '.publicKey'))
+                       ->willReturn('recoveryKey');
+               $keymanager = new KeyManager(
+                       $this->keyStorageMock,
+                       $this->cryptMock,
+                       $this->configMock,
+                       $this->userMock,
+                       $this->sessionMock,
+                       $this->logMock,
+                       $this->utilMock);
+
+               $this->assertTrue($keymanager->recoveryKeyExists());
        }
 
        /**
         *
         */
        public function testCheckRecoveryKeyPassword() {
-               $this->assertFalse($this->instance->checkRecoveryPassword('pass'));
+               $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');
+               $keymanager = new KeyManager(
+                       $this->keyStorageMock,
+                       $this->cryptMock,
+                       $this->configMock,
+                       $this->userMock,
+                       $this->sessionMock,
+                       $this->logMock,
+                       $this->utilMock);
+
+               $this->assertTrue($keymanager->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);
+               $keymanager = new KeyManager(
+                       $this->keyStorageMock,
+                       $this->cryptMock,
+                       $this->configMock,
+                       $this->userMock,
+                       $this->sessionMock,
+                       $this->logMock,
+                       $this->utilMock);
+
+               $this->assertTrue(
+                       $keymanager->setPublicKey($this->userId, 'key')
+               );
 
-               $this->assertTrue($this->instance->setPublicKey($this->userId,
-                       $this->dummyKeys['publicKey']));
        }
 
-       /**
-        *
-        */
        public function testSetPrivateKey() {
-               $this->assertTrue($this->instance->setPrivateKey($this->userId,
-                       $this->dummyKeys['privateKey']));
+               $this->keyStorageMock->expects($this->any())
+                       ->method('setUserKey')
+                       ->with(
+                               $this->equalTo($this->userId),
+                               $this->equalTo('privateKey'),
+                               $this->equalTo('key'))
+                       ->willReturn(true);
+               $keymanager = new KeyManager(
+                       $this->keyStorageMock,
+                       $this->cryptMock,
+                       $this->configMock,
+                       $this->userMock,
+                       $this->sessionMock,
+                       $this->logMock,
+                       $this->utilMock);
+
+               $this->assertTrue(
+                       $keymanager->setPrivateKey($this->userId, 'key')
+               );
        }
 
-       /**
-        *
-        */
        public function testUserHasKeys() {
-               $this->assertFalse($this->instance->userHasKeys($this->userId));
+               $this->keyStorageMock->expects($this->exactly(2))
+                       ->method('getUserKey')
+                       ->with($this->equalTo($this->userId), $this->anything())
+                       ->willReturn('key');
+               $keymanager = new KeyManager(
+                       $this->keyStorageMock,
+                       $this->cryptMock,
+                       $this->configMock,
+                       $this->userMock,
+                       $this->sessionMock,
+                       $this->logMock,
+                       $this->utilMock);
+
+               $this->assertTrue(
+                       $keymanager->userHasKeys($this->userId)
+               );
        }
 
        /**
         *
         */
        public function testInit() {
-               $this->assertFalse($this->instance->init($this->userId, 'pass'));
+               $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');
+               $keymanager = new KeyManager(
+                       $this->keyStorageMock,
+                       $this->cryptMock,
+                       $this->configMock,
+                       $this->userMock,
+                       $this->sessionMock,
+                       $this->logMock,
+                       $this->utilMock);
+
+               $this->assertTrue(
+                       $keymanager->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');
+               $keymanager = new KeyManager(
+                       $this->keyStorageMock,
+                       $this->cryptMock,
+                       $this->configMock,
+                       $this->userMock,
+                       $this->sessionMock,
+                       $this->logMock,
+                       $this->utilMock);
+
+               $this->assertTrue(
+                       $keymanager->setRecoveryKey('pass', array('publicKey' => 'publicKey', 'privateKey' => 'privateKey'))
+               );
+       }
+
+       public function setSystemPrivateKey() {
+               $this->keyStorageMock->expects($this->exactly(1))
+                       ->method('setSystemUserKey')
+                       ->with($this->equalTo('keyId.privateKey'), $this->equalTo('key'))
+                       ->willReturn(true);
+               $keymanager = new KeyManager(
+                       $this->keyStorageMock,
+                       $this->cryptMock,
+                       $this->configMock,
+                       $this->userMock,
+                       $this->sessionMock,
+                       $this->logMock,
+                       $this->utilMock);
+
+               $this->assertTrue(
+                       $keymanager->setSystemPrivateKey('keyId', 'key')
+               );
+       }
+
+       public function getSystemPrivateKey() {
+               $this->keyStorageMock->expects($this->exactly(1))
+                       ->method('setSystemUserKey')
+                       ->with($this->equalTo('keyId.privateKey'))
+                       ->willReturn('systemPrivateKey');
+               $keymanager = new KeyManager(
+                       $this->keyStorageMock,
+                       $this->cryptMock,
+                       $this->configMock,
+                       $this->userMock,
+                       $this->sessionMock,
+                       $this->logMock,
+                       $this->utilMock);
+
+               $this->assertSame('systemPrivateKey',
+                       $keymanager->getSystemPrivateKey('keyId')
+               );
        }