Blame view

sources/apps/files_encryption/tests/crypt.php 21.1 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);
f7d878ff1   kload   [enh] Update to 7...
98
  		\OCP\Config::deleteSystemValue('cipher');
03e52840d   Kload   Init
99
100
101
102
103
104
  	}
  
  	public static function tearDownAfterClass() {
  		// cleanup test user
  		\OC_User::deleteUser(\Test_Encryption_Crypt::TEST_ENCRYPTION_CRYPT_USER1);
  	}
31b7f2792   Kload   Upgrade to ownclo...
105
106
107
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
108
109
110
111
112
113
114
115
116
  	function testGenerateKey() {
  
  		# TODO: use more accurate (larger) string length for test confirmation
  
  		$key = Encryption\Crypt::generateKey();
  
  		$this->assertTrue(strlen($key) > 16);
  
  	}
03e52840d   Kload   Init
117
118
119
120
  	function testDecryptPrivateKey() {
  
  		// test successful decrypt
  		$crypted = Encryption\Crypt::symmetricEncryptFileContent($this->genPrivateKey, 'hat');
f7d878ff1   kload   [enh] Update to 7...
121
122
123
  		$header = Encryption\Crypt::generateHeader();
  
  		$decrypted = Encryption\Crypt::decryptPrivateKey($header . $crypted, 'hat');
03e52840d   Kload   Init
124
125
126
127
128
129
130
131
132
  
  		$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...
133
134
135
136
  
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  	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...
151
152
153
  	/**
  	 * @medium
  	 */
f7d878ff1   kload   [enh] Update to 7...
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  	function testSymmetricEncryptFileContentAes128() {
  
  		# TODO: search in keyfile for actual content as IV will ensure this test always passes
  
  		$crypted = Encryption\Crypt::symmetricEncryptFileContent($this->dataShort, 'hat', 'AES-128-CFB');
  
  		$this->assertNotEquals($this->dataShort, $crypted);
  
  
  		$decrypt = Encryption\Crypt::symmetricDecryptFileContent($crypted, 'hat', 'AES-128-CFB');
  
  		$this->assertEquals($this->dataShort, $decrypt);
  
  	}
  
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
172
  	function testSymmetricStreamEncryptShortFileContent() {
a293d369c   Kload   Update sources to...
173
  		$filename = 'tmp-' . uniqid() . '.test';
03e52840d   Kload   Init
174

31b7f2792   Kload   Upgrade to ownclo...
175
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/'. $filename, $this->dataShort);
03e52840d   Kload   Init
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
  
  		// 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);
f7d878ff1   kload   [enh] Update to 7...
192
193
  		// Get file contents with the encryption wrapper
  		$decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename);
03e52840d   Kload   Init
194

f7d878ff1   kload   [enh] Update to 7...
195
196
197
198
199
  		// Check that decrypted data matches
  		$this->assertEquals($this->dataShort, $decrypted);
  
  		// Teardown
  		$this->view->unlink($this->userId . '/files/' . $filename);
03e52840d   Kload   Init
200

f7d878ff1   kload   [enh] Update to 7...
201
202
  		Encryption\Keymanager::deleteFileKey($this->view, $filename);
  	}
03e52840d   Kload   Init
203

f7d878ff1   kload   [enh] Update to 7...
204
205
206
207
  	/**
  	 * @medium
  	 */
  	function testSymmetricStreamEncryptShortFileContentAes128() {
03e52840d   Kload   Init
208

f7d878ff1   kload   [enh] Update to 7...
209
  		$filename = 'tmp-' . uniqid() . '.test';
03e52840d   Kload   Init
210

f7d878ff1   kload   [enh] Update to 7...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
  		\OCP\Config::setSystemValue('cipher', 'AES-128-CFB');
  
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/'. $filename, $this->dataShort);
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		\OCP\Config::deleteSystemValue('cipher');
  
  		// 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 file contents with the encryption wrapper
  		$decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename);
03e52840d   Kload   Init
235
236
  
  		// Check that decrypted data matches
f7d878ff1   kload   [enh] Update to 7...
237
  		$this->assertEquals($this->dataShort, $decrypted);
03e52840d   Kload   Init
238
239
240
  
  		// Teardown
  		$this->view->unlink($this->userId . '/files/' . $filename);
31b7f2792   Kload   Upgrade to ownclo...
241
  		Encryption\Keymanager::deleteFileKey($this->view, $filename);
03e52840d   Kload   Init
242
243
244
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
245
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
246
  	 * Test that data that is written by the crypto stream wrapper
03e52840d   Kload   Init
247
248
249
250
251
252
253
  	 * @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...
254
  		$filename = 'tmp-' . uniqid() . '.test';
03e52840d   Kload   Init
255
256
  
  		// Save long data as encrypted file using stream wrapper
31b7f2792   Kload   Upgrade to ownclo...
257
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong);
03e52840d   Kload   Init
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
  
  		// 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);
f7d878ff1   kload   [enh] Update to 7...
275
  		$decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename);
03e52840d   Kload   Init
276

f7d878ff1   kload   [enh] Update to 7...
277
  		$this->assertEquals($this->dataLong . $this->dataLong, $decrypted);
03e52840d   Kload   Init
278

f7d878ff1   kload   [enh] Update to 7...
279
  		// Teardown
03e52840d   Kload   Init
280

f7d878ff1   kload   [enh] Update to 7...
281
  		$this->view->unlink($this->userId . '/files/' . $filename);
03e52840d   Kload   Init
282

f7d878ff1   kload   [enh] Update to 7...
283
  		Encryption\Keymanager::deleteFileKey($this->view, $filename);
03e52840d   Kload   Init
284

f7d878ff1   kload   [enh] Update to 7...
285
  	}
03e52840d   Kload   Init
286

f7d878ff1   kload   [enh] Update to 7...
287
288
289
290
291
292
293
294
  	/**
  	 * @medium
  	 * Test that data that is written by the crypto stream wrapper with AES 128
  	 * @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 testSymmetricStreamEncryptLongFileContentAes128() {
03e52840d   Kload   Init
295

f7d878ff1   kload   [enh] Update to 7...
296
297
  		// Generate a a random filename
  		$filename = 'tmp-' . uniqid() . '.test';
03e52840d   Kload   Init
298

f7d878ff1   kload   [enh] Update to 7...
299
  		\OCP\Config::setSystemValue('cipher', 'AES-128-CFB');
03e52840d   Kload   Init
300

f7d878ff1   kload   [enh] Update to 7...
301
302
  		// Save long data as encrypted file using stream wrapper
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong);
03e52840d   Kload   Init
303

f7d878ff1   kload   [enh] Update to 7...
304
305
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
03e52840d   Kload   Init
306

f7d878ff1   kload   [enh] Update to 7...
307
308
309
  		// Disable encryption proxy to prevent recursive calls
  		$proxyStatus = \OC_FileProxy::$enabled;
  		\OC_FileProxy::$enabled = false;
03e52840d   Kload   Init
310

f7d878ff1   kload   [enh] Update to 7...
311
  		\OCP\Config::deleteSystemValue('cipher');
03e52840d   Kload   Init
312

f7d878ff1   kload   [enh] Update to 7...
313
314
  		// 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);
03e52840d   Kload   Init
315

f7d878ff1   kload   [enh] Update to 7...
316
317
318
319
320
321
322
323
324
325
  		// 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);
  
  		$decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename);
  
  		$this->assertEquals($this->dataLong . $this->dataLong, $decrypted);
03e52840d   Kload   Init
326
327
328
329
  
  		// Teardown
  
  		$this->view->unlink($this->userId . '/files/' . $filename);
31b7f2792   Kload   Upgrade to ownclo...
330
  		Encryption\Keymanager::deleteFileKey($this->view, $filename);
03e52840d   Kload   Init
331
332
333
334
  
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
335
  	 * @medium
f7d878ff1   kload   [enh] Update to 7...
336
337
338
339
  	 * Test that data that is written by the crypto stream wrapper with AES 128
  	 * @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
03e52840d   Kload   Init
340
  	 */
f7d878ff1   kload   [enh] Update to 7...
341
  	function testStreamDecryptLongFileContentWithoutHeader() {
03e52840d   Kload   Init
342

f7d878ff1   kload   [enh] Update to 7...
343
344
345
346
  		// Generate a a random filename
  		$filename = 'tmp-' . uniqid() . '.test';
  
  		\OCP\Config::setSystemValue('cipher', 'AES-128-CFB');
03e52840d   Kload   Init
347
348
  
  		// Save long data as encrypted file using stream wrapper
f7d878ff1   kload   [enh] Update to 7...
349
350
351
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong);
  
  		\OCP\Config::deleteSystemValue('cipher');
03e52840d   Kload   Init
352
353
354
355
356
357
358
  
  		// 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;
f7d878ff1   kload   [enh] Update to 7...
359
360
  		// 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);
03e52840d   Kload   Init
361

f7d878ff1   kload   [enh] Update to 7...
362
363
  		// Check that the file was encrypted before being written to disk
  		$this->assertNotEquals($this->dataLong . $this->dataLong, $retreivedCryptedFile);
03e52840d   Kload   Init
364

f7d878ff1   kload   [enh] Update to 7...
365
366
367
368
  		// remove the header to check if we can also decrypt old files without a header,
  		//  this files should fall back to AES-128
  		$cryptedWithoutHeader = substr($retreivedCryptedFile, Encryption\Crypt::BLOCKSIZE);
  		$this->view->file_put_contents($this->userId . '/files/' . $filename, $cryptedWithoutHeader);
03e52840d   Kload   Init
369

f7d878ff1   kload   [enh] Update to 7...
370
371
  		// Re-enable proxy - our work is done
  		\OC_FileProxy::$enabled = $proxyStatus;
03e52840d   Kload   Init
372

f7d878ff1   kload   [enh] Update to 7...
373
  		$decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename);
03e52840d   Kload   Init
374

f7d878ff1   kload   [enh] Update to 7...
375
  		$this->assertEquals($this->dataLong . $this->dataLong, $decrypted);
03e52840d   Kload   Init
376

f7d878ff1   kload   [enh] Update to 7...
377
  		// Teardown
03e52840d   Kload   Init
378

f7d878ff1   kload   [enh] Update to 7...
379
  		$this->view->unlink($this->userId . '/files/' . $filename);
03e52840d   Kload   Init
380

f7d878ff1   kload   [enh] Update to 7...
381
  		Encryption\Keymanager::deleteFileKey($this->view, $filename);
03e52840d   Kload   Init
382

03e52840d   Kload   Init
383
  	}
31b7f2792   Kload   Upgrade to ownclo...
384
385
386
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
387
388
389
390
391
  	function testIsEncryptedContent() {
  
  		$this->assertFalse(Encryption\Crypt::isCatfileContent($this->dataUrl));
  
  		$this->assertFalse(Encryption\Crypt::isCatfileContent($this->legacyEncryptedData));
f7d878ff1   kload   [enh] Update to 7...
392
  		$keyfileContent = Encryption\Crypt::symmetricEncryptFileContent($this->dataUrl, 'hat', 'AES-128-CFB');
03e52840d   Kload   Init
393
394
395
396
  
  		$this->assertTrue(Encryption\Crypt::isCatfileContent($keyfileContent));
  
  	}
31b7f2792   Kload   Upgrade to ownclo...
397
398
399
  	/**
  	 * @large
  	 */
03e52840d   Kload   Init
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
  	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
423
  	/**
31b7f2792   Kload   Upgrade to ownclo...
424
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
425
  	 * test decryption using legacy blowfish method
03e52840d   Kload   Init
426
  	 */
31b7f2792   Kload   Upgrade to ownclo...
427
428
429
  	function testLegacyDecryptShort() {
  
  		$crypted = $this->legacyEncrypt($this->dataShort, $this->pass);
03e52840d   Kload   Init
430
431
432
433
434
435
436
437
  
  		$decrypted = Encryption\Crypt::legacyBlockDecrypt($crypted, $this->pass);
  
  		$this->assertEquals($this->dataShort, $decrypted);
  
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
438
  	 * @medium
6d9380f96   Cédric Dupont   Update sources OC...
439
  	 * test decryption using legacy blowfish method
03e52840d   Kload   Init
440
  	 */
31b7f2792   Kload   Upgrade to ownclo...
441
442
443
  	function testLegacyDecryptLong() {
  
  		$crypted = $this->legacyEncrypt($this->dataLong, $this->pass);
03e52840d   Kload   Init
444
445
446
447
  
  		$decrypted = Encryption\Crypt::legacyBlockDecrypt($crypted, $this->pass);
  
  		$this->assertEquals($this->dataLong, $decrypted);
03e52840d   Kload   Init
448
449
450
  	}
  
  	/**
31b7f2792   Kload   Upgrade to ownclo...
451
  	 * @medium
03e52840d   Kload   Init
452
  	 */
03e52840d   Kload   Init
453
  	function testRenameFile() {
a293d369c   Kload   Update sources to...
454
  		$filename = 'tmp-' . uniqid();
03e52840d   Kload   Init
455
456
  
  		// Save long data as encrypted file using stream wrapper
31b7f2792   Kload   Upgrade to ownclo...
457
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong);
03e52840d   Kload   Init
458
459
460
461
462
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
463
  		$decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
03e52840d   Kload   Init
464
465
  
  		$this->assertEquals($this->dataLong, $decrypt);
a293d369c   Kload   Update sources to...
466
  		$newFilename = 'tmp-new-' . uniqid();
03e52840d   Kload   Init
467
468
469
470
  		$view = new \OC\Files\View('/' . $this->userId . '/files');
  		$view->rename($filename, $newFilename);
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
471
  		$newDecrypt = file_get_contents('crypt:///'. $this->userId . '/files/' . $newFilename);
03e52840d   Kload   Init
472
473
474
475
476
477
  
  		$this->assertEquals($this->dataLong, $newDecrypt);
  
  		// tear down
  		$view->unlink($newFilename);
  	}
31b7f2792   Kload   Upgrade to ownclo...
478
479
480
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
481
  	function testMoveFileIntoFolder() {
a293d369c   Kload   Update sources to...
482
  		$filename = 'tmp-' . uniqid();
03e52840d   Kload   Init
483
484
  
  		// Save long data as encrypted file using stream wrapper
31b7f2792   Kload   Upgrade to ownclo...
485
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong);
03e52840d   Kload   Init
486
487
488
489
490
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
491
  		$decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
03e52840d   Kload   Init
492
493
  
  		$this->assertEquals($this->dataLong, $decrypt);
a293d369c   Kload   Update sources to...
494
495
  		$newFolder = '/newfolder' . uniqid();
  		$newFilename = 'tmp-new-' . uniqid();
03e52840d   Kload   Init
496
497
498
499
500
  		$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...
501
  		$newDecrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $newFolder . '/' . $newFilename);
03e52840d   Kload   Init
502
503
504
505
506
507
  
  		$this->assertEquals($this->dataLong, $newDecrypt);
  
  		// tear down
  		$view->unlink($newFolder);
  	}
31b7f2792   Kload   Upgrade to ownclo...
508
509
510
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
511
512
513
  	function testMoveFolder() {
  
  		$view = new \OC\Files\View('/' . $this->userId . '/files');
a293d369c   Kload   Update sources to...
514
515
  		$filename = '/tmp-' . uniqid();
  		$folder = '/folder' . uniqid();
03e52840d   Kload   Init
516
517
518
519
  
  		$view->mkdir($folder);
  
  		// Save long data as encrypted file using stream wrapper
31b7f2792   Kload   Upgrade to ownclo...
520
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $folder . $filename, $this->dataLong);
03e52840d   Kload   Init
521
522
523
524
525
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
526
  		$decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $folder . $filename);
03e52840d   Kload   Init
527
528
  
  		$this->assertEquals($this->dataLong, $decrypt);
a293d369c   Kload   Update sources to...
529
  		$newFolder = '/newfolder/subfolder' . uniqid();
03e52840d   Kload   Init
530
531
532
533
534
  		$view->mkdir('/newfolder');
  
  		$view->rename($folder, $newFolder);
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
535
  		$newDecrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $newFolder . $filename);
03e52840d   Kload   Init
536
537
538
539
540
541
542
  
  		$this->assertEquals($this->dataLong, $newDecrypt);
  
  		// tear down
  		$view->unlink($newFolder);
  		$view->unlink('/newfolder');
  	}
31b7f2792   Kload   Upgrade to ownclo...
543
544
545
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
546
  	function testChangePassphrase() {
a293d369c   Kload   Update sources to...
547
  		$filename = 'tmp-' . uniqid();
03e52840d   Kload   Init
548
549
  
  		// Save long data as encrypted file using stream wrapper
31b7f2792   Kload   Upgrade to ownclo...
550
  		$cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong);
03e52840d   Kload   Init
551
552
553
554
555
  
  		// Test that data was successfully written
  		$this->assertTrue(is_int($cryptedFile));
  
  		// Get file decrypted contents
31b7f2792   Kload   Upgrade to ownclo...
556
  		$decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
03e52840d   Kload   Init
557
558
559
560
561
562
563
564
565
566
567
568
  
  		$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...
569
  		$newDecrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
03e52840d   Kload   Init
570
571
572
573
574
575
576
577
578
  
  		$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...
579
580
581
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
582
  	function testViewFilePutAndGetContents() {
a293d369c   Kload   Update sources to...
583
  		$filename = '/tmp-' . uniqid();
03e52840d   Kload   Init
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
  		$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...
611
612
613
  	/**
  	 * @large
  	 */
03e52840d   Kload   Init
614
  	function testTouchExistingFile() {
a293d369c   Kload   Update sources to...
615
  		$filename = '/tmp-' . uniqid();
03e52840d   Kload   Init
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
  		$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...
634
635
636
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
637
  	function testTouchFile() {
a293d369c   Kload   Update sources to...
638
  		$filename = '/tmp-' . uniqid();
03e52840d   Kload   Init
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
  		$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...
657
658
659
  	/**
  	 * @medium
  	 */
03e52840d   Kload   Init
660
  	function testFopenFile() {
a293d369c   Kload   Update sources to...
661
  		$filename = '/tmp-' . uniqid();
03e52840d   Kload   Init
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
  		$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...
680
681
682
  
  
  	/**
6d9380f96   Cédric Dupont   Update sources OC...
683
684
685
  	 * encryption using legacy blowfish method
  	 * @param string $data data to encrypt
  	 * @param string $passwd password
31b7f2792   Kload   Upgrade to ownclo...
686
687
688
  	 * @return string
  	 */
  	function legacyEncrypt($data, $passwd) {
6d9380f96   Cédric Dupont   Update sources OC...
689
  		$bf = new Legacy_Crypt_Blowfish($passwd);
31b7f2792   Kload   Upgrade to ownclo...
690
691
692
693
  		$crypted = $bf->encrypt($data);
  
  		return $crypted;
  	}
03e52840d   Kload   Init
694
  }