You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crypt.php 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <?php
  2. /**
  3. * @author Andreas Fischer <bantu@owncloud.com>
  4. * @author Björn Schießle <schiessle@owncloud.com>
  5. * @author Florin Peter <github@florin-peter.de>
  6. * @author Joas Schilling <nickvergessen@owncloud.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <icewind@owncloud.com>
  10. * @author Sam Tuke <mail@samtuke.com>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @copyright Copyright (c) 2015, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files_Encryption\Tests;
  30. /**
  31. * Class Crypt
  32. */
  33. class Crypt extends TestCase {
  34. const TEST_ENCRYPTION_CRYPT_USER1 = "test-crypt-user1";
  35. public $userId;
  36. public $pass;
  37. public $stateFilesTrashbin;
  38. public $dataLong;
  39. public $dataUrl;
  40. public $dataShort;
  41. /**
  42. * @var \OC\Files\View
  43. */
  44. public $view;
  45. public $legacyEncryptedData;
  46. public $genPrivateKey;
  47. public $genPublicKey;
  48. /** @var \OCP\IConfig */
  49. private $config;
  50. public static function setUpBeforeClass() {
  51. parent::setUpBeforeClass();
  52. // create test user
  53. self::loginHelper(self::TEST_ENCRYPTION_CRYPT_USER1, true);
  54. }
  55. protected function setUp() {
  56. parent::setUp();
  57. // set user id
  58. self::loginHelper(self::TEST_ENCRYPTION_CRYPT_USER1);
  59. $this->userId = self::TEST_ENCRYPTION_CRYPT_USER1;
  60. $this->pass = self::TEST_ENCRYPTION_CRYPT_USER1;
  61. // set content for encrypting / decrypting in tests
  62. $this->dataLong = file_get_contents(__DIR__ . '/../lib/crypt.php');
  63. $this->dataShort = 'hats';
  64. $this->dataUrl = __DIR__ . '/../lib/crypt.php';
  65. $this->legacyData = __DIR__ . '/legacy-text.txt';
  66. $this->legacyEncryptedData = __DIR__ . '/legacy-encrypted-text.txt';
  67. $this->legacyEncryptedDataKey = __DIR__ . '/encryption.key';
  68. $this->randomKey = \OCA\Files_Encryption\Crypt::generateKey();
  69. $keypair = \OCA\Files_Encryption\Crypt::createKeypair();
  70. $this->genPublicKey = $keypair['publicKey'];
  71. $this->genPrivateKey = $keypair['privateKey'];
  72. $this->view = new \OC\Files\View('/');
  73. // remember files_trashbin state
  74. $this->stateFilesTrashbin = \OC_App::isEnabled('files_trashbin');
  75. // we don't want to tests with app files_trashbin enabled
  76. \OC_App::disable('files_trashbin');
  77. $this->config = \OC::$server->getConfig();
  78. }
  79. protected function tearDown() {
  80. // reset app files_trashbin
  81. if ($this->stateFilesTrashbin) {
  82. \OC_App::enable('files_trashbin');
  83. } else {
  84. \OC_App::disable('files_trashbin');
  85. }
  86. $this->assertTrue(\OC_FileProxy::$enabled);
  87. $this->config->deleteSystemValue('cipher');
  88. parent::tearDown();
  89. }
  90. public static function tearDownAfterClass() {
  91. // cleanup test user
  92. \OC_User::deleteUser(self::TEST_ENCRYPTION_CRYPT_USER1);
  93. parent::tearDownAfterClass();
  94. }
  95. /**
  96. * @medium
  97. */
  98. public function testGenerateKey() {
  99. # TODO: use more accurate (larger) string length for test confirmation
  100. $key = \OCA\Files_Encryption\Crypt::generateKey();
  101. $this->assertTrue(strlen($key) > 16);
  102. }
  103. public function testDecryptPrivateKey() {
  104. // test successful decrypt
  105. $crypted = \OCA\Files_Encryption\Crypt::symmetricEncryptFileContent($this->genPrivateKey, 'hat');
  106. $header = \OCA\Files_Encryption\Crypt::generateHeader();
  107. $decrypted = \OCA\Files_Encryption\Crypt::decryptPrivateKey($header . $crypted, 'hat');
  108. $this->assertEquals($this->genPrivateKey, $decrypted);
  109. //test private key decrypt with wrong password
  110. $wrongPasswd = \OCA\Files_Encryption\Crypt::decryptPrivateKey($crypted, 'hat2');
  111. $this->assertEquals(false, $wrongPasswd);
  112. }
  113. /**
  114. * @medium
  115. */
  116. public function testSymmetricEncryptFileContent() {
  117. # TODO: search in keyfile for actual content as IV will ensure this test always passes
  118. $crypted = \OCA\Files_Encryption\Crypt::symmetricEncryptFileContent($this->dataShort, 'hat');
  119. $this->assertNotEquals($this->dataShort, $crypted);
  120. $decrypt = \OCA\Files_Encryption\Crypt::symmetricDecryptFileContent($crypted, 'hat');
  121. $this->assertEquals($this->dataShort, $decrypt);
  122. }
  123. /**
  124. * @medium
  125. */
  126. public function testSymmetricEncryptFileContentAes128() {
  127. # TODO: search in keyfile for actual content as IV will ensure this test always passes
  128. $crypted = \OCA\Files_Encryption\Crypt::symmetricEncryptFileContent($this->dataShort, 'hat', 'AES-128-CFB');
  129. $this->assertNotEquals($this->dataShort, $crypted);
  130. $decrypt = \OCA\Files_Encryption\Crypt::symmetricDecryptFileContent($crypted, 'hat', 'AES-128-CFB');
  131. $this->assertEquals($this->dataShort, $decrypt);
  132. }
  133. /**
  134. * @medium
  135. */
  136. public function testSymmetricStreamEncryptShortFileContent() {
  137. $filename = 'tmp-' . $this->getUniqueID() . '.test';
  138. $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/'. $filename, $this->dataShort);
  139. // Test that data was successfully written
  140. $this->assertTrue(is_int($cryptedFile));
  141. // Disable encryption proxy to prevent recursive calls
  142. $proxyStatus = \OC_FileProxy::$enabled;
  143. \OC_FileProxy::$enabled = false;
  144. // Get file contents without using any wrapper to get it's actual contents on disk
  145. $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename);
  146. // Re-enable proxy - our work is done
  147. \OC_FileProxy::$enabled = $proxyStatus;
  148. // Check that the file was encrypted before being written to disk
  149. $this->assertNotEquals($this->dataShort, $retreivedCryptedFile);
  150. // Get file contents with the encryption wrapper
  151. $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename);
  152. // Check that decrypted data matches
  153. $this->assertEquals($this->dataShort, $decrypted);
  154. // Teardown
  155. $this->view->unlink($this->userId . '/files/' . $filename);
  156. }
  157. /**
  158. * @medium
  159. */
  160. public function testSymmetricStreamEncryptShortFileContentAes128() {
  161. $filename = 'tmp-' . $this->getUniqueID() . '.test';
  162. $this->config->setSystemValue('cipher', 'AES-128-CFB');
  163. $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/'. $filename, $this->dataShort);
  164. // Test that data was successfully written
  165. $this->assertTrue(is_int($cryptedFile));
  166. $this->config->deleteSystemValue('cipher');
  167. // Disable encryption proxy to prevent recursive calls
  168. $proxyStatus = \OC_FileProxy::$enabled;
  169. \OC_FileProxy::$enabled = false;
  170. // Get file contents without using any wrapper to get it's actual contents on disk
  171. $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename);
  172. // Re-enable proxy - our work is done
  173. \OC_FileProxy::$enabled = $proxyStatus;
  174. // Check that the file was encrypted before being written to disk
  175. $this->assertNotEquals($this->dataShort, $retreivedCryptedFile);
  176. // Get file contents with the encryption wrapper
  177. $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename);
  178. // Check that decrypted data matches
  179. $this->assertEquals($this->dataShort, $decrypted);
  180. // Teardown
  181. $this->view->unlink($this->userId . '/files/' . $filename);
  182. }
  183. /**
  184. * @medium
  185. * Test that data that is written by the crypto stream wrapper
  186. * @note Encrypted data is manually prepared and decrypted here to avoid dependency on success of stream_read
  187. * @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
  188. * reassembly of its data
  189. */
  190. public function testSymmetricStreamEncryptLongFileContent() {
  191. // Generate a a random filename
  192. $filename = 'tmp-' . $this->getUniqueID() . '.test';
  193. // Save long data as encrypted file using stream wrapper
  194. $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong);
  195. // Test that data was successfully written
  196. $this->assertTrue(is_int($cryptedFile));
  197. // Disable encryption proxy to prevent recursive calls
  198. $proxyStatus = \OC_FileProxy::$enabled;
  199. \OC_FileProxy::$enabled = false;
  200. // Get file contents without using any wrapper to get it's actual contents on disk
  201. $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename);
  202. // Re-enable proxy - our work is done
  203. \OC_FileProxy::$enabled = $proxyStatus;
  204. // Check that the file was encrypted before being written to disk
  205. $this->assertNotEquals($this->dataLong . $this->dataLong, $retreivedCryptedFile);
  206. $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename);
  207. $this->assertEquals($this->dataLong . $this->dataLong, $decrypted);
  208. // Teardown
  209. $this->view->unlink($this->userId . '/files/' . $filename);
  210. }
  211. /**
  212. * @medium
  213. * Test that data that is written by the crypto stream wrapper with AES 128
  214. * @note Encrypted data is manually prepared and decrypted here to avoid dependency on success of stream_read
  215. * @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
  216. * reassembly of its data
  217. */
  218. public function testSymmetricStreamEncryptLongFileContentAes128() {
  219. // Generate a a random filename
  220. $filename = 'tmp-' . $this->getUniqueID() . '.test';
  221. $this->config->setSystemValue('cipher', 'AES-128-CFB');
  222. // Save long data as encrypted file using stream wrapper
  223. $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong);
  224. // Test that data was successfully written
  225. $this->assertTrue(is_int($cryptedFile));
  226. // Disable encryption proxy to prevent recursive calls
  227. $proxyStatus = \OC_FileProxy::$enabled;
  228. \OC_FileProxy::$enabled = false;
  229. $this->config->deleteSystemValue('cipher');
  230. // Get file contents without using any wrapper to get it's actual contents on disk
  231. $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename);
  232. // Re-enable proxy - our work is done
  233. \OC_FileProxy::$enabled = $proxyStatus;
  234. // Check that the file was encrypted before being written to disk
  235. $this->assertNotEquals($this->dataLong . $this->dataLong, $retreivedCryptedFile);
  236. $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename);
  237. $this->assertEquals($this->dataLong . $this->dataLong, $decrypted);
  238. // Teardown
  239. $this->view->unlink($this->userId . '/files/' . $filename);
  240. }
  241. /**
  242. * @medium
  243. * Test that data that is written by the crypto stream wrapper with AES 128
  244. * @note Encrypted data is manually prepared and decrypted here to avoid dependency on success of stream_read
  245. * @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
  246. * reassembly of its data
  247. */
  248. public function testStreamDecryptLongFileContentWithoutHeader() {
  249. // Generate a a random filename
  250. $filename = 'tmp-' . $this->getUniqueID() . '.test';
  251. $this->config->setSystemValue('cipher', 'AES-128-CFB');
  252. // Save long data as encrypted file using stream wrapper
  253. $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong . $this->dataLong);
  254. $this->config->deleteSystemValue('cipher');
  255. // Test that data was successfully written
  256. $this->assertTrue(is_int($cryptedFile));
  257. // Disable encryption proxy to prevent recursive calls
  258. $proxyStatus = \OC_FileProxy::$enabled;
  259. \OC_FileProxy::$enabled = false;
  260. // Get file contents without using any wrapper to get it's actual contents on disk
  261. $retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename);
  262. // Check that the file was encrypted before being written to disk
  263. $this->assertNotEquals($this->dataLong . $this->dataLong, $retreivedCryptedFile);
  264. // remove the header to check if we can also decrypt old files without a header,
  265. // this files should fall back to AES-128
  266. $cryptedWithoutHeader = substr($retreivedCryptedFile, \OCA\Files_Encryption\Crypt::BLOCKSIZE);
  267. $this->view->file_put_contents($this->userId . '/files/' . $filename, $cryptedWithoutHeader);
  268. // Re-enable proxy - our work is done
  269. \OC_FileProxy::$enabled = $proxyStatus;
  270. $decrypted = file_get_contents('crypt:///' . $this->userId . '/files/'. $filename);
  271. $this->assertEquals($this->dataLong . $this->dataLong, $decrypted);
  272. // Teardown
  273. $this->view->unlink($this->userId . '/files/' . $filename);
  274. }
  275. /**
  276. * @medium
  277. */
  278. public function testIsEncryptedContent() {
  279. $this->assertFalse(\OCA\Files_Encryption\Crypt::isCatfileContent($this->dataUrl));
  280. $this->assertFalse(\OCA\Files_Encryption\Crypt::isCatfileContent($this->legacyEncryptedData));
  281. $keyfileContent = \OCA\Files_Encryption\Crypt::symmetricEncryptFileContent($this->dataUrl, 'hat', 'AES-128-CFB');
  282. $this->assertTrue(\OCA\Files_Encryption\Crypt::isCatfileContent($keyfileContent));
  283. }
  284. /**
  285. * @large
  286. */
  287. public function testMultiKeyEncrypt() {
  288. # TODO: search in keyfile for actual content as IV will ensure this test always passes
  289. $pair1 = \OCA\Files_Encryption\Crypt::createKeypair();
  290. $this->assertEquals(2, count($pair1));
  291. $this->assertTrue(strlen($pair1['publicKey']) > 1);
  292. $this->assertTrue(strlen($pair1['privateKey']) > 1);
  293. $crypted = \OCA\Files_Encryption\Crypt::multiKeyEncrypt($this->dataShort, array($pair1['publicKey']));
  294. $this->assertNotEquals($this->dataShort, $crypted['data']);
  295. $decrypt = \OCA\Files_Encryption\Crypt::multiKeyDecrypt($crypted['data'], $crypted['keys'][0], $pair1['privateKey']);
  296. $this->assertEquals($this->dataShort, $decrypt);
  297. }
  298. /**
  299. * @medium
  300. */
  301. public function testRenameFile() {
  302. $filename = 'tmp-' . $this->getUniqueID();
  303. // Save long data as encrypted file using stream wrapper
  304. $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong);
  305. // Test that data was successfully written
  306. $this->assertTrue(is_int($cryptedFile));
  307. // Get file decrypted contents
  308. $decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
  309. $this->assertEquals($this->dataLong, $decrypt);
  310. $newFilename = 'tmp-new-' . $this->getUniqueID();
  311. $view = new \OC\Files\View('/' . $this->userId . '/files');
  312. $view->rename($filename, $newFilename);
  313. // Get file decrypted contents
  314. $newDecrypt = file_get_contents('crypt:///'. $this->userId . '/files/' . $newFilename);
  315. $this->assertEquals($this->dataLong, $newDecrypt);
  316. // tear down
  317. $view->unlink($newFilename);
  318. }
  319. /**
  320. * @medium
  321. */
  322. public function testMoveFileIntoFolder() {
  323. $filename = 'tmp-' . $this->getUniqueID();
  324. // Save long data as encrypted file using stream wrapper
  325. $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong);
  326. // Test that data was successfully written
  327. $this->assertTrue(is_int($cryptedFile));
  328. // Get file decrypted contents
  329. $decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
  330. $this->assertEquals($this->dataLong, $decrypt);
  331. $newFolder = '/newfolder' . $this->getUniqueID();
  332. $newFilename = 'tmp-new-' . $this->getUniqueID();
  333. $view = new \OC\Files\View('/' . $this->userId . '/files');
  334. $view->mkdir($newFolder);
  335. $view->rename($filename, $newFolder . '/' . $newFilename);
  336. // Get file decrypted contents
  337. $newDecrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $newFolder . '/' . $newFilename);
  338. $this->assertEquals($this->dataLong, $newDecrypt);
  339. // tear down
  340. $view->unlink($newFolder);
  341. }
  342. /**
  343. * @medium
  344. */
  345. public function testMoveFolder() {
  346. $view = new \OC\Files\View('/' . $this->userId . '/files');
  347. $filename = '/tmp-' . $this->getUniqueID();
  348. $folder = '/folder' . $this->getUniqueID();
  349. $view->mkdir($folder);
  350. // Save long data as encrypted file using stream wrapper
  351. $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $folder . $filename, $this->dataLong);
  352. // Test that data was successfully written
  353. $this->assertTrue(is_int($cryptedFile));
  354. // Get file decrypted contents
  355. $decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $folder . $filename);
  356. $this->assertEquals($this->dataLong, $decrypt);
  357. $newFolder = '/newfolder/subfolder' . $this->getUniqueID();
  358. $view->mkdir('/newfolder');
  359. $view->rename($folder, $newFolder);
  360. // Get file decrypted contents
  361. $newDecrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $newFolder . $filename);
  362. $this->assertEquals($this->dataLong, $newDecrypt);
  363. // tear down
  364. $view->unlink($newFolder);
  365. $view->unlink('/newfolder');
  366. }
  367. /**
  368. * @medium
  369. */
  370. public function testChangePassphrase() {
  371. $filename = 'tmp-' . $this->getUniqueID();
  372. // Save long data as encrypted file using stream wrapper
  373. $cryptedFile = file_put_contents('crypt:///' . $this->userId . '/files/' . $filename, $this->dataLong);
  374. // Test that data was successfully written
  375. $this->assertTrue(is_int($cryptedFile));
  376. // Get file decrypted contents
  377. $decrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
  378. $this->assertEquals($this->dataLong, $decrypt);
  379. // change password
  380. \OC_User::setPassword($this->userId, 'test', null);
  381. // relogin
  382. $params['uid'] = $this->userId;
  383. $params['password'] = 'test';
  384. \OCA\Files_Encryption\Hooks::login($params);
  385. // Get file decrypted contents
  386. $newDecrypt = file_get_contents('crypt:///' . $this->userId . '/files/' . $filename);
  387. $this->assertEquals($this->dataLong, $newDecrypt);
  388. // tear down
  389. // change password back
  390. \OC_User::setPassword($this->userId, $this->pass);
  391. $view = new \OC\Files\View('/' . $this->userId . '/files');
  392. $view->unlink($filename);
  393. }
  394. /**
  395. * @medium
  396. */
  397. public function testViewFilePutAndGetContents() {
  398. $filename = '/tmp-' . $this->getUniqueID();
  399. $view = new \OC\Files\View('/' . $this->userId . '/files');
  400. // Save short data as encrypted file using stream wrapper
  401. $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  402. // Test that data was successfully written
  403. $this->assertTrue(is_int($cryptedFile));
  404. // Get file decrypted contents
  405. $decrypt = $view->file_get_contents($filename);
  406. $this->assertEquals($this->dataShort, $decrypt);
  407. // Save long data as encrypted file using stream wrapper
  408. $cryptedFileLong = $view->file_put_contents($filename, $this->dataLong);
  409. // Test that data was successfully written
  410. $this->assertTrue(is_int($cryptedFileLong));
  411. // Get file decrypted contents
  412. $decryptLong = $view->file_get_contents($filename);
  413. $this->assertEquals($this->dataLong, $decryptLong);
  414. // tear down
  415. $view->unlink($filename);
  416. }
  417. /**
  418. * @large
  419. */
  420. public function testTouchExistingFile() {
  421. $filename = '/tmp-' . $this->getUniqueID();
  422. $view = new \OC\Files\View('/' . $this->userId . '/files');
  423. // Save short data as encrypted file using stream wrapper
  424. $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  425. // Test that data was successfully written
  426. $this->assertTrue(is_int($cryptedFile));
  427. $view->touch($filename);
  428. // Get file decrypted contents
  429. $decrypt = $view->file_get_contents($filename);
  430. $this->assertEquals($this->dataShort, $decrypt);
  431. // tear down
  432. $view->unlink($filename);
  433. }
  434. /**
  435. * @medium
  436. */
  437. public function testTouchFile() {
  438. $filename = '/tmp-' . $this->getUniqueID();
  439. $view = new \OC\Files\View('/' . $this->userId . '/files');
  440. $view->touch($filename);
  441. // Save short data as encrypted file using stream wrapper
  442. $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  443. // Test that data was successfully written
  444. $this->assertTrue(is_int($cryptedFile));
  445. // Get file decrypted contents
  446. $decrypt = $view->file_get_contents($filename);
  447. $this->assertEquals($this->dataShort, $decrypt);
  448. // tear down
  449. $view->unlink($filename);
  450. }
  451. /**
  452. * @medium
  453. */
  454. public function testFopenFile() {
  455. $filename = '/tmp-' . $this->getUniqueID();
  456. $view = new \OC\Files\View('/' . $this->userId . '/files');
  457. // Save short data as encrypted file using stream wrapper
  458. $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
  459. // Test that data was successfully written
  460. $this->assertTrue(is_int($cryptedFile));
  461. $handle = $view->fopen($filename, 'r');
  462. // Get file decrypted contents
  463. $decrypt = fgets($handle);
  464. $this->assertEquals($this->dataShort, $decrypt);
  465. // tear down
  466. fclose($handle);
  467. $view->unlink($filename);
  468. }
  469. }