share.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  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. class Test_Share extends \Test\TestCase {
  22. protected $itemType;
  23. protected $userBackend;
  24. protected $user1;
  25. protected $user2;
  26. protected $user3;
  27. protected $user4;
  28. protected $user5;
  29. protected $user6;
  30. protected $groupAndUser;
  31. protected $groupBackend;
  32. protected $group1;
  33. protected $group2;
  34. protected $resharing;
  35. protected $dateInFuture;
  36. protected $dateInPast;
  37. protected function setUp() {
  38. parent::setUp();
  39. OC_User::clearBackends();
  40. OC_User::useBackend('dummy');
  41. $this->user1 = $this->getUniqueID('user1_');
  42. $this->user2 = $this->getUniqueID('user2_');
  43. $this->user3 = $this->getUniqueID('user3_');
  44. $this->user4 = $this->getUniqueID('user4_');
  45. $this->user5 = $this->getUniqueID('user5_');
  46. $this->user6 = $this->getUniqueID('user6_');
  47. $this->groupAndUser = $this->getUniqueID('groupAndUser_');
  48. OC_User::createUser($this->user1, 'pass');
  49. OC_User::createUser($this->user2, 'pass');
  50. OC_User::createUser($this->user3, 'pass');
  51. OC_User::createUser($this->user4, 'pass');
  52. OC_User::createUser($this->user5, 'pass');
  53. OC_User::createUser($this->user6, 'pass'); // no group
  54. OC_User::createUser($this->groupAndUser, 'pass');
  55. OC_User::setUserId($this->user1);
  56. OC_Group::clearBackends();
  57. OC_Group::useBackend(new OC_Group_Dummy);
  58. $this->group1 = $this->getUniqueID('group1_');
  59. $this->group2 = $this->getUniqueID('group2_');
  60. OC_Group::createGroup($this->group1);
  61. OC_Group::createGroup($this->group2);
  62. OC_Group::createGroup($this->groupAndUser);
  63. OC_Group::addToGroup($this->user1, $this->group1);
  64. OC_Group::addToGroup($this->user2, $this->group1);
  65. OC_Group::addToGroup($this->user3, $this->group1);
  66. OC_Group::addToGroup($this->user2, $this->group2);
  67. OC_Group::addToGroup($this->user4, $this->group2);
  68. OC_Group::addToGroup($this->user2, $this->groupAndUser);
  69. OC_Group::addToGroup($this->user3, $this->groupAndUser);
  70. OCP\Share::registerBackend('test', 'Test_Share_Backend');
  71. OC_Hook::clear('OCP\\Share');
  72. OC::registerShareHooks();
  73. $this->resharing = OC_Appconfig::getValue('core', 'shareapi_allow_resharing', 'yes');
  74. OC_Appconfig::setValue('core', 'shareapi_allow_resharing', 'yes');
  75. // 20 Minutes in the past, 20 minutes in the future.
  76. $now = time();
  77. $dateFormat = 'Y-m-d H:i:s';
  78. $this->dateInPast = date($dateFormat, $now - 20 * 60);
  79. $this->dateInFuture = date($dateFormat, $now + 20 * 60);
  80. }
  81. protected function tearDown() {
  82. $query = OC_DB::prepare('DELETE FROM `*PREFIX*share` WHERE `item_type` = ?');
  83. $query->execute(array('test'));
  84. OC_Appconfig::setValue('core', 'shareapi_allow_resharing', $this->resharing);
  85. OC_User::deleteUser($this->user1);
  86. OC_User::deleteUser($this->user2);
  87. OC_User::deleteUser($this->user3);
  88. OC_User::deleteUser($this->user4);
  89. OC_User::deleteUser($this->user5);
  90. OC_User::deleteUser($this->user6);
  91. OC_User::deleteUser($this->groupAndUser);
  92. OC_Group::deleteGroup($this->group1);
  93. OC_Group::deleteGroup($this->group2);
  94. OC_Group::deleteGroup($this->groupAndUser);
  95. $this->logout();
  96. parent::tearDown();
  97. }
  98. public function testShareInvalidShareType() {
  99. $message = 'Share type foobar is not valid for test.txt';
  100. try {
  101. OCP\Share::shareItem('test', 'test.txt', 'foobar', $this->user2, \OCP\Constants::PERMISSION_READ);
  102. } catch (Exception $exception) {
  103. $this->assertEquals($message, $exception->getMessage());
  104. }
  105. }
  106. public function testInvalidItemType() {
  107. $message = 'Sharing backend for foobar not found';
  108. try {
  109. OCP\Share::shareItem('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ);
  110. $this->fail('Exception was expected: '.$message);
  111. } catch (Exception $exception) {
  112. $this->assertEquals($message, $exception->getMessage());
  113. }
  114. try {
  115. OCP\Share::getItemsSharedWith('foobar');
  116. $this->fail('Exception was expected: '.$message);
  117. } catch (Exception $exception) {
  118. $this->assertEquals($message, $exception->getMessage());
  119. }
  120. try {
  121. OCP\Share::getItemSharedWith('foobar', 'test.txt');
  122. $this->fail('Exception was expected: '.$message);
  123. } catch (Exception $exception) {
  124. $this->assertEquals($message, $exception->getMessage());
  125. }
  126. try {
  127. OCP\Share::getItemSharedWithBySource('foobar', 'test.txt');
  128. $this->fail('Exception was expected: '.$message);
  129. } catch (Exception $exception) {
  130. $this->assertEquals($message, $exception->getMessage());
  131. }
  132. try {
  133. OCP\Share::getItemShared('foobar', 'test.txt');
  134. $this->fail('Exception was expected: '.$message);
  135. } catch (Exception $exception) {
  136. $this->assertEquals($message, $exception->getMessage());
  137. }
  138. try {
  139. OCP\Share::unshare('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2);
  140. $this->fail('Exception was expected: '.$message);
  141. } catch (Exception $exception) {
  142. $this->assertEquals($message, $exception->getMessage());
  143. }
  144. try {
  145. OCP\Share::setPermissions('foobar', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_UPDATE);
  146. $this->fail('Exception was expected: '.$message);
  147. } catch (Exception $exception) {
  148. $this->assertEquals($message, $exception->getMessage());
  149. }
  150. }
  151. protected function shareUserOneTestFileWithUserTwo() {
  152. OC_User::setUserId($this->user1);
  153. $this->assertTrue(
  154. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ),
  155. 'Failed asserting that user 1 successfully shared text.txt with user 2.'
  156. );
  157. $this->assertContains(
  158. 'test.txt',
  159. OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  160. 'Failed asserting that test.txt is a shared file of user 1.'
  161. );
  162. OC_User::setUserId($this->user2);
  163. $this->assertContains(
  164. 'test.txt',
  165. OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  166. 'Failed asserting that user 2 has access to test.txt after initial sharing.'
  167. );
  168. }
  169. protected function shareUserTestFileAsLink() {
  170. OC_User::setUserId($this->user1);
  171. $result = OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ);
  172. $this->assertTrue(is_string($result));
  173. }
  174. /**
  175. * @param string $sharer
  176. * @param string $receiver
  177. */
  178. protected function shareUserTestFileWithUser($sharer, $receiver) {
  179. OC_User::setUserId($sharer);
  180. $this->assertTrue(
  181. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $receiver, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE),
  182. 'Failed asserting that ' . $sharer . ' successfully shared text.txt with ' . $receiver . '.'
  183. );
  184. $this->assertContains(
  185. 'test.txt',
  186. OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  187. 'Failed asserting that test.txt is a shared file of ' . $sharer . '.'
  188. );
  189. OC_User::setUserId($receiver);
  190. $this->assertContains(
  191. 'test.txt',
  192. OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  193. 'Failed asserting that ' . $receiver . ' has access to test.txt after initial sharing.'
  194. );
  195. }
  196. public function testShareWithUser() {
  197. // Invalid shares
  198. $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the item owner';
  199. try {
  200. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, \OCP\Constants::PERMISSION_READ);
  201. $this->fail('Exception was expected: '.$message);
  202. } catch (Exception $exception) {
  203. $this->assertEquals($message, $exception->getMessage());
  204. }
  205. $message = 'Sharing test.txt failed, because the user foobar does not exist';
  206. try {
  207. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, 'foobar', \OCP\Constants::PERMISSION_READ);
  208. $this->fail('Exception was expected: '.$message);
  209. } catch (Exception $exception) {
  210. $this->assertEquals($message, $exception->getMessage());
  211. }
  212. $message = 'Sharing foobar failed, because the sharing backend for test could not find its source';
  213. try {
  214. OCP\Share::shareItem('test', 'foobar', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ);
  215. $this->fail('Exception was expected: '.$message);
  216. } catch (Exception $exception) {
  217. $this->assertEquals($message, $exception->getMessage());
  218. }
  219. // Valid share
  220. $this->shareUserOneTestFileWithUserTwo();
  221. // Attempt to share again
  222. OC_User::setUserId($this->user1);
  223. $message = 'Sharing test.txt failed, because this item is already shared with '.$this->user2;
  224. try {
  225. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ);
  226. $this->fail('Exception was expected: '.$message);
  227. } catch (Exception $exception) {
  228. $this->assertEquals($message, $exception->getMessage());
  229. }
  230. // Attempt to share back
  231. OC_User::setUserId($this->user2);
  232. $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the original sharer';
  233. try {
  234. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, \OCP\Constants::PERMISSION_READ);
  235. $this->fail('Exception was expected: '.$message);
  236. } catch (Exception $exception) {
  237. $this->assertEquals($message, $exception->getMessage());
  238. }
  239. // Unshare
  240. OC_User::setUserId($this->user1);
  241. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
  242. // Attempt reshare without share permission
  243. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ));
  244. OC_User::setUserId($this->user2);
  245. $message = 'Sharing test.txt failed, because resharing is not allowed';
  246. try {
  247. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, \OCP\Constants::PERMISSION_READ);
  248. $this->fail('Exception was expected: '.$message);
  249. } catch (Exception $exception) {
  250. $this->assertEquals($message, $exception->getMessage());
  251. }
  252. // Owner grants share and update permission
  253. OC_User::setUserId($this->user1);
  254. $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_SHARE));
  255. // Attempt reshare with escalated permissions
  256. OC_User::setUserId($this->user2);
  257. $message = 'Sharing test.txt failed, because the permissions exceed permissions granted to '.$this->user2;
  258. try {
  259. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE);
  260. $this->fail('Exception was expected: '.$message);
  261. } catch (Exception $exception) {
  262. $this->assertEquals($message, $exception->getMessage());
  263. }
  264. // Valid reshare
  265. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE));
  266. $this->assertEquals(array('test.txt'), OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
  267. OC_User::setUserId($this->user3);
  268. $this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE));
  269. $this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  270. // Attempt to escalate permissions
  271. OC_User::setUserId($this->user2);
  272. $message = 'Setting permissions for test.txt failed, because the permissions exceed permissions granted to '.$this->user2;
  273. try {
  274. OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE);
  275. $this->fail('Exception was expected: '.$message);
  276. } catch (Exception $exception) {
  277. $this->assertEquals($message, $exception->getMessage());
  278. }
  279. // Remove update permission
  280. OC_User::setUserId($this->user1);
  281. $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE));
  282. OC_User::setUserId($this->user2);
  283. $this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  284. OC_User::setUserId($this->user3);
  285. $this->assertEquals(array(\OCP\Constants::PERMISSION_READ), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  286. // Remove share permission
  287. OC_User::setUserId($this->user1);
  288. $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ));
  289. OC_User::setUserId($this->user2);
  290. $this->assertEquals(array(\OCP\Constants::PERMISSION_READ), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  291. OC_User::setUserId($this->user3);
  292. $this->assertSame(array(), OCP\Share::getItemSharedWith('test', 'test.txt'));
  293. // Reshare again, and then have owner unshare
  294. OC_User::setUserId($this->user1);
  295. $this->assertTrue(OCP\Share::setPermissions('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE));
  296. OC_User::setUserId($this->user2);
  297. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, \OCP\Constants::PERMISSION_READ));
  298. OC_User::setUserId($this->user1);
  299. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
  300. OC_User::setUserId($this->user2);
  301. $this->assertSame(array(), OCP\Share::getItemSharedWith('test', 'test.txt'));
  302. OC_User::setUserId($this->user3);
  303. $this->assertSame(array(), OCP\Share::getItemSharedWith('test', 'test.txt'));
  304. // Attempt target conflict
  305. OC_User::setUserId($this->user1);
  306. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ));
  307. OC_User::setUserId($this->user3);
  308. $this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ));
  309. OC_User::setUserId($this->user2);
  310. $to_test = OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET);
  311. $this->assertEquals(2, count($to_test));
  312. $this->assertTrue(in_array('test.txt', $to_test));
  313. $this->assertTrue(in_array('test1.txt', $to_test));
  314. // Unshare from self
  315. $this->assertTrue(OCP\Share::unshareFromSelf('test', 'test.txt'));
  316. $this->assertEquals(array('test1.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  317. // Unshare from self via source
  318. $this->assertTrue(OCP\Share::unshareFromSelf('test', 'share.txt', true));
  319. $this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  320. OC_User::setUserId($this->user1);
  321. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ));
  322. OC_User::setUserId($this->user3);
  323. $this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ));
  324. OC_User::setUserId($this->user2);
  325. $to_test = OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET);
  326. $this->assertEquals(2, count($to_test));
  327. $this->assertTrue(in_array('test.txt', $to_test));
  328. $this->assertTrue(in_array('test1.txt', $to_test));
  329. // Remove user
  330. OC_User::setUserId($this->user1);
  331. OC_User::deleteUser($this->user1);
  332. OC_User::setUserId($this->user2);
  333. $this->assertEquals(array('test1.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  334. }
  335. public function testShareWithUserExpirationExpired() {
  336. OC_User::setUserId($this->user1);
  337. $this->shareUserOneTestFileWithUserTwo();
  338. $this->shareUserTestFileAsLink();
  339. // manipulate share table and set expire date to the past
  340. $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `expiration` = ? WHERE `item_type` = ? AND `item_source` = ? AND `uid_owner` = ? AND `share_type` = ?');
  341. $query->bindValue(1, new \DateTime($this->dateInPast), 'datetime');
  342. $query->bindValue(2, 'test');
  343. $query->bindValue(3, 'test.txt');
  344. $query->bindValue(4, $this->user1);
  345. $query->bindValue(5, \OCP\Share::SHARE_TYPE_LINK);
  346. $query->execute();
  347. $shares = OCP\Share::getItemsShared('test');
  348. $this->assertSame(1, count($shares));
  349. $share = reset($shares);
  350. $this->assertSame(\OCP\Share::SHARE_TYPE_USER, $share['share_type']);
  351. }
  352. public function testGetShareFromOutsideFilesFolder() {
  353. OC_User::setUserId($this->user1);
  354. $view = new \OC\Files\View('/' . $this->user1 . '/');
  355. $view->mkdir('files/test');
  356. $view->mkdir('files/test/sub');
  357. $view->mkdir('files_trashbin');
  358. $view->mkdir('files_trashbin/files');
  359. $fileInfo = $view->getFileInfo('files/test/sub');
  360. $fileId = $fileInfo->getId();
  361. $this->assertTrue(
  362. OCP\Share::shareItem('folder', $fileId, OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ),
  363. 'Failed asserting that user 1 successfully shared "test/sub" with user 2.'
  364. );
  365. $result = OCP\Share::getItemShared('folder', $fileId, Test_Share_Backend::FORMAT_SOURCE);
  366. $this->assertNotEmpty($result);
  367. $result = OCP\Share::getItemSharedWithUser('folder', $fileId, $this->user2);
  368. $this->assertNotEmpty($result);
  369. $result = OCP\Share::getItemsSharedWithUser('folder', $this->user2);
  370. $this->assertNotEmpty($result);
  371. // move to trash (keeps file id)
  372. $view->rename('files/test', 'files_trashbin/files/test');
  373. $result = OCP\Share::getItemShared('folder', $fileId, Test_Share_Backend::FORMAT_SOURCE);
  374. $this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
  375. $result = OCP\Share::getItemSharedWithUser('folder', $fileId, $this->user2);
  376. $this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
  377. $result = OCP\Share::getItemsSharedWithUser('folder', $this->user2);
  378. $this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
  379. }
  380. public function testSetExpireDateInPast() {
  381. OC_User::setUserId($this->user1);
  382. $this->shareUserOneTestFileWithUserTwo();
  383. $this->shareUserTestFileAsLink();
  384. $setExpireDateFailed = false;
  385. try {
  386. $this->assertTrue(
  387. OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInPast, ''),
  388. 'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
  389. );
  390. } catch (\Exception $e) {
  391. $setExpireDateFailed = true;
  392. }
  393. $this->assertTrue($setExpireDateFailed);
  394. }
  395. public function testShareWithUserExpirationValid() {
  396. OC_User::setUserId($this->user1);
  397. $this->shareUserOneTestFileWithUserTwo();
  398. $this->shareUserTestFileAsLink();
  399. $this->assertTrue(
  400. OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInFuture, ''),
  401. 'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
  402. );
  403. $shares = OCP\Share::getItemsShared('test');
  404. $this->assertSame(2, count($shares));
  405. }
  406. /*
  407. * if user is in a group excluded from resharing, then the share permission should
  408. * be removed
  409. */
  410. public function testShareWithUserAndUserIsExcludedFromResharing() {
  411. OC_User::setUserId($this->user1);
  412. $this->assertTrue(
  413. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, \OCP\Constants::PERMISSION_ALL),
  414. 'Failed asserting that user 1 successfully shared text.txt with user 4.'
  415. );
  416. $this->assertContains(
  417. 'test.txt',
  418. OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  419. 'Failed asserting that test.txt is a shared file of user 1.'
  420. );
  421. // exclude group2 from sharing
  422. \OC_Appconfig::setValue('core', 'shareapi_exclude_groups_list', $this->group2);
  423. \OC_Appconfig::setValue('core', 'shareapi_exclude_groups', "yes");
  424. OC_User::setUserId($this->user4);
  425. $share = OCP\Share::getItemSharedWith('test', 'test.txt');
  426. $this->assertSame(\OCP\Constants::PERMISSION_ALL & ~\OCP\Constants::PERMISSION_SHARE, $share['permissions'],
  427. 'Failed asserting that user 4 is excluded from re-sharing');
  428. \OC_Appconfig::deleteKey('core', 'shareapi_exclude_groups_list');
  429. \OC_Appconfig::deleteKey('core', 'shareapi_exclude_groups');
  430. }
  431. protected function shareUserOneTestFileWithGroupOne() {
  432. OC_User::setUserId($this->user1);
  433. $this->assertTrue(
  434. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ),
  435. 'Failed asserting that user 1 successfully shared text.txt with group 1.'
  436. );
  437. $this->assertContains(
  438. 'test.txt',
  439. OCP\Share::getItemShared('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  440. 'Failed asserting that test.txt is a shared file of user 1.'
  441. );
  442. OC_User::setUserId($this->user2);
  443. $this->assertContains(
  444. 'test.txt',
  445. OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  446. 'Failed asserting that user 2 has access to test.txt after initial sharing.'
  447. );
  448. OC_User::setUserId($this->user3);
  449. $this->assertContains(
  450. 'test.txt',
  451. OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  452. 'Failed asserting that user 3 has access to test.txt after initial sharing.'
  453. );
  454. }
  455. public function testShareWithGroup() {
  456. // Invalid shares
  457. $message = 'Sharing test.txt failed, because the group foobar does not exist';
  458. try {
  459. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, 'foobar', \OCP\Constants::PERMISSION_READ);
  460. $this->fail('Exception was expected: '.$message);
  461. } catch (Exception $exception) {
  462. $this->assertEquals($message, $exception->getMessage());
  463. }
  464. $policy = OC_Appconfig::getValue('core', 'shareapi_only_share_with_group_members', 'no');
  465. OC_Appconfig::setValue('core', 'shareapi_only_share_with_group_members', 'yes');
  466. $message = 'Sharing test.txt failed, because '.$this->user1.' is not a member of the group '.$this->group2;
  467. try {
  468. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group2, \OCP\Constants::PERMISSION_READ);
  469. $this->fail('Exception was expected: '.$message);
  470. } catch (Exception $exception) {
  471. $this->assertEquals($message, $exception->getMessage());
  472. }
  473. OC_Appconfig::setValue('core', 'shareapi_only_share_with_group_members', $policy);
  474. // Valid share
  475. $this->shareUserOneTestFileWithGroupOne();
  476. // Attempt to share again
  477. OC_User::setUserId($this->user1);
  478. $message = 'Sharing test.txt failed, because this item is already shared with '.$this->group1;
  479. try {
  480. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ);
  481. $this->fail('Exception was expected: '.$message);
  482. } catch (Exception $exception) {
  483. $this->assertEquals($message, $exception->getMessage());
  484. }
  485. // Attempt to share back to owner of group share
  486. OC_User::setUserId($this->user2);
  487. $message = 'Sharing test.txt failed, because the user '.$this->user1.' is the original sharer';
  488. try {
  489. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user1, \OCP\Constants::PERMISSION_READ);
  490. $this->fail('Exception was expected: '.$message);
  491. } catch (Exception $exception) {
  492. $this->assertEquals($message, $exception->getMessage());
  493. }
  494. // Attempt to share back to group
  495. $message = 'Sharing test.txt failed, because this item is already shared with '.$this->group1;
  496. try {
  497. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ);
  498. $this->fail('Exception was expected: '.$message);
  499. } catch (Exception $exception) {
  500. $this->assertEquals($message, $exception->getMessage());
  501. }
  502. // Attempt to share back to member of group
  503. $message ='Sharing test.txt failed, because this item is already shared with '.$this->user3;
  504. try {
  505. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user3, \OCP\Constants::PERMISSION_READ);
  506. $this->fail('Exception was expected: '.$message);
  507. } catch (Exception $exception) {
  508. $this->assertEquals($message, $exception->getMessage());
  509. }
  510. // Unshare
  511. OC_User::setUserId($this->user1);
  512. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1));
  513. // Valid share with same person - user then group
  514. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_SHARE));
  515. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE));
  516. OC_User::setUserId($this->user2);
  517. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  518. $this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_SHARE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  519. OC_User::setUserId($this->user3);
  520. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  521. $this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  522. // Valid reshare
  523. OC_User::setUserId($this->user2);
  524. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, \OCP\Constants::PERMISSION_READ));
  525. OC_User::setUserId($this->user4);
  526. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  527. // Unshare from user only
  528. OC_User::setUserId($this->user1);
  529. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2));
  530. OC_User::setUserId($this->user2);
  531. $this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  532. OC_User::setUserId($this->user4);
  533. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  534. // Valid share with same person - group then user
  535. OC_User::setUserId($this->user1);
  536. $this->assertTrue(OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE));
  537. OC_User::setUserId($this->user2);
  538. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  539. $this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  540. // Unshare from group only
  541. OC_User::setUserId($this->user1);
  542. $this->assertTrue(OCP\Share::unshare('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1));
  543. OC_User::setUserId($this->user2);
  544. $this->assertEquals(array(\OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_DELETE), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_PERMISSIONS));
  545. // Attempt user specific target conflict
  546. OC_User::setUserId($this->user3);
  547. \OCP\Util::connectHook('OCP\\Share', 'post_shared', 'DummyHookListener', 'listen');
  548. $this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE));
  549. $this->assertEquals(OCP\Share::SHARE_TYPE_GROUP, DummyHookListener::$shareType);
  550. OC_User::setUserId($this->user2);
  551. $to_test = OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET);
  552. $this->assertEquals(2, count($to_test));
  553. $this->assertTrue(in_array('test.txt', $to_test));
  554. $this->assertTrue(in_array('test1.txt', $to_test));
  555. // Valid reshare
  556. $this->assertTrue(OCP\Share::shareItem('test', 'share.txt', OCP\Share::SHARE_TYPE_USER, $this->user4, \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_SHARE));
  557. OC_User::setUserId($this->user4);
  558. $this->assertEquals(array('test1.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  559. // Remove user from group
  560. OC_Group::removeFromGroup($this->user2, $this->group1);
  561. OC_User::setUserId($this->user2);
  562. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  563. OC_User::setUserId($this->user4);
  564. $this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  565. // Add user to group
  566. OC_Group::addToGroup($this->user4, $this->group1);
  567. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  568. // Unshare from self
  569. $this->assertTrue(OCP\Share::unshareFromSelf('test', 'test.txt'));
  570. $this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  571. OC_User::setUserId($this->user2);
  572. $this->assertEquals(array('test.txt'), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  573. // Unshare from self via source
  574. OC_User::setUserId($this->user1);
  575. $this->assertTrue(OCP\Share::unshareFromSelf('test', 'share.txt', true));
  576. $this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  577. // Remove group
  578. OC_Group::deleteGroup($this->group1);
  579. OC_User::setUserId($this->user4);
  580. $this->assertEquals(array(), OCP\Share::getItemsSharedWith('test', Test_Share_Backend::FORMAT_TARGET));
  581. OC_User::setUserId($this->user3);
  582. $this->assertEquals(array(), OCP\Share::getItemsShared('test'));
  583. }
  584. /**
  585. * Test that unsharing from group will also delete all
  586. * child entries
  587. */
  588. public function testShareWithGroupThenUnshare() {
  589. OC_User::setUserId($this->user5);
  590. OCP\Share::shareItem(
  591. 'test',
  592. 'test.txt',
  593. OCP\Share::SHARE_TYPE_GROUP,
  594. $this->group1,
  595. \OCP\Constants::PERMISSION_ALL
  596. );
  597. $targetUsers = array($this->user1, $this->user2, $this->user3);
  598. foreach($targetUsers as $targetUser) {
  599. OC_User::setUserId($targetUser);
  600. $items = OCP\Share::getItemsSharedWithUser(
  601. 'test',
  602. $targetUser,
  603. Test_Share_Backend::FORMAT_TARGET
  604. );
  605. $this->assertEquals(1, count($items));
  606. }
  607. OC_User::setUserId($this->user5);
  608. OCP\Share::unshare(
  609. 'test',
  610. 'test.txt',
  611. OCP\Share::SHARE_TYPE_GROUP,
  612. $this->group1
  613. );
  614. // verify that all were deleted
  615. foreach($targetUsers as $targetUser) {
  616. OC_User::setUserId($targetUser);
  617. $items = OCP\Share::getItemsSharedWithUser(
  618. 'test',
  619. $targetUser,
  620. Test_Share_Backend::FORMAT_TARGET
  621. );
  622. $this->assertEquals(0, count($items));
  623. }
  624. }
  625. public function testShareWithGroupAndUserBothHaveTheSameId() {
  626. $this->shareUserTestFileWithUser($this->user1, $this->groupAndUser);
  627. OC_User::setUserId($this->groupAndUser);
  628. $this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  629. '"groupAndUser"-User does not see the file but it was shared with him');
  630. OC_User::setUserId($this->user2);
  631. $this->assertEquals(array(), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  632. 'User2 sees test.txt but it was only shared with the user "groupAndUser" and not with group');
  633. OC_User::setUserId($this->user1);
  634. $this->assertTrue(OCP\Share::unshareAll('test', 'test.txt'));
  635. $this->assertTrue(
  636. OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_GROUP, $this->groupAndUser, \OCP\Constants::PERMISSION_READ),
  637. 'Failed asserting that user 1 successfully shared text.txt with group 1.'
  638. );
  639. OC_User::setUserId($this->groupAndUser);
  640. $this->assertEquals(array(), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  641. '"groupAndUser"-User sees test.txt but it was only shared with the group "groupAndUser" and not with the user');
  642. OC_User::setUserId($this->user2);
  643. $this->assertEquals(array('test.txt'), OCP\Share::getItemSharedWith('test', 'test.txt', Test_Share_Backend::FORMAT_SOURCE),
  644. 'User2 does not see test.txt but it was shared with the group "groupAndUser"');
  645. OC_User::setUserId($this->user1);
  646. $this->assertTrue(OCP\Share::unshareAll('test', 'test.txt'));
  647. }
  648. /**
  649. * @param boolean|string $token
  650. */
  651. protected function getShareByValidToken($token) {
  652. $row = OCP\Share::getShareByToken($token);
  653. $this->assertInternalType(
  654. 'array',
  655. $row,
  656. "Failed asserting that a share for token $token exists."
  657. );
  658. return $row;
  659. }
  660. public function testGetItemSharedWithUser() {
  661. OC_User::setUserId($this->user1);
  662. //add dummy values to the share table
  663. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
  664. .' `item_type`, `item_source`, `item_target`, `share_type`,'
  665. .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)');
  666. $args = array('test', 99, 'target1', OCP\Share::SHARE_TYPE_USER, $this->user2, $this->user1);
  667. $query->execute($args);
  668. $args = array('test', 99, 'target2', OCP\Share::SHARE_TYPE_USER, $this->user4, $this->user1);
  669. $query->execute($args);
  670. $args = array('test', 99, 'target3', OCP\Share::SHARE_TYPE_USER, $this->user3, $this->user2);
  671. $query->execute($args);
  672. $args = array('test', 99, 'target4', OCP\Share::SHARE_TYPE_USER, $this->user3, $this->user4);
  673. $query->execute($args);
  674. $args = array('test', 99, 'target4', OCP\Share::SHARE_TYPE_USER, $this->user6, $this->user4);
  675. $query->execute($args);
  676. $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2, $this->user1);
  677. $this->assertSame(1, count($result1));
  678. $this->verifyResult($result1, array('target1'));
  679. $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1);
  680. $this->assertSame(2, count($result2));
  681. $this->verifyResult($result2, array('target1', 'target2'));
  682. $result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3);
  683. $this->assertSame(2, count($result3));
  684. $this->verifyResult($result3, array('target3', 'target4'));
  685. $result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null);
  686. $this->assertSame(5, count($result4)); // 5 because target4 appears twice
  687. $this->verifyResult($result4, array('target1', 'target2', 'target3', 'target4'));
  688. $result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6, null);
  689. $this->assertSame(1, count($result6));
  690. $this->verifyResult($result6, array('target4'));
  691. }
  692. public function testGetItemSharedWithUserFromGroupShare() {
  693. OC_User::setUserId($this->user1);
  694. //add dummy values to the share table
  695. $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` ('
  696. .' `item_type`, `item_source`, `item_target`, `share_type`,'
  697. .' `share_with`, `uid_owner`) VALUES (?,?,?,?,?,?)');
  698. $args = array('test', 99, 'target1', OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user1);
  699. $query->execute($args);
  700. $args = array('test', 99, 'target2', OCP\Share::SHARE_TYPE_GROUP, $this->group2, $this->user1);
  701. $query->execute($args);
  702. $args = array('test', 99, 'target3', OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user2);
  703. $query->execute($args);
  704. $args = array('test', 99, 'target4', OCP\Share::SHARE_TYPE_GROUP, $this->group1, $this->user4);
  705. $query->execute($args);
  706. // user2 is in group1 and group2
  707. $result1 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user2, $this->user1);
  708. $this->assertSame(2, count($result1));
  709. $this->verifyResult($result1, array('target1', 'target2'));
  710. $result2 = \OCP\Share::getItemSharedWithUser('test', 99, null, $this->user1);
  711. $this->assertSame(2, count($result2));
  712. $this->verifyResult($result2, array('target1', 'target2'));
  713. // user3 is in group1 and group2
  714. $result3 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user3);
  715. $this->assertSame(3, count($result3));
  716. $this->verifyResult($result3, array('target1', 'target3', 'target4'));
  717. $result4 = \OCP\Share::getItemSharedWithUser('test', 99, null, null);
  718. $this->assertSame(4, count($result4));
  719. $this->verifyResult($result4, array('target1', 'target2', 'target3', 'target4'));
  720. $result6 = \OCP\Share::getItemSharedWithUser('test', 99, $this->user6, null);
  721. $this->assertSame(0, count($result6));
  722. }
  723. public function verifyResult($result, $expected) {
  724. foreach ($result as $r) {
  725. if (in_array($r['item_target'], $expected)) {
  726. $key = array_search($r['item_target'], $expected);
  727. unset($expected[$key]);
  728. }
  729. }
  730. $this->assertEmpty($expected, 'did not found all expected values');
  731. }
  732. public function testShareItemWithLink() {
  733. OC_User::setUserId($this->user1);
  734. $token = OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ);
  735. $this->assertInternalType(
  736. 'string',
  737. $token,
  738. 'Failed asserting that user 1 successfully shared text.txt as link with token.'
  739. );
  740. // testGetShareByTokenNoExpiration
  741. $row = $this->getShareByValidToken($token);
  742. $this->assertEmpty(
  743. $row['expiration'],
  744. 'Failed asserting that the returned row does not have an expiration date.'
  745. );
  746. // testGetShareByTokenExpirationValid
  747. $this->assertTrue(
  748. OCP\Share::setExpirationDate('test', 'test.txt', $this->dateInFuture, ''),
  749. 'Failed asserting that user 1 successfully set a future expiration date for the test.txt share.'
  750. );
  751. $row = $this->getShareByValidToken($token);
  752. $this->assertNotEmpty(
  753. $row['expiration'],
  754. 'Failed asserting that the returned row has an expiration date.'
  755. );
  756. // manipulate share table and set expire date to the past
  757. $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `expiration` = ? WHERE `item_type` = ? AND `item_source` = ? AND `uid_owner` = ? AND `share_type` = ?');
  758. $query->bindValue(1, new \DateTime($this->dateInPast), 'datetime');
  759. $query->bindValue(2, 'test');
  760. $query->bindValue(3, 'test.txt');
  761. $query->bindValue(4, $this->user1);
  762. $query->bindValue(5, \OCP\Share::SHARE_TYPE_LINK);
  763. $query->execute();
  764. $this->assertFalse(
  765. OCP\Share::getShareByToken($token),
  766. 'Failed asserting that an expired share could not be found.'
  767. );
  768. }
  769. public function testShareItemWithLinkAndDefaultExpireDate() {
  770. OC_User::setUserId($this->user1);
  771. $config = \OC::$server->getConfig();
  772. $config->setAppValue('core', 'shareapi_default_expire_date', 'yes');
  773. $config->setAppValue('core', 'shareapi_expire_after_n_days', '2');
  774. $token = OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ);
  775. $this->assertInternalType(
  776. 'string',
  777. $token,
  778. 'Failed asserting that user 1 successfully shared text.txt as link with token.'
  779. );
  780. // share should have default expire date
  781. $row = $this->getShareByValidToken($token);
  782. $this->assertNotEmpty(
  783. $row['expiration'],
  784. 'Failed asserting that the returned row has an default expiration date.'
  785. );
  786. $config->deleteAppValue('core', 'shareapi_default_expire_date');
  787. $config->deleteAppValue('core', 'shareapi_expire_after_n_days');
  788. }
  789. public function testUnshareAll() {
  790. $this->shareUserTestFileWithUser($this->user1, $this->user2);
  791. $this->shareUserTestFileWithUser($this->user2, $this->user3);
  792. $this->shareUserTestFileWithUser($this->user3, $this->user4);
  793. $this->shareUserOneTestFileWithGroupOne();
  794. OC_User::setUserId($this->user1);
  795. $this->assertEquals(
  796. array('test.txt', 'test.txt'),
  797. OCP\Share::getItemsShared('test', Test_Share_Backend::FORMAT_SOURCE),
  798. 'Failed asserting that the test.txt file is shared exactly two times by user1.'
  799. );
  800. OC_User::setUserId($this->user2);
  801. $this->assertEquals(
  802. array('test.txt'),
  803. OCP\Share::getItemsShared('test', Test_Share_Backend::FORMAT_SOURCE),
  804. 'Failed asserting that the test.txt file is shared exactly once by user2.'
  805. );
  806. OC_User::setUserId($this->user3);
  807. $this->assertEquals(
  808. array('test.txt'),
  809. OCP\Share::getItemsShared('test', Test_Share_Backend::FORMAT_SOURCE),
  810. 'Failed asserting that the test.txt file is shared exactly once by user3.'
  811. );
  812. $this->assertTrue(
  813. OCP\Share::unshareAll('test', 'test.txt'),
  814. 'Failed asserting that user 3 successfully unshared all shares of the test.txt share.'
  815. );
  816. $this->assertEquals(
  817. array(),
  818. OCP\Share::getItemsShared('test'),
  819. 'Failed asserting that the share of the test.txt file by user 3 has been removed.'
  820. );
  821. OC_User::setUserId($this->user1);
  822. $this->assertEquals(
  823. array(),
  824. OCP\Share::getItemsShared('test'),
  825. 'Failed asserting that both shares of the test.txt file by user 1 have been removed.'
  826. );
  827. OC_User::setUserId($this->user2);
  828. $this->assertEquals(
  829. array(),
  830. OCP\Share::getItemsShared('test'),
  831. 'Failed asserting that the share of the test.txt file by user 2 has been removed.'
  832. );
  833. }
  834. /**
  835. * @dataProvider checkPasswordProtectedShareDataProvider
  836. * @param $expected
  837. * @param $item
  838. */
  839. public function testCheckPasswordProtectedShare($expected, $item) {
  840. \OC::$server->getSession()->set('public_link_authenticated', 100);
  841. $result = \OCP\Share::checkPasswordProtectedShare($item);
  842. $this->assertEquals($expected, $result);
  843. }
  844. function checkPasswordProtectedShareDataProvider() {
  845. return array(
  846. array(true, array()),
  847. array(true, array('share_with' => null)),
  848. array(true, array('share_with' => '')),
  849. array(true, array('share_with' => '1234567890', 'share_type' => '1')),
  850. array(true, array('share_with' => '1234567890', 'share_type' => 1)),
  851. array(true, array('share_with' => '1234567890', 'share_type' => '3', 'id' => 100)),
  852. array(true, array('share_with' => '1234567890', 'share_type' => 3, 'id' => 100)),
  853. array(false, array('share_with' => '1234567890', 'share_type' => '3', 'id' => 101)),
  854. array(false, array('share_with' => '1234567890', 'share_type' => 3, 'id' => 101)),
  855. );
  856. }
  857. /**
  858. * @dataProvider urls
  859. */
  860. function testRemoveProtocolFromUrl($url, $expectedResult) {
  861. $share = new \OC\Share\Share();
  862. $result = \Test_Helper::invokePrivate($share, 'removeProtocolFromUrl', array($url));
  863. $this->assertSame($expectedResult, $result);
  864. }
  865. function urls() {
  866. return array(
  867. array('http://owncloud.org', 'owncloud.org'),
  868. array('https://owncloud.org', 'owncloud.org'),
  869. array('owncloud.org', 'owncloud.org'),
  870. );
  871. }
  872. /**
  873. * @dataProvider dataProviderTestGroupItems
  874. * @param type $ungrouped
  875. * @param type $grouped
  876. */
  877. function testGroupItems($ungrouped, $grouped) {
  878. $result = DummyShareClass::groupItemsTest($ungrouped);
  879. $this->compareArrays($grouped, $result);
  880. }
  881. function compareArrays($result, $expectedResult) {
  882. foreach ($expectedResult as $key => $value) {
  883. if (is_array($value)) {
  884. $this->compareArrays($result[$key], $value);
  885. } else {
  886. $this->assertSame($value, $result[$key]);
  887. }
  888. }
  889. }
  890. function dataProviderTestGroupItems() {
  891. return array(
  892. // one array with one share
  893. array(
  894. array( // input
  895. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1')),
  896. array( // expected result
  897. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_ALL, 'item_target' => 't1'))),
  898. // two shares both point to the same source
  899. array(
  900. array( // input
  901. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  902. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
  903. ),
  904. array( // expected result
  905. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1',
  906. 'grouped' => array(
  907. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  908. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
  909. )
  910. ),
  911. )
  912. ),
  913. // two shares both point to the same source but with different targets
  914. array(
  915. array( // input
  916. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  917. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'),
  918. ),
  919. array( // expected result
  920. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  921. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't2'),
  922. )
  923. ),
  924. // three shares two point to the same source
  925. array(
  926. array( // input
  927. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  928. array('item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'),
  929. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
  930. ),
  931. array( // expected result
  932. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ | \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1',
  933. 'grouped' => array(
  934. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_READ, 'item_target' => 't1'),
  935. array('item_source' => 1, 'permissions' => \OCP\Constants::PERMISSION_UPDATE, 'item_target' => 't1'),
  936. )
  937. ),
  938. array('item_source' => 2, 'permissions' => \OCP\Constants::PERMISSION_CREATE, 'item_target' => 't2'),
  939. )
  940. ),
  941. );
  942. }
  943. /**
  944. * Ensure that we do not allow removing a an expiration date from a link share if this
  945. * is enforced by the settings.
  946. */
  947. public function testClearExpireDateWhileEnforced() {
  948. OC_User::setUserId($this->user1);
  949. \OC_Appconfig::setValue('core', 'shareapi_default_expire_date', 'yes');
  950. \OC_Appconfig::setValue('core', 'shareapi_expire_after_n_days', '2');
  951. \OC_Appconfig::setValue('core', 'shareapi_enforce_expire_date', 'yes');
  952. $token = OCP\Share::shareItem('test', 'test.txt', OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ);
  953. $this->assertInternalType(
  954. 'string',
  955. $token,
  956. 'Failed asserting that user 1 successfully shared text.txt as link with token.'
  957. );
  958. $setExpireDateFailed = false;
  959. try {
  960. $this->assertTrue(
  961. OCP\Share::setExpirationDate('test', 'test.txt', '', ''),
  962. 'Failed asserting that user 1 successfully set an expiration date for the test.txt share.'
  963. );
  964. } catch (\Exception $e) {
  965. $setExpireDateFailed = true;
  966. }
  967. $this->assertTrue($setExpireDateFailed);
  968. \OC_Appconfig::deleteKey('core', 'shareapi_default_expire_date');
  969. \OC_Appconfig::deleteKey('core', 'shareapi_expire_after_n_days');
  970. \OC_Appconfig::deleteKey('core', 'shareapi_enforce_expire_date');
  971. }
  972. /**
  973. * Cannot set password is there is no user
  974. *
  975. * @expectedException Exception
  976. * @expectedExceptionMessage User not logged in
  977. */
  978. public function testSetPasswordNoUser() {
  979. $userSession = $this->getMockBuilder('\OCP\IUserSession')
  980. ->disableOriginalConstructor()
  981. ->getMock();
  982. $connection = $this->getMockBuilder('\OC\DB\Connection')
  983. ->disableOriginalConstructor()
  984. ->getMock();
  985. $config = $this->getMockBuilder('\OCP\IConfig')
  986. ->disableOriginalConstructor()
  987. ->getMock();
  988. \OC\Share\Share::setPassword($userSession, $connection, $config, 1, 'pass');
  989. }
  990. /**
  991. * Test setting a password when everything is fine
  992. */
  993. public function testSetPassword() {
  994. $user = $this->getMockBuilder('\OCP\IUser')
  995. ->disableOriginalConstructor()
  996. ->getMock();
  997. $user->method('getUID')->willReturn('user');
  998. $userSession = $this->getMockBuilder('\OCP\IUserSession')
  999. ->disableOriginalConstructor()
  1000. ->getMock();
  1001. $userSession->method('getUser')->willReturn($user);
  1002. $ex = $this->getMockBuilder('\Doctrine\DBAL\Query\Expression\ExpressionBuilder')
  1003. ->disableOriginalConstructor()
  1004. ->getMock();
  1005. $qb = $this->getMockBuilder('\Doctrine\DBAL\Query\QueryBuilder')
  1006. ->disableOriginalConstructor()
  1007. ->getMock();
  1008. $qb->method('update')->will($this->returnSelf());
  1009. $qb->method('set')->will($this->returnSelf());
  1010. $qb->method('where')->will($this->returnSelf());
  1011. $qb->method('andWhere')->will($this->returnSelf());
  1012. $qb->method('select')->will($this->returnSelf());
  1013. $qb->method('from')->will($this->returnSelf());
  1014. $qb->method('setParameter')->will($this->returnSelf());
  1015. $qb->method('expr')->willReturn($ex);
  1016. $ret = $this->getMockBuilder('\Doctrine\DBAL\Driver\ResultStatement')
  1017. ->disableOriginalConstructor()
  1018. ->getMock();
  1019. $ret->method('fetch')->willReturn(['uid_owner' => 'user']);
  1020. $qb->method('execute')->willReturn($ret);
  1021. $connection = $this->getMockBuilder('\OC\DB\Connection')
  1022. ->disableOriginalConstructor()
  1023. ->getMock();
  1024. $connection->method('createQueryBuilder')->willReturn($qb);
  1025. $config = $this->getMockBuilder('\OCP\IConfig')
  1026. ->disableOriginalConstructor()
  1027. ->getMock();
  1028. $res = \OC\Share\Share::setPassword($userSession, $connection, $config, 1, 'pass');
  1029. $this->assertTrue($res);
  1030. }
  1031. /**
  1032. * @expectedException Exception
  1033. * @expectedExceptionMessage Cannot remove password
  1034. *
  1035. * Test removing a password when password is enforced
  1036. */
  1037. public function testSetPasswordRemove() {
  1038. $user = $this->getMockBuilder('\OCP\IUser')
  1039. ->disableOriginalConstructor()
  1040. ->getMock();
  1041. $user->method('getUID')->willReturn('user');
  1042. $userSession = $this->getMockBuilder('\OCP\IUserSession')
  1043. ->disableOriginalConstructor()
  1044. ->getMock();
  1045. $userSession->method('getUser')->willReturn($user);
  1046. $ex = $this->getMockBuilder('\Doctrine\DBAL\Query\Expression\ExpressionBuilder')
  1047. ->disableOriginalConstructor()
  1048. ->getMock();
  1049. $qb = $this->getMockBuilder('\Doctrine\DBAL\Query\QueryBuilder')
  1050. ->disableOriginalConstructor()
  1051. ->getMock();
  1052. $qb->method('update')->will($this->returnSelf());
  1053. $qb->method('select')->will($this->returnSelf());
  1054. $qb->method('from')->will($this->returnSelf());
  1055. $qb->method('set')->will($this->returnSelf());
  1056. $qb->method('where')->will($this->returnSelf());
  1057. $qb->method('andWhere')->will($this->returnSelf());
  1058. $qb->method('setParameter')->will($this->returnSelf());
  1059. $qb->method('expr')->willReturn($ex);
  1060. $ret = $this->getMockBuilder('\Doctrine\DBAL\Driver\ResultStatement')
  1061. ->disableOriginalConstructor()
  1062. ->getMock();
  1063. $ret->method('fetch')->willReturn(['uid_owner' => 'user']);
  1064. $qb->method('execute')->willReturn($ret);
  1065. $connection = $this->getMockBuilder('\OC\DB\Connection')
  1066. ->disableOriginalConstructor()
  1067. ->getMock();
  1068. $connection->method('createQueryBuilder')->willReturn($qb);
  1069. $config = $this->getMockBuilder('\OCP\IConfig')
  1070. ->disableOriginalConstructor()
  1071. ->getMock();
  1072. $config->method('getAppValue')->willReturn('yes');
  1073. \OC\Share\Share::setPassword($userSession, $connection, $config, 1, '');
  1074. }
  1075. /**
  1076. * @expectedException Exception
  1077. * @expectedExceptionMessage Share not found
  1078. *
  1079. * Test modification of invaid share
  1080. */
  1081. public function testSetPasswordInvalidShare() {
  1082. $user = $this->getMockBuilder('\OCP\IUser')
  1083. ->disableOriginalConstructor()
  1084. ->getMock();
  1085. $user->method('getUID')->willReturn('user');
  1086. $userSession = $this->getMockBuilder('\OCP\IUserSession')
  1087. ->disableOriginalConstructor()
  1088. ->getMock();
  1089. $userSession->method('getUser')->willReturn($user);
  1090. $ex = $this->getMockBuilder('\Doctrine\DBAL\Query\Expression\ExpressionBuilder')
  1091. ->disableOriginalConstructor()
  1092. ->getMock();
  1093. $qb = $this->getMockBuilder('\Doctrine\DBAL\Query\QueryBuilder')
  1094. ->disableOriginalConstructor()
  1095. ->getMock();
  1096. $qb->method('update')->will($this->returnSelf());
  1097. $qb->method('set')->will($this->returnSelf());
  1098. $qb->method('where')->will($this->returnSelf());
  1099. $qb->method('andWhere')->will($this->returnSelf());
  1100. $qb->method('select')->will($this->returnSelf());
  1101. $qb->method('from')->will($this->returnSelf());
  1102. $qb->method('setParameter')->will($this->returnSelf());
  1103. $qb->method('expr')->willReturn($ex);
  1104. $ret = $this->getMockBuilder('\Doctrine\DBAL\Driver\ResultStatement')
  1105. ->disableOriginalConstructor()
  1106. ->getMock();
  1107. $ret->method('fetch')->willReturn([]);
  1108. $qb->method('execute')->willReturn($ret);
  1109. $connection = $this->getMockBuilder('\OC\DB\Connection')
  1110. ->disableOriginalConstructor()
  1111. ->getMock();
  1112. $connection->method('createQueryBuilder')->willReturn($qb);
  1113. $config = $this->getMockBuilder('\OCP\IConfig')
  1114. ->disableOriginalConstructor()
  1115. ->getMock();
  1116. \OC\Share\Share::setPassword($userSession, $connection, $config, 1, 'pass');
  1117. }
  1118. /**
  1119. * @expectedException Exception
  1120. * @expectedExceptionMessage Cannot update share of a different user
  1121. *
  1122. * Test modification of share of another user
  1123. */
  1124. public function testSetPasswordShareOtherUser() {
  1125. $user = $this->getMockBuilder('\OCP\IUser')
  1126. ->disableOriginalConstructor()
  1127. ->getMock();
  1128. $user->method('getUID')->willReturn('user');
  1129. $userSession = $this->getMockBuilder('\OCP\IUserSession')
  1130. ->disableOriginalConstructor()
  1131. ->getMock();
  1132. $userSession->method('getUser')->willReturn($user);
  1133. $ex = $this->getMockBuilder('\Doctrine\DBAL\Query\Expression\ExpressionBuilder')
  1134. ->disableOriginalConstructor()
  1135. ->getMock();
  1136. $qb = $this->getMockBuilder('\Doctrine\DBAL\Query\QueryBuilder')
  1137. ->disableOriginalConstructor()
  1138. ->getMock();
  1139. $qb->method('update')->will($this->returnSelf());
  1140. $qb->method('set')->will($this->returnSelf());
  1141. $qb->method('where')->will($this->returnSelf());
  1142. $qb->method('andWhere')->will($this->returnSelf());
  1143. $qb->method('select')->will($this->returnSelf());
  1144. $qb->method('from')->will($this->returnSelf());
  1145. $qb->method('setParameter')->will($this->returnSelf());
  1146. $qb->method('expr')->willReturn($ex);
  1147. $ret = $this->getMockBuilder('\Doctrine\DBAL\Driver\ResultStatement')
  1148. ->disableOriginalConstructor()
  1149. ->getMock();
  1150. $ret->method('fetch')->willReturn(['uid_owner' => 'user2']);
  1151. $qb->method('execute')->willReturn($ret);
  1152. $connection = $this->getMockBuilder('\OC\DB\Connection')
  1153. ->disableOriginalConstructor()
  1154. ->getMock();
  1155. $connection->method('createQueryBuilder')->willReturn($qb);
  1156. $config = $this->getMockBuilder('\OCP\IConfig')
  1157. ->disableOriginalConstructor()
  1158. ->getMock();
  1159. \OC\Share\Share::setPassword($userSession, $connection, $config, 1, 'pass');
  1160. }
  1161. }
  1162. class DummyShareClass extends \OC\Share\Share {
  1163. public static function groupItemsTest($items) {
  1164. return parent::groupItems($items, 'test');
  1165. }
  1166. }
  1167. class DummyHookListener {
  1168. static $shareType = null;
  1169. public static function listen($params) {
  1170. self::$shareType = $params['shareType'];
  1171. }
  1172. }