SharedMountTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 <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 OCP\IGroupManager;
  32. use OCP\IUserManager;
  33. use OCP\Share\IShare;
  34. /**
  35. * Class SharedMountTest
  36. *
  37. * @group SLOWDB
  38. */
  39. class SharedMountTest extends TestCase {
  40. /** @var IGroupManager */
  41. private $groupManager;
  42. /** @var IUserManager */
  43. private $userManager;
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->folder = '/folder_share_storage_test';
  47. $this->filename = '/share-api-storage.txt';
  48. $this->view->mkdir($this->folder);
  49. // save file with content
  50. $this->view->file_put_contents($this->filename, 'root file');
  51. $this->view->file_put_contents($this->folder . $this->filename, 'file in subfolder');
  52. $this->groupManager = \OC::$server->getGroupManager();
  53. $this->userManager = \OC::$server->getUserManager();
  54. }
  55. protected function tearDown(): void {
  56. if ($this->view) {
  57. if ($this->view->file_exists($this->folder)) {
  58. $this->view->unlink($this->folder);
  59. }
  60. if ($this->view->file_exists($this->filename)) {
  61. $this->view->unlink($this->filename);
  62. }
  63. }
  64. parent::tearDown();
  65. }
  66. /**
  67. * test if the mount point moves up if the parent folder no longer exists
  68. */
  69. public function testShareMountLoseParentFolder() {
  70. // share to user
  71. $share = $this->share(
  72. IShare::TYPE_USER,
  73. $this->folder,
  74. self::TEST_FILES_SHARING_API_USER1,
  75. self::TEST_FILES_SHARING_API_USER2,
  76. \OCP\Constants::PERMISSION_ALL);
  77. $this->shareManager->acceptShare($share, self::TEST_FILES_SHARING_API_USER2);
  78. $share->setTarget('/foo/bar' . $this->folder);
  79. $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2);
  80. $share = $this->shareManager->getShareById($share->getFullId());
  81. $this->assertSame('/foo/bar' . $this->folder, $share->getTarget());
  82. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  83. // share should have moved up
  84. $share = $this->shareManager->getShareById($share->getFullId());
  85. $this->assertSame($this->folder, $share->getTarget());
  86. //cleanup
  87. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  88. $this->shareManager->deleteShare($share);
  89. $this->view->unlink($this->folder);
  90. }
  91. /**
  92. * @medium
  93. */
  94. public function testDeleteParentOfMountPoint() {
  95. // share to user
  96. $share = $this->share(
  97. IShare::TYPE_USER,
  98. $this->folder,
  99. self::TEST_FILES_SHARING_API_USER1,
  100. self::TEST_FILES_SHARING_API_USER2,
  101. \OCP\Constants::PERMISSION_ALL
  102. );
  103. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  104. $user2View = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2 . '/files');
  105. $this->assertTrue($user2View->file_exists($this->folder));
  106. // create a local folder
  107. $result = $user2View->mkdir('localfolder');
  108. $this->assertTrue($result);
  109. // move mount point to local folder
  110. $result = $user2View->rename($this->folder, '/localfolder/' . $this->folder);
  111. $this->assertTrue($result);
  112. // mount point in the root folder should no longer exist
  113. $this->assertFalse($user2View->is_dir($this->folder));
  114. // delete the local folder
  115. $result = $user2View->unlink('/localfolder');
  116. $this->assertTrue($result);
  117. //enforce reload of the mount points
  118. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  119. //mount point should be back at the root
  120. $this->assertTrue($user2View->is_dir($this->folder));
  121. //cleanup
  122. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  123. $this->view->unlink($this->folder);
  124. }
  125. public function testMoveSharedFile() {
  126. $share = $this->share(
  127. IShare::TYPE_USER,
  128. $this->filename,
  129. self::TEST_FILES_SHARING_API_USER1,
  130. self::TEST_FILES_SHARING_API_USER2,
  131. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  132. );
  133. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  134. \OC\Files\Filesystem::rename($this->filename, $this->filename . '_renamed');
  135. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  136. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  137. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  138. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  139. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  140. // rename back to original name
  141. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  142. \OC\Files\Filesystem::rename($this->filename . '_renamed', $this->filename);
  143. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename . '_renamed'));
  144. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  145. //cleanup
  146. $this->shareManager->deleteShare($share);
  147. }
  148. /**
  149. * share file with a group if a user renames the file the filename should not change
  150. * for the other users
  151. */
  152. public function testMoveGroupShare() {
  153. $testGroup = $this->groupManager->createGroup('testGroup');
  154. $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1);
  155. $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2);
  156. $user3 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER3);
  157. $testGroup->addUser($user1);
  158. $testGroup->addUser($user2);
  159. $testGroup->addUser($user3);
  160. $fileinfo = $this->view->getFileInfo($this->filename);
  161. $share = $this->share(
  162. IShare::TYPE_GROUP,
  163. $this->filename,
  164. self::TEST_FILES_SHARING_API_USER1,
  165. 'testGroup',
  166. \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE
  167. );
  168. $this->shareManager->acceptShare($share, $user1->getUID());
  169. $this->shareManager->acceptShare($share, $user2->getUID());
  170. $this->shareManager->acceptShare($share, $user3->getUID());
  171. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  172. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  173. \OC\Files\Filesystem::rename($this->filename, 'newFileName');
  174. $this->assertTrue(\OC\Files\Filesystem::file_exists('newFileName'));
  175. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->filename));
  176. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  177. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  178. $this->assertFalse(\OC\Files\Filesystem::file_exists('newFileName'));
  179. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  180. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->filename));
  181. $this->assertFalse(\OC\Files\Filesystem::file_exists('newFileName'));
  182. //cleanup
  183. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  184. $this->shareManager->deleteShare($share);
  185. $testGroup->removeUser($user1);
  186. $testGroup->removeUser($user2);
  187. $testGroup->removeUser($user3);
  188. }
  189. /**
  190. * @dataProvider dataProviderTestStripUserFilesPath
  191. * @param string $path
  192. * @param string $expectedResult
  193. * @param bool $exception if a exception is expected
  194. */
  195. public function testStripUserFilesPath($path, $expectedResult, $exception) {
  196. $testClass = new DummyTestClassSharedMount(null, null);
  197. try {
  198. $result = $testClass->stripUserFilesPathDummy($path);
  199. $this->assertSame($expectedResult, $result);
  200. } catch (\Exception $e) {
  201. if ($exception) {
  202. $this->assertSame(10, $e->getCode());
  203. } else {
  204. $this->assertTrue(false, 'Exception catched, but expected: ' . $expectedResult);
  205. }
  206. }
  207. }
  208. public function dataProviderTestStripUserFilesPath() {
  209. return [
  210. ['/user/files/foo.txt', '/foo.txt', false],
  211. ['/user/files/folder/foo.txt', '/folder/foo.txt', false],
  212. ['/data/user/files/foo.txt', null, true],
  213. ['/data/user/files/', null, true],
  214. ['/files/foo.txt', null, true],
  215. ['/foo.txt', null, true],
  216. ];
  217. }
  218. public function dataPermissionMovedGroupShare() {
  219. $data = [];
  220. $powerset = function ($permissions) {
  221. $results = [\OCP\Constants::PERMISSION_READ];
  222. foreach ($permissions as $permission) {
  223. foreach ($results as $combination) {
  224. $results[] = $permission | $combination;
  225. }
  226. }
  227. return $results;
  228. };
  229. //Generate file permissions
  230. $permissions = [
  231. \OCP\Constants::PERMISSION_UPDATE,
  232. \OCP\Constants::PERMISSION_SHARE,
  233. ];
  234. $allPermissions = $powerset($permissions);
  235. foreach ($allPermissions as $before) {
  236. foreach ($allPermissions as $after) {
  237. if ($before === $after) {
  238. continue;
  239. }
  240. $data[] = [
  241. 'file',
  242. $before,
  243. $after,
  244. ];
  245. }
  246. }
  247. //Generate folder permissions
  248. $permissions = [
  249. \OCP\Constants::PERMISSION_UPDATE,
  250. \OCP\Constants::PERMISSION_CREATE,
  251. \OCP\Constants::PERMISSION_SHARE,
  252. \OCP\Constants::PERMISSION_DELETE,
  253. ];
  254. $allPermissions = $powerset($permissions);
  255. foreach ($allPermissions as $before) {
  256. foreach ($allPermissions as $after) {
  257. if ($before === $after) {
  258. continue;
  259. }
  260. $data[] = [
  261. 'folder',
  262. $before,
  263. $after,
  264. ];
  265. }
  266. }
  267. return $data;
  268. }
  269. /**
  270. * moved mountpoints of a group share should keep the same permission as their parent group share.
  271. * See #15253
  272. *
  273. * @dataProvider dataPermissionMovedGroupShare
  274. */
  275. public function testPermissionMovedGroupShare($type, $beforePerm, $afterPerm) {
  276. $this->markTestSkipped('Unreliable test');
  277. if ($type === 'file') {
  278. $path = $this->filename;
  279. } elseif ($type === 'folder') {
  280. $path = $this->folder;
  281. }
  282. $testGroup = $this->groupManager->createGroup('testGroup');
  283. $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1);
  284. $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2);
  285. $user3 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER3);
  286. $testGroup->addUser($user1);
  287. $testGroup->addUser($user2);
  288. $testGroup->addUser($user3);
  289. // Share item with group
  290. $share = $this->share(
  291. IShare::TYPE_GROUP,
  292. $path,
  293. self::TEST_FILES_SHARING_API_USER1,
  294. 'testGroup',
  295. $beforePerm
  296. );
  297. $this->shareManager->acceptShare($share, $user1->getUID());
  298. $this->shareManager->acceptShare($share, $user2->getUID());
  299. $this->shareManager->acceptShare($share, $user3->getUID());
  300. // Login as user 2 and verify the item exists
  301. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  302. $this->assertTrue(\OC\Files\Filesystem::file_exists($path)); // TODO: unreliable - this is sometimes false
  303. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  304. $this->assertEquals($beforePerm, $result->getPermissions());
  305. // Now move the item forcing a new entry in the share table
  306. \OC\Files\Filesystem::rename($path, 'newPath');
  307. $this->assertTrue(\OC\Files\Filesystem::file_exists('newPath'));
  308. $this->assertFalse(\OC\Files\Filesystem::file_exists($path));
  309. // change permissions
  310. $share->setPermissions($afterPerm);
  311. $this->shareManager->updateShare($share);
  312. // Login as user 3 and verify that the permissions are changed
  313. self::loginHelper(self::TEST_FILES_SHARING_API_USER3);
  314. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER3);
  315. $this->assertNotEmpty($result);
  316. $this->assertEquals($afterPerm, $result->getPermissions());
  317. // Login as user 2 and verify that the permissions are changed
  318. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  319. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  320. $this->assertNotEmpty($result);
  321. $this->assertEquals($afterPerm, $result->getPermissions());
  322. $this->assertEquals('/newPath', $result->getTarget());
  323. //cleanup
  324. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  325. $this->shareManager->deleteShare($share);
  326. $testGroup->removeUser($user1);
  327. $testGroup->removeUser($user2);
  328. $testGroup->removeUser($user3);
  329. }
  330. /**
  331. * If the permissions on a group share are upgraded be sure to still respect
  332. * removed shares by a member of that group
  333. */
  334. public function testPermissionUpgradeOnUserDeletedGroupShare() {
  335. $testGroup = $this->groupManager->createGroup('testGroup');
  336. $user1 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER1);
  337. $user2 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER2);
  338. $user3 = $this->userManager->get(self::TEST_FILES_SHARING_API_USER3);
  339. $testGroup->addUser($user1);
  340. $testGroup->addUser($user2);
  341. $testGroup->addUser($user3);
  342. $connection = \OC::$server->getDatabaseConnection();
  343. // Share item with group
  344. $fileinfo = $this->view->getFileInfo($this->folder);
  345. $share = $this->share(
  346. IShare::TYPE_GROUP,
  347. $this->folder,
  348. self::TEST_FILES_SHARING_API_USER1,
  349. 'testGroup',
  350. \OCP\Constants::PERMISSION_READ
  351. );
  352. $this->shareManager->acceptShare($share, $user1->getUID());
  353. $this->shareManager->acceptShare($share, $user2->getUID());
  354. $this->shareManager->acceptShare($share, $user3->getUID());
  355. // Login as user 2 and verify the item exists
  356. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  357. $this->assertTrue(\OC\Files\Filesystem::file_exists($this->folder));
  358. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  359. $this->assertNotEmpty($result);
  360. $this->assertEquals(\OCP\Constants::PERMISSION_READ, $result->getPermissions());
  361. // Delete the share
  362. $this->assertTrue(\OC\Files\Filesystem::rmdir($this->folder));
  363. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->folder));
  364. // Verify we do not get a share
  365. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  366. $this->assertEquals(0, $result->getPermissions());
  367. // Login as user 1 again and change permissions
  368. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  369. $share->setPermissions(\OCP\Constants::PERMISSION_ALL);
  370. $share = $this->shareManager->updateShare($share);
  371. // Login as user 2 and verify
  372. self::loginHelper(self::TEST_FILES_SHARING_API_USER2);
  373. $this->assertFalse(\OC\Files\Filesystem::file_exists($this->folder));
  374. $result = $this->shareManager->getShareById($share->getFullId(), self::TEST_FILES_SHARING_API_USER2);
  375. $this->assertEquals(0, $result->getPermissions());
  376. $this->shareManager->deleteShare($share);
  377. //cleanup
  378. self::loginHelper(self::TEST_FILES_SHARING_API_USER1);
  379. $testGroup->removeUser($user1);
  380. $testGroup->removeUser($user2);
  381. $testGroup->removeUser($user3);
  382. }
  383. }
  384. class DummyTestClassSharedMount extends \OCA\Files_Sharing\SharedMount {
  385. public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
  386. // noop
  387. }
  388. public function stripUserFilesPathDummy($path) {
  389. return $this->stripUserFilesPath($path);
  390. }
  391. }