Blame view

sources/apps/files_encryption/tests/util.php 22.2 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
  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__ . '/../appinfo/app.php';
03e52840d   Kload   Init
15
16
17
18
19
20
21
22
23
  
  use OCA\Encryption;
  
  /**
   * Class Test_Encryption_Util
   */
  class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
  
  	const TEST_ENCRYPTION_UTIL_USER1 = "test-util-user1";
f7d878ff1   kload   [enh] Update to 7...
24
25
26
  	const TEST_ENCRYPTION_UTIL_USER2 = "test-util-user2";
  	const TEST_ENCRYPTION_UTIL_GROUP1 = "test-util-group1";
  	const TEST_ENCRYPTION_UTIL_GROUP2 = "test-util-group2";
03e52840d   Kload   Init
27
28
29
30
31
32
33
  	const TEST_ENCRYPTION_UTIL_LEGACY_USER = "test-legacy-user";
  
  	public $userId;
  	public $encryptionDir;
  	public $publicKeyDir;
  	public $pass;
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
34
  	 * @var OC\Files\View
03e52840d   Kload   Init
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
  	 */
  	public $view;
  	public $keyfilesPath;
  	public $publicKeyPath;
  	public $privateKeyPath;
  	/**
  	 * @var \OCA\Encryption\Util
  	 */
  	public $util;
  	public $dataShort;
  	public $legacyEncryptedData;
  	public $legacyEncryptedDataKey;
  	public $legacyKey;
  	public $stateFilesTrashbin;
  
  	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());
  
  		// create test user
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1, true);
f7d878ff1   kload   [enh] Update to 7...
64
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER2, true);
03e52840d   Kload   Init
65
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER, true);
f7d878ff1   kload   [enh] Update to 7...
66
67
68
69
70
71
72
  
  		// create groups
  		\OC_Group::createGroup(self::TEST_ENCRYPTION_UTIL_GROUP1);
  		\OC_Group::createGroup(self::TEST_ENCRYPTION_UTIL_GROUP2);
  
  		// add user 1 to group1
  		\OC_Group::addToGroup(self::TEST_ENCRYPTION_UTIL_USER1, self::TEST_ENCRYPTION_UTIL_GROUP1);
03e52840d   Kload   Init
73
74
75
76
  	}
  
  
  	function setUp() {
a293d369c   Kload   Update sources to...
77
78
  		// login user
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1);
03e52840d   Kload   Init
79
80
81
82
83
  		\OC_User::setUserId(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1);
  		$this->userId = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1;
  		$this->pass = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1;
  
  		// set content for encrypting / decrypting in tests
31b7f2792   Kload   Upgrade to ownclo...
84
  		$this->dataUrl = __DIR__ . '/../lib/crypt.php';
03e52840d   Kload   Init
85
  		$this->dataShort = 'hats';
31b7f2792   Kload   Upgrade to ownclo...
86
87
88
89
  		$this->dataLong = file_get_contents(__DIR__ . '/../lib/crypt.php');
  		$this->legacyData = __DIR__ . '/legacy-text.txt';
  		$this->legacyEncryptedData = __DIR__ . '/legacy-encrypted-text.txt';
  		$this->legacyEncryptedDataKey = __DIR__ . '/encryption.key';
03e52840d   Kload   Init
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  		$this->legacyKey = "30943623843030686906\0\0\0\0";
  
  		$keypair = Encryption\Crypt::createKeypair();
  
  		$this->genPublicKey = $keypair['publicKey'];
  		$this->genPrivateKey = $keypair['privateKey'];
  
  		$this->publicKeyDir = '/' . 'public-keys';
  		$this->encryptionDir = '/' . $this->userId . '/' . 'files_encryption';
  		$this->keyfilesPath = $this->encryptionDir . '/' . 'keyfiles';
  		$this->publicKeyPath =
  			$this->publicKeyDir . '/' . $this->userId . '.public.key'; // e.g. data/public-keys/admin.public.key
  		$this->privateKeyPath =
  			$this->encryptionDir . '/' . $this->userId . '.private.key'; // e.g. data/admin/admin.private.key
6d9380f96   Cédric Dupont   Update sources OC...
104
  		$this->view = new \OC\Files\View('/');
03e52840d   Kload   Init
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  
  		$this->util = new Encryption\Util($this->view, $this->userId);
  
  		// 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() {
  		// cleanup test user
  		\OC_User::deleteUser(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1);
f7d878ff1   kload   [enh] Update to 7...
128
  		\OC_User::deleteUser(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER2);
03e52840d   Kload   Init
129
  		\OC_User::deleteUser(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
f7d878ff1   kload   [enh] Update to 7...
130
131
132
  		//cleanup groups
  		\OC_Group::deleteGroup(self::TEST_ENCRYPTION_UTIL_GROUP1);
  		\OC_Group::deleteGroup(self::TEST_ENCRYPTION_UTIL_GROUP2);
03e52840d   Kload   Init
133
134
135
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
136
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
137
  	 * test that paths set during User construction are correct
03e52840d   Kload   Init
138
139
140
141
142
143
144
145
146
147
148
149
150
  	 */
  	function testKeyPaths() {
  		$util = new Encryption\Util($this->view, $this->userId);
  
  		$this->assertEquals($this->publicKeyDir, $util->getPath('publicKeyDir'));
  		$this->assertEquals($this->encryptionDir, $util->getPath('encryptionDir'));
  		$this->assertEquals($this->keyfilesPath, $util->getPath('keyfilesPath'));
  		$this->assertEquals($this->publicKeyPath, $util->getPath('publicKeyPath'));
  		$this->assertEquals($this->privateKeyPath, $util->getPath('privateKeyPath'));
  
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
151
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
152
  	 * test detection of encrypted files
837968727   Kload   [enh] Upgrade to ...
153
154
155
156
157
158
  	 */
  	function testIsEncryptedPath() {
  
  		$util = new Encryption\Util($this->view, $this->userId);
  
  		self::loginHelper($this->userId);
6d9380f96   Cédric Dupont   Update sources OC...
159
160
  		$unencryptedFile = '/tmpUnencrypted-' . uniqid() . '.txt';
  		$encryptedFile =  '/tmpEncrypted-' . uniqid() . '.txt';
837968727   Kload   [enh] Upgrade to ...
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
  
  		// Disable encryption proxy to write a unencrypted file
  		$proxyStatus = \OC_FileProxy::$enabled;
  		\OC_FileProxy::$enabled = false;
  
  		$this->view->file_put_contents($this->userId . '/files/' . $unencryptedFile, $this->dataShort);
  
  		// Re-enable proxy - our work is done
  		\OC_FileProxy::$enabled = $proxyStatus;
  
  		// write a encrypted file
  		$this->view->file_put_contents($this->userId . '/files/' . $encryptedFile, $this->dataShort);
  
  		// test if both files are detected correctly
  		$this->assertFalse($util->isEncryptedPath($this->userId . '/files/' . $unencryptedFile));
  		$this->assertTrue($util->isEncryptedPath($this->userId . '/files/' . $encryptedFile));
  
  		// cleanup
6d9380f96   Cédric Dupont   Update sources OC...
179
180
  		$this->view->unlink($this->userId . '/files/' . $unencryptedFile);
  		$this->view->unlink($this->userId . '/files/' . $encryptedFile);
837968727   Kload   [enh] Upgrade to ...
181
182
183
184
185
  
  	}
  
  	/**
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
186
  	 * test setup of encryption directories
03e52840d   Kload   Init
187
188
189
190
191
192
  	 */
  	function testSetupServerSide() {
  		$this->assertEquals(true, $this->util->setupServerSide($this->pass));
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
193
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
194
  	 * test checking whether account is ready for encryption,
03e52840d   Kload   Init
195
196
197
198
199
200
  	 */
  	function testUserIsReady() {
  		$this->assertEquals(true, $this->util->ready());
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
201
  	 * test checking whether account is not ready for encryption,
03e52840d   Kload   Init
202
203
204
205
206
207
208
209
210
211
212
213
  	 */
  //	function testUserIsNotReady() {
  //		$this->view->unlink($this->publicKeyDir);
  //
  //		$params['uid'] = $this->userId;
  //		$params['password'] = $this->pass;
  //		$this->assertFalse(OCA\Encryption\Hooks::login($params));
  //
  //		$this->view->unlink($this->privateKeyPath);
  //	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
214
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
215
  	 * test checking whether account is not ready for encryption,
03e52840d   Kload   Init
216
217
218
  	 */
  	function testIsLegacyUser() {
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
6d9380f96   Cédric Dupont   Update sources OC...
219
  		$userView = new \OC\Files\View('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
03e52840d   Kload   Init
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
  
  		// Disable encryption proxy to prevent recursive calls
  		$proxyStatus = \OC_FileProxy::$enabled;
  		\OC_FileProxy::$enabled = false;
  
  		$encryptionKeyContent = file_get_contents($this->legacyEncryptedDataKey);
  		$userView->file_put_contents('/encryption.key', $encryptionKeyContent);
  
  		\OC_FileProxy::$enabled = $proxyStatus;
  
  		$params['uid'] = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER;
  		$params['password'] = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER;
  
  		$this->setMigrationStatus(0, \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
  
  		$this->assertTrue(OCA\Encryption\Hooks::login($params));
31b7f2792   Kload   Upgrade to ownclo...
236
  		$this->assertEquals($this->legacyKey, \OC::$session->get('legacyKey'));
03e52840d   Kload   Init
237
  	}
31b7f2792   Kload   Upgrade to ownclo...
238
239
240
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
241
242
243
244
245
246
  	function testRecoveryEnabledForUser() {
  
  		$util = new Encryption\Util($this->view, $this->userId);
  
  		// Record the value so we can return it to it's original state later
  		$enabled = $util->recoveryEnabledForUser();
6d9380f96   Cédric Dupont   Update sources OC...
247
  		$this->assertTrue($util->setRecoveryForUser(!$enabled));
03e52840d   Kload   Init
248

6d9380f96   Cédric Dupont   Update sources OC...
249
  		$this->assertEquals(!$enabled, $util->recoveryEnabledForUser());
03e52840d   Kload   Init
250

6d9380f96   Cédric Dupont   Update sources OC...
251
  		$this->assertTrue($util->setRecoveryForUser($enabled));
03e52840d   Kload   Init
252

6d9380f96   Cédric Dupont   Update sources OC...
253
  		$this->assertEquals($enabled, $util->recoveryEnabledForUser());
03e52840d   Kload   Init
254

03e52840d   Kload   Init
255
256
  
  	}
31b7f2792   Kload   Upgrade to ownclo...
257
258
259
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
260
261
262
  	function testGetUidAndFilename() {
  
  		\OC_User::setUserId(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1);
a293d369c   Kload   Update sources to...
263
  		$filename = '/tmp-' . uniqid() . '.test';
03e52840d   Kload   Init
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  
  		// Disable encryption proxy to prevent recursive calls
  		$proxyStatus = \OC_FileProxy::$enabled;
  		\OC_FileProxy::$enabled = false;
  
  		$this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort);
  
  		// Re-enable proxy - our work is done
  		\OC_FileProxy::$enabled = $proxyStatus;
  
  		$util = new Encryption\Util($this->view, $this->userId);
  
  		list($fileOwnerUid, $file) = $util->getUidAndFilename($filename);
  
  		$this->assertEquals(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1, $fileOwnerUid);
  
  		$this->assertEquals($file, $filename);
  
  		$this->view->unlink($this->userId . '/files/' . $filename);
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
286
  <	 * Test that data that is read by the crypto stream wrapper
03e52840d   Kload   Init
287
288
289
  	 */
  	function testGetFileSize() {
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1);
a293d369c   Kload   Update sources to...
290
  		$filename = 'tmp-' . uniqid();
03e52840d   Kload   Init
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
  		$externalFilename = '/' . $this->userId . '/files/' . $filename;
  
  		// Test for 0 byte files
  		$problematicFileSizeData = "";
  		$cryptedFile = $this->view->file_put_contents($externalFilename, $problematicFileSizeData);
  		$this->assertTrue(is_int($cryptedFile));
  		$this->assertEquals($this->util->getFileSize($externalFilename), 0);
  		$decrypt = $this->view->file_get_contents($externalFilename);
  		$this->assertEquals($problematicFileSizeData, $decrypt);
  		$this->view->unlink($this->userId . '/files/' . $filename);
  
  		// Test a file with 18377 bytes as in https://github.com/owncloud/mirall/issues/1009
  		$problematicFileSizeData = str_pad("", 18377, "abc");
  		$cryptedFile = $this->view->file_put_contents($externalFilename, $problematicFileSizeData);
  		$this->assertTrue(is_int($cryptedFile));
  		$this->assertEquals($this->util->getFileSize($externalFilename), 18377);
  		$decrypt = $this->view->file_get_contents($externalFilename);
  		$this->assertEquals($problematicFileSizeData, $decrypt);
  		$this->view->unlink($this->userId . '/files/' . $filename);
  	}
31b7f2792   Kload   Upgrade to ownclo...
311
  	function testEncryptAll() {
a293d369c   Kload   Update sources to...
312
  		$filename = "/encryptAll" . uniqid() . ".txt";
31b7f2792   Kload   Upgrade to ownclo...
313
314
315
316
317
318
319
320
  		$util = new Encryption\Util($this->view, $this->userId);
  
  		// disable encryption to upload a unencrypted file
  		\OC_App::disable('files_encryption');
  
  		$this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort);
  
  		$fileInfoUnencrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
6d9380f96   Cédric Dupont   Update sources OC...
321
  		$this->assertTrue($fileInfoUnencrypted instanceof \OC\Files\FileInfo);
31b7f2792   Kload   Upgrade to ownclo...
322
323
324
325
326
327
328
329
  
  		// enable file encryption again
  		\OC_App::enable('files_encryption');
  
  		// encrypt all unencrypted files
  		$util->encryptAll('/' . $this->userId . '/' . 'files');
  
  		$fileInfoEncrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
6d9380f96   Cédric Dupont   Update sources OC...
330
  		$this->assertTrue($fileInfoEncrypted instanceof \OC\Files\FileInfo);
31b7f2792   Kload   Upgrade to ownclo...
331
332
333
  
  		// check if mtime and etags unchanged
  		$this->assertEquals($fileInfoEncrypted['mtime'], $fileInfoUnencrypted['mtime']);
6d9380f96   Cédric Dupont   Update sources OC...
334
  		$this->assertSame($fileInfoEncrypted['etag'], $fileInfoUnencrypted['etag']);
31b7f2792   Kload   Upgrade to ownclo...
335
336
337
  
  		$this->view->unlink($this->userId . '/files/' . $filename);
  	}
31b7f2792   Kload   Upgrade to ownclo...
338
  	function testDecryptAll() {
a293d369c   Kload   Update sources to...
339
  		$filename = "/decryptAll" . uniqid() . ".txt";
6d9380f96   Cédric Dupont   Update sources OC...
340
341
  		$datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data/');
  		$userdir = $datadir . '/' . $this->userId . '/files/';
31b7f2792   Kload   Upgrade to ownclo...
342
343
344
345
  
  		$this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort);
  
  		$fileInfoEncrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
6d9380f96   Cédric Dupont   Update sources OC...
346
  		$this->assertTrue($fileInfoEncrypted instanceof \OC\Files\FileInfo);
a293d369c   Kload   Update sources to...
347
  		$this->assertEquals($fileInfoEncrypted['encrypted'], 1);
31b7f2792   Kload   Upgrade to ownclo...
348

6d9380f96   Cédric Dupont   Update sources OC...
349
  		$encContent = file_get_contents($userdir . $filename);
a293d369c   Kload   Update sources to...
350

6d9380f96   Cédric Dupont   Update sources OC...
351
  		\OC_App::disable('files_encryption');
31b7f2792   Kload   Upgrade to ownclo...
352

6d9380f96   Cédric Dupont   Update sources OC...
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  		$user = \OCP\User::getUser();
  		$this->logoutHelper();
  		$this->loginHelper($user, false, false, false);
  
  		$content = file_get_contents($userdir . $filename);
  
  		//content should be encrypted
  		$this->assertSame($encContent, $content);
  
  		// now we load the encryption app again
  		OC_App::loadApp('files_encryption');
  
  		// init encryption app
  		$params = array('uid' => \OCP\User::getUser(),
  			'password' => \OCP\User::getUser());
31b7f2792   Kload   Upgrade to ownclo...
368

6d9380f96   Cédric Dupont   Update sources OC...
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
  		$view = new OC\Files\View('/');
  		$util = new \OCA\Encryption\Util($view, \OCP\User::getUser());
  
  		$result = $util->initEncryption($params);
  
  		$this->assertTrue($result instanceof \OCA\Encryption\Session);
  
  		$successful = $util->decryptAll();
  
  		$this->assertTrue($successful);
  
  		$this->logoutHelper();
  		$this->loginHelper($user, false, false, false);
  
  		// file should be unencrypted and fileInfo should contain the correct values
  		$content = file_get_contents($userdir . $filename);
  
  		// now we should get the plain data
  		$this->assertSame($this->dataShort, $content);
  
  		$fileInfoUnencrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
  		$this->assertTrue($fileInfoUnencrypted instanceof \OC\Files\FileInfo);
31b7f2792   Kload   Upgrade to ownclo...
391
392
393
  
  		// check if mtime and etags unchanged
  		$this->assertEquals($fileInfoEncrypted['mtime'], $fileInfoUnencrypted['mtime']);
6d9380f96   Cédric Dupont   Update sources OC...
394
  		$this->assertSame($fileInfoEncrypted['etag'], $fileInfoUnencrypted['etag']);
a293d369c   Kload   Update sources to...
395
396
  		// file should no longer be encrypted
  		$this->assertEquals(0, $fileInfoUnencrypted['encrypted']);
31b7f2792   Kload   Upgrade to ownclo...
397

6d9380f96   Cédric Dupont   Update sources OC...
398
399
400
401
402
403
404
  		// check if the keys where moved to the backup location
  		$this->assertTrue($this->view->is_dir($this->userId . '/files_encryption/keyfiles.backup'));
  		$this->assertTrue($this->view->file_exists($this->userId . '/files_encryption/keyfiles.backup/' . $filename . '.key'));
  		$this->assertTrue($this->view->is_dir($this->userId . '/files_encryption/share-keys.backup'));
  		$this->assertTrue($this->view->file_exists($this->userId . '/files_encryption/share-keys.backup/' . $filename . '.' . $user . '.shareKey'));
  
  		// cleanup
31b7f2792   Kload   Upgrade to ownclo...
405
  		$this->view->unlink($this->userId . '/files/' . $filename);
6d9380f96   Cédric Dupont   Update sources OC...
406
407
408
  		$this->view->deleteAll($this->userId . '/files_encryption/keyfiles.backup');
  		$this->view->deleteAll($this->userId . '/files_encryption/share-keys.backup');
  		OC_App::enable('files_encryption');
31b7f2792   Kload   Upgrade to ownclo...
409
410
  
  	}
6d9380f96   Cédric Dupont   Update sources OC...
411

a293d369c   Kload   Update sources to...
412
413
414
415
416
417
418
419
420
421
422
423
  	function testDescryptAllWithBrokenFiles() {
  
  		$file1 = "/decryptAll1" . uniqid() . ".txt";
  		$file2 = "/decryptAll2" . uniqid() . ".txt";
  
  		$util = new Encryption\Util($this->view, $this->userId);
  
  		$this->view->file_put_contents($this->userId . '/files/' . $file1, $this->dataShort);
  		$this->view->file_put_contents($this->userId . '/files/' . $file2, $this->dataShort);
  
  		$fileInfoEncrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1);
  		$fileInfoEncrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2);
6d9380f96   Cédric Dupont   Update sources OC...
424
425
  		$this->assertTrue($fileInfoEncrypted1 instanceof \OC\Files\FileInfo);
  		$this->assertTrue($fileInfoEncrypted2 instanceof \OC\Files\FileInfo);
a293d369c   Kload   Update sources to...
426
427
428
429
430
431
432
433
434
  		$this->assertEquals($fileInfoEncrypted1['encrypted'], 1);
  		$this->assertEquals($fileInfoEncrypted2['encrypted'], 1);
  
  		// rename keyfile for file1 so that the decryption for file1 fails
  		// Expected behaviour: decryptAll() returns false, file2 gets decrypted anyway
  		$this->view->rename($this->userId . '/files_encryption/keyfiles/' . $file1 . '.key',
  				$this->userId . '/files_encryption/keyfiles/' . $file1 . '.key.moved');
  
  		// decrypt all encrypted files
6d9380f96   Cédric Dupont   Update sources OC...
435
  		$result = $util->decryptAll();
a293d369c   Kload   Update sources to...
436
437
438
439
440
  
  		$this->assertFalse($result);
  
  		$fileInfoUnencrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1);
  		$fileInfoUnencrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2);
6d9380f96   Cédric Dupont   Update sources OC...
441
442
  		$this->assertTrue($fileInfoUnencrypted1 instanceof \OC\Files\FileInfo);
  		$this->assertTrue($fileInfoUnencrypted2 instanceof \OC\Files\FileInfo);
a293d369c   Kload   Update sources to...
443
444
445
446
447
448
449
450
451
452
453
454
455
456
  
  		// file1 should be still encrypted; file2 should be decrypted
  		$this->assertEquals(1, $fileInfoUnencrypted1['encrypted']);
  		$this->assertEquals(0, $fileInfoUnencrypted2['encrypted']);
  
  		// keyfiles and share keys should still exist
  		$this->assertTrue($this->view->is_dir($this->userId . '/files_encryption/keyfiles/'));
  		$this->assertTrue($this->view->is_dir($this->userId . '/files_encryption/share-keys/'));
  
  		// rename the keyfile for file1 back
  		$this->view->rename($this->userId . '/files_encryption/keyfiles/' . $file1 . '.key.moved',
  				$this->userId . '/files_encryption/keyfiles/' . $file1 . '.key');
  
  		// try again to decrypt all encrypted files
6d9380f96   Cédric Dupont   Update sources OC...
457
  		$result = $util->decryptAll();
a293d369c   Kload   Update sources to...
458
459
460
461
462
  
  		$this->assertTrue($result);
  
  		$fileInfoUnencrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1);
  		$fileInfoUnencrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2);
6d9380f96   Cédric Dupont   Update sources OC...
463
464
  		$this->assertTrue($fileInfoUnencrypted1 instanceof \OC\Files\FileInfo);
  		$this->assertTrue($fileInfoUnencrypted2 instanceof \OC\Files\FileInfo);
a293d369c   Kload   Update sources to...
465
466
467
468
469
470
471
472
  
  		// now both files should be decrypted
  		$this->assertEquals(0, $fileInfoUnencrypted1['encrypted']);
  		$this->assertEquals(0, $fileInfoUnencrypted2['encrypted']);
  
  		// keyfiles and share keys should be deleted
  		$this->assertFalse($this->view->is_dir($this->userId . '/files_encryption/keyfiles/'));
  		$this->assertFalse($this->view->is_dir($this->userId . '/files_encryption/share-keys/'));
6d9380f96   Cédric Dupont   Update sources OC...
473
  		//cleanup
a293d369c   Kload   Update sources to...
474
475
  		$this->view->unlink($this->userId . '/files/' . $file1);
  		$this->view->unlink($this->userId . '/files/' . $file2);
6d9380f96   Cédric Dupont   Update sources OC...
476
477
  		$this->view->deleteAll($this->userId . '/files_encryption/keyfiles.backup');
  		$this->view->deleteAll($this->userId . '/files_encryption/share-keys.backup');
a293d369c   Kload   Update sources to...
478
479
  
  	}
31b7f2792   Kload   Upgrade to ownclo...
480
481
482
  	/**
  	 * @large
  	 */
03e52840d   Kload   Init
483
484
  	function testEncryptLegacyFiles() {
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
6d9380f96   Cédric Dupont   Update sources OC...
485
486
  		$userView = new \OC\Files\View('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
  		$view = new \OC\Files\View('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER . '/files');
03e52840d   Kload   Init
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
  
  		// Disable encryption proxy to prevent recursive calls
  		$proxyStatus = \OC_FileProxy::$enabled;
  		\OC_FileProxy::$enabled = false;
  
  		$encryptionKeyContent = file_get_contents($this->legacyEncryptedDataKey);
  		$userView->file_put_contents('/encryption.key', $encryptionKeyContent);
  
  		$legacyEncryptedData = file_get_contents($this->legacyEncryptedData);
  		$view->mkdir('/test/');
  		$view->mkdir('/test/subtest/');
  		$view->file_put_contents('/test/subtest/legacy-encrypted-text.txt', $legacyEncryptedData);
  
  		$fileInfo = $view->getFileInfo('/test/subtest/legacy-encrypted-text.txt');
  		$fileInfo['encrypted'] = true;
  		$view->putFileInfo('/test/subtest/legacy-encrypted-text.txt', $fileInfo);
  
  		\OC_FileProxy::$enabled = $proxyStatus;
  
  		$params['uid'] = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER;
  		$params['password'] = \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER;
  
  		$util = new Encryption\Util($this->view, \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
  		$this->setMigrationStatus(0, \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER);
  
  		$this->assertTrue(OCA\Encryption\Hooks::login($params));
31b7f2792   Kload   Upgrade to ownclo...
513
  		$this->assertEquals($this->legacyKey, \OC::$session->get('legacyKey'));
03e52840d   Kload   Init
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
  
  		$files = $util->findEncFiles('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER . '/files/');
  
  		$this->assertTrue(is_array($files));
  
  		$found = false;
  		foreach ($files['encrypted'] as $encryptedFile) {
  			if ($encryptedFile['name'] === 'legacy-encrypted-text.txt') {
  				$found = true;
  				break;
  			}
  		}
  
  		$this->assertTrue($found);
  	}
  
  	/**
f7d878ff1   kload   [enh] Update to 7...
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
  	 * @dataProvider dataProviderFortestIsMountPointApplicableToUser
  	 */
  	function testIsMountPointApplicableToUser($mount, $expectedResult) {
  		self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1);
  		$dummyClass = new DummyUtilClass($this->view, self::TEST_ENCRYPTION_UTIL_USER1);
  		$result = $dummyClass->testIsMountPointApplicableToUser($mount);
  
  		$this->assertSame($expectedResult, $result);
  	}
  
  	function dataProviderFortestIsMountPointApplicableToUser() {
  		return array(
  			array(array('applicable' => array('groups' => array(), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER1))), true),
  			array(array('applicable' => array('groups' => array(), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER2))), false),
  			array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP1), 'users' => array())), true),
  			array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP1), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER2))), true),
  			array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP2), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER2))), false),
  			array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP2), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER2, 'all'))), true),
  			array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP2), 'users' => array('all'))), true),
  		);
  	}
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
554
  	 * @param string $user
03e52840d   Kload   Init
555
556
557
  	 * @param bool $create
  	 * @param bool $password
  	 */
6d9380f96   Cédric Dupont   Update sources OC...
558
  	public static function loginHelper($user, $create = false, $password = false, $loadEncryption = true) {
03e52840d   Kload   Init
559
  		if ($create) {
6d9380f96   Cédric Dupont   Update sources OC...
560
561
562
563
564
  			try {
  				\OC_User::createUser($user, $user);
  			} catch(\Exception $e) { // catch username is already being used from previous aborted runs
  
  			}
03e52840d   Kload   Init
565
566
567
568
569
570
571
572
573
  		}
  
  		if ($password === false) {
  			$password = $user;
  		}
  
  		\OC_Util::tearDownFS();
  		\OC_User::setUserId('');
  		\OC\Files\Filesystem::tearDown();
03e52840d   Kload   Init
574
  		\OC_User::setUserId($user);
6d9380f96   Cédric Dupont   Update sources OC...
575
  		\OC_Util::setupFS($user);
03e52840d   Kload   Init
576

6d9380f96   Cédric Dupont   Update sources OC...
577
578
579
580
581
  		if ($loadEncryption) {
  			$params['uid'] = $user;
  			$params['password'] = $password;
  			OCA\Encryption\Hooks::login($params);
  		}
03e52840d   Kload   Init
582
  	}
a293d369c   Kload   Update sources to...
583
584
  	public static function logoutHelper() {
  		\OC_Util::tearDownFS();
f7d878ff1   kload   [enh] Update to 7...
585
  		\OC_User::setUserId(false);
a293d369c   Kload   Update sources to...
586
587
  		\OC\Files\Filesystem::tearDown();
  	}
03e52840d   Kload   Init
588
589
590
  	/**
  	 * helper function to set migration status to the right value
  	 * to be able to test the migration path
31b7f2792   Kload   Upgrade to ownclo...
591
  	 *
6d9380f96   Cédric Dupont   Update sources OC...
592
593
  	 * @param integer $status needed migration status for test
  	 * @param string $user for which user the status should be set
03e52840d   Kload   Init
594
595
596
  	 * @return boolean
  	 */
  	private function setMigrationStatus($status, $user) {
6d9380f96   Cédric Dupont   Update sources OC...
597
  		return \OC_Preferences::setValue($user, 'files_encryption', 'migration_status', (string)$status);
03e52840d   Kload   Init
598
599
600
  	}
  
  }
f7d878ff1   kload   [enh] Update to 7...
601
602
603
604
605
606
607
608
609
  
  /**
   * dummy class extends  \OCA\Encryption\Util to access protected methods for testing
   */
  class DummyUtilClass extends \OCA\Encryption\Util {
  	public function testIsMountPointApplicableToUser($mount) {
  		return $this->isMountPointApplicableToUser($mount);
  	}
  }