server/apps/encryption/tests/lib/KeyManagerTest.php

195 lines
4.1 KiB
PHP
Raw Normal View History

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;
use OC\Files\View;
2015-02-24 18:05:19 +00:00
use OCA\Encryption\KeyManager;
use Test\TestCase;
class KeyManagerTest extends TestCase {
/**
* @var bool
*/
private static $trashbinState;
2015-02-24 18:05:19 +00:00
/**
* @var KeyManager
*/
private $instance;
/**
* @var string
2015-02-24 18:05:19 +00:00
*/
private static $testUser = 'test-keyManager-user.dot';
2015-02-24 18:05:19 +00:00
/**
* @var
*/
private $dummyKeys;
/**
* @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
/**
*
*/
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();
$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');
$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');
$logMock = $this->getMock('OCP\ILogger');
$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,
$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
$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-02-24 18:05:19 +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));
}
/**
* @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-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,
$this->dummyKeys['publicKey']));
2015-02-24 18:05:19 +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,
$this->dummyKeys['privateKey']));
2015-02-24 18:05:19 +00:00
}
/**
*
*/
2015-02-24 18:05:19 +00:00
public function testUserHasKeys() {
$this->assertFalse($this->instance->userHasKeys($this->userId));
}
/**
*
*/
public function testInit() {
$this->assertFalse($this->instance->init($this->userId, 'pass'));
}
2015-02-24 18:05:19 +00:00
}