1
0

ShareTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Michael Gapczynski
  6. * @copyright 2012 Michael Gapczynski mtgap@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. namespace Test\Share;
  22. use OC\Share\Share;
  23. use OCP\IGroup;
  24. use OCP\IGroupManager;
  25. use OCP\ILogger;
  26. use OCP\IUser;
  27. use OCP\IUserManager;
  28. /**
  29. * Class Test_Share
  30. *
  31. * @group DB
  32. */
  33. class ShareTest extends \Test\TestCase {
  34. protected $itemType;
  35. /** @var IUser */
  36. protected $user1;
  37. /** @var IUser */
  38. protected $user2;
  39. /** @var IUser */
  40. protected $user3;
  41. /** @var IUser */
  42. protected $user4;
  43. /** @var IUser */
  44. protected $user5;
  45. /** @var IUser */
  46. protected $user6;
  47. /** @var IUser */
  48. protected $groupAndUser_user;
  49. /** @var IGroup */
  50. protected $group1;
  51. /** @var IGroup */
  52. protected $group2;
  53. /** @var IGroup */
  54. protected $groupAndUser_group;
  55. protected $resharing;
  56. protected $dateInFuture;
  57. protected $dateInPast;
  58. /** @var IGroupManager */
  59. protected $groupManager;
  60. /** @var IUserManager */
  61. protected $userManager;
  62. protected function setUp() {
  63. parent::setUp();
  64. $this->groupManager = \OC::$server->getGroupManager();
  65. $this->userManager = \OC::$server->getUserManager();
  66. $this->userManager->clearBackends();
  67. $this->userManager->registerBackend(new \Test\Util\User\Dummy());
  68. $this->user1 = $this->userManager->createUser($this->getUniqueID('user1_'), 'pass');
  69. $this->user2 = $this->userManager->createUser($this->getUniqueID('user2_'), 'pass');
  70. $this->user3 = $this->userManager->createUser($this->getUniqueID('user3_'), 'pass');
  71. $this->user4 = $this->userManager->createUser($this->getUniqueID('user4_'), 'pass');
  72. $this->user5 = $this->userManager->createUser($this->getUniqueID('user5_'), 'pass');
  73. $this->user6 = $this->userManager->createUser($this->getUniqueID('user6_'), 'pass');
  74. $groupAndUserId = $this->getUniqueID('groupAndUser_');
  75. $this->groupAndUser_user = $this->userManager->createUser($groupAndUserId, 'pass');
  76. \OC_User::setUserId($this->user1->getUID());
  77. $this->groupManager->clearBackends();
  78. $this->groupManager->addBackend(new \Test\Util\Group\Dummy());
  79. $this->group1 = $this->groupManager->createGroup($this->getUniqueID('group1_'));
  80. $this->group2 = $this->groupManager->createGroup($this->getUniqueID('group2_'));
  81. $this->groupAndUser_group = $this->groupManager->createGroup($groupAndUserId);
  82. $this->group1->addUser($this->user1);
  83. $this->group1->addUser($this->user2);
  84. $this->group1->addUser($this->user3);
  85. $this->group2->addUser($this->user2);
  86. $this->group2->addUser($this->user4);
  87. $this->groupAndUser_group->addUser($this->user2);
  88. $this->groupAndUser_group->addUser($this->user3);
  89. \OC\Share\Share::registerBackend('test', 'Test\Share\Backend');
  90. \OC_Hook::clear('OCP\\Share');
  91. \OC::registerShareHooks();
  92. $this->resharing = \OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes');
  93. \OC::$server->getConfig()->setAppValue('core', 'shareapi_allow_resharing', 'yes');
  94. // 20 Minutes in the past, 20 minutes in the future.
  95. $now = time();
  96. $dateFormat = 'Y-m-d H:i:s';
  97. $this->dateInPast = date($dateFormat, $now - 20 * 60);
  98. $this->dateInFuture = date($dateFormat, $now + 20 * 60);
  99. }
  100. protected function tearDown() {
  101. $query = \OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `item_type` = ?');
  102. $query->execute(array('test'));
  103. \OC::$server->getConfig()->setAppValue('core', 'shareapi_allow_resharing', $this->resharing);
  104. $this->user1->delete();
  105. $this->user2->delete();
  106. $this->user3->delete();
  107. $this->user4->delete();
  108. $this->user5->delete();
  109. $this->user6->delete();
  110. $this->groupAndUser_user->delete();
  111. $this->group1->delete();
  112. $this->group2->delete();
  113. $this->groupAndUser_group->delete();
  114. $this->logout();
  115. parent::tearDown();
  116. }
  117. public function testShareInvalidShareType() {
  118. $message = 'Share type foobar is not valid for test.txt';
  119. try {
  120. \OC\Share\Share::shareItem('test', 'test.txt', 'foobar', $this->user2, \OCP\Constants::PERMISSION_READ);
  121. } catch (\Exception $exception) {
  122. $this->assertEquals($message, $exception->getMessage());
  123. }
  124. }
  125. public function testGetShareFromOutsideFilesFolder() {
  126. \OC_User::setUserId($this->user1->getUID());
  127. $view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
  128. $view->mkdir('files/test');
  129. $view->mkdir('files/test/sub');
  130. $view->mkdir('files_trashbin');
  131. $view->mkdir('files_trashbin/files');
  132. $fileInfo = $view->getFileInfo('files/test/sub');
  133. $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
  134. $fileId = $fileInfo->getId();
  135. $this->assertTrue(
  136. \OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), \OCP\Constants::PERMISSION_READ),
  137. 'Failed asserting that user 1 successfully shared "test/sub" with user 2.'
  138. );
  139. $result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
  140. $this->assertNotEmpty($result);
  141. $result = \OC\Share\Share::getItemSharedWithUser('folder', $fileId, $this->user2->getUID());
  142. $this->assertNotEmpty($result);
  143. $result = \OC\Share\Share::getItemsSharedWithUser('folder', $this->user2->getUID());
  144. $this->assertNotEmpty($result);
  145. // move to trash (keeps file id)
  146. $view->rename('files/test', 'files_trashbin/files/test');
  147. $result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
  148. $this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
  149. $result = \OC\Share\Share::getItemSharedWithUser('folder', $fileId, $this->user2->getUID());
  150. $this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
  151. $result = \OC\Share\Share::getItemsSharedWithUser('folder', $this->user2->getUID());
  152. $this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
  153. }
  154. public function testSharingAFolderThatIsSharedWithAGroupOfTheOwner() {
  155. \OC_User::setUserId($this->user1->getUID());
  156. $view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
  157. $view->mkdir('files/test');
  158. $view->mkdir('files/test/sub1');
  159. $view->mkdir('files/test/sub1/sub2');
  160. $fileInfo = $view->getFileInfo('files/test/sub1');
  161. $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
  162. $fileId = $fileInfo->getId();
  163. $this->assertTrue(
  164. \OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_CREATE),
  165. 'Failed asserting that user 1 successfully shared "test/sub1" with group 1.'
  166. );
  167. $result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
  168. $this->assertNotEmpty($result);
  169. $this->assertEquals(\OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_CREATE, $result['permissions']);
  170. $fileInfo = $view->getFileInfo('files/test/sub1/sub2');
  171. $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
  172. $fileId = $fileInfo->getId();
  173. $this->assertTrue(
  174. \OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user4->getUID(), \OCP\Constants::PERMISSION_READ),
  175. 'Failed asserting that user 1 successfully shared "test/sub1/sub2" with user 4.'
  176. );
  177. $result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
  178. $this->assertNotEmpty($result);
  179. $this->assertEquals(\OCP\Constants::PERMISSION_READ, $result['permissions']);
  180. }
  181. public function testSharingAFileInsideAFolderThatIsAlreadyShared() {
  182. \OC_User::setUserId($this->user1->getUID());
  183. $view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
  184. $view->mkdir('files/test');
  185. $view->mkdir('files/test/sub1');
  186. $view->file_put_contents('files/test/sub1/file.txt', 'abc');
  187. $folderInfo = $view->getFileInfo('files/test/sub1');
  188. $this->assertInstanceOf('\OC\Files\FileInfo', $folderInfo);
  189. $folderId = $folderInfo->getId();
  190. $fileInfo = $view->getFileInfo('files/test/sub1/file.txt');
  191. $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
  192. $fileId = $fileInfo->getId();
  193. $this->assertTrue(
  194. \OC\Share\Share::shareItem('folder', $folderId, \OCP\Share::SHARE_TYPE_GROUP, $this->group2->getGID(), \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_UPDATE),
  195. 'Failed asserting that user 1 successfully shared "test/sub1" with group 2.'
  196. );
  197. $this->assertTrue(
  198. \OC\Share\Share::shareItem('file', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), \OCP\Constants::PERMISSION_READ),
  199. 'Failed asserting that user 1 successfully shared "test/sub1/file.txt" with user 2.'
  200. );
  201. $result = \OC\Share\Share::getItemsSharedWithUser('file', $this->user2->getUID());
  202. $this->assertCount(2, $result);
  203. foreach ($result as $share) {
  204. $itemName = substr($share['path'], strrpos($share['path'], '/'));
  205. $this->assertSame($itemName, $share['file_target'], 'Asserting that the file_target is the last segment of the path');
  206. $this->assertSame($share['item_target'], '/' . $share['item_source'], 'Asserting that the item is the item that was shared');
  207. }
  208. }
  209. /**
  210. * Test that unsharing from group will also delete all
  211. * child entries
  212. */
  213. public function testShareWithGroupThenUnshare() {
  214. \OC_User::setUserId($this->user5->getUID());
  215. \OC\Share\Share::shareItem(
  216. 'test',
  217. 'test.txt',
  218. \OCP\Share::SHARE_TYPE_GROUP,
  219. $this->group1->getGID(),
  220. \OCP\Constants::PERMISSION_ALL
  221. );
  222. $targetUsers = array($this->user1->getUID(), $this->user2->getUID(), $this->user3->getUID());
  223. foreach($targetUsers as $targetUser) {
  224. \OC_User::setUserId($targetUser);
  225. $items = \OC\Share\Share::getItemsSharedWithUser(
  226. 'test',
  227. $targetUser,
  228. Backend::FORMAT_TARGET
  229. );
  230. $this->assertEquals(1, count($items));
  231. }
  232. \OC_User::setUserId($this->user5->getUID());
  233. \OC\Share\Share::unshare(
  234. 'test',
  235. 'test.txt',
  236. \OCP\Share::SHARE_TYPE_GROUP,
  237. $this->group1->getGID()
  238. );
  239. // verify that all were deleted
  240. foreach($targetUsers as $targetUser) {
  241. \OC_User::setUserId($targetUser);
  242. $items = \OC\Share\Share::getItemsSharedWithUser(
  243. 'test',
  244. $targetUser,
  245. Backend::FORMAT_TARGET
  246. );
  247. $this->assertEquals(0, count($items));
  248. }
  249. }
  250. /**
  251. * @param boolean|string $token
  252. * @return array
  253. */
  254. protected function getShareByValidToken($token) {
  255. $row = \OCP\Share::getShareByToken($token);
  256. $this->assertInternalType(
  257. 'array',
  258. $row,
  259. "Failed asserting that a share for token $token exists."
  260. );
  261. return $row;
  262. }
  263. public function testGetItemSharedWithUser() {
  264. \OC_User::setUserId($this->user1->getUID());
  265. //add dummy values to the share table
  266. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
  267. .' `item_type`, `item_source`, `item_target`, `share_type`,'
  268. .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)');
  269. $args = array('test', 99, 'target1', \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), $this->user1->getUID());
  270. $query->execute($args);
  271. $args = array('test', 99, 'target2', \OCP\Share::SHARE_TYPE_USER, $this->user4->getUID(), $this->user1->getUID());
  272. $query->execute($args);
  273. $args = array('test', 99, 'target3', \OCP\Share::SHARE_TYPE_USER, $this->user3->getUID(), $this->user2->getUID());
  274. $query->execute($args);
  275. $args = array('test', 99, 'target4', \OCP\Share::SHARE_TYPE_USER, $this->user3->getUID(), $this->user4->getUID());
  276. $query->execute($args);
  277. $args = array('test', 99, 'target4', \OCP\Share::SHARE_TYPE_USER, $this->user6->getUID(), $this->user4->getUID());
  278. $query->execute($args);
  279. $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID());
  280. $this->assertSame(1, count($result1));
  281. $this->verifyResult($result1, array('target1'));
  282. $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID());
  283. $this->assertSame(2, count($result2));
  284. $this->verifyResult($result2, array('target1', 'target2'));
  285. $result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3->getUID());
  286. $this->assertSame(2, count($result3));
  287. $this->verifyResult($result3, array('target3', 'target4'));
  288. $result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null);
  289. $this->assertSame(5, count($result4)); // 5 because target4 appears twice
  290. $this->verifyResult($result4, array('target1', 'target2', 'target3', 'target4'));
  291. $result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null);
  292. $this->assertSame(1, count($result6));
  293. $this->verifyResult($result6, array('target4'));
  294. }
  295. public function testGetItemSharedWithUserFromGroupShare() {
  296. \OC_User::setUserId($this->user1->getUID());
  297. //add dummy values to the share table
  298. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
  299. .' `item_type`, `item_source`, `item_target`, `share_type`,'
  300. .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)');
  301. $args = array('test', 99, 'target1', \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), $this->user1->getUID());
  302. $query->execute($args);
  303. $args = array('test', 99, 'target2', \OCP\Share::SHARE_TYPE_GROUP, $this->group2->getGID(), $this->user1->getUID());
  304. $query->execute($args);
  305. $args = array('test', 99, 'target3', \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), $this->user2->getUID());
  306. $query->execute($args);
  307. $args = array('test', 99, 'target4', \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), $this->user4->getUID());
  308. $query->execute($args);
  309. // user2 is in group1 and group2
  310. $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2->getUID(), $this->user1->getUID());
  311. $this->assertSame(2, count($result1));
  312. $this->verifyResult($result1, array('target1', 'target2'));
  313. $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1->getUID());
  314. $this->assertSame(2, count($result2));
  315. $this->verifyResult($result2, array('target1', 'target2'));
  316. // user3 is in group1 and group2
  317. $result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3->getUID());
  318. $this->assertSame(3, count($result3));
  319. $this->verifyResult($result3, array('target1', 'target3', 'target4'));
  320. $result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null);
  321. $this->assertSame(4, count($result4));
  322. $this->verifyResult($result4, array('target1', 'target2', 'target3', 'target4'));
  323. $result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6->getUID(), null);
  324. $this->assertSame(0, count($result6));
  325. }
  326. public function verifyResult($result, $expected) {
  327. foreach ($result as $r) {
  328. if (in_array($r['item_target'], $expected)) {
  329. $key = array_search($r['item_target'], $expected);
  330. unset($expected[$key]);
  331. }
  332. }
  333. $this->assertEmpty($expected, 'did not found all expected values');
  334. }
  335. public function testGetShareSubItemsWhenUserNotInGroup() {
  336. \OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_GROUP, $this->group1->getGID(), \OCP\Constants::PERMISSION_READ);
  337. $result = \OC\Share\Share::getItemsSharedWithUser('test', $this->user2->getUID());
  338. $this->assertCount(1, $result);
  339. $groupShareId = array_keys($result)[0];
  340. // remove user from group
  341. $userObject = \OC::$server->getUserManager()->get($this->user2->getUID());
  342. \OC::$server->getGroupManager()->get($this->group1->getGID())->removeUser($userObject);
  343. $result = \OC\Share\Share::getItemsSharedWithUser('test', $this->user2->getUID());
  344. $this->assertCount(0, $result);
  345. // test with buggy data
  346. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  347. $qb->insert('share')
  348. ->values([
  349. 'share_type' => $qb->expr()->literal(2), // group sub-share
  350. 'share_with' => $qb->expr()->literal($this->user2->getUID()),
  351. 'parent' => $qb->expr()->literal($groupShareId),
  352. 'uid_owner' => $qb->expr()->literal($this->user1->getUID()),
  353. 'item_type' => $qb->expr()->literal('test'),
  354. 'item_source' => $qb->expr()->literal('test.txt'),
  355. 'item_target' => $qb->expr()->literal('test.txt'),
  356. 'file_target' => $qb->expr()->literal('test2.txt'),
  357. 'permissions' => $qb->expr()->literal(1),
  358. 'stime' => $qb->expr()->literal(time()),
  359. ])->execute();
  360. $result = \OC\Share\Share::getItemsSharedWithUser('test', $this->user2->getUID());
  361. $this->assertCount(0, $result);
  362. $qb->delete('share')->execute();
  363. }
  364. public function testShareItemWithLinkAndDefaultExpireDate() {
  365. \OC_User::setUserId($this->user1->getUID());
  366. $config = \OC::$server->getConfig();
  367. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  368. $config->setAppValue('core', 'shareapi_expire_after_n_days', '2');
  369. $token = \OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ);
  370. $this->assertInternalType(
  371. 'string',
  372. $token,
  373. 'Failed asserting that user 1 successfully shared text.txt as link with token.'
  374. );
  375. // share should have default expire date
  376. $row = $this->getShareByValidToken($token);
  377. $this->assertNotEmpty(
  378. $row['expiration'],
  379. 'Failed asserting that the returned row has an default expiration date.'
  380. );
  381. $config->deleteAppValue('core', 'shareapi_default_expire_date');
  382. $config->deleteAppValue('core', 'shareapi_expire_after_n_days');
  383. }
  384. public function dataShareWithRemoteUserAndRemoteIsInvalid() {
  385. return [
  386. // Invalid path
  387. array('user@'),
  388. // Invalid user
  389. array('@server'),
  390. array('us/er@server'),
  391. array('us:er@server'),
  392. // Invalid splitting
  393. array('user'),
  394. array(''),
  395. array('us/erserver'),
  396. array('us:erserver'),
  397. ];
  398. }
  399. /**
  400. * @dataProvider dataShareWithRemoteUserAndRemoteIsInvalid
  401. *
  402. * @param string $remoteId
  403. * @expectedException \OC\HintException
  404. */
  405. public function testShareWithRemoteUserAndRemoteIsInvalid($remoteId) {
  406. \OC_User::setUserId($this->user1->getUID());
  407. \OC\Share\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_REMOTE, $remoteId, \OCP\Constants::PERMISSION_ALL);
  408. }
  409. /**
  410. * @dataProvider checkPasswordProtectedShareDataProvider
  411. * @param $expected
  412. * @param $item
  413. */
  414. public function testCheckPasswordProtectedShare($expected, $item) {
  415. \OC::$server->getSession()->set('public_link_authenticated', '100');
  416. $result = \OC\Share\Share::checkPasswordProtectedShare($item);
  417. $this->assertEquals($expected, $result);
  418. }
  419. function checkPasswordProtectedShareDataProvider() {
  420. return array(
  421. array(true, array()),
  422. array(true, array('share_with' => null)),
  423. array(true, array('share_with' => '')),
  424. array(true, array('share_with' => '1234567890', 'share_type' => '1')),
  425. array(true, array('share_with' => '1234567890', 'share_type' => 1)),
  426. array(true, array('share_with' => '1234567890', 'share_type' => '3', 'id' => '100')),
  427. array(true, array('share_with' => '1234567890', 'share_type' => 3, 'id' => '100')),
  428. array(true, array('share_with' => '1234567890', 'share_type' => '3', 'id' => 100)),
  429. array(true, array('share_with' => '1234567890', 'share_type' => 3, 'id' => 100)),
  430. array(false, array('share_with' => '1234567890', 'share_type' => '3', 'id' => '101')),
  431. array(false, array('share_with' => '1234567890', 'share_type' => 3, 'id' => '101')),
  432. array(false, array('share_with' => '1234567890', 'share_type' => '3', 'id' => 101)),
  433. array(false, array('share_with' => '1234567890', 'share_type' => 3, 'id' => 101)),
  434. );
  435. }
  436. /**
  437. * @dataProvider urls
  438. * @param string $url
  439. * @param string $expectedResult
  440. */
  441. function testRemoveProtocolFromUrl($url, $expectedResult) {
  442. $share = new \OC\Share\Share();
  443. $result = self::invokePrivate($share, 'removeProtocolFromUrl', array($url));
  444. $this->assertSame($expectedResult, $result);
  445. }
  446. function urls() {
  447. return array(
  448. array('http://owncloud.org', 'owncloud.org'),
  449. array('https://owncloud.org', 'owncloud.org'),
  450. array('owncloud.org', 'owncloud.org'),
  451. );
  452. }
  453. /**
  454. * @dataProvider dataProviderTestGroupItems
  455. * @param array $ungrouped
  456. * @param array $grouped
  457. */
  458. function testGroupItems($ungrouped, $grouped) {
  459. $result = DummyShareClass::groupItemsTest($ungrouped);
  460. $this->compareArrays($grouped, $result);
  461. }
  462. function compareArrays($result, $expectedResult) {
  463. foreach ($expectedResult as $key => $value) {
  464. if (is_array($value)) {
  465. $this->compareArrays($result[$key], $value);
  466. } else {
  467. $this->assertSame($value, $result[$key]);
  468. }
  469. }
  470. }
  471. function dataProviderTestGroupItems() {
  472. return array(
  473. // one array with one share
  474. array(
  475. array( // input
  476. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1')),
  477. array( // expected result
  478. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1'))),
  479. // two shares both point to the same source
  480. array(
  481. array( // input
  482. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  483. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
  484. ),
  485. array( // expected result
  486. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1',
  487. 'grouped' => array(
  488. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  489. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
  490. )
  491. ),
  492. )
  493. ),
  494. // two shares both point to the same source but with different targets
  495. array(
  496. array( // input
  497. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  498. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'),
  499. ),
  500. array( // expected result
  501. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  502. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'),
  503. )
  504. ),
  505. // three shares two point to the same source
  506. array(
  507. array( // input
  508. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  509. array('item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'),
  510. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
  511. ),
  512. array( // expected result
  513. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1',
  514. 'grouped' => array(
  515. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  516. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
  517. )
  518. ),
  519. array('item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'),
  520. )
  521. ),
  522. );
  523. }
  524. /**
  525. * Test case for #17560
  526. */
  527. public function testAccesToSharedSubFolder() {
  528. \OC_User::setUserId($this->user1->getUID());
  529. $view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
  530. $view->mkdir('files/folder1');
  531. $fileInfo = $view->getFileInfo('files/folder1');
  532. $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
  533. $fileId = $fileInfo->getId();
  534. $this->assertTrue(
  535. \OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), \OCP\Constants::PERMISSION_ALL),
  536. 'Failed asserting that user 1 successfully shared "test" with user 2.'
  537. );
  538. $this->assertTrue(
  539. \OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user3->getUID(), \OCP\Constants::PERMISSION_ALL),
  540. 'Failed asserting that user 1 successfully shared "test" with user 3.'
  541. );
  542. $view->mkdir('files/folder1/folder2');
  543. $fileInfo = $view->getFileInfo('files/folder1/folder2');
  544. $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
  545. $fileId = $fileInfo->getId();
  546. $this->assertTrue(
  547. \OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user4->getUID(), \OCP\Constants::PERMISSION_ALL),
  548. 'Failed asserting that user 1 successfully shared "test" with user 4.'
  549. );
  550. $res = \OCP\Share::getItemShared(
  551. 'folder',
  552. $fileId,
  553. \OCP\Share::FORMAT_NONE,
  554. null,
  555. true
  556. );
  557. $this->assertCount(3, $res);
  558. $this->assertTrue(
  559. \OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user5->getUID(), \OCP\Constants::PERMISSION_ALL),
  560. 'Failed asserting that user 1 successfully shared "test" with user 5.'
  561. );
  562. $res = \OCP\Share::getItemShared(
  563. 'folder',
  564. $fileId,
  565. \OCP\Share::FORMAT_NONE,
  566. null,
  567. true
  568. );
  569. $this->assertCount(4, $res);
  570. }
  571. public function testShareWithSelfError() {
  572. \OC_User::setUserId($this->user1->getUID());
  573. $view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
  574. $view->mkdir('files/folder1');
  575. $fileInfo = $view->getFileInfo('files/folder1');
  576. $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
  577. $fileId = $fileInfo->getId();
  578. try {
  579. \OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user1->getUID(), \OCP\Constants::PERMISSION_ALL);
  580. $this->fail();
  581. } catch (\Exception $e) {
  582. $this->assertEquals('Sharing /folder1 failed, because you can not share with yourself', $e->getMessage());
  583. }
  584. }
  585. public function testShareWithOwnerError() {
  586. \OC_User::setUserId($this->user1->getUID());
  587. $view = new \OC\Files\View('/' . $this->user1->getUID() . '/');
  588. $view->mkdir('files/folder1');
  589. $fileInfo = $view->getFileInfo('files/folder1');
  590. $this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
  591. $fileId = $fileInfo->getId();
  592. $this->assertTrue(
  593. \OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2->getUID(), \OCP\Constants::PERMISSION_ALL),
  594. 'Failed asserting that user 1 successfully shared "test" with user 2.'
  595. );
  596. \OC_User::setUserId($this->user2->getUID());
  597. try {
  598. \OC\Share\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user1->getUID(), \OCP\Constants::PERMISSION_ALL);
  599. $this->fail();
  600. } catch (\Exception $e) {
  601. $this->assertEquals('Sharing failed, because the user ' . $this->user1->getUID() . ' is the original sharer', $e->getMessage());
  602. }
  603. }
  604. }
  605. class DummyShareClass extends \OC\Share\Share {
  606. public static function groupItemsTest($items) {
  607. return parent::groupItems($items, 'test');
  608. }
  609. }
  610. class DummyHookListener {
  611. static $shareType = null;
  612. public static function listen($params) {
  613. self::$shareType = $params['shareType'];
  614. }
  615. }