CacheTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Stefan Weil <sw@weilnetz.de>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <pvince81@owncloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\Files_Sharing\Tests;
  31. use OC\Files\Storage\Temporary;
  32. use OC\Files\Storage\Wrapper\Jail;
  33. use OCA\Files_Sharing\SharedStorage;
  34. use OCP\Share\IShare;
  35. /**
  36. * Class CacheTest
  37. *
  38. * @group DB
  39. */
  40. class CacheTest extends TestCase {
  41. /**
  42. * @var \OC\Files\View
  43. */
  44. public $user2View;
  45. /** @var \OC\Files\Cache\Cache */
  46. protected $ownerCache;
  47. /** @var \OC\Files\Cache\Cache */
  48. protected $sharedCache;
  49. /** @var \OC\Files\Storage\Storage */
  50. protected $ownerStorage;
  51. /** @var \OC\Files\Storage\Storage */
  52. protected $sharedStorage;
  53. /** @var \OCP\Share\IManager */
  54. protected $shareManager;
  55. protected function setUp(): void {
  56. parent::setUp();
  57. $this->shareManager = \OC::$server->getShareManager();
  58. $userManager = \OC::$server->getUserManager();
  59. $userManager->get(self::TEST_FILES_SHARING_API_USER1)->setDisplayName('User One');
  60. $userManager->get(self::TEST_FILES_SHARING_API_USER2)->setDisplayName('User Two');
  61. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  62. $this->user2View = new \OC\Files\View('/'. self::TEST_FILES_SHARING_API_USER2 . '/files');
  63. // prepare user1's dir structure
  64. $this->view->mkdir('container');
  65. $this->view->mkdir('container/shareddir');
  66. $this->view->mkdir('container/shareddir/subdir');
  67. $this->view->mkdir('container/shareddir/emptydir');
  68. $textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  69. $this->view->file_put_contents('container/not shared.txt', $textData);
  70. $this->view->file_put_contents('container/shared single file.txt', $textData);
  71. $this->view->file_put_contents('container/shareddir/bar.txt', $textData);
  72. $this->view->file_put_contents('container/shareddir/subdir/another.txt', $textData);
  73. $this->view->file_put_contents('container/shareddir/subdir/another too.txt', $textData);
  74. $this->view->file_put_contents('container/shareddir/subdir/not a text file.xml', '<xml></xml>');
  75. list($this->ownerStorage,) = $this->view->resolvePath('');
  76. $this->ownerCache = $this->ownerStorage->getCache();
  77. $this->ownerStorage->getScanner()->scan('');
  78. // share "shareddir" with user2
  79. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  80. $node = $rootFolder->get('container/shareddir');
  81. $share = $this->shareManager->newShare();
  82. $share->setNode($node)
  83. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  84. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  85. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  86. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  87. $share = $this->shareManager->createShare($share);
  88. $share->setStatus(IShare::STATUS_ACCEPTED);
  89. $this->shareManager->updateShare($share);
  90. $node = $rootFolder->get('container/shared single file.txt');
  91. $share = $this->shareManager->newShare();
  92. $share->setNode($node)
  93. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  94. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  95. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  96. ->setPermissions(\OCP\Constants::PERMISSION_ALL & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE));
  97. $share = $this->shareManager->createShare($share);
  98. $share->setStatus(IShare::STATUS_ACCEPTED);
  99. $this->shareManager->updateShare($share);
  100. // login as user2
  101. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  102. // retrieve the shared storage
  103. $secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
  104. list($this->sharedStorage,) = $secondView->resolvePath('files/shareddir');
  105. $this->sharedCache = $this->sharedStorage->getCache();
  106. }
  107. protected function tearDown(): void {
  108. if($this->sharedCache) {
  109. $this->sharedCache->clear();
  110. }
  111. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  112. $shares = $this->shareManager->getSharesBy(self::TEST_FILES_SHARING_API_USER1, \OCP\Share::SHARE_TYPE_USER);
  113. foreach ($shares as $share) {
  114. $this->shareManager->deleteShare($share);
  115. }
  116. $this->view->deleteAll('container');
  117. $this->ownerCache->clear();
  118. parent::tearDown();
  119. }
  120. function searchDataProvider() {
  121. return array(
  122. array('%another%',
  123. array(
  124. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  125. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  126. )
  127. ),
  128. array('%Another%',
  129. array(
  130. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  131. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  132. )
  133. ),
  134. array('%dir%',
  135. array(
  136. array('name' => 'emptydir', 'path' => 'emptydir'),
  137. array('name' => 'subdir', 'path' => 'subdir'),
  138. array('name' => 'shareddir', 'path' => ''),
  139. )
  140. ),
  141. array('%Dir%',
  142. array(
  143. array('name' => 'emptydir', 'path' => 'emptydir'),
  144. array('name' => 'subdir', 'path' => 'subdir'),
  145. array('name' => 'shareddir', 'path' => ''),
  146. )
  147. ),
  148. array('%txt%',
  149. array(
  150. array('name' => 'bar.txt', 'path' => 'bar.txt'),
  151. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  152. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  153. )
  154. ),
  155. array('%Txt%',
  156. array(
  157. array('name' => 'bar.txt', 'path' => 'bar.txt'),
  158. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  159. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  160. )
  161. ),
  162. array('%',
  163. array(
  164. array('name' => 'bar.txt', 'path' => 'bar.txt'),
  165. array('name' => 'emptydir', 'path' => 'emptydir'),
  166. array('name' => 'subdir', 'path' => 'subdir'),
  167. array('name' => 'another too.txt', 'path' => 'subdir/another too.txt'),
  168. array('name' => 'another.txt', 'path' => 'subdir/another.txt'),
  169. array('name' => 'not a text file.xml', 'path' => 'subdir/not a text file.xml'),
  170. array('name' => 'shareddir', 'path' => ''),
  171. )
  172. ),
  173. array('%nonexistent%',
  174. array(
  175. )
  176. ),
  177. );
  178. }
  179. /**
  180. * we cannot use a dataProvider because that would cause the stray hook detection to remove the hooks
  181. * that were added in setUpBeforeClass.
  182. */
  183. function testSearch() {
  184. foreach ($this->searchDataProvider() as $data) {
  185. list($pattern, $expectedFiles) = $data;
  186. $results = $this->sharedStorage->getCache()->search($pattern);
  187. $this->verifyFiles($expectedFiles, $results);
  188. }
  189. }
  190. /**
  191. * Test searching by mime type
  192. */
  193. function testSearchByMime() {
  194. $results = $this->sharedStorage->getCache()->searchByMime('text');
  195. $check = array(
  196. array(
  197. 'name' => 'bar.txt',
  198. 'path' => 'bar.txt'
  199. ),
  200. array(
  201. 'name' => 'another too.txt',
  202. 'path' => 'subdir/another too.txt'
  203. ),
  204. array(
  205. 'name' => 'another.txt',
  206. 'path' => 'subdir/another.txt'
  207. ),
  208. );
  209. $this->verifyFiles($check, $results);
  210. }
  211. function testGetFolderContentsInRoot() {
  212. $results = $this->user2View->getDirectoryContent('/');
  213. // we should get the shared items "shareddir" and "shared single file.txt"
  214. // additional root will always contain the example file "welcome.txt",
  215. // so this will be part of the result
  216. $this->verifyFiles(
  217. array(
  218. array(
  219. 'name' => 'welcome.txt',
  220. 'path' => 'files/welcome.txt',
  221. 'mimetype' => 'text/plain',
  222. ),
  223. array(
  224. 'name' => 'shareddir',
  225. 'path' => 'files/shareddir',
  226. 'mimetype' => 'httpd/unix-directory',
  227. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  228. 'displayname_owner' => 'User One',
  229. ),
  230. array(
  231. 'name' => 'shared single file.txt',
  232. 'path' => 'files/shared single file.txt',
  233. 'mimetype' => 'text/plain',
  234. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  235. 'displayname_owner' => 'User One',
  236. ),
  237. ),
  238. $results
  239. );
  240. }
  241. function testGetFolderContentsInSubdir() {
  242. $results = $this->user2View->getDirectoryContent('/shareddir');
  243. $this->verifyFiles(
  244. array(
  245. array(
  246. 'name' => 'bar.txt',
  247. 'path' => 'bar.txt',
  248. 'mimetype' => 'text/plain',
  249. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  250. 'displayname_owner' => 'User One',
  251. ),
  252. array(
  253. 'name' => 'emptydir',
  254. 'path' => 'emptydir',
  255. 'mimetype' => 'httpd/unix-directory',
  256. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  257. 'displayname_owner' => 'User One',
  258. ),
  259. array(
  260. 'name' => 'subdir',
  261. 'path' => 'subdir',
  262. 'mimetype' => 'httpd/unix-directory',
  263. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  264. 'displayname_owner' => 'User One',
  265. ),
  266. ),
  267. $results
  268. );
  269. }
  270. function testGetFolderContentsWhenSubSubdirShared() {
  271. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  272. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  273. $node = $rootFolder->get('container/shareddir/subdir');
  274. $share = $this->shareManager->newShare();
  275. $share->setNode($node)
  276. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  277. ->setSharedWith(self::TEST_FILES_SHARING_API_USER3)
  278. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  279. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  280. $share = $this->shareManager->createShare($share);
  281. $share->setStatus(IShare::STATUS_ACCEPTED);
  282. $this->shareManager->updateShare($share);
  283. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  284. $thirdView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER3 . '/files');
  285. $results = $thirdView->getDirectoryContent('/subdir');
  286. $this->verifyFiles(
  287. array(
  288. array(
  289. 'name' => 'another too.txt',
  290. 'path' => 'another too.txt',
  291. 'mimetype' => 'text/plain',
  292. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  293. 'displayname_owner' => 'User One',
  294. ),
  295. array(
  296. 'name' => 'another.txt',
  297. 'path' => 'another.txt',
  298. 'mimetype' => 'text/plain',
  299. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  300. 'displayname_owner' => 'User One',
  301. ),
  302. array(
  303. 'name' => 'not a text file.xml',
  304. 'path' => 'not a text file.xml',
  305. 'mimetype' => 'application/xml',
  306. 'uid_owner' => self::TEST_FILES_SHARING_API_USER1,
  307. 'displayname_owner' => 'User One',
  308. ),
  309. ),
  310. $results
  311. );
  312. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  313. $this->shareManager->deleteShare($share);
  314. }
  315. /**
  316. * Check if 'results' contains the expected 'examples' only.
  317. *
  318. * @param array $examples array of example files
  319. * @param array $results array of files
  320. */
  321. private function verifyFiles($examples, $results) {
  322. $this->assertEquals(count($examples), count($results));
  323. foreach ($examples as $example) {
  324. foreach ($results as $key => $result) {
  325. if ($result['name'] === $example['name']) {
  326. $this->verifyKeys($example, $result);
  327. unset($results[$key]);
  328. break;
  329. }
  330. }
  331. }
  332. $this->assertEquals(array(), $results);
  333. }
  334. /**
  335. * verify if each value from the result matches the expected result
  336. * @param array $example array with the expected results
  337. * @param array $result array with the results
  338. */
  339. private function verifyKeys($example, $result) {
  340. foreach ($example as $key => $value) {
  341. $this->assertEquals($value, $result[$key]);
  342. }
  343. }
  344. public function testGetPathByIdDirectShare() {
  345. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  346. \OC\Files\Filesystem::file_put_contents('test.txt', 'foo');
  347. $info = \OC\Files\Filesystem::getFileInfo('test.txt');
  348. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  349. $node = $rootFolder->get('test.txt');
  350. $share = $this->shareManager->newShare();
  351. $share->setNode($node)
  352. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  353. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  354. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  355. ->setPermissions(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE);
  356. $share = $this->shareManager->createShare($share);
  357. $share->setStatus(IShare::STATUS_ACCEPTED);
  358. $this->shareManager->updateShare($share);
  359. \OC_Util::tearDownFS();
  360. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  361. $this->assertTrue(\OC\Files\Filesystem::file_exists('/test.txt'));
  362. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/test.txt');
  363. /**
  364. * @var \OCA\Files_Sharing\SharedStorage $sharedStorage
  365. */
  366. $sharedCache = $sharedStorage->getCache();
  367. $this->assertEquals('', $sharedCache->getPathById($info->getId()));
  368. }
  369. public function testGetPathByIdShareSubFolder() {
  370. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  371. \OC\Files\Filesystem::mkdir('foo');
  372. \OC\Files\Filesystem::mkdir('foo/bar');
  373. \OC\Files\Filesystem::touch('foo/bar/test.txt');
  374. $folderInfo = \OC\Files\Filesystem::getFileInfo('foo');
  375. $fileInfo = \OC\Files\Filesystem::getFileInfo('foo/bar/test.txt');
  376. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  377. $node = $rootFolder->get('foo');
  378. $share = $this->shareManager->newShare();
  379. $share->setNode($node)
  380. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  381. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  382. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  383. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  384. $share = $this->shareManager->createShare($share);
  385. $share->setStatus(IShare::STATUS_ACCEPTED);
  386. $this->shareManager->updateShare($share);
  387. \OC_Util::tearDownFS();
  388. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  389. $this->assertTrue(\OC\Files\Filesystem::file_exists('/foo'));
  390. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo');
  391. /**
  392. * @var \OCA\Files_Sharing\SharedStorage $sharedStorage
  393. */
  394. $sharedCache = $sharedStorage->getCache();
  395. $this->assertEquals('', $sharedCache->getPathById($folderInfo->getId()));
  396. $this->assertEquals('bar/test.txt', $sharedCache->getPathById($fileInfo->getId()));
  397. }
  398. public function testNumericStorageId() {
  399. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  400. \OC\Files\Filesystem::mkdir('foo');
  401. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  402. $node = $rootFolder->get('foo');
  403. $share = $this->shareManager->newShare();
  404. $share->setNode($node)
  405. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  406. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  407. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  408. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  409. $share = $this->shareManager->createShare($share);
  410. $share->setStatus(IShare::STATUS_ACCEPTED);
  411. $this->shareManager->updateShare($share);
  412. \OC_Util::tearDownFS();
  413. list($sourceStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER1 . '/files/foo');
  414. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  415. $this->assertTrue(\OC\Files\Filesystem::file_exists('/foo'));
  416. /** @var SharedStorage $sharedStorage */
  417. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/foo');
  418. $this->assertEquals($sourceStorage->getCache()->getNumericStorageId(), $sharedStorage->getCache()->getNumericStorageId());
  419. }
  420. public function testShareJailedStorage() {
  421. $sourceStorage = new Temporary();
  422. $sourceStorage->mkdir('jail');
  423. $sourceStorage->mkdir('jail/sub');
  424. $sourceStorage->file_put_contents('jail/sub/foo.txt', 'foo');
  425. $jailedSource = new Jail([
  426. 'storage' => $sourceStorage,
  427. 'root' => 'jail'
  428. ]);
  429. $sourceStorage->getScanner()->scan('');
  430. $this->registerMount(self::TEST_FILES_SHARING_API_USER1, $jailedSource, '/' . self::TEST_FILES_SHARING_API_USER1 . '/files/foo');
  431. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  432. $rootFolder = \OC::$server->getUserFolder(self::TEST_FILES_SHARING_API_USER1);
  433. $node = $rootFolder->get('foo/sub');
  434. $share = $this->shareManager->newShare();
  435. $share->setNode($node)
  436. ->setShareType(\OCP\Share::SHARE_TYPE_USER)
  437. ->setSharedWith(self::TEST_FILES_SHARING_API_USER2)
  438. ->setSharedBy(self::TEST_FILES_SHARING_API_USER1)
  439. ->setPermissions(\OCP\Constants::PERMISSION_ALL);
  440. $share = $this->shareManager->createShare($share);
  441. $share->setStatus(IShare::STATUS_ACCEPTED);
  442. $this->shareManager->updateShare($share);
  443. \OC_Util::tearDownFS();
  444. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  445. $this->assertEquals('foo', \OC\Files\Filesystem::file_get_contents('/sub/foo.txt'));
  446. \OC\Files\Filesystem::file_put_contents('/sub/bar.txt', 'bar');
  447. /** @var SharedStorage $sharedStorage */
  448. list($sharedStorage) = \OC\Files\Filesystem::resolvePath('/' . self::TEST_FILES_SHARING_API_USER2 . '/files/sub');
  449. $this->assertTrue($sharedStorage->getCache()->inCache('bar.txt'));
  450. $this->assertTrue($sourceStorage->getCache()->inCache('jail/sub/bar.txt'));
  451. }
  452. }