NodeTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  29. use OC\Files\FileInfo;
  30. use OC\Files\Mount\MountPoint;
  31. use OC\Files\View;
  32. use OC\Share20\ShareAttributes;
  33. use OCA\Files_Sharing\SharedMount;
  34. use OCA\Files_Sharing\SharedStorage;
  35. use OCP\Constants;
  36. use OCP\Files\Cache\ICacheEntry;
  37. use OCP\Files\Mount\IMountPoint;
  38. use OCP\Files\Storage;
  39. use OCP\ICache;
  40. use OCP\Share\IManager;
  41. use OCP\Share\IShare;
  42. /**
  43. * Class NodeTest
  44. *
  45. * @group DB
  46. * @package OCA\DAV\Tests\unit\Connector\Sabre
  47. */
  48. class NodeTest extends \Test\TestCase {
  49. public function davPermissionsProvider() {
  50. return [
  51. [Constants::PERMISSION_ALL, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RGDNVW'],
  52. [Constants::PERMISSION_ALL, 'dir', false, Constants::PERMISSION_ALL, false, 'test', 'RGDNVCK'],
  53. [Constants::PERMISSION_ALL, 'file', true, Constants::PERMISSION_ALL, false, 'test', 'SRGDNVW'],
  54. [Constants::PERMISSION_ALL, 'file', true, Constants::PERMISSION_ALL, true, 'test', 'SRMGDNVW'],
  55. [Constants::PERMISSION_ALL, 'file', true, Constants::PERMISSION_ALL, true, '' , 'SRMGDNVW'],
  56. [Constants::PERMISSION_ALL, 'file', true, Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE, true, '' , 'SRMGDNV'],
  57. [Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE, 'file', true, Constants::PERMISSION_ALL, false, 'test', 'SGDNVW'],
  58. [Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RGD'],
  59. [Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RGNVW'],
  60. [Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RGDNVW'],
  61. [Constants::PERMISSION_ALL - Constants::PERMISSION_READ, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RDNVW'],
  62. [Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, 'dir', false, Constants::PERMISSION_ALL, false, 'test', 'RGDNV'],
  63. [Constants::PERMISSION_ALL - Constants::PERMISSION_READ, 'dir', false, Constants::PERMISSION_ALL, false, 'test', 'RDNVCK'],
  64. ];
  65. }
  66. /**
  67. * @dataProvider davPermissionsProvider
  68. */
  69. public function testDavPermissions($permissions, $type, $shared, $shareRootPermissions, $mounted, $internalPath, $expected): void {
  70. $info = $this->getMockBuilder(FileInfo::class)
  71. ->disableOriginalConstructor()
  72. ->onlyMethods(['getPermissions', 'isShared', 'isMounted', 'getType', 'getInternalPath', 'getStorage', 'getMountPoint'])
  73. ->getMock();
  74. $info->method('getPermissions')
  75. ->willReturn($permissions);
  76. $info->method('isShared')
  77. ->willReturn($shared);
  78. $info->method('isMounted')
  79. ->willReturn($mounted);
  80. $info->method('getType')
  81. ->willReturn($type);
  82. $info->method('getInternalPath')
  83. ->willReturn($internalPath);
  84. $info->method('getMountPoint')
  85. ->willReturnCallback(function () use ($shared) {
  86. if ($shared) {
  87. return $this->createMock(SharedMount::class);
  88. } else {
  89. return $this->createMock(MountPoint::class);
  90. }
  91. });
  92. $storage = $this->createMock(Storage\IStorage::class);
  93. if ($shared) {
  94. $storage->method('instanceOfStorage')
  95. ->willReturn(true);
  96. $cache = $this->createMock(ICache::class);
  97. $storage->method('getCache')
  98. ->willReturn($cache);
  99. $shareRootEntry = $this->createMock(ICacheEntry::class);
  100. $cache->method('get')
  101. ->willReturn($shareRootEntry);
  102. $shareRootEntry->method('getPermissions')
  103. ->willReturn($shareRootPermissions);
  104. } else {
  105. $storage->method('instanceOfStorage')
  106. ->willReturn(false);
  107. }
  108. $info->method('getStorage')
  109. ->willReturn($storage);
  110. $view = $this->getMockBuilder(View::class)
  111. ->disableOriginalConstructor()
  112. ->getMock();
  113. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  114. $this->assertEquals($expected, $node->getDavPermissions());
  115. }
  116. public function sharePermissionsProvider() {
  117. return [
  118. [\OCP\Files\FileInfo::TYPE_FILE, null, 1, 1],
  119. [\OCP\Files\FileInfo::TYPE_FILE, null, 3, 3],
  120. [\OCP\Files\FileInfo::TYPE_FILE, null, 5, 1],
  121. [\OCP\Files\FileInfo::TYPE_FILE, null, 7, 3],
  122. [\OCP\Files\FileInfo::TYPE_FILE, null, 9, 1],
  123. [\OCP\Files\FileInfo::TYPE_FILE, null, 11, 3],
  124. [\OCP\Files\FileInfo::TYPE_FILE, null, 13, 1],
  125. [\OCP\Files\FileInfo::TYPE_FILE, null, 15, 3],
  126. [\OCP\Files\FileInfo::TYPE_FILE, null, 17, 17],
  127. [\OCP\Files\FileInfo::TYPE_FILE, null, 19, 19],
  128. [\OCP\Files\FileInfo::TYPE_FILE, null, 21, 17],
  129. [\OCP\Files\FileInfo::TYPE_FILE, null, 23, 19],
  130. [\OCP\Files\FileInfo::TYPE_FILE, null, 25, 17],
  131. [\OCP\Files\FileInfo::TYPE_FILE, null, 27, 19],
  132. [\OCP\Files\FileInfo::TYPE_FILE, null, 29, 17],
  133. [\OCP\Files\FileInfo::TYPE_FILE, null, 30, 18],
  134. [\OCP\Files\FileInfo::TYPE_FILE, null, 31, 19],
  135. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 1, 1],
  136. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 3, 3],
  137. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 5, 5],
  138. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 7, 7],
  139. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 9, 9],
  140. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 11, 11],
  141. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 13, 13],
  142. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 15, 15],
  143. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 17, 17],
  144. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 19, 19],
  145. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 21, 21],
  146. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 23, 23],
  147. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 25, 25],
  148. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 27, 27],
  149. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 29, 29],
  150. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 30, 30],
  151. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 31, 31],
  152. [\OCP\Files\FileInfo::TYPE_FOLDER, 'shareToken', 7, 7],
  153. ];
  154. }
  155. /**
  156. * @dataProvider sharePermissionsProvider
  157. */
  158. public function testSharePermissions($type, $user, $permissions, $expected): void {
  159. $storage = $this->getMockBuilder(Storage::class)
  160. ->disableOriginalConstructor()
  161. ->getMock();
  162. $storage->method('getPermissions')->willReturn($permissions);
  163. $mountpoint = $this->getMockBuilder(IMountPoint::class)
  164. ->disableOriginalConstructor()
  165. ->getMock();
  166. $mountpoint->method('getMountPoint')->willReturn('myPath');
  167. $shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  168. $share = $this->getMockBuilder(IShare::class)->disableOriginalConstructor()->getMock();
  169. if ($user === null) {
  170. $shareManager->expects($this->never())->method('getShareByToken');
  171. $share->expects($this->never())->method('getPermissions');
  172. } else {
  173. $shareManager->expects($this->once())->method('getShareByToken')->with($user)
  174. ->willReturn($share);
  175. $share->expects($this->once())->method('getPermissions')->willReturn($permissions);
  176. }
  177. $info = $this->getMockBuilder(FileInfo::class)
  178. ->disableOriginalConstructor()
  179. ->setMethods(['getStorage', 'getType', 'getMountPoint', 'getPermissions'])
  180. ->getMock();
  181. $info->method('getStorage')->willReturn($storage);
  182. $info->method('getType')->willReturn($type);
  183. $info->method('getMountPoint')->willReturn($mountpoint);
  184. $info->method('getPermissions')->willReturn($permissions);
  185. $view = $this->getMockBuilder(View::class)
  186. ->disableOriginalConstructor()
  187. ->getMock();
  188. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  189. $this->invokePrivate($node, 'shareManager', [$shareManager]);
  190. $this->assertEquals($expected, $node->getSharePermissions($user));
  191. }
  192. public function testShareAttributes(): void {
  193. $storage = $this->getMockBuilder(SharedStorage::class)
  194. ->disableOriginalConstructor()
  195. ->setMethods(['getShare'])
  196. ->getMock();
  197. $shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  198. $share = $this->getMockBuilder(IShare::class)->disableOriginalConstructor()->getMock();
  199. $storage->expects($this->once())
  200. ->method('getShare')
  201. ->willReturn($share);
  202. $attributes = new ShareAttributes();
  203. $attributes->setAttribute('permissions', 'download', false);
  204. $share->expects($this->once())->method('getAttributes')->willReturn($attributes);
  205. $info = $this->getMockBuilder(FileInfo::class)
  206. ->disableOriginalConstructor()
  207. ->setMethods(['getStorage', 'getType'])
  208. ->getMock();
  209. $info->method('getStorage')->willReturn($storage);
  210. $info->method('getType')->willReturn(FileInfo::TYPE_FOLDER);
  211. $view = $this->getMockBuilder(View::class)
  212. ->disableOriginalConstructor()
  213. ->getMock();
  214. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  215. $this->invokePrivate($node, 'shareManager', [$shareManager]);
  216. $this->assertEquals($attributes->toArray(), $node->getShareAttributes());
  217. }
  218. public function testShareAttributesNonShare(): void {
  219. $storage = $this->getMockBuilder(Storage::class)
  220. ->disableOriginalConstructor()
  221. ->getMock();
  222. $shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  223. $info = $this->getMockBuilder(FileInfo::class)
  224. ->disableOriginalConstructor()
  225. ->setMethods(['getStorage', 'getType'])
  226. ->getMock();
  227. $info->method('getStorage')->willReturn($storage);
  228. $info->method('getType')->willReturn(FileInfo::TYPE_FOLDER);
  229. $view = $this->getMockBuilder(View::class)
  230. ->disableOriginalConstructor()
  231. ->getMock();
  232. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  233. $this->invokePrivate($node, 'shareManager', [$shareManager]);
  234. $this->assertEquals([], $node->getShareAttributes());
  235. }
  236. public function sanitizeMtimeProvider() {
  237. return [
  238. [123456789, 123456789],
  239. ['987654321', 987654321],
  240. ];
  241. }
  242. /**
  243. * @dataProvider sanitizeMtimeProvider
  244. */
  245. public function testSanitizeMtime($mtime, $expected): void {
  246. $view = $this->getMockBuilder(View::class)
  247. ->disableOriginalConstructor()
  248. ->getMock();
  249. $info = $this->getMockBuilder(FileInfo::class)
  250. ->disableOriginalConstructor()
  251. ->getMock();
  252. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  253. $result = $this->invokePrivate($node, 'sanitizeMtime', [$mtime]);
  254. $this->assertEquals($expected, $result);
  255. }
  256. public function invalidSanitizeMtimeProvider() {
  257. return [
  258. [-1337], [0], ['abcdef'], ['-1337'], ['0'], [12321], [24 * 60 * 60 - 1],
  259. ];
  260. }
  261. /**
  262. * @dataProvider invalidSanitizeMtimeProvider
  263. */
  264. public function testInvalidSanitizeMtime($mtime): void {
  265. $this->expectException(\InvalidArgumentException::class);
  266. $view = $this->getMockBuilder(View::class)
  267. ->disableOriginalConstructor()
  268. ->getMock();
  269. $info = $this->getMockBuilder(FileInfo::class)
  270. ->disableOriginalConstructor()
  271. ->getMock();
  272. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  273. $result = $this->invokePrivate($node, 'sanitizeMtime', [$mtime]);
  274. }
  275. }