trashbin.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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__ . '/../../../lib/base.php';
  23. require_once __DIR__ . '/../lib/crypt.php';
  24. require_once __DIR__ . '/../lib/keymanager.php';
  25. require_once __DIR__ . '/../lib/proxy.php';
  26. require_once __DIR__ . '/../lib/stream.php';
  27. require_once __DIR__ . '/../lib/util.php';
  28. require_once __DIR__ . '/../appinfo/app.php';
  29. require_once __DIR__ . '/../../files_trashbin/appinfo/app.php';
  30. require_once __DIR__ . '/util.php';
  31. use OCA\Encryption;
  32. /**
  33. * Class Test_Encryption_Trashbin
  34. * @brief this class provide basic trashbin app tests
  35. */
  36. class Test_Encryption_Trashbin extends \PHPUnit_Framework_TestCase {
  37. const TEST_ENCRYPTION_TRASHBIN_USER1 = "test-trashbin-user1";
  38. public $userId;
  39. public $pass;
  40. /**
  41. * @var \OC_FilesystemView
  42. */
  43. public $view;
  44. public $dataShort;
  45. public $stateFilesTrashbin;
  46. public $folder1;
  47. public $subfolder;
  48. public $subsubfolder;
  49. public static function setUpBeforeClass() {
  50. // reset backend
  51. \OC_User::clearBackends();
  52. \OC_User::useBackend('database');
  53. \OC_Hook::clear('OC_Filesystem');
  54. \OC_Hook::clear('OC_User');
  55. // trashbin hooks
  56. \OCA\Files_Trashbin\Trashbin::registerHooks();
  57. // Filesystem related hooks
  58. \OCA\Encryption\Helper::registerFilesystemHooks();
  59. // clear and register hooks
  60. \OC_FileProxy::clearProxies();
  61. \OC_FileProxy::register(new OCA\Encryption\Proxy());
  62. // create test user
  63. \Test_Encryption_Util::loginHelper(\Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1, true);
  64. }
  65. function setUp() {
  66. // set user id
  67. \OC_User::setUserId(\Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1);
  68. $this->userId = \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1;
  69. $this->pass = \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1;
  70. // init filesystem view
  71. $this->view = new \OC_FilesystemView('/');
  72. // init short data
  73. $this->dataShort = 'hats';
  74. $this->folder1 = '/folder1';
  75. $this->subfolder = '/subfolder1';
  76. $this->subsubfolder = '/subsubfolder1';
  77. // remember files_trashbin state
  78. $this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
  79. // we want to tests with app files_trashbin enabled
  80. \OC_App::enable('files_trashbin');
  81. }
  82. function tearDown() {
  83. // reset app files_trashbin
  84. if ($this->stateFilesTrashbin) {
  85. OC_App::enable('files_trashbin');
  86. }
  87. else {
  88. OC_App::disable('files_trashbin');
  89. }
  90. }
  91. public static function tearDownAfterClass() {
  92. // cleanup test user
  93. \OC_User::deleteUser(\Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1);
  94. }
  95. /**
  96. * @medium
  97. * @brief test delete file
  98. */
  99. function testDeleteFile() {
  100. // generate filename
  101. $filename = 'tmp-' . time() . '.txt';
  102. // save file with content
  103. $cryptedFile = file_put_contents('crypt:///' .\Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1. '/files/'. $filename, $this->dataShort);
  104. // test that data was successfully written
  105. $this->assertTrue(is_int($cryptedFile));
  106. // check if key for admin exists
  107. $this->assertTrue($this->view->file_exists(
  108. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keyfiles/' . $filename
  109. . '.key'));
  110. // check if share key for admin exists
  111. $this->assertTrue($this->view->file_exists(
  112. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/share-keys/'
  113. . $filename . '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
  114. // delete file
  115. \OC\FIles\Filesystem::unlink($filename);
  116. // check if file not exists
  117. $this->assertFalse($this->view->file_exists(
  118. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename));
  119. // check if key for admin not exists
  120. $this->assertFalse($this->view->file_exists(
  121. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keyfiles/' . $filename
  122. . '.key'));
  123. // check if share key for admin not exists
  124. $this->assertFalse($this->view->file_exists(
  125. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/share-keys/'
  126. . $filename . '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
  127. // get files
  128. $trashFiles = $this->view->getDirectoryContent(
  129. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/files/');
  130. $trashFileSuffix = null;
  131. // find created file with timestamp
  132. foreach ($trashFiles as $file) {
  133. if (strncmp($file['path'], $filename, strlen($filename))) {
  134. $path_parts = pathinfo($file['name']);
  135. $trashFileSuffix = $path_parts['extension'];
  136. }
  137. }
  138. // check if we found the file we created
  139. $this->assertNotNull($trashFileSuffix);
  140. // check if key for admin not exists
  141. $this->assertTrue($this->view->file_exists(
  142. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keyfiles/' . $filename
  143. . '.key.' . $trashFileSuffix));
  144. // check if share key for admin not exists
  145. $this->assertTrue($this->view->file_exists(
  146. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/share-keys/' . $filename
  147. . '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey.' . $trashFileSuffix));
  148. // return filename for next test
  149. return $filename . '.' . $trashFileSuffix;
  150. }
  151. /**
  152. * @medium
  153. * @brief test restore file
  154. *
  155. * @depends testDeleteFile
  156. */
  157. function testRestoreFile($filename) {
  158. // prepare file information
  159. $path_parts = pathinfo($filename);
  160. $trashFileSuffix = $path_parts['extension'];
  161. $timestamp = str_replace('d', '', $trashFileSuffix);
  162. $fileNameWithoutSuffix = str_replace('.' . $trashFileSuffix, '', $filename);
  163. // restore file
  164. $this->assertTrue(\OCA\Files_Trashbin\Trashbin::restore($filename, $fileNameWithoutSuffix, $timestamp));
  165. // check if file exists
  166. $this->assertTrue($this->view->file_exists(
  167. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $fileNameWithoutSuffix));
  168. // check if key for admin exists
  169. $this->assertTrue($this->view->file_exists(
  170. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keyfiles/'
  171. . $fileNameWithoutSuffix . '.key'));
  172. // check if share key for admin exists
  173. $this->assertTrue($this->view->file_exists(
  174. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/share-keys/'
  175. . $fileNameWithoutSuffix . '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
  176. }
  177. /**
  178. * @medium
  179. * @brief test delete file forever
  180. */
  181. function testPermanentDeleteFile() {
  182. // generate filename
  183. $filename = 'tmp-' . time() . '.txt';
  184. // save file with content
  185. $cryptedFile = file_put_contents('crypt:///' .$this->userId. '/files/' . $filename, $this->dataShort);
  186. // test that data was successfully written
  187. $this->assertTrue(is_int($cryptedFile));
  188. // check if key for admin exists
  189. $this->assertTrue($this->view->file_exists(
  190. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keyfiles/' . $filename
  191. . '.key'));
  192. // check if share key for admin exists
  193. $this->assertTrue($this->view->file_exists(
  194. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/share-keys/'
  195. . $filename . '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
  196. // delete file
  197. \OC\FIles\Filesystem::unlink($filename);
  198. // check if file not exists
  199. $this->assertFalse($this->view->file_exists(
  200. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files/' . $filename));
  201. // check if key for admin not exists
  202. $this->assertFalse($this->view->file_exists(
  203. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/keyfiles/' . $filename
  204. . '.key'));
  205. // check if share key for admin not exists
  206. $this->assertFalse($this->view->file_exists(
  207. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_encryption/share-keys/'
  208. . $filename . '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey'));
  209. // find created file with timestamp
  210. $query = \OC_DB::prepare('SELECT `timestamp`,`type` FROM `*PREFIX*files_trash`'
  211. . ' WHERE `id`=?');
  212. $result = $query->execute(array($filename))->fetchRow();
  213. $this->assertTrue(is_array($result));
  214. // build suffix
  215. $trashFileSuffix = 'd' . $result['timestamp'];
  216. // check if key for admin exists
  217. $this->assertTrue($this->view->file_exists(
  218. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keyfiles/' . $filename
  219. . '.key.' . $trashFileSuffix));
  220. // check if share key for admin exists
  221. $this->assertTrue($this->view->file_exists(
  222. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/share-keys/' . $filename
  223. . '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey.' . $trashFileSuffix));
  224. // get timestamp from file
  225. $timestamp = str_replace('d', '', $trashFileSuffix);
  226. // delete file forever
  227. $this->assertGreaterThan(0, \OCA\Files_Trashbin\Trashbin::delete($filename, $timestamp));
  228. // check if key for admin not exists
  229. $this->assertFalse($this->view->file_exists(
  230. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/files/' . $filename . '.'
  231. . $trashFileSuffix));
  232. // check if key for admin not exists
  233. $this->assertFalse($this->view->file_exists(
  234. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/keyfiles/' . $filename
  235. . '.key.' . $trashFileSuffix));
  236. // check if share key for admin not exists
  237. $this->assertFalse($this->view->file_exists(
  238. '/' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '/files_trashbin/share-keys/' . $filename
  239. . '.' . \Test_Encryption_Trashbin::TEST_ENCRYPTION_TRASHBIN_USER1 . '.shareKey.' . $trashFileSuffix));
  240. }
  241. }