2015-02-24 18:05:19 +00:00
|
|
|
<?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;
|
|
|
|
|
|
|
|
|
2015-03-30 21:01:50 +00:00
|
|
|
use OC\Files\View;
|
2015-02-24 18:05:19 +00:00
|
|
|
use OCA\Encryption\KeyManager;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class KeyManagerTest extends TestCase {
|
2015-03-30 21:01:50 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $trashbinState;
|
2015-02-24 18:05:19 +00:00
|
|
|
/**
|
|
|
|
* @var KeyManager
|
|
|
|
*/
|
|
|
|
private $instance;
|
|
|
|
/**
|
2015-03-30 21:01:50 +00:00
|
|
|
* @var string
|
2015-02-24 18:05:19 +00:00
|
|
|
*/
|
2015-03-30 21:01:50 +00:00
|
|
|
private static $testUser = 'test-keyManager-user.dot';
|
2015-02-24 18:05:19 +00:00
|
|
|
/**
|
|
|
|
* @var
|
|
|
|
*/
|
|
|
|
private $dummyKeys;
|
2015-03-30 21:01:50 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $userId;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $userPassword;
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\View
|
|
|
|
*/
|
|
|
|
private $view;
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $dataDir;
|
2015-02-24 18:05:19 +00:00
|
|
|
|
2015-03-24 21:29:10 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2015-03-30 21:01:50 +00:00
|
|
|
public static function setUpBeforeClass() {
|
|
|
|
parent::setUpBeforeClass();
|
|
|
|
|
|
|
|
// Remember files_trashbin state
|
|
|
|
self::$trashbinState = \OC_App::isEnabled('files_trashbin');
|
|
|
|
|
|
|
|
// We dont want tests with app files_trashbin enabled
|
|
|
|
\OC_App::disable('files_trashbin');
|
|
|
|
|
|
|
|
$userManager = \OC::$server->getUserManager();
|
|
|
|
$userManager->createUser(self::$testUser,
|
|
|
|
self::$testUser);
|
|
|
|
|
|
|
|
// Create test user
|
|
|
|
parent::loginAsUser(self::$testUser);
|
|
|
|
}
|
|
|
|
|
2015-02-24 18:05:19 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2015-03-24 21:29:10 +00:00
|
|
|
$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')
|
2015-02-24 18:05:19 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$configMock = $this->getMock('OCP\IConfig');
|
2015-03-24 21:29:10 +00:00
|
|
|
$userMock = $this->getMock('OCP\IUserSession');
|
|
|
|
$userMock
|
2015-02-24 18:05:19 +00:00
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('admin'));
|
2015-03-27 00:35:36 +00:00
|
|
|
$sessionMock = $this->getMock('OCP\ISession');
|
2015-03-24 21:29:10 +00:00
|
|
|
$logMock = $this->getMock('OCP\ILogger');
|
2015-03-30 21:01:50 +00:00
|
|
|
$recoveryMock = $this->getMockBuilder('OCA\Encryption\Recovery')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2015-03-27 00:35:36 +00:00
|
|
|
$this->instance = new KeyManager($keyStorageMock,
|
|
|
|
$cryptMock,
|
|
|
|
$configMock,
|
|
|
|
$userMock,
|
|
|
|
$sessionMock,
|
2015-03-30 21:01:50 +00:00
|
|
|
$logMock,
|
|
|
|
$recoveryMock);
|
|
|
|
|
|
|
|
self::loginAsUser(self::$testUser);
|
|
|
|
$this->userId = self::$testUser;
|
|
|
|
$this->userPassword = self::$testUser;
|
|
|
|
$this->view = new View('/');
|
|
|
|
|
|
|
|
$this->dummyKeys = [
|
|
|
|
'privateKey' => 'superinsecureprivatekey',
|
|
|
|
'publicKey' => 'superinsecurepublickey'
|
|
|
|
];
|
2015-02-24 18:05:19 +00:00
|
|
|
|
2015-03-30 21:01:50 +00:00
|
|
|
|
|
|
|
$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 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');
|
|
|
|
}
|
2015-02-24 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
2015-03-30 21:01:50 +00:00
|
|
|
|
2015-02-24 18:05:19 +00:00
|
|
|
/**
|
2015-03-24 21:29:10 +00:00
|
|
|
* @expectedException \OC\Encryption\Exceptions\PrivateKeyMissingException
|
2015-02-24 18:05:19 +00:00
|
|
|
*/
|
|
|
|
public function testGetPrivateKey() {
|
|
|
|
$this->assertFalse($this->instance->getPrivateKey($this->userId));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-24 21:29:10 +00:00
|
|
|
* @expectedException \OC\Encryption\Exceptions\PublicKeyMissingException
|
2015-02-24 18:05:19 +00:00
|
|
|
*/
|
|
|
|
public function testGetPublicKey() {
|
|
|
|
$this->assertFalse($this->instance->getPublicKey($this->userId));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testRecoveryKeyExists() {
|
|
|
|
$this->assertFalse($this->instance->recoveryKeyExists());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testCheckRecoveryKeyPassword() {
|
|
|
|
$this->assertFalse($this->instance->checkRecoveryPassword('pass'));
|
|
|
|
}
|
|
|
|
|
2015-03-24 21:29:10 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2015-02-24 18:05:19 +00:00
|
|
|
public function testSetPublicKey() {
|
|
|
|
|
2015-03-27 00:35:36 +00:00
|
|
|
$this->assertTrue($this->instance->setPublicKey($this->userId,
|
2015-03-30 21:01:50 +00:00
|
|
|
$this->dummyKeys['publicKey']));
|
2015-02-24 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 21:29:10 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2015-02-24 18:05:19 +00:00
|
|
|
public function testSetPrivateKey() {
|
2015-03-27 00:35:36 +00:00
|
|
|
$this->assertTrue($this->instance->setPrivateKey($this->userId,
|
2015-03-30 21:01:50 +00:00
|
|
|
$this->dummyKeys['privateKey']));
|
2015-02-24 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
2015-03-24 21:29:10 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2015-02-24 18:05:19 +00:00
|
|
|
public function testUserHasKeys() {
|
|
|
|
$this->assertFalse($this->instance->userHasKeys($this->userId));
|
|
|
|
}
|
|
|
|
|
2015-03-24 21:29:10 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testInit() {
|
|
|
|
$this->assertFalse($this->instance->init($this->userId, 'pass'));
|
|
|
|
}
|
|
|
|
|
2015-02-24 18:05:19 +00:00
|
|
|
|
|
|
|
}
|