summaryrefslogtreecommitdiffstats
path: root/apps/encryption/tests/lib/KeyManagerTest.php
blob: 510ab1798883f74730ff9e389702cbde1fa09bfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?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;
use OCA\Encryption\KeyManager;
use Test\TestCase;

class KeyManagerTest extends TestCase {
	/**
	 * @var bool
	 */
	private static $trashbinState;
	/**
	 * @var KeyManager
	 */
	private $instance;
	/**
	 * @var string
	 */
	private static $testUser = 'test-keyManager-user.dot';
	/**
	 * @var
	 */
	private $dummyKeys;
	/**
	 * @var string
	 */
	private $userId;
	/**
	 * @var string
	 */
	private $userPassword;
	/**
	 * @var \OC\Files\View
	 */
	private $view;
	/**
	 * @var string
	 */
	private $dataDir;

	/**
	 *
	 */
	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);
	}

	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')
			->disableOriginalConstructor()
			->getMock();
		$configMock = $this->getMock('OCP\IConfig');
		$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')
			->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 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');
		}
	}


	/**
	 * @expectedException \OC\Encryption\Exceptions\PrivateKeyMissingException
	 */
	public function testGetPrivateKey() {
		$this->assertFalse($this->instance->getPrivateKey($this->userId));
	}

	/**
	 * @expectedException \OC\Encryption\Exceptions\PublicKeyMissingException
	 */
	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'));
	}

	/**
	 *
	 */
	public function testSetPublicKey() {

		$this->assertTrue($this->instance->setPublicKey($this->userId,
			$this->dummyKeys['publicKey']));
	}

	/**
	 *
	 */
	public function testSetPrivateKey() {
		$this->assertTrue($this->instance->setPrivateKey($this->userId,
			$this->dummyKeys['privateKey']));
	}

	/**
	 *
	 */
	public function testUserHasKeys() {
		$this->assertFalse($this->instance->userHasKeys($this->userId));
	}

	/**
	 *
	 */
	public function testInit() {
		$this->assertFalse($this->instance->init($this->userId, 'pass'));
	}


}