share.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Florin Peter
  6. * @copyright 2013 Florin Peter <owncloud@florin-peter.de>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. require_once __DIR__ . '/../3rdparty/Crypt_Blowfish/Blowfish.php';
  23. require_once __DIR__ . '/../../../lib/base.php';
  24. require_once __DIR__ . '/../lib/crypt.php';
  25. require_once __DIR__ . '/../lib/keymanager.php';
  26. require_once __DIR__ . '/../lib/proxy.php';
  27. require_once __DIR__ . '/../lib/stream.php';
  28. require_once __DIR__ . '/../lib/util.php';
  29. require_once __DIR__ . '/../lib/helper.php';
  30. require_once __DIR__ . '/../appinfo/app.php';
  31. require_once __DIR__ . '/util.php';
  32. use OCA\Encryption;
  33. /**
  34. * Class Test_Encryption_Share
  35. */
  36. class Test_Encryption_Share extends \PHPUnit_Framework_TestCase {
  37. const TEST_ENCRYPTION_SHARE_USER1 = "test-share-user1";
  38. const TEST_ENCRYPTION_SHARE_USER2 = "test-share-user2";
  39. const TEST_ENCRYPTION_SHARE_USER3 = "test-share-user3";
  40. const TEST_ENCRYPTION_SHARE_USER4 = "test-share-user4";
  41. const TEST_ENCRYPTION_SHARE_GROUP1 = "test-share-group1";
  42. public $stateFilesTrashbin;
  43. public $filename;
  44. public $dataShort;
  45. /**
  46. * @var OC_FilesystemView
  47. */
  48. public $view;
  49. public $folder1;
  50. public $subfolder;
  51. public $subsubfolder;
  52. public static function setUpBeforeClass() {
  53. // reset backend
  54. \OC_User::clearBackends();
  55. \OC_User::useBackend('database');
  56. // enable resharing
  57. \OC_Appconfig::setValue('core', 'shareapi_allow_resharing', 'yes');
  58. // clear share hooks
  59. \OC_Hook::clear('OCP\\Share');
  60. \OC::registerShareHooks();
  61. \OCP\Util::connectHook('OC_Filesystem', 'setup', '\OC\Files\Storage\Shared', 'setup');
  62. // Sharing related hooks
  63. \OCA\Encryption\Helper::registerShareHooks();
  64. // Filesystem related hooks
  65. \OCA\Encryption\Helper::registerFilesystemHooks();
  66. // clear and register hooks
  67. \OC_FileProxy::clearProxies();
  68. \OC_FileProxy::register(new OCA\Encryption\Proxy());
  69. // create users
  70. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1, true);
  71. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2, true);
  72. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3, true);
  73. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER4, true);
  74. // create group and assign users
  75. \OC_Group::createGroup(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_GROUP1);
  76. \OC_Group::addToGroup(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_GROUP1);
  77. \OC_Group::addToGroup(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER4, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_GROUP1);
  78. }
  79. function setUp() {
  80. $this->dataShort = 'hats';
  81. $this->view = new \OC_FilesystemView('/');
  82. $this->folder1 = '/folder1';
  83. $this->subfolder = '/subfolder1';
  84. $this->subsubfolder = '/subsubfolder1';
  85. $this->filename = 'share-tmp.test';
  86. // we don't want to tests with app files_trashbin enabled
  87. \OC_App::disable('files_trashbin');
  88. // remember files_trashbin state
  89. $this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
  90. }
  91. function tearDown() {
  92. // reset app files_trashbin
  93. if ($this->stateFilesTrashbin) {
  94. OC_App::enable('files_trashbin');
  95. } else {
  96. OC_App::disable('files_trashbin');
  97. }
  98. }
  99. public static function tearDownAfterClass() {
  100. // clean group
  101. \OC_Group::deleteGroup(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_GROUP1);
  102. // cleanup users
  103. \OC_User::deleteUser(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  104. \OC_User::deleteUser(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2);
  105. \OC_User::deleteUser(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3);
  106. \OC_User::deleteUser(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER4);
  107. }
  108. /**
  109. * @medium
  110. * @param bool $withTeardown
  111. */
  112. function testShareFile($withTeardown = true) {
  113. // login as admin
  114. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  115. // save file with content
  116. $cryptedFile = file_put_contents('crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort);
  117. // test that data was successfully written
  118. $this->assertTrue(is_int($cryptedFile));
  119. // disable encryption proxy to prevent recursive calls
  120. $proxyStatus = \OC_FileProxy::$enabled;
  121. \OC_FileProxy::$enabled = false;
  122. // get the file info from previous created file
  123. $fileInfo = $this->view->getFileInfo(
  124. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
  125. // check if we have a valid file info
  126. $this->assertTrue(is_array($fileInfo));
  127. // check if the unencrypted file size is stored
  128. $this->assertGreaterThan(0, $fileInfo['unencrypted_size']);
  129. // re-enable the file proxy
  130. \OC_FileProxy::$enabled = $proxyStatus;
  131. // share the file
  132. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2, OCP\PERMISSION_ALL);
  133. // login as admin
  134. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  135. // check if share key for user1 exists
  136. $this->assertTrue($this->view->file_exists(
  137. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  138. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey'));
  139. // login as user1
  140. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2);
  141. // get file contents
  142. $retrievedCryptedFile = $this->view->file_get_contents(
  143. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files/Shared/' . $this->filename);
  144. // check if data is the same as we previously written
  145. $this->assertEquals($this->dataShort, $retrievedCryptedFile);
  146. // cleanup
  147. if ($withTeardown) {
  148. // login as admin
  149. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  150. // unshare the file
  151. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2);
  152. // check if share key not exists
  153. $this->assertFalse($this->view->file_exists(
  154. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  155. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey'));
  156. // cleanup
  157. $this->view->unlink(
  158. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
  159. // check if share key not exists
  160. $this->assertFalse($this->view->file_exists(
  161. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  162. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey'));
  163. }
  164. }
  165. /**
  166. * @medium
  167. * @param bool $withTeardown
  168. */
  169. function testReShareFile($withTeardown = true) {
  170. $this->testShareFile(false);
  171. // login as user1
  172. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2);
  173. // get the file info
  174. $fileInfo = $this->view->getFileInfo(
  175. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files/Shared/' . $this->filename);
  176. // share the file with user2
  177. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3, OCP\PERMISSION_ALL);
  178. // login as admin
  179. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  180. // check if share key for user2 exists
  181. $this->assertTrue($this->view->file_exists(
  182. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  183. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey'));
  184. // login as user2
  185. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3);
  186. // get file contents
  187. $retrievedCryptedFile = $this->view->file_get_contents(
  188. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '/files/Shared/' . $this->filename);
  189. // check if data is the same as previously written
  190. $this->assertEquals($this->dataShort, $retrievedCryptedFile);
  191. // cleanup
  192. if ($withTeardown) {
  193. // login as user1
  194. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2);
  195. // unshare the file with user2
  196. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3);
  197. // login as admin
  198. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  199. // check if share key not exists
  200. $this->assertFalse($this->view->file_exists(
  201. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  202. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey'));
  203. // unshare the file with user1
  204. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2);
  205. // check if share key not exists
  206. $this->assertFalse($this->view->file_exists(
  207. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  208. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey'));
  209. // cleanup
  210. $this->view->unlink(
  211. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
  212. // check if share key not exists
  213. $this->assertFalse($this->view->file_exists(
  214. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  215. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey'));
  216. }
  217. }
  218. /**
  219. * @medium
  220. * @param bool $withTeardown
  221. * @return array
  222. */
  223. function testShareFolder($withTeardown = true) {
  224. // login as admin
  225. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  226. // create folder structure
  227. $this->view->mkdir('/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1);
  228. $this->view->mkdir(
  229. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1 . $this->subfolder);
  230. $this->view->mkdir(
  231. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1 . $this->subfolder
  232. . $this->subsubfolder);
  233. // save file with content
  234. $cryptedFile = file_put_contents('crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/'
  235. . $this->filename, $this->dataShort);
  236. // test that data was successfully written
  237. $this->assertTrue(is_int($cryptedFile));
  238. // disable encryption proxy to prevent recursive calls
  239. $proxyStatus = \OC_FileProxy::$enabled;
  240. \OC_FileProxy::$enabled = false;
  241. // get the file info from previous created folder
  242. $fileInfo = $this->view->getFileInfo(
  243. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1);
  244. // check if we have a valid file info
  245. $this->assertTrue(is_array($fileInfo));
  246. // re-enable the file proxy
  247. \OC_FileProxy::$enabled = $proxyStatus;
  248. // share the folder with user1
  249. \OCP\Share::shareItem('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2, OCP\PERMISSION_ALL);
  250. // login as admin
  251. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  252. // check if share key for user1 exists
  253. $this->assertTrue($this->view->file_exists(
  254. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys' . $this->folder1
  255. . $this->subfolder . $this->subsubfolder . '/'
  256. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey'));
  257. // login as user1
  258. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2);
  259. // get file contents
  260. $retrievedCryptedFile = $this->view->file_get_contents(
  261. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files/Shared' . $this->folder1
  262. . $this->subfolder . $this->subsubfolder . '/' . $this->filename);
  263. // check if data is the same
  264. $this->assertEquals($this->dataShort, $retrievedCryptedFile);
  265. // cleanup
  266. if ($withTeardown) {
  267. // login as admin
  268. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  269. // unshare the folder with user1
  270. \OCP\Share::unshare('folder', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2);
  271. // check if share key not exists
  272. $this->assertFalse($this->view->file_exists(
  273. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys'
  274. . $this->folder1 . $this->subfolder . $this->subsubfolder . '/'
  275. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey'));
  276. // cleanup
  277. $this->view->unlink('/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1);
  278. // check if share key not exists
  279. $this->assertFalse($this->view->file_exists(
  280. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys'
  281. . $this->folder1 . $this->subfolder . $this->subsubfolder . '/'
  282. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey'));
  283. }
  284. return $fileInfo;
  285. }
  286. /**
  287. * @medium
  288. * @param bool $withTeardown
  289. */
  290. function testReShareFolder($withTeardown = true) {
  291. $fileInfoFolder1 = $this->testShareFolder(false);
  292. // login as user1
  293. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2);
  294. // disable encryption proxy to prevent recursive calls
  295. $proxyStatus = \OC_FileProxy::$enabled;
  296. \OC_FileProxy::$enabled = false;
  297. // get the file info from previous created folder
  298. $fileInfoSubFolder = $this->view->getFileInfo(
  299. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files/Shared' . $this->folder1
  300. . $this->subfolder);
  301. // check if we have a valid file info
  302. $this->assertTrue(is_array($fileInfoSubFolder));
  303. // re-enable the file proxy
  304. \OC_FileProxy::$enabled = $proxyStatus;
  305. // share the file with user2
  306. \OCP\Share::shareItem('folder', $fileInfoSubFolder['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3, OCP\PERMISSION_ALL);
  307. // login as admin
  308. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  309. // check if share key for user2 exists
  310. $this->assertTrue($this->view->file_exists(
  311. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys' . $this->folder1
  312. . $this->subfolder . $this->subsubfolder . '/'
  313. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey'));
  314. // login as user2
  315. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3);
  316. // get file contents
  317. $retrievedCryptedFile = $this->view->file_get_contents(
  318. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '/files/Shared' . $this->subfolder
  319. . $this->subsubfolder . '/' . $this->filename);
  320. // check if data is the same
  321. $this->assertEquals($this->dataShort, $retrievedCryptedFile);
  322. // get the file info
  323. $fileInfo = $this->view->getFileInfo(
  324. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '/files/Shared' . $this->subfolder
  325. . $this->subsubfolder . '/' . $this->filename);
  326. // check if we have fileInfos
  327. $this->assertTrue(is_array($fileInfo));
  328. // share the file with user3
  329. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER4, OCP\PERMISSION_ALL);
  330. // login as admin
  331. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  332. // check if share key for user3 exists
  333. $this->assertTrue($this->view->file_exists(
  334. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys' . $this->folder1
  335. . $this->subfolder . $this->subsubfolder . '/'
  336. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER4 . '.shareKey'));
  337. // login as user3
  338. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER4);
  339. // get file contents
  340. $retrievedCryptedFile = $this->view->file_get_contents(
  341. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER4 . '/files/Shared/' . $this->filename);
  342. // check if data is the same
  343. $this->assertEquals($this->dataShort, $retrievedCryptedFile);
  344. // cleanup
  345. if ($withTeardown) {
  346. // login as user2
  347. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3);
  348. // unshare the file with user3
  349. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER4);
  350. // check if share key not exists
  351. $this->assertFalse($this->view->file_exists(
  352. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys'
  353. . $this->folder1 . $this->subfolder . $this->subsubfolder . '/'
  354. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER4 . '.shareKey'));
  355. // login as user1
  356. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2);
  357. // unshare the folder with user2
  358. \OCP\Share::unshare('folder', $fileInfoSubFolder['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3);
  359. // check if share key not exists
  360. $this->assertFalse($this->view->file_exists(
  361. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys'
  362. . $this->folder1 . $this->subfolder . $this->subsubfolder . '/'
  363. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey'));
  364. // login as admin
  365. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  366. // unshare the folder1 with user1
  367. \OCP\Share::unshare('folder', $fileInfoFolder1['fileid'], \OCP\Share::SHARE_TYPE_USER, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2);
  368. // check if share key not exists
  369. $this->assertFalse($this->view->file_exists(
  370. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys'
  371. . $this->folder1 . $this->subfolder . $this->subsubfolder . '/'
  372. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey'));
  373. // cleanup
  374. $this->view->unlink(
  375. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1 . $this->subfolder
  376. . $this->subsubfolder . '/' . $this->filename);
  377. // check if share key not exists
  378. $this->assertFalse($this->view->file_exists(
  379. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys'
  380. . $this->folder1 . $this->subfolder . $this->subsubfolder . '/'
  381. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey'));
  382. }
  383. }
  384. function testPublicShareFile() {
  385. // login as admin
  386. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  387. // save file with content
  388. $cryptedFile = file_put_contents('crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort);
  389. // test that data was successfully written
  390. $this->assertTrue(is_int($cryptedFile));
  391. // disable encryption proxy to prevent recursive calls
  392. $proxyStatus = \OC_FileProxy::$enabled;
  393. \OC_FileProxy::$enabled = false;
  394. // get the file info from previous created file
  395. $fileInfo = $this->view->getFileInfo(
  396. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
  397. // check if we have a valid file info
  398. $this->assertTrue(is_array($fileInfo));
  399. // check if the unencrypted file size is stored
  400. $this->assertGreaterThan(0, $fileInfo['unencrypted_size']);
  401. // re-enable the file proxy
  402. \OC_FileProxy::$enabled = $proxyStatus;
  403. // share the file
  404. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, false, OCP\PERMISSION_ALL);
  405. // login as admin
  406. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  407. $publicShareKeyId = \OC_Appconfig::getValue('files_encryption', 'publicShareKeyId');
  408. // check if share key for public exists
  409. $this->assertTrue($this->view->file_exists(
  410. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  411. . $this->filename . '.' . $publicShareKeyId . '.shareKey'));
  412. // some hacking to simulate public link
  413. $GLOBALS['app'] = 'files_sharing';
  414. $GLOBALS['fileOwner'] = \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1;
  415. \OC_User::setUserId(false);
  416. // get file contents
  417. $retrievedCryptedFile = file_get_contents('crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
  418. // check if data is the same as we previously written
  419. $this->assertEquals($this->dataShort, $retrievedCryptedFile);
  420. // tear down
  421. // login as admin
  422. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  423. // unshare the file
  424. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_LINK, null);
  425. // check if share key not exists
  426. $this->assertFalse($this->view->file_exists(
  427. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  428. . $this->filename . '.' . $publicShareKeyId . '.shareKey'));
  429. // cleanup
  430. $this->view->unlink('/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
  431. // check if share key not exists
  432. $this->assertFalse($this->view->file_exists(
  433. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  434. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey'));
  435. }
  436. /**
  437. * @medium
  438. */
  439. function testShareFileWithGroup() {
  440. // login as admin
  441. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  442. // save file with content
  443. $cryptedFile = file_put_contents('crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort);
  444. // test that data was successfully written
  445. $this->assertTrue(is_int($cryptedFile));
  446. // disable encryption proxy to prevent recursive calls
  447. $proxyStatus = \OC_FileProxy::$enabled;
  448. \OC_FileProxy::$enabled = false;
  449. // get the file info from previous created file
  450. $fileInfo = $this->view->getFileInfo(
  451. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
  452. // check if we have a valid file info
  453. $this->assertTrue(is_array($fileInfo));
  454. // check if the unencrypted file size is stored
  455. $this->assertGreaterThan(0, $fileInfo['unencrypted_size']);
  456. // re-enable the file proxy
  457. \OC_FileProxy::$enabled = $proxyStatus;
  458. // share the file
  459. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_GROUP1, OCP\PERMISSION_ALL);
  460. // login as admin
  461. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  462. // check if share key for user2 and user3 exists
  463. $this->assertTrue($this->view->file_exists(
  464. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  465. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey'));
  466. $this->assertTrue($this->view->file_exists(
  467. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  468. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER4 . '.shareKey'));
  469. // login as user1
  470. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3);
  471. // get file contents
  472. $retrievedCryptedFile = $this->view->file_get_contents(
  473. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '/files/Shared/' . $this->filename);
  474. // check if data is the same as we previously written
  475. $this->assertEquals($this->dataShort, $retrievedCryptedFile);
  476. // login as admin
  477. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  478. // unshare the file
  479. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_GROUP1);
  480. // check if share key not exists
  481. $this->assertFalse($this->view->file_exists(
  482. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  483. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey'));
  484. $this->assertFalse($this->view->file_exists(
  485. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  486. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER4 . '.shareKey'));
  487. // cleanup
  488. $this->view->unlink('/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
  489. // check if share key not exists
  490. $this->assertFalse($this->view->file_exists(
  491. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  492. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey'));
  493. }
  494. /**
  495. * @large
  496. */
  497. function testRecoveryFile() {
  498. $this->markTestIncomplete(
  499. 'No idea what\'s wrong here, this works perfectly in real-world. removeRecoveryKeys(\'/\') L709 removes correctly the keys, but for some reasons afterwards also the top-level folder "share-keys" is gone...'
  500. );
  501. // login as admin
  502. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  503. \OCA\Encryption\Helper::adminEnableRecovery(null, 'test123');
  504. $recoveryKeyId = OC_Appconfig::getValue('files_encryption', 'recoveryKeyId');
  505. // login as admin
  506. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  507. $util = new \OCA\Encryption\Util(new \OC_FilesystemView('/'), \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  508. // check if recovery password match
  509. $this->assertTrue($util->checkRecoveryPassword('test123'));
  510. // enable recovery for admin
  511. $this->assertTrue($util->setRecoveryForUser(1));
  512. // create folder structure
  513. $this->view->mkdir('/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1);
  514. $this->view->mkdir(
  515. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1 . $this->subfolder);
  516. $this->view->mkdir(
  517. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files' . $this->folder1 . $this->subfolder
  518. . $this->subsubfolder);
  519. // save file with content
  520. $cryptedFile1 = file_put_contents('crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort);
  521. $cryptedFile2 = file_put_contents('crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/'
  522. . $this->filename, $this->dataShort);
  523. // test that data was successfully written
  524. $this->assertTrue(is_int($cryptedFile1));
  525. $this->assertTrue(is_int($cryptedFile2));
  526. // check if share key for admin and recovery exists
  527. $this->assertTrue($this->view->file_exists(
  528. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  529. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey'));
  530. $this->assertTrue($this->view->file_exists(
  531. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  532. . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
  533. $this->assertTrue($this->view->file_exists(
  534. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/' . $this->folder1
  535. . $this->subfolder . $this->subsubfolder . '/'
  536. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '.shareKey'));
  537. $this->assertTrue($this->view->file_exists(
  538. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/' . $this->folder1
  539. . $this->subfolder . $this->subsubfolder . '/'
  540. . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
  541. // disable recovery for admin
  542. $this->assertTrue($util->setRecoveryForUser(0));
  543. // remove all recovery keys
  544. $util->removeRecoveryKeys('/');
  545. // check if share key for recovery not exists
  546. $this->assertFalse($this->view->file_exists(
  547. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  548. . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
  549. $this->assertFalse($this->view->file_exists(
  550. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/' . $this->folder1
  551. . $this->subfolder . $this->subsubfolder . '/'
  552. . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
  553. // enable recovery for admin
  554. $this->assertTrue($util->setRecoveryForUser(1));
  555. // add recovery keys again
  556. $util->addRecoveryKeys('/');
  557. // check if share key for admin and recovery exists
  558. $this->assertTrue($this->view->file_exists(
  559. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  560. . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
  561. $this->assertTrue($this->view->file_exists(
  562. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/' . $this->folder1
  563. . $this->subfolder . $this->subsubfolder . '/'
  564. . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
  565. // cleanup
  566. $this->view->unlink('/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
  567. $this->view->unlink('/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->folder1);
  568. // check if share key for recovery not exists
  569. $this->assertFalse($this->view->file_exists(
  570. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  571. . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
  572. $this->assertFalse($this->view->file_exists(
  573. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/' . $this->folder1
  574. . $this->subfolder . $this->subsubfolder . '/'
  575. . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
  576. $this->assertTrue(\OCA\Encryption\Helper::adminEnableRecovery(null, 'test123'));
  577. $this->assertTrue(\OCA\Encryption\Helper::adminDisableRecovery('test123'));
  578. $this->assertEquals(0, \OC_Appconfig::getValue('files_encryption', 'recoveryAdminEnabled'));
  579. }
  580. /**
  581. * @large
  582. */
  583. function testRecoveryForUser() {
  584. $this->markTestIncomplete(
  585. 'This test drives Jenkins crazy - "Cannot modify header information - headers already sent" - line 811'
  586. );
  587. // login as admin
  588. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  589. \OCA\Encryption\Helper::adminEnableRecovery(null, 'test123');
  590. $recoveryKeyId = OC_Appconfig::getValue('files_encryption', 'recoveryKeyId');
  591. // login as user2
  592. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2);
  593. $util = new \OCA\Encryption\Util(new \OC_FilesystemView('/'), \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2);
  594. // enable recovery for admin
  595. $this->assertTrue($util->setRecoveryForUser(1));
  596. // create folder structure
  597. $this->view->mkdir('/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files' . $this->folder1);
  598. $this->view->mkdir(
  599. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files' . $this->folder1 . $this->subfolder);
  600. $this->view->mkdir(
  601. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files' . $this->folder1 . $this->subfolder
  602. . $this->subsubfolder);
  603. // save file with content
  604. $cryptedFile1 = file_put_contents('crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2. '/files/' . $this->filename, $this->dataShort);
  605. $cryptedFile2 = file_put_contents('crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/'
  606. . $this->filename, $this->dataShort);
  607. // test that data was successfully written
  608. $this->assertTrue(is_int($cryptedFile1));
  609. $this->assertTrue(is_int($cryptedFile2));
  610. // check if share key for user and recovery exists
  611. $this->assertTrue($this->view->file_exists(
  612. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/share-keys/'
  613. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey'));
  614. $this->assertTrue($this->view->file_exists(
  615. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/share-keys/'
  616. . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
  617. $this->assertTrue($this->view->file_exists(
  618. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/share-keys/' . $this->folder1
  619. . $this->subfolder . $this->subsubfolder . '/'
  620. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey'));
  621. $this->assertTrue($this->view->file_exists(
  622. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/share-keys/' . $this->folder1
  623. . $this->subfolder . $this->subsubfolder . '/'
  624. . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
  625. // login as admin
  626. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  627. // change password
  628. \OC_User::setPassword(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2, 'test', 'test123');
  629. // login as user2
  630. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2, false, 'test');
  631. // get file contents
  632. $retrievedCryptedFile1 = file_get_contents('crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files/' . $this->filename);
  633. $retrievedCryptedFile2 = file_get_contents(
  634. 'crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename);
  635. // check if data is the same as we previously written
  636. $this->assertEquals($this->dataShort, $retrievedCryptedFile1);
  637. $this->assertEquals($this->dataShort, $retrievedCryptedFile2);
  638. // cleanup
  639. $this->view->unlink('/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files' . $this->folder1);
  640. $this->view->unlink('/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files' . $this->filename);
  641. // check if share key for user and recovery exists
  642. $this->assertFalse($this->view->file_exists(
  643. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/share-keys/'
  644. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey'));
  645. $this->assertFalse($this->view->file_exists(
  646. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/share-keys/'
  647. . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
  648. $this->assertFalse($this->view->file_exists(
  649. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/share-keys/' . $this->folder1
  650. . $this->subfolder . $this->subsubfolder . '/'
  651. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '.shareKey'));
  652. $this->assertFalse($this->view->file_exists(
  653. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER2 . '/files_encryption/share-keys/' . $this->folder1
  654. . $this->subfolder . $this->subsubfolder . '/'
  655. . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
  656. // enable recovery for admin
  657. $this->assertTrue($util->setRecoveryForUser(0));
  658. \OCA\Encryption\Helper::adminDisableRecovery('test123');
  659. $this->assertEquals(0, \OC_Appconfig::getValue('files_encryption', 'recoveryAdminEnabled'));
  660. }
  661. /**
  662. * @medium
  663. */
  664. function testFailShareFile() {
  665. // login as admin
  666. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  667. // save file with content
  668. $cryptedFile = file_put_contents('crypt:///' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename, $this->dataShort);
  669. // test that data was successfully written
  670. $this->assertTrue(is_int($cryptedFile));
  671. // disable encryption proxy to prevent recursive calls
  672. $proxyStatus = \OC_FileProxy::$enabled;
  673. \OC_FileProxy::$enabled = false;
  674. // get the file info from previous created file
  675. $fileInfo = $this->view->getFileInfo(
  676. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
  677. // check if we have a valid file info
  678. $this->assertTrue(is_array($fileInfo));
  679. // check if the unencrypted file size is stored
  680. $this->assertGreaterThan(0, $fileInfo['unencrypted_size']);
  681. // break users public key
  682. $this->view->rename('/public-keys/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '.public.key',
  683. '/public-keys/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '.public.key_backup');
  684. // re-enable the file proxy
  685. \OC_FileProxy::$enabled = $proxyStatus;
  686. // share the file
  687. try {
  688. \OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_GROUP1, OCP\PERMISSION_ALL);
  689. } catch (Exception $e) {
  690. $this->assertEquals(0, strpos($e->getMessage(), "Following users are not set up for encryption"));
  691. }
  692. // login as admin
  693. \Test_Encryption_Util::loginHelper(\Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1);
  694. // check if share key for user1 not exists
  695. $this->assertFalse($this->view->file_exists(
  696. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  697. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey'));
  698. // disable encryption proxy to prevent recursive calls
  699. $proxyStatus = \OC_FileProxy::$enabled;
  700. \OC_FileProxy::$enabled = false;
  701. // break user1 public key
  702. $this->view->rename(
  703. '/public-keys/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '.public.key_backup',
  704. '/public-keys/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '.public.key');
  705. // remove share file
  706. $this->view->unlink('/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  707. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3
  708. . '.shareKey');
  709. // re-enable the file proxy
  710. \OC_FileProxy::$enabled = $proxyStatus;
  711. // unshare the file with user1
  712. \OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_GROUP, \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_GROUP1);
  713. // check if share key not exists
  714. $this->assertFalse($this->view->file_exists(
  715. '/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files_encryption/share-keys/'
  716. . $this->filename . '.' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER3 . '.shareKey'));
  717. // cleanup
  718. $this->view->unlink('/' . \Test_Encryption_Share::TEST_ENCRYPTION_SHARE_USER1 . '/files/' . $this->filename);
  719. }
  720. }