2015-02-24 21:05:19 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Clark Tomlinson <fallen013@gmail.com>
|
2015-06-25 12:43:55 +03:00
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @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/>
|
2015-02-24 21:05:19 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Encryption\Tests;
|
|
|
|
|
|
|
|
|
|
|
|
use OCA\Encryption\KeyManager;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class KeyManagerTest extends TestCase {
|
|
|
|
/**
|
|
|
|
* @var KeyManager
|
|
|
|
*/
|
|
|
|
private $instance;
|
2015-03-31 00:01:50 +03:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $userId;
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
/** @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;
|
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->userId = 'user1';
|
|
|
|
$this->systemKeyId = 'systemKeyId';
|
|
|
|
$this->keyStorageMock = $this->getMock('OCP\Encryption\Keys\IStorage');
|
|
|
|
$this->cryptMock = $this->getMockBuilder('OCA\Encryption\Crypto\Crypt')
|
2015-02-24 21:05:19 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-03-31 21:30:54 +03:00
|
|
|
$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')
|
2015-03-31 00:01:50 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance = new KeyManager(
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock,
|
|
|
|
$this->cryptMock,
|
|
|
|
$this->configMock,
|
|
|
|
$this->userMock,
|
|
|
|
$this->sessionMock,
|
|
|
|
$this->logMock,
|
|
|
|
$this->utilMock);
|
2015-04-01 20:07:54 +03:00
|
|
|
}
|
2015-03-31 00:01:50 +03:00
|
|
|
|
2015-04-01 20:07:54 +03:00
|
|
|
public function testDeleteShareKey() {
|
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('deleteFileKey')
|
|
|
|
->with($this->equalTo('/path'), $this->equalTo('keyId.shareKey'))
|
|
|
|
->willReturn(true);
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->assertTrue(
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->deleteShareKey('/path', 'keyId')
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPrivateKey() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('getUserKey')
|
|
|
|
->with($this->equalTo($this->userId), $this->equalTo('privateKey'))
|
|
|
|
->willReturn('privateKey');
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertSame('privateKey',
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->getPrivateKey($this->userId)
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPublicKey() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('getUserKey')
|
|
|
|
->with($this->equalTo($this->userId), $this->equalTo('publicKey'))
|
|
|
|
->willReturn('publicKey');
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertSame('publicKey',
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->getPublicKey($this->userId)
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRecoveryKeyExists() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('getSystemUserKey')
|
|
|
|
->with($this->equalTo($this->systemKeyId . '.publicKey'))
|
|
|
|
->willReturn('recoveryKey');
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->assertTrue($this->instance->recoveryKeyExists());
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckRecoveryKeyPassword() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$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');
|
|
|
|
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->assertTrue($this->instance->checkRecoveryPassword('pass'));
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetPublicKey() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('setUserKey')
|
|
|
|
->with(
|
|
|
|
$this->equalTo($this->userId),
|
|
|
|
$this->equalTo('publicKey'),
|
|
|
|
$this->equalTo('key'))
|
|
|
|
->willReturn(true);
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->setPublicKey($this->userId, 'key')
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetPrivateKey() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('setUserKey')
|
|
|
|
->with(
|
|
|
|
$this->equalTo($this->userId),
|
|
|
|
$this->equalTo('privateKey'),
|
|
|
|
$this->equalTo('key'))
|
|
|
|
->willReturn(true);
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->setPrivateKey($this->userId, 'key')
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testUserHasKeys() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->exactly(2))
|
|
|
|
->method('getUserKey')
|
|
|
|
->with($this->equalTo($this->userId), $this->anything())
|
|
|
|
->willReturn('key');
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->userHasKeys($this->userId)
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
public function testInit() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$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');
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->init($this->userId, 'pass')
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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');
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2015-04-06 22:08:09 +03:00
|
|
|
$this->instance->setRecoveryKey('pass',
|
|
|
|
array('publicKey' => 'publicKey', 'privateKey' => 'privateKey'))
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-02 11:26:40 +03:00
|
|
|
public function testSetSystemPrivateKey() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->exactly(1))
|
|
|
|
->method('setSystemUserKey')
|
|
|
|
->with($this->equalTo('keyId.privateKey'), $this->equalTo('key'))
|
|
|
|
->willReturn(true);
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->setSystemPrivateKey('keyId', 'key')
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-02 11:26:40 +03:00
|
|
|
public function testGetSystemPrivateKey() {
|
2015-03-31 21:30:54 +03:00
|
|
|
$this->keyStorageMock->expects($this->exactly(1))
|
2015-04-02 11:26:40 +03:00
|
|
|
->method('getSystemUserKey')
|
2015-03-31 21:30:54 +03:00
|
|
|
->with($this->equalTo('keyId.privateKey'))
|
|
|
|
->willReturn('systemPrivateKey');
|
2015-04-06 22:08:09 +03:00
|
|
|
|
2015-03-31 21:30:54 +03:00
|
|
|
|
|
|
|
$this->assertSame('systemPrivateKey',
|
2015-04-01 20:07:54 +03:00
|
|
|
$this->instance->getSystemPrivateKey('keyId')
|
2015-03-31 21:30:54 +03:00
|
|
|
);
|
2015-03-25 00:29:10 +03:00
|
|
|
}
|
2015-04-06 22:08:09 +03:00
|
|
|
|
|
|
|
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())
|
2015-04-15 20:37:03 +03:00
|
|
|
->method('decryptPrivateKey')
|
2015-04-06 22:08:09 +03:00
|
|
|
->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);
|
|
|
|
|
2015-06-03 13:03:02 +03:00
|
|
|
$this->assertTrue(self::invokePrivate($this->instance,
|
2015-04-06 22:08:09 +03:00
|
|
|
'deletePrivateKey',
|
|
|
|
[$this->userId]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteAllFileKeys() {
|
|
|
|
$this->keyStorageMock->expects($this->once())
|
|
|
|
->method('deleteAllFileKeys')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->assertTrue($this->instance->deleteAllFileKeys('/'));
|
|
|
|
}
|
2015-04-29 18:18:41 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* test add public share key and or recovery key to the list of public keys
|
|
|
|
*
|
|
|
|
* @dataProvider dataTestAddSystemKeys
|
|
|
|
*
|
|
|
|
* @param array $accessList
|
|
|
|
* @param array $publicKeys
|
|
|
|
* @param string $uid
|
|
|
|
* @param array $expectedKeys
|
|
|
|
*/
|
|
|
|
public function testAddSystemKeys($accessList, $publicKeys, $uid, $expectedKeys) {
|
|
|
|
|
|
|
|
$publicShareKeyId = 'publicShareKey';
|
|
|
|
$recoveryKeyId = 'recoveryKey';
|
|
|
|
|
|
|
|
$this->keyStorageMock->expects($this->any())
|
|
|
|
->method('getSystemUserKey')
|
|
|
|
->willReturnCallback(function($keyId, $encryptionModuleId) {
|
|
|
|
return $keyId;
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->utilMock->expects($this->any())
|
|
|
|
->method('isRecoveryEnabledForUser')
|
|
|
|
->willReturnCallback(function($uid) {
|
|
|
|
if ($uid === 'user1') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// set key IDs
|
2015-06-03 13:03:02 +03:00
|
|
|
self::invokePrivate($this->instance, 'publicShareKeyId', [$publicShareKeyId]);
|
|
|
|
self::invokePrivate($this->instance, 'recoveryKeyId', [$recoveryKeyId]);
|
2015-04-29 18:18:41 +03:00
|
|
|
|
|
|
|
$result = $this->instance->addSystemKeys($accessList, $publicKeys, $uid);
|
|
|
|
|
|
|
|
foreach ($expectedKeys as $expected) {
|
|
|
|
$this->assertArrayHasKey($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSameSize($expectedKeys, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* data provider for testAddSystemKeys()
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function dataTestAddSystemKeys() {
|
|
|
|
return array(
|
|
|
|
array(['public' => true],[], 'user1', ['publicShareKey', 'recoveryKey']),
|
|
|
|
array(['public' => false], [], 'user1', ['recoveryKey']),
|
|
|
|
array(['public' => true],[], 'user2', ['publicShareKey']),
|
|
|
|
array(['public' => false], [], 'user2', []),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|