Blame view

sources/apps/files_encryption/tests/keymanager.php 7.12 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
  <?php
  /**
   * Copyright (c) 2012 Sam Tuke <samtuke@owncloud.com>
   * This file is licensed under the Affero General Public License version 3 or
   * later.
   * See the COPYING-README file.
   */
31b7f2792   Kload   Upgrade to ownclo...
8
9
10
11
12
13
14
15
16
  require_once __DIR__ . '/../../../lib/base.php';
  require_once __DIR__ . '/../lib/crypt.php';
  require_once __DIR__ . '/../lib/keymanager.php';
  require_once __DIR__ . '/../lib/proxy.php';
  require_once __DIR__ . '/../lib/stream.php';
  require_once __DIR__ . '/../lib/util.php';
  require_once __DIR__ . '/../lib/helper.php';
  require_once __DIR__ . '/../appinfo/app.php';
  require_once __DIR__ . '/util.php';
03e52840d   Kload   Init
17
18
19
20
21
22
23
  
  use OCA\Encryption;
  
  /**
   * Class Test_Encryption_Keymanager
   */
  class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase {
31b7f2792   Kload   Upgrade to ownclo...
24
  	const TEST_USER = "test-keymanager-user";
03e52840d   Kload   Init
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  	public $userId;
  	public $pass;
  	public $stateFilesTrashbin;
  	/**
  	 * @var OC_FilesystemView
  	 */
  	public $view;
  	public $randomKey;
  	public $dataShort;
  
  	public static function setUpBeforeClass() {
  		// reset backend
  		\OC_User::clearBackends();
  		\OC_User::useBackend('database');
  
  		// Filesystem related hooks
  		\OCA\Encryption\Helper::registerFilesystemHooks();
  
  		// clear and register hooks
  		\OC_FileProxy::clearProxies();
  		\OC_FileProxy::register(new OCA\Encryption\Proxy());
  
  		// disable file proxy by default
  		\OC_FileProxy::$enabled = false;
31b7f2792   Kload   Upgrade to ownclo...
49
50
51
  		// create test user
  		\OC_User::deleteUser(\Test_Encryption_Keymanager::TEST_USER);
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Keymanager::TEST_USER, true);
03e52840d   Kload   Init
52
53
54
55
  	}
  
  	function setUp() {
  		// set content for encrypting / decrypting in tests
31b7f2792   Kload   Upgrade to ownclo...
56
  		$this->dataLong = file_get_contents(__DIR__ . '/../lib/crypt.php');
03e52840d   Kload   Init
57
  		$this->dataShort = 'hats';
31b7f2792   Kload   Upgrade to ownclo...
58
59
60
  		$this->dataUrl = __DIR__ . '/../lib/crypt.php';
  		$this->legacyData = __DIR__ . '/legacy-text.txt';
  		$this->legacyEncryptedData = __DIR__ . '/legacy-encrypted-text.txt';
03e52840d   Kload   Init
61
62
63
64
65
66
67
  		$this->randomKey = Encryption\Crypt::generateKey();
  
  		$keypair = Encryption\Crypt::createKeypair();
  		$this->genPublicKey = $keypair['publicKey'];
  		$this->genPrivateKey = $keypair['privateKey'];
  
  		$this->view = new \OC_FilesystemView('/');
31b7f2792   Kload   Upgrade to ownclo...
68
69
70
  		\OC_User::setUserId(\Test_Encryption_Keymanager::TEST_USER);
  		$this->userId = \Test_Encryption_Keymanager::TEST_USER;
  		$this->pass = \Test_Encryption_Keymanager::TEST_USER;
03e52840d   Kload   Init
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  
  		$userHome = \OC_User::getHome($this->userId);
  		$this->dataDir = str_replace('/' . $this->userId, '', $userHome);
  
  		// remember files_trashbin state
  		$this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
  
  		// we don't want to tests with app files_trashbin enabled
  		\OC_App::disable('files_trashbin');
  	}
  
  	function tearDown() {
  		// reset app files_trashbin
  		if ($this->stateFilesTrashbin) {
  			OC_App::enable('files_trashbin');
  		}
  		else {
  			OC_App::disable('files_trashbin');
  		}
  	}
  
  	public static function tearDownAfterClass() {
  		\OC_FileProxy::$enabled = true;
31b7f2792   Kload   Upgrade to ownclo...
94
95
96
  
  		// cleanup test user
  		\OC_User::deleteUser(\Test_Encryption_Keymanager::TEST_USER);
03e52840d   Kload   Init
97
  	}
31b7f2792   Kload   Upgrade to ownclo...
98
99
100
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  	function testGetPrivateKey() {
  
  		$key = Encryption\Keymanager::getPrivateKey($this->view, $this->userId);
  
  		$privateKey = Encryption\Crypt::symmetricDecryptFileContent($key, $this->pass);
  
  		$res = openssl_pkey_get_private($privateKey);
  
  		$this->assertTrue(is_resource($res));
  
  		$sslInfo = openssl_pkey_get_details($res);
  
  		$this->assertArrayHasKey('key', $sslInfo);
  
  	}
31b7f2792   Kload   Upgrade to ownclo...
116
117
118
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
119
120
121
122
123
124
125
126
127
128
129
130
  	function testGetPublicKey() {
  
  		$publiceKey = Encryption\Keymanager::getPublicKey($this->view, $this->userId);
  
  		$res = openssl_pkey_get_public($publiceKey);
  
  		$this->assertTrue(is_resource($res));
  
  		$sslInfo = openssl_pkey_get_details($res);
  
  		$this->assertArrayHasKey('key', $sslInfo);
  	}
31b7f2792   Kload   Upgrade to ownclo...
131
  	/**
a293d369c   Kload   Update sources to...
132
133
134
135
136
137
138
139
140
141
142
  	 * @small
  	 */
  	function testGetFilenameFromShareKey() {
  		$this->assertEquals("file",
  				\TestProtectedKeymanagerMethods::testGetFilenameFromShareKey("file.user.shareKey"));
  		$this->assertEquals("file.name.with.dots",
  				\TestProtectedKeymanagerMethods::testGetFilenameFromShareKey("file.name.with.dots.user.shareKey"));
  		$this->assertFalse(\TestProtectedKeymanagerMethods::testGetFilenameFromShareKey("file.txt"));
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
143
144
  	 * @medium
  	 */
03e52840d   Kload   Init
145
  	function testSetFileKey() {
31b7f2792   Kload   Upgrade to ownclo...
146
  		$key = $this->randomKey;
03e52840d   Kload   Init
147

a293d369c   Kload   Update sources to...
148
  		$file = 'unittest-' . uniqid() . '.txt';
03e52840d   Kload   Init
149

31b7f2792   Kload   Upgrade to ownclo...
150
  		$util = new Encryption\Util($this->view, $this->userId);
03e52840d   Kload   Init
151
152
153
  		// Disable encryption proxy to prevent recursive calls
  		$proxyStatus = \OC_FileProxy::$enabled;
  		\OC_FileProxy::$enabled = false;
31b7f2792   Kload   Upgrade to ownclo...
154
  		$this->view->file_put_contents($this->userId . '/files/' . $file, $this->dataShort);
03e52840d   Kload   Init
155

31b7f2792   Kload   Upgrade to ownclo...
156
  		Encryption\Keymanager::setFileKey($this->view, $util, $file, $key);
03e52840d   Kload   Init
157

31b7f2792   Kload   Upgrade to ownclo...
158
  		$this->assertTrue($this->view->file_exists('/' . $this->userId . '/files_encryption/keyfiles/' . $file . '.key'));
03e52840d   Kload   Init
159
160
161
162
163
164
  
  		// cleanup
  		$this->view->unlink('/' . $this->userId . '/files/' . $file);
  
  		// change encryption proxy to previous state
  		\OC_FileProxy::$enabled = $proxyStatus;
03e52840d   Kload   Init
165
  	}
31b7f2792   Kload   Upgrade to ownclo...
166
167
168
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
  	function testGetUserKeys() {
  
  		$keys = Encryption\Keymanager::getUserKeys($this->view, $this->userId);
  
  		$resPublic = openssl_pkey_get_public($keys['publicKey']);
  
  		$this->assertTrue(is_resource($resPublic));
  
  		$sslInfoPublic = openssl_pkey_get_details($resPublic);
  
  		$this->assertArrayHasKey('key', $sslInfoPublic);
  
  		$privateKey = Encryption\Crypt::symmetricDecryptFileContent($keys['privateKey'], $this->pass);
  
  		$resPrivate = openssl_pkey_get_private($privateKey);
  
  		$this->assertTrue(is_resource($resPrivate));
  
  		$sslInfoPrivate = openssl_pkey_get_details($resPrivate);
  
  		$this->assertArrayHasKey('key', $sslInfoPrivate);
  	}
31b7f2792   Kload   Upgrade to ownclo...
191
192
193
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
194
195
196
  	function testRecursiveDelShareKeys() {
  
  		// generate filename
a293d369c   Kload   Update sources to...
197
  		$filename = '/tmp-' . uniqid() . '.txt';
03e52840d   Kload   Init
198
199
  
  		// create folder structure
31b7f2792   Kload   Upgrade to ownclo...
200
201
202
  		$this->view->mkdir('/'.Test_Encryption_Keymanager::TEST_USER.'/files/folder1');
  		$this->view->mkdir('/'.Test_Encryption_Keymanager::TEST_USER.'/files/folder1/subfolder');
  		$this->view->mkdir('/'.Test_Encryption_Keymanager::TEST_USER.'/files/folder1/subfolder/subsubfolder');
03e52840d   Kload   Init
203
204
205
206
207
208
  
  		// enable encryption proxy
  		$proxyStatus = \OC_FileProxy::$enabled;
  		\OC_FileProxy::$enabled = true;
  
  		// save file with content
31b7f2792   Kload   Upgrade to ownclo...
209
  		$cryptedFile = file_put_contents('crypt:///'.Test_Encryption_Keymanager::TEST_USER.'/files/folder1/subfolder/subsubfolder' . $filename, $this->dataShort);
03e52840d   Kload   Init
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  
  		// test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// change encryption proxy to previous state
  		\OC_FileProxy::$enabled = $proxyStatus;
  
  		// recursive delete keys
  		Encryption\Keymanager::delShareKey($this->view, array('admin'), '/folder1/');
  
  		// check if share key not exists
  		$this->assertFalse($this->view->file_exists(
  			'/admin/files_encryption/share-keys/folder1/subfolder/subsubfolder/' . $filename . '.admin.shareKey'));
  
  		// enable encryption proxy
  		$proxyStatus = \OC_FileProxy::$enabled;
  		\OC_FileProxy::$enabled = true;
  
  		// cleanup
  		$this->view->unlink('/admin/files/folder1');
  
  		// change encryption proxy to previous state
  		\OC_FileProxy::$enabled = $proxyStatus;
  	}
  }
a293d369c   Kload   Update sources to...
235
236
237
238
239
240
241
242
243
  
  /**
   * dummy class to access protected methods of \OCA\Encryption\Keymanager for testing
   */
  class TestProtectedKeymanagerMethods extends \OCA\Encryption\Keymanager {
  	public static function testGetFilenameFromShareKey($sharekey) {
  		return self::getFilenameFromShareKey($sharekey);
  	}
  }