1
0

trashbin.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Vincent Petry <pvince81@owncloud.com>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. use OCA\Files_Trashbin;
  26. /**
  27. * Class Test_Encryption
  28. */
  29. class Test_Trashbin extends \Test\TestCase {
  30. const TEST_TRASHBIN_USER1 = "test-trashbin-user1";
  31. const TEST_TRASHBIN_USER2 = "test-trashbin-user2";
  32. private $trashRoot1;
  33. private $trashRoot2;
  34. private static $rememberRetentionObligation;
  35. private static $rememberAutoExpire;
  36. /**
  37. * @var \OC\Files\View
  38. */
  39. private $rootView;
  40. public static function setUpBeforeClass() {
  41. parent::setUpBeforeClass();
  42. // reset backend
  43. \OC_User::clearBackends();
  44. \OC_User::useBackend('database');
  45. // clear share hooks
  46. \OC_Hook::clear('OCP\\Share');
  47. \OC::registerShareHooks();
  48. \OCP\Util::connectHook('OC_Filesystem', 'setup', '\OC\Files\Storage\Shared', 'setup');
  49. //disable encryption
  50. \OC_App::disable('files_encryption');
  51. //configure trashbin
  52. self::$rememberRetentionObligation = \OC_Config::getValue('trashbin_retention_obligation', Files_Trashbin\Trashbin::DEFAULT_RETENTION_OBLIGATION);
  53. \OC_Config::setValue('trashbin_retention_obligation', 2);
  54. self::$rememberAutoExpire = \OC_Config::getValue('trashbin_auto_expire', true);
  55. \OC_Config::setValue('trashbin_auto_expire', true);
  56. // register hooks
  57. Files_Trashbin\Trashbin::registerHooks();
  58. // create test user
  59. self::loginHelper(self::TEST_TRASHBIN_USER2, true);
  60. self::loginHelper(self::TEST_TRASHBIN_USER1, true);
  61. }
  62. public static function tearDownAfterClass() {
  63. // cleanup test user
  64. \OC_User::deleteUser(self::TEST_TRASHBIN_USER1);
  65. \OC_Config::setValue('trashbin_retention_obligation', self::$rememberRetentionObligation);
  66. \OC_Config::setValue('trashbin_auto_expire', self::$rememberAutoExpire);
  67. \OC_Hook::clear();
  68. \OC\Files\Filesystem::getLoader()->removeStorageWrapper('oc_trashbin');
  69. parent::tearDownAfterClass();
  70. }
  71. protected function setUp() {
  72. parent::setUp();
  73. $this->trashRoot1 = '/' . self::TEST_TRASHBIN_USER1 . '/files_trashbin';
  74. $this->trashRoot2 = '/' . self::TEST_TRASHBIN_USER2 . '/files_trashbin';
  75. $this->rootView = new \OC\Files\View();
  76. self::loginHelper(self::TEST_TRASHBIN_USER1);
  77. }
  78. protected function tearDown() {
  79. $this->rootView->deleteAll($this->trashRoot1);
  80. $this->rootView->deleteAll($this->trashRoot2);
  81. parent::tearDown();
  82. }
  83. /**
  84. * test expiration of files older then the max storage time defined for the trash
  85. */
  86. public function testExpireOldFiles() {
  87. $currentTime = time();
  88. $expireAt = $currentTime - 2*24*60*60;
  89. $expiredDate = $currentTime - 3*24*60*60;
  90. // create some files
  91. \OC\Files\Filesystem::file_put_contents('file1.txt', 'file1');
  92. \OC\Files\Filesystem::file_put_contents('file2.txt', 'file2');
  93. \OC\Files\Filesystem::file_put_contents('file3.txt', 'file3');
  94. // delete them so that they end up in the trash bin
  95. \OC\Files\Filesystem::unlink('file1.txt');
  96. \OC\Files\Filesystem::unlink('file2.txt');
  97. \OC\Files\Filesystem::unlink('file3.txt');
  98. //make sure that files are in the trash bin
  99. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name');
  100. $this->assertSame(3, count($filesInTrash));
  101. // every second file will get a date in the past so that it will get expired
  102. $manipulatedList = $this->manipulateDeleteTime($filesInTrash, $this->trashRoot1, $expiredDate);
  103. $testClass = new TrashbinForTesting();
  104. list($sizeOfDeletedFiles, $count) = $testClass->dummyDeleteExpiredFiles($manipulatedList, $expireAt);
  105. $this->assertSame(10, $sizeOfDeletedFiles);
  106. $this->assertSame(2, $count);
  107. // only file2.txt should be left
  108. $remainingFiles = array_slice($manipulatedList, $count);
  109. $this->assertSame(1, count($remainingFiles));
  110. $remainingFile = reset($remainingFiles);
  111. $this->assertSame('file2.txt', $remainingFile['name']);
  112. // check that file1.txt and file3.txt was really deleted
  113. $newTrashContent = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1);
  114. $this->assertSame(1, count($newTrashContent));
  115. $element = reset($newTrashContent);
  116. $this->assertSame('file2.txt', $element['name']);
  117. }
  118. /**
  119. * test expiration of files older then the max storage time defined for the trash
  120. * in this test we delete a shared file and check if both trash bins, the one from
  121. * the owner of the file and the one from the user who deleted the file get expired
  122. * correctly
  123. */
  124. public function testExpireOldFilesShared() {
  125. $currentTime = time();
  126. $folder = "trashTest-" . $currentTime . '/';
  127. $expiredDate = $currentTime - 3*24*60*60;
  128. // create some files
  129. \OC\Files\Filesystem::mkdir($folder);
  130. \OC\Files\Filesystem::file_put_contents($folder . 'user1-1.txt', 'file1');
  131. \OC\Files\Filesystem::file_put_contents($folder . 'user1-2.txt', 'file2');
  132. \OC\Files\Filesystem::file_put_contents($folder . 'user1-3.txt', 'file3');
  133. \OC\Files\Filesystem::file_put_contents($folder . 'user1-4.txt', 'file4');
  134. //share user1-4.txt with user2
  135. $fileInfo = \OC\Files\Filesystem::getFileInfo($folder);
  136. $result = \OCP\Share::shareItem('folder', $fileInfo->getId(), \OCP\Share::SHARE_TYPE_USER, self::TEST_TRASHBIN_USER2, 31);
  137. $this->assertTrue($result);
  138. // delete them so that they end up in the trash bin
  139. \OC\Files\Filesystem::unlink($folder . 'user1-1.txt');
  140. \OC\Files\Filesystem::unlink($folder . 'user1-2.txt');
  141. \OC\Files\Filesystem::unlink($folder . 'user1-3.txt');
  142. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'name');
  143. $this->assertSame(3, count($filesInTrash));
  144. // every second file will get a date in the past so that it will get expired
  145. $this->manipulateDeleteTime($filesInTrash, $this->trashRoot1, $expiredDate);
  146. // login as user2
  147. self::loginHelper(self::TEST_TRASHBIN_USER2);
  148. $this->assertTrue(\OC\Files\Filesystem::file_exists($folder . "user1-4.txt"));
  149. // create some files
  150. \OC\Files\Filesystem::file_put_contents('user2-1.txt', 'file1');
  151. \OC\Files\Filesystem::file_put_contents('user2-2.txt', 'file2');
  152. // delete them so that they end up in the trash bin
  153. \OC\Files\Filesystem::unlink('user2-1.txt');
  154. \OC\Files\Filesystem::unlink('user2-2.txt');
  155. $filesInTrashUser2 = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2, 'name');
  156. $this->assertSame(2, count($filesInTrashUser2));
  157. // every second file will get a date in the past so that it will get expired
  158. $this->manipulateDeleteTime($filesInTrashUser2, $this->trashRoot2, $expiredDate);
  159. \OC\Files\Filesystem::unlink($folder . 'user1-4.txt');
  160. $this->runCommands();
  161. $filesInTrashUser2AfterDelete = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER2);
  162. // user2-1.txt should have been expired
  163. $this->verifyArray($filesInTrashUser2AfterDelete, array('user2-2.txt', 'user1-4.txt'));
  164. // user1-1.txt and user1-3.txt should have been expired
  165. $filesInTrashUser1AfterDelete = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1);
  166. $this->verifyArray($filesInTrashUser1AfterDelete, array('user1-2.txt', 'user1-4.txt'));
  167. }
  168. /**
  169. * verify that the array contains the expected results
  170. * @param array $result
  171. * @param array $expected
  172. */
  173. private function verifyArray($result, $expected) {
  174. $this->assertSame(count($expected), count($result));
  175. foreach ($expected as $expectedFile) {
  176. $found = false;
  177. foreach ($result as $fileInTrash) {
  178. if ($expectedFile === $fileInTrash['name']) {
  179. $found = true;
  180. break;
  181. }
  182. }
  183. if (!$found) {
  184. // if we didn't found the expected file, something went wrong
  185. $this->assertTrue(false, "can't find expected file '" . $expectedFile . "' in trash bin");
  186. }
  187. }
  188. }
  189. private function manipulateDeleteTime($files, $trashRoot, $expireDate) {
  190. $counter = 0;
  191. foreach ($files as &$file) {
  192. // modify every second file
  193. $counter = ($counter + 1) % 2;
  194. if ($counter === 1) {
  195. $source = $trashRoot . '/files/' . $file['name'].'.d'.$file['mtime'];
  196. $target = \OC\Files\Filesystem::normalizePath($trashRoot . '/files/' . $file['name'] . '.d' . $expireDate);
  197. $this->rootView->rename($source, $target);
  198. $file['mtime'] = $expireDate;
  199. }
  200. }
  201. return \OCA\Files\Helper::sortFiles($files, 'mtime');
  202. }
  203. /**
  204. * test expiration of old files in the trash bin until the max size
  205. * of the trash bin is met again
  206. */
  207. public function testExpireOldFilesUtilLimitsAreMet() {
  208. // create some files
  209. \OC\Files\Filesystem::file_put_contents('file1.txt', 'file1');
  210. \OC\Files\Filesystem::file_put_contents('file2.txt', 'file2');
  211. \OC\Files\Filesystem::file_put_contents('file3.txt', 'file3');
  212. // delete them so that they end up in the trash bin
  213. \OC\Files\Filesystem::unlink('file3.txt');
  214. sleep(1); // make sure that every file has a unique mtime
  215. \OC\Files\Filesystem::unlink('file2.txt');
  216. sleep(1); // make sure that every file has a unique mtime
  217. \OC\Files\Filesystem::unlink('file1.txt');
  218. //make sure that files are in the trash bin
  219. $filesInTrash = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1, 'mtime');
  220. $this->assertSame(3, count($filesInTrash));
  221. $testClass = new TrashbinForTesting();
  222. $sizeOfDeletedFiles = $testClass->dummyDeleteFiles($filesInTrash, -8);
  223. // the two oldest files (file3.txt and file2.txt) should be deleted
  224. $this->assertSame(10, $sizeOfDeletedFiles);
  225. $newTrashContent = OCA\Files_Trashbin\Helper::getTrashFiles('/', self::TEST_TRASHBIN_USER1);
  226. $this->assertSame(1, count($newTrashContent));
  227. $element = reset($newTrashContent);
  228. $this->assertSame('file1.txt', $element['name']);
  229. }
  230. /**
  231. * @param string $user
  232. * @param bool $create
  233. * @param bool $password
  234. */
  235. public static function loginHelper($user, $create = false) {
  236. if ($create) {
  237. try {
  238. \OC_User::createUser($user, $user);
  239. } catch(\Exception $e) { // catch username is already being used from previous aborted runs
  240. }
  241. }
  242. $storage = new \ReflectionClass('\OC\Files\Storage\Shared');
  243. $isInitialized = $storage->getProperty('isInitialized');
  244. $isInitialized->setAccessible(true);
  245. $isInitialized->setValue(array());
  246. $isInitialized->setAccessible(false);
  247. \OC_Util::tearDownFS();
  248. \OC_User::setUserId('');
  249. \OC\Files\Filesystem::tearDown();
  250. \OC_User::setUserId($user);
  251. \OC_Util::setupFS($user);
  252. }
  253. }
  254. // just a dummy class to make protected methods available for testing
  255. class TrashbinForTesting extends Files_Trashbin\Trashbin {
  256. public function dummyDeleteExpiredFiles($files, $limit) {
  257. // dummy value for $retention_obligation because it is not needed here
  258. return parent::deleteExpiredFiles($files, \Test_Trashbin::TEST_TRASHBIN_USER1, $limit, 0);
  259. }
  260. public function dummyDeleteFiles($files, $availableSpace) {
  261. return parent::deleteFiles($files, \Test_Trashbin::TEST_TRASHBIN_USER1, $availableSpace);
  262. }
  263. }