You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SharedMountTest.php 14KB

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