Blame view

sources/apps/files_encryption/tests/crypt.php 18.6 KB
03e52840d   Kload   Init
1
2
3
4
5
6
7
8
  <?php
  /**
   * Copyright (c) 2012 Sam Tuke <samtuke@owncloud.com>, and
   * Robin Appelman <icewind@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...
9
10
11
12
13
14
15
16
17
18
  require_once __DIR__ . '/../3rdparty/Crypt_Blowfish/Blowfish.php';
  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
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  
  use OCA\Encryption;
  
  /**
   * Class Test_Encryption_Crypt
   */
  class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase {
  
  	const TEST_ENCRYPTION_CRYPT_USER1 = "test-crypt-user1";
  
  	public $userId;
  	public $pass;
  	public $stateFilesTrashbin;
  	public $dataLong;
  	public $dataUrl;
  	public $dataShort;
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
36
  	 * @var OC\Files\View
03e52840d   Kload   Init
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
  	 */
  	public $view;
  	public $legacyEncryptedData;
  	public $genPrivateKey;
  	public $genPublicKey;
  
  	public static function setUpBeforeClass() {
  		// reset backend
  		\OC_User::clearBackends();
  		\OC_User::useBackend('database');
  
  		// Filesystem related hooks
  		\OCA\Encryption\Helper::registerFilesystemHooks();
  
  		// Filesystem related hooks
  		\OCA\Encryption\Helper::registerUserHooks();
  
  		// clear and register hooks
  		\OC_FileProxy::clearProxies();
  		\OC_FileProxy::register(new OCA\Encryption\Proxy());
  
  		// create test user
  		\Test_Encryption_Util::loginHelper(\Test_Encryption_Crypt::TEST_ENCRYPTION_CRYPT_USER1, true);
  	}
  
  	function setUp() {
  		// set user id
  		\OC_User::setUserId(\Test_Encryption_Crypt::TEST_ENCRYPTION_CRYPT_USER1);
  		$this->userId = \Test_Encryption_Crypt::TEST_ENCRYPTION_CRYPT_USER1;
  		$this->pass = \Test_Encryption_Crypt::TEST_ENCRYPTION_CRYPT_USER1;
  
  		// set content for encrypting / decrypting in tests
31b7f2792   Kload   Upgrade to ownclo...
69
  		$this->dataLong = file_get_contents(__DIR__ . '/../lib/crypt.php');
03e52840d   Kload   Init
70
  		$this->dataShort = 'hats';
31b7f2792   Kload   Upgrade to ownclo...
71
72
73
74
  		$this->dataUrl = __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
75
76
77
78
79
  		$this->randomKey = Encryption\Crypt::generateKey();
  
  		$keypair = Encryption\Crypt::createKeypair();
  		$this->genPublicKey = $keypair['publicKey'];
  		$this->genPrivateKey = $keypair['privateKey'];
6d9380f96   Cédric Dupont   Update sources OC...
80
  		$this->view = new \OC\Files\View('/');
03e52840d   Kload   Init
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  
  		// 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');
  		}
6d9380f96   Cédric Dupont   Update sources OC...
96
97
  
  		$this->assertTrue(\OC_FileProxy::$enabled);
03e52840d   Kload   Init
98
99
100
101
102
103
  	}
  
  	public static function tearDownAfterClass() {
  		// cleanup test user
  		\OC_User::deleteUser(\Test_Encryption_Crypt::TEST_ENCRYPTION_CRYPT_USER1);
  	}
31b7f2792   Kload   Upgrade to ownclo...
104
105
106
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
107
108
109
110
111
112
113
114
115
  	function testGenerateKey() {
  
  		# TODO: use more accurate (larger) string length for test confirmation
  
  		$key = Encryption\Crypt::generateKey();
  
  		$this->assertTrue(strlen($key) > 16);
  
  	}
03e52840d   Kload   Init
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  	function testDecryptPrivateKey() {
  
  		// test successful decrypt
  		$crypted = Encryption\Crypt::symmetricEncryptFileContent($this->genPrivateKey, 'hat');
  
  		$decrypted = Encryption\Crypt::decryptPrivateKey($crypted, 'hat');
  
  		$this->assertEquals($this->genPrivateKey, $decrypted);
  
  		//test private key decrypt with wrong password
  		$wrongPasswd = Encryption\Crypt::decryptPrivateKey($crypted, 'hat2');
  
  		$this->assertEquals(false, $wrongPasswd);
  
  	}
31b7f2792   Kload   Upgrade to ownclo...
131
132
133
134
  
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  	function testSymmetricEncryptFileContent() {
  
  		# TODO: search in keyfile for actual content as IV will ensure this test always passes
  
  		$crypted = Encryption\Crypt::symmetricEncryptFileContent($this->dataShort, 'hat');
  
  		$this->assertNotEquals($this->dataShort, $crypted);
  
  
  		$decrypt = Encryption\Crypt::symmetricDecryptFileContent($crypted, 'hat');
  
  		$this->assertEquals($this->dataShort, $decrypt);
  
  	}
31b7f2792   Kload   Upgrade to ownclo...
149
150
151
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
152
  	function testSymmetricStreamEncryptShortFileContent() {
a293d369c   Kload   Update sources to...
153
  		$filename = 'tmp-' . uniqid() . '.test';
03e52840d   Kload   Init
154

6d9380f96   Cédric Dupont   Update sources OC...
155
  		$util = new Encryption\Util(new \OC\Files\View(), $this->userId);
31b7f2792   Kload   Upgrade to ownclo...
156
157
  
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/'. $filename, $this->dataShort);
03e52840d   Kload   Init
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Disable encryption proxy to prevent recursive calls
  		$proxyStatus = \OC_FileProxy::$enabled;
  		\OC_FileProxy::$enabled = false;
  
  		// Get file contents without using any wrapper to get it's actual contents on disk
  		$retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename);
  
  		// Re-enable proxy - our work is done
  		\OC_FileProxy::$enabled = $proxyStatus;
  
  		// Check that the file was encrypted before being written to disk
  		$this->assertNotEquals($this->dataShort, $retreivedCryptedFile);
  
  		// Get the encrypted keyfile
31b7f2792   Kload   Upgrade to ownclo...
176
  		$encKeyfile = Encryption\Keymanager::getFileKey($this->view, $util, $filename);
03e52840d   Kload   Init
177
178
  
  		// Attempt to fetch the user's shareKey
31b7f2792   Kload   Upgrade to ownclo...
179
  		$shareKey = Encryption\Keymanager::getShareKey($this->view, $this->userId, $util, $filename);
03e52840d   Kload   Init
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  
  		// get session
  		$session = new \OCA\Encryption\Session($this->view);
  
  		// get private key
  		$privateKey = $session->getPrivateKey($this->userId);
  
  		// Decrypt keyfile with shareKey
  		$plainKeyfile = Encryption\Crypt::multiKeyDecrypt($encKeyfile, $shareKey, $privateKey);
  
  		// Manually decrypt
  		$manualDecrypt = Encryption\Crypt::symmetricDecryptFileContent($retreivedCryptedFile, $plainKeyfile);
  
  		// Check that decrypted data matches
  		$this->assertEquals($this->dataShort, $manualDecrypt);
  
  		// Teardown
  		$this->view->unlink($this->userId . '/files/' . $filename);
31b7f2792   Kload   Upgrade to ownclo...
198
  		Encryption\Keymanager::deleteFileKey($this->view, $filename);
03e52840d   Kload   Init
199
200
201
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
202
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
203
  	 * Test that data that is written by the crypto stream wrapper
03e52840d   Kload   Init
204
205
206
207
208
209
210
  	 * @note Encrypted data is manually prepared and decrypted here to avoid dependency on success of stream_read
  	 * @note If this test fails with truncate content, check that enough array slices are being rejoined to form $e, as the crypt.php file may have gotten longer and broken the manual
  	 * reassembly of its data
  	 */
  	function testSymmetricStreamEncryptLongFileContent() {
  
  		// Generate a a random filename
a293d369c   Kload   Update sources to...
211
  		$filename = 'tmp-' . uniqid() . '.test';
03e52840d   Kload   Init
212

6d9380f96   Cédric Dupont   Update sources OC...
213
  		$util = new Encryption\Util(new \OC\Files\View(), $this->userId);
31b7f2792   Kload   Upgrade to ownclo...
214

03e52840d   Kload   Init
215
  		// Save long data as encrypted file using stream wrapper
31b7f2792   Kload   Upgrade to ownclo...
216
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong);
03e52840d   Kload   Init
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Disable encryption proxy to prevent recursive calls
  		$proxyStatus = \OC_FileProxy::$enabled;
  		\OC_FileProxy::$enabled = false;
  
  		// Get file contents without using any wrapper to get it's actual contents on disk
  		$retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename);
  
  		// Re-enable proxy - our work is done
  		\OC_FileProxy::$enabled = $proxyStatus;
  
  
  		// Check that the file was encrypted before being written to disk
  		$this->assertNotEquals($this->dataLong . $this->dataLong, $retreivedCryptedFile);
  
  		// Manuallly split saved file into separate IVs and encrypted chunks
  		$r = preg_split('/(00iv00.{16,18})/', $retreivedCryptedFile, NULL, PREG_SPLIT_DELIM_CAPTURE);
  
  		//print_r($r);
  
  		// Join IVs and their respective data chunks
31b7f2792   Kload   Upgrade to ownclo...
241
242
243
244
245
246
  		$e = array();
  		$i = 0;
  		while ($i < count($r)-1) {
  			$e[] = $r[$i] . $r[$i+1];
  			$i = $i + 2;
  		}
03e52840d   Kload   Init
247
248
249
250
  
  		//print_r($e);
  
  		// Get the encrypted keyfile
31b7f2792   Kload   Upgrade to ownclo...
251
  		$encKeyfile = Encryption\Keymanager::getFileKey($this->view, $util, $filename);
03e52840d   Kload   Init
252
253
  
  		// Attempt to fetch the user's shareKey
31b7f2792   Kload   Upgrade to ownclo...
254
  		$shareKey = Encryption\Keymanager::getShareKey($this->view, $this->userId, $util, $filename);
03e52840d   Kload   Init
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  
  		// get session
  		$session = new \OCA\Encryption\Session($this->view);
  
  		// get private key
  		$privateKey = $session->getPrivateKey($this->userId);
  
  		// Decrypt keyfile with shareKey
  		$plainKeyfile = Encryption\Crypt::multiKeyDecrypt($encKeyfile, $shareKey, $privateKey);
  
  		// Set var for reassembling decrypted content
  		$decrypt = '';
  
  		// Manually decrypt chunk
  		foreach ($e as $chunk) {
  
  			$chunkDecrypt = Encryption\Crypt::symmetricDecryptFileContent($chunk, $plainKeyfile);
  
  			// Assemble decrypted chunks
  			$decrypt .= $chunkDecrypt;
  
  		}
  
  		$this->assertEquals($this->dataLong . $this->dataLong, $decrypt);
  
  		// Teardown
  
  		$this->view->unlink($this->userId . '/files/' . $filename);
31b7f2792   Kload   Upgrade to ownclo...
283
  		Encryption\Keymanager::deleteFileKey($this->view, $filename);
03e52840d   Kload   Init
284
285
286
287
  
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
288
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
289
  	 * Test that data that is read by the crypto stream wrapper
03e52840d   Kload   Init
290
291
  	 */
  	function testSymmetricStreamDecryptShortFileContent() {
a293d369c   Kload   Update sources to...
292
  		$filename = 'tmp-' . uniqid();
03e52840d   Kload   Init
293
294
  
  		// Save long data as encrypted file using stream wrapper
31b7f2792   Kload   Upgrade to ownclo...
295
  		$cryptedFile = file_put_contents('crypt:///'. $this->userId . '/files/' . $filename, $this->dataShort);
03e52840d   Kload   Init
296
297
298
299
300
301
302
303
304
305
306
307
308
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Disable encryption proxy to prevent recursive calls
  		$proxyStatus = \OC_FileProxy::$enabled;
  		\OC_FileProxy::$enabled = false;
  
  		$this->assertTrue(Encryption\Crypt::isEncryptedMeta($filename));
  
  		\OC_FileProxy::$enabled = $proxyStatus;
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
309
  		$decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
03e52840d   Kload   Init
310
311
312
313
314
315
  
  		$this->assertEquals($this->dataShort, $decrypt);
  
  		// tear down
  		$this->view->unlink($this->userId . '/files/' . $filename);
  	}
31b7f2792   Kload   Upgrade to ownclo...
316
317
318
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
319
  	function testSymmetricStreamDecryptLongFileContent() {
a293d369c   Kload   Update sources to...
320
  		$filename = 'tmp-' . uniqid();
03e52840d   Kload   Init
321
322
  
  		// Save long data as encrypted file using stream wrapper
31b7f2792   Kload   Upgrade to ownclo...
323
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong);
03e52840d   Kload   Init
324
325
326
327
328
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
329
  		$decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
03e52840d   Kload   Init
330
331
332
333
334
335
  
  		$this->assertEquals($this->dataLong, $decrypt);
  
  		// tear down
  		$this->view->unlink($this->userId . '/files/' . $filename);
  	}
31b7f2792   Kload   Upgrade to ownclo...
336
337
338
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
339
340
341
342
343
344
345
346
347
348
349
  	function testIsEncryptedContent() {
  
  		$this->assertFalse(Encryption\Crypt::isCatfileContent($this->dataUrl));
  
  		$this->assertFalse(Encryption\Crypt::isCatfileContent($this->legacyEncryptedData));
  
  		$keyfileContent = Encryption\Crypt::symmetricEncryptFileContent($this->dataUrl, 'hat');
  
  		$this->assertTrue(Encryption\Crypt::isCatfileContent($keyfileContent));
  
  	}
31b7f2792   Kload   Upgrade to ownclo...
350
351
352
  	/**
  	 * @large
  	 */
03e52840d   Kload   Init
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  	function testMultiKeyEncrypt() {
  
  		# TODO: search in keyfile for actual content as IV will ensure this test always passes
  
  		$pair1 = Encryption\Crypt::createKeypair();
  
  		$this->assertEquals(2, count($pair1));
  
  		$this->assertTrue(strlen($pair1['publicKey']) > 1);
  
  		$this->assertTrue(strlen($pair1['privateKey']) > 1);
  
  
  		$crypted = Encryption\Crypt::multiKeyEncrypt($this->dataShort, array($pair1['publicKey']));
  
  		$this->assertNotEquals($this->dataShort, $crypted['data']);
  
  
  		$decrypt = Encryption\Crypt::multiKeyDecrypt($crypted['data'], $crypted['keys'][0], $pair1['privateKey']);
  
  		$this->assertEquals($this->dataShort, $decrypt);
  
  	}
03e52840d   Kload   Init
376
  	/**
31b7f2792   Kload   Upgrade to ownclo...
377
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
378
  	 * test decryption using legacy blowfish method
03e52840d   Kload   Init
379
  	 */
31b7f2792   Kload   Upgrade to ownclo...
380
381
382
  	function testLegacyDecryptShort() {
  
  		$crypted = $this->legacyEncrypt($this->dataShort, $this->pass);
03e52840d   Kload   Init
383
384
385
386
387
388
389
390
  
  		$decrypted = Encryption\Crypt::legacyBlockDecrypt($crypted, $this->pass);
  
  		$this->assertEquals($this->dataShort, $decrypted);
  
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
391
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
392
  	 * test decryption using legacy blowfish method
03e52840d   Kload   Init
393
  	 */
31b7f2792   Kload   Upgrade to ownclo...
394
395
396
  	function testLegacyDecryptLong() {
  
  		$crypted = $this->legacyEncrypt($this->dataLong, $this->pass);
03e52840d   Kload   Init
397
398
399
400
  
  		$decrypted = Encryption\Crypt::legacyBlockDecrypt($crypted, $this->pass);
  
  		$this->assertEquals($this->dataLong, $decrypted);
03e52840d   Kload   Init
401
402
403
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
404
  	 * @medium
03e52840d   Kload   Init
405
  	 */
03e52840d   Kload   Init
406
  	function testRenameFile() {
a293d369c   Kload   Update sources to...
407
  		$filename = 'tmp-' . uniqid();
03e52840d   Kload   Init
408
409
  
  		// Save long data as encrypted file using stream wrapper
31b7f2792   Kload   Upgrade to ownclo...
410
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong);
03e52840d   Kload   Init
411
412
413
414
415
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
416
  		$decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
03e52840d   Kload   Init
417
418
  
  		$this->assertEquals($this->dataLong, $decrypt);
a293d369c   Kload   Update sources to...
419
  		$newFilename = 'tmp-new-' . uniqid();
03e52840d   Kload   Init
420
421
422
423
  		$view = new \OC\Files\View('/' . $this->userId . '/files');
  		$view->rename($filename, $newFilename);
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
424
  		$newDecrypt = file_get_contents('crypt:///'. $this->userId . '/files/' . $newFilename);
03e52840d   Kload   Init
425
426
427
428
429
430
  
  		$this->assertEquals($this->dataLong, $newDecrypt);
  
  		// tear down
  		$view->unlink($newFilename);
  	}
31b7f2792   Kload   Upgrade to ownclo...
431
432
433
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
434
  	function testMoveFileIntoFolder() {
a293d369c   Kload   Update sources to...
435
  		$filename = 'tmp-' . uniqid();
03e52840d   Kload   Init
436
437
  
  		// Save long data as encrypted file using stream wrapper
31b7f2792   Kload   Upgrade to ownclo...
438
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong);
03e52840d   Kload   Init
439
440
441
442
443
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
444
  		$decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
03e52840d   Kload   Init
445
446
  
  		$this->assertEquals($this->dataLong, $decrypt);
a293d369c   Kload   Update sources to...
447
448
  		$newFolder = '/newfolder' . uniqid();
  		$newFilename = 'tmp-new-' . uniqid();
03e52840d   Kload   Init
449
450
451
452
453
  		$view = new \OC\Files\View('/' . $this->userId . '/files');
  		$view->mkdir($newFolder);
  		$view->rename($filename, $newFolder . '/' . $newFilename);
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
454
  		$newDecrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $newFolder . '/' . $newFilename);
03e52840d   Kload   Init
455
456
457
458
459
460
  
  		$this->assertEquals($this->dataLong, $newDecrypt);
  
  		// tear down
  		$view->unlink($newFolder);
  	}
31b7f2792   Kload   Upgrade to ownclo...
461
462
463
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
464
465
466
  	function testMoveFolder() {
  
  		$view = new \OC\Files\View('/' . $this->userId . '/files');
a293d369c   Kload   Update sources to...
467
468
  		$filename = '/tmp-' . uniqid();
  		$folder = '/folder' . uniqid();
03e52840d   Kload   Init
469
470
471
472
  
  		$view->mkdir($folder);
  
  		// Save long data as encrypted file using stream wrapper
31b7f2792   Kload   Upgrade to ownclo...
473
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $folder . $filename, $this->dataLong);
03e52840d   Kload   Init
474
475
476
477
478
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
479
  		$decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $folder . $filename);
03e52840d   Kload   Init
480
481
  
  		$this->assertEquals($this->dataLong, $decrypt);
a293d369c   Kload   Update sources to...
482
  		$newFolder = '/newfolder/subfolder' . uniqid();
03e52840d   Kload   Init
483
484
485
486
487
  		$view->mkdir('/newfolder');
  
  		$view->rename($folder, $newFolder);
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
488
  		$newDecrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $newFolder . $filename);
03e52840d   Kload   Init
489
490
491
492
493
494
495
  
  		$this->assertEquals($this->dataLong, $newDecrypt);
  
  		// tear down
  		$view->unlink($newFolder);
  		$view->unlink('/newfolder');
  	}
31b7f2792   Kload   Upgrade to ownclo...
496
497
498
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
499
  	function testChangePassphrase() {
a293d369c   Kload   Update sources to...
500
  		$filename = 'tmp-' . uniqid();
03e52840d   Kload   Init
501
502
  
  		// Save long data as encrypted file using stream wrapper
31b7f2792   Kload   Upgrade to ownclo...
503
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong);
03e52840d   Kload   Init
504
505
506
507
508
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
509
  		$decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
03e52840d   Kload   Init
510
511
512
513
514
515
516
517
518
519
520
521
  
  		$this->assertEquals($this->dataLong, $decrypt);
  
  		// change password
  		\OC_User::setPassword($this->userId, 'test', null);
  
  		// relogin
  		$params['uid'] = $this->userId;
  		$params['password'] = 'test';
  		OCA\Encryption\Hooks::login($params);
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
522
  		$newDecrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
03e52840d   Kload   Init
523
524
525
526
527
528
529
530
531
  
  		$this->assertEquals($this->dataLong, $newDecrypt);
  
  		// tear down
  		// change password back
  		\OC_User::setPassword($this->userId, $this->pass);
  		$view = new \OC\Files\View('/' . $this->userId . '/files');
  		$view->unlink($filename);
  	}
31b7f2792   Kload   Upgrade to ownclo...
532
533
534
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
535
  	function testViewFilePutAndGetContents() {
a293d369c   Kload   Update sources to...
536
  		$filename = '/tmp-' . uniqid();
03e52840d   Kload   Init
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
  		$view = new \OC\Files\View('/' . $this->userId . '/files');
  
  		// Save short data as encrypted file using stream wrapper
  		$cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Get file decrypted contents
  		$decrypt = $view->file_get_contents($filename);
  
  		$this->assertEquals($this->dataShort, $decrypt);
  
  		// Save long data as encrypted file using stream wrapper
  		$cryptedFileLong = $view->file_put_contents($filename, $this->dataLong);
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFileLong));
  
  		// Get file decrypted contents
  		$decryptLong = $view->file_get_contents($filename);
  
  		$this->assertEquals($this->dataLong, $decryptLong);
  
  		// tear down
  		$view->unlink($filename);
  	}
31b7f2792   Kload   Upgrade to ownclo...
564
565
566
  	/**
  	 * @large
  	 */
03e52840d   Kload   Init
567
  	function testTouchExistingFile() {
a293d369c   Kload   Update sources to...
568
  		$filename = '/tmp-' . uniqid();
03e52840d   Kload   Init
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
  		$view = new \OC\Files\View('/' . $this->userId . '/files');
  
  		// Save short data as encrypted file using stream wrapper
  		$cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		$view->touch($filename);
  
  		// Get file decrypted contents
  		$decrypt = $view->file_get_contents($filename);
  
  		$this->assertEquals($this->dataShort, $decrypt);
  
  		// tear down
  		$view->unlink($filename);
  	}
31b7f2792   Kload   Upgrade to ownclo...
587
588
589
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
590
  	function testTouchFile() {
a293d369c   Kload   Update sources to...
591
  		$filename = '/tmp-' . uniqid();
03e52840d   Kload   Init
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
  		$view = new \OC\Files\View('/' . $this->userId . '/files');
  
  		$view->touch($filename);
  
  		// Save short data as encrypted file using stream wrapper
  		$cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Get file decrypted contents
  		$decrypt = $view->file_get_contents($filename);
  
  		$this->assertEquals($this->dataShort, $decrypt);
  
  		// tear down
  		$view->unlink($filename);
  	}
31b7f2792   Kload   Upgrade to ownclo...
610
611
612
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
613
  	function testFopenFile() {
a293d369c   Kload   Update sources to...
614
  		$filename = '/tmp-' . uniqid();
03e52840d   Kload   Init
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
  		$view = new \OC\Files\View('/' . $this->userId . '/files');
  
  		// Save short data as encrypted file using stream wrapper
  		$cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		$handle = $view->fopen($filename, 'r');
  
  		// Get file decrypted contents
  		$decrypt = fgets($handle);
  
  		$this->assertEquals($this->dataShort, $decrypt);
  
  		// tear down
  		$view->unlink($filename);
  	}
31b7f2792   Kload   Upgrade to ownclo...
633
634
635
  
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
636
637
638
  	 * encryption using legacy blowfish method
  	 * @param string $data data to encrypt
  	 * @param string $passwd password
31b7f2792   Kload   Upgrade to ownclo...
639
640
641
  	 * @return string
  	 */
  	function legacyEncrypt($data, $passwd) {
6d9380f96   Cédric Dupont   Update sources OC...
642
  		$bf = new Legacy_Crypt_Blowfish($passwd);
31b7f2792   Kload   Upgrade to ownclo...
643
644
645
646
  		$crypted = $bf->encrypt($data);
  
  		return $crypted;
  	}
03e52840d   Kload   Init
647
  }