SharedMountTest.php 15 KB

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