1
0

SharedMountTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <vincent@nextcloud.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\Memcache\ArrayCache;
  32. use OCA\Files_Sharing\MountProvider;
  33. use OCP\ICacheFactory;
  34. use OCP\IGroupManager;
  35. use OCP\IUserManager;
  36. use OCP\Share\IShare;
  37. /**
  38. * Class SharedMountTest
  39. *
  40. * @group SLOWDB
  41. */
  42. class SharedMountTest extends TestCase {
  43. /** @var IGroupManager */
  44. private $groupManager;
  45. /** @var IUserManager */
  46. private $userManager;
  47. private $folder2;
  48. protected function setUp(): void {
  49. parent::setUp();
  50. $this->folder = '/folder_share_storage_test';
  51. $this->folder2 = '/folder_share_storage_test2';
  52. $this->filename = '/share-api-storage.txt';
  53. $this->view->mkdir($this->folder);
  54. $this->view->mkdir($this->folder2);
  55. // save file with content
  56. $this->view->file_put_contents($this->filename, 'root file');
  57. $this->view->file_put_contents($this->folder . $this->filename, 'file in subfolder');
  58. $this->view->file_put_contents($this->folder2 . $this->filename, 'file in subfolder2');
  59. $this->groupManager = \OC::$server->getGroupManager();
  60. $this->userManager = \OC::$server->getUserManager();
  61. }
  62. protected function tearDown(): void {
  63. if ($this->view) {
  64. if ($this->view->file_exists($this->folder)) {
  65. $this->view->unlink($this->folder);
  66. }
  67. if ($this->view->file_exists($this->filename)) {
  68. $this->view->unlink($this->filename);
  69. }
  70. }
  71. parent::tearDown();
  72. }
  73. /**
  74. * test if the mount point moves up if the parent folder no longer exists
  75. */
  76. public function testShareMountLoseParentFolder() {
  77. // share to user
  78. $share = $this->share(
  79. IShare::TYPE_USER,
  80. $this->folder,
  81. self::TEST_FILES_SHARING_API_USER1,
  82. self::TEST_FILES_SHARING_API_USER2,
  83. \OCP\Constants::PERMISSION_ALL);
  84. $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2);
  85. $share->setTarget('/foo/bar' . $this->folder);
  86. $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);
  87. $share = $this->shareManager->getShareById($share->getFullId());
  88. $this->assertSame('/foo/bar' . $this->folder, $share->getTarget());
  89. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  90. // share should have moved up
  91. $share = $this->shareManager->getShareById($share->getFullId());
  92. $this->assertSame($this->folder, $share->getTarget());
  93. //cleanup
  94. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  95. $this->shareManager->deleteShare($share);
  96. $this->view->unlink($this->folder);
  97. }
  98. /**
  99. * @medium
  100. */
  101. public function testDeleteParentOfMountPoint() {
  102. // share to user
  103. $share = $this->share(
  104. IShare::TYPE_USER,
  105. $this->folder,
  106. self::TEST_FILES_SHARING_API_USER1,
  107. self::TEST_FILES_SHARING_API_USER2,
  108. \OCP\Constants::PERMISSION_ALL
  109. );
  110. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  111. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  112. $this->assertTrue($user2View->file_exists($this->folder));
  113. // create a local folder
  114. $result = $user2View->mkdir('localfolder');
  115. $this->assertTrue($result);
  116. // move mount point to local folder
  117. $result = $user2View->rename($this->folder, '/localfolder/' . $this->folder);
  118. $this->assertTrue($result);
  119. // mount point in the root folder should no longer exist
  120. $this->assertFalse($user2View->is_dir($this->folder));
  121. // delete the local folder
  122. $result = $user2View->unlink('/localfolder');
  123. $this->assertTrue($result);
  124. //enforce reload of the mount points
  125. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  126. //mount point should be back at the root
  127. $this->assertTrue($user2View->is_dir($this->folder));
  128. //cleanup
  129. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  130. $this->view->unlink($this->folder);
  131. }
  132. public function testMoveSharedFile() {
  133. $share = $this->share(
  134. IShare::TYPE_USER,
  135. $this->filename,
  136. self::TEST_FILES_SHARING_API_USER1,
  137. self::TEST_FILES_SHARING_API_USER2,
  138. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  139. );
  140. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  141. \OC\Files\Filesystem::rename($this->filename, $this->filename . '_renamed');
  142. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  143. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  144. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  145. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  146. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  147. // rename back to original name
  148. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  149. \OC\Files\Filesystem::rename($this->filename . '_renamed', $this->filename);
  150. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  151. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  152. //cleanup
  153. $this->shareManager->deleteShare($share);
  154. }
  155. /**
  156. * share file with a group if a user renames the file the filename should not change
  157. * for the other users
  158. */
  159. public function testMoveGroupShare() {
  160. $testGroup = $this->groupManager->createGroup('testGroup');
  161. $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1);
  162. $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2);
  163. $user3 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER3);
  164. $testGroup->addUser($user1);
  165. $testGroup->addUser($user2);
  166. $testGroup->addUser($user3);
  167. $fileinfo = $this->view->getFileInfo($this->filename);
  168. $share = $this->share(
  169. IShare::TYPE_GROUP,
  170. $this->filename,
  171. self::TEST_FILES_SHARING_API_USER1,
  172. 'testGroup',
  173. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  174. );
  175. $this->shareManager->acceptShare($share, $user1->getUID());
  176. $this->shareManager->acceptShare($share, $user2->getUID());
  177. $this->shareManager->acceptShare($share, $user3->getUID());
  178. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  179. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  180. \OC\Files\Filesystem::rename($this->filename, 'newFileName');
  181. $this->assertTrue(\OC\Files\Filesystem::file_exists('newFileName'));
  182. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  183. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  184. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  185. $this->assertFalse(\OC\Files\Filesystem::file_exists('newFileName'));
  186. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  187. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  188. $this->assertFalse(\OC\Files\Filesystem::file_exists('newFileName'));
  189. //cleanup
  190. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  191. $this->shareManager->deleteShare($share);
  192. $testGroup->removeUser($user1);
  193. $testGroup->removeUser($user2);
  194. $testGroup->removeUser($user3);
  195. }
  196. /**
  197. * @dataProvider dataProviderTestStripUserFilesPath
  198. * @param string $path
  199. * @param string $expectedResult
  200. * @param bool $exception if a exception is expected
  201. */
  202. public function testStripUserFilesPath($path, $expectedResult, $exception) {
  203. $testClass = new DummyTestClassSharedMount(null, null);
  204. try {
  205. $result = $testClass->stripUserFilesPathDummy($path);
  206. $this->assertSame($expectedResult, $result);
  207. } catch (\Exception $e) {
  208. if ($exception) {
  209. $this->assertSame(10, $e->getCode());
  210. } else {
  211. $this->assertTrue(false, 'Exception caught, but expected: ' . $expectedResult);
  212. }
  213. }
  214. }
  215. public function dataProviderTestStripUserFilesPath() {
  216. return [
  217. ['/user/files/foo.txt', '/foo.txt', false],
  218. ['/user/files/folder/foo.txt', '/folder/foo.txt', false],
  219. ['/data/user/files/foo.txt', null, true],
  220. ['/data/user/files/', null, true],
  221. ['/files/foo.txt', null, true],
  222. ['/foo.txt', null, true],
  223. ];
  224. }
  225. /**
  226. * If the permissions on a group share are upgraded be sure to still respect
  227. * removed shares by a member of that group
  228. */
  229. public function testPermissionUpgradeOnUserDeletedGroupShare() {
  230. $testGroup = $this->groupManager->createGroup('testGroup');
  231. $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1);
  232. $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2);
  233. $user3 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER3);
  234. $testGroup->addUser($user1);
  235. $testGroup->addUser($user2);
  236. $testGroup->addUser($user3);
  237. $connection = \OC::$server->getDatabaseConnection();
  238. // Share item with group
  239. $fileinfo = $this->view->getFileInfo($this->folder);
  240. $share = $this->share(
  241. IShare::TYPE_GROUP,
  242. $this->folder,
  243. self::TEST_FILES_SHARING_API_USER1,
  244. 'testGroup',
  245. \OCP\Constants::PERMISSION_READ
  246. );
  247. $this->shareManager->acceptShare($share, $user1->getUID());
  248. $this->shareManager->acceptShare($share, $user2->getUID());
  249. $this->shareManager->acceptShare($share, $user3->getUID());
  250. // Login as user 2 and verify the item exists
  251. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  252. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder));
  253. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  254. $this->assertNotEmpty($result);
  255. $this->assertEquals(\OCP\Constants::PERMISSION_READ, $result->getPermissions());
  256. // Delete the share
  257. $this->assertTrue(\OC\Files\Filesystem::rmdir($this->folder));
  258. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->folder));
  259. // Verify we do not get a share
  260. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  261. $this->assertEquals(0, $result->getPermissions());
  262. // Login as user 1 again and change permissions
  263. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  264. $share->setPermissions(\OCP\Constants::PERMISSION_ALL);
  265. $share = $this->shareManager->updateShare($share);
  266. // Login as user 2 and verify
  267. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  268. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->folder));
  269. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  270. $this->assertEquals(0, $result->getPermissions());
  271. $this->shareManager->deleteShare($share);
  272. //cleanup
  273. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  274. $testGroup->removeUser($user1);
  275. $testGroup->removeUser($user2);
  276. $testGroup->removeUser($user3);
  277. }
  278. /**
  279. * test if the mount point gets renamed if a folder exists at the target
  280. */
  281. public function testShareMountOverFolder() {
  282. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  283. $this->view2->mkdir('bar');
  284. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  285. // share to user
  286. $share = $this->share(
  287. IShare::TYPE_USER,
  288. $this->folder,
  289. self::TEST_FILES_SHARING_API_USER1,
  290. self::TEST_FILES_SHARING_API_USER2,
  291. \OCP\Constants::PERMISSION_ALL);
  292. $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2);
  293. $share->setTarget('/bar');
  294. $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);
  295. $share = $this->shareManager->getShareById($share->getFullId());
  296. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  297. // share should have been moved
  298. $share = $this->shareManager->getShareById($share->getFullId());
  299. $this->assertSame('/bar (2)', $share->getTarget());
  300. //cleanup
  301. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  302. $this->shareManager->deleteShare($share);
  303. $this->view->unlink($this->folder);
  304. }
  305. /**
  306. * test if the mount point gets renamed if another share exists at the target
  307. */
  308. public function testShareMountOverShare() {
  309. // create a shared cache
  310. $caches = [];
  311. $cacheFactory = $this->createMock(ICacheFactory::class);
  312. $cacheFactory->method('createLocal')
  313. ->willReturnCallback(function (string $prefix) use (&$caches) {
  314. if (!isset($caches[$prefix])) {
  315. $caches[$prefix] = new ArrayCache($prefix);
  316. }
  317. return $caches[$prefix];
  318. });
  319. $cacheFactory->method('createDistributed')
  320. ->willReturnCallback(function (string $prefix) use (&$caches) {
  321. if (!isset($caches[$prefix])) {
  322. $caches[$prefix] = new ArrayCache($prefix);
  323. }
  324. return $caches[$prefix];
  325. });
  326. // hack to overwrite the cache factory, we can't use the proper "overwriteService" since the mount provider is created before this test is called
  327. $mountProvider = \OCP\Server::get(MountProvider::class);
  328. $reflectionClass = new \ReflectionClass($mountProvider);
  329. $reflectionCacheFactory = $reflectionClass->getProperty("cacheFactory");
  330. $reflectionCacheFactory->setAccessible(true);
  331. $reflectionCacheFactory->setValue($mountProvider, $cacheFactory);
  332. // share to user
  333. $share = $this->share(
  334. IShare::TYPE_USER,
  335. $this->folder,
  336. self::TEST_FILES_SHARING_API_USER1,
  337. self::TEST_FILES_SHARING_API_USER2,
  338. \OCP\Constants::PERMISSION_ALL);
  339. $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2);
  340. $share->setTarget('/foobar');
  341. $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);
  342. // share to user
  343. $share2 = $this->share(
  344. IShare::TYPE_USER,
  345. $this->folder2,
  346. self::TEST_FILES_SHARING_API_USER1,
  347. self::TEST_FILES_SHARING_API_USER2,
  348. \OCP\Constants::PERMISSION_ALL);
  349. $this->shareManager->acceptShare($share2, self::TEST_FILES_SHARING_API_USER2);
  350. $share2->setTarget('/foobar');
  351. $this->shareManager->moveShare($share2, self::TEST_FILES_SHARING_API_USER2);
  352. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  353. // one of the shares should have been moved
  354. $share = $this->shareManager->getShareById($share->getFullId());
  355. $share2 = $this->shareManager->getShareById($share2->getFullId());
  356. // we don't know or care which share got the "(2)" just that one of them did
  357. $this->assertNotEquals($share->getTarget(), $share2->getTarget());
  358. $this->assertSame('/foobar', min($share->getTarget(), $share2->getTarget()));
  359. $this->assertSame('/foobar (2)', max($share->getTarget(), $share2->getTarget()));
  360. //cleanup
  361. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  362. $this->shareManager->deleteShare($share);
  363. $this->view->unlink($this->folder);
  364. }
  365. }
  366. class DummyTestClassSharedMount extends \OCA\Files_Sharing\SharedMount {
  367. public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
  368. // noop
  369. }
  370. public function stripUserFilesPathDummy($path) {
  371. return $this->stripUserFilesPath($path);
  372. }
  373. }