NodeTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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\IAttributes;
  41. use OCP\Share\IManager;
  42. use OCP\Share\IShare;
  43. /**
  44. * Class NodeTest
  45. *
  46. * @group DB
  47. * @package OCA\DAV\Tests\unit\Connector\Sabre
  48. */
  49. class NodeTest extends \Test\TestCase {
  50. public function davPermissionsProvider() {
  51. return [
  52. [Constants::PERMISSION_ALL, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RGDNVW'],
  53. [Constants::PERMISSION_ALL, 'dir', false, Constants::PERMISSION_ALL, false, 'test', 'RGDNVCK'],
  54. [Constants::PERMISSION_ALL, 'file', true, Constants::PERMISSION_ALL, false, 'test', 'SRGDNVW'],
  55. [Constants::PERMISSION_ALL, 'file', true, Constants::PERMISSION_ALL, true, 'test', 'SRMGDNVW'],
  56. [Constants::PERMISSION_ALL, 'file', true, Constants::PERMISSION_ALL, true, '' , 'SRMGDNVW'],
  57. [Constants::PERMISSION_ALL, 'file', true, Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE, true, '' , 'SRMGDNV'],
  58. [Constants::PERMISSION_ALL - Constants::PERMISSION_SHARE, 'file', true, Constants::PERMISSION_ALL, false, 'test', 'SGDNVW'],
  59. [Constants::PERMISSION_ALL - Constants::PERMISSION_UPDATE, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RGD'],
  60. [Constants::PERMISSION_ALL - Constants::PERMISSION_DELETE, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RGNVW'],
  61. [Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RGDNVW'],
  62. [Constants::PERMISSION_ALL - Constants::PERMISSION_READ, 'file', false, Constants::PERMISSION_ALL, false, 'test', 'RDNVW'],
  63. [Constants::PERMISSION_ALL - Constants::PERMISSION_CREATE, 'dir', false, Constants::PERMISSION_ALL, false, 'test', 'RGDNV'],
  64. [Constants::PERMISSION_ALL - Constants::PERMISSION_READ, 'dir', false, Constants::PERMISSION_ALL, false, 'test', 'RDNVCK'],
  65. ];
  66. }
  67. /**
  68. * @dataProvider davPermissionsProvider
  69. */
  70. public function testDavPermissions($permissions, $type, $shared, $shareRootPermissions, $mounted, $internalPath, $expected): void {
  71. $info = $this->getMockBuilder(FileInfo::class)
  72. ->disableOriginalConstructor()
  73. ->onlyMethods(['getPermissions', 'isShared', 'isMounted', 'getType', 'getInternalPath', 'getStorage', 'getMountPoint'])
  74. ->getMock();
  75. $info->method('getPermissions')
  76. ->willReturn($permissions);
  77. $info->method('isShared')
  78. ->willReturn($shared);
  79. $info->method('isMounted')
  80. ->willReturn($mounted);
  81. $info->method('getType')
  82. ->willReturn($type);
  83. $info->method('getInternalPath')
  84. ->willReturn($internalPath);
  85. $info->method('getMountPoint')
  86. ->willReturnCallback(function() use ($shared) {
  87. if ($shared) {
  88. return $this->createMock(SharedMount::class);
  89. } else {
  90. return $this->createMock(MountPoint::class);
  91. }
  92. });
  93. $storage = $this->createMock(Storage\IStorage::class);
  94. if ($shared) {
  95. $storage->method('instanceOfStorage')
  96. ->willReturn(true);
  97. $cache = $this->createMock(ICache::class);
  98. $storage->method('getCache')
  99. ->willReturn($cache);
  100. $shareRootEntry = $this->createMock(ICacheEntry::class);
  101. $cache->method('get')
  102. ->willReturn($shareRootEntry);
  103. $shareRootEntry->method('getPermissions')
  104. ->willReturn($shareRootPermissions);
  105. } else {
  106. $storage->method('instanceOfStorage')
  107. ->willReturn(false);
  108. }
  109. $info->method('getStorage')
  110. ->willReturn($storage);
  111. $view = $this->getMockBuilder(View::class)
  112. ->disableOriginalConstructor()
  113. ->getMock();
  114. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  115. $this->assertEquals($expected, $node->getDavPermissions());
  116. }
  117. public function sharePermissionsProvider() {
  118. return [
  119. [\OCP\Files\FileInfo::TYPE_FILE, null, 1, 1],
  120. [\OCP\Files\FileInfo::TYPE_FILE, null, 3, 3],
  121. [\OCP\Files\FileInfo::TYPE_FILE, null, 5, 1],
  122. [\OCP\Files\FileInfo::TYPE_FILE, null, 7, 3],
  123. [\OCP\Files\FileInfo::TYPE_FILE, null, 9, 1],
  124. [\OCP\Files\FileInfo::TYPE_FILE, null, 11, 3],
  125. [\OCP\Files\FileInfo::TYPE_FILE, null, 13, 1],
  126. [\OCP\Files\FileInfo::TYPE_FILE, null, 15, 3],
  127. [\OCP\Files\FileInfo::TYPE_FILE, null, 17, 17],
  128. [\OCP\Files\FileInfo::TYPE_FILE, null, 19, 19],
  129. [\OCP\Files\FileInfo::TYPE_FILE, null, 21, 17],
  130. [\OCP\Files\FileInfo::TYPE_FILE, null, 23, 19],
  131. [\OCP\Files\FileInfo::TYPE_FILE, null, 25, 17],
  132. [\OCP\Files\FileInfo::TYPE_FILE, null, 27, 19],
  133. [\OCP\Files\FileInfo::TYPE_FILE, null, 29, 17],
  134. [\OCP\Files\FileInfo::TYPE_FILE, null, 30, 18],
  135. [\OCP\Files\FileInfo::TYPE_FILE, null, 31, 19],
  136. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 1, 1],
  137. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 3, 3],
  138. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 5, 5],
  139. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 7, 7],
  140. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 9, 9],
  141. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 11, 11],
  142. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 13, 13],
  143. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 15, 15],
  144. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 17, 17],
  145. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 19, 19],
  146. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 21, 21],
  147. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 23, 23],
  148. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 25, 25],
  149. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 27, 27],
  150. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 29, 29],
  151. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 30, 30],
  152. [\OCP\Files\FileInfo::TYPE_FOLDER, null, 31, 31],
  153. [\OCP\Files\FileInfo::TYPE_FOLDER, 'shareToken', 7, 7],
  154. ];
  155. }
  156. /**
  157. * @dataProvider sharePermissionsProvider
  158. */
  159. public function testSharePermissions($type, $user, $permissions, $expected): void {
  160. $storage = $this->getMockBuilder(Storage::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $storage->method('getPermissions')->willReturn($permissions);
  164. $mountpoint = $this->getMockBuilder(IMountPoint::class)
  165. ->disableOriginalConstructor()
  166. ->getMock();
  167. $mountpoint->method('getMountPoint')->willReturn('myPath');
  168. $shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  169. $share = $this->getMockBuilder(IShare::class)->disableOriginalConstructor()->getMock();
  170. if ($user === null) {
  171. $shareManager->expects($this->never())->method('getShareByToken');
  172. $share->expects($this->never())->method('getPermissions');
  173. } else {
  174. $shareManager->expects($this->once())->method('getShareByToken')->with($user)
  175. ->willReturn($share);
  176. $share->expects($this->once())->method('getPermissions')->willReturn($permissions);
  177. }
  178. $info = $this->getMockBuilder(FileInfo::class)
  179. ->disableOriginalConstructor()
  180. ->setMethods(['getStorage', 'getType', 'getMountPoint', 'getPermissions'])
  181. ->getMock();
  182. $info->method('getStorage')->willReturn($storage);
  183. $info->method('getType')->willReturn($type);
  184. $info->method('getMountPoint')->willReturn($mountpoint);
  185. $info->method('getPermissions')->willReturn($permissions);
  186. $view = $this->getMockBuilder(View::class)
  187. ->disableOriginalConstructor()
  188. ->getMock();
  189. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  190. $this->invokePrivate($node, 'shareManager', [$shareManager]);
  191. $this->assertEquals($expected, $node->getSharePermissions($user));
  192. }
  193. public function testShareAttributes(): void {
  194. $storage = $this->getMockBuilder(SharedStorage::class)
  195. ->disableOriginalConstructor()
  196. ->setMethods(['getShare'])
  197. ->getMock();
  198. $shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  199. $share = $this->getMockBuilder(IShare::class)->disableOriginalConstructor()->getMock();
  200. $storage->expects($this->once())
  201. ->method('getShare')
  202. ->willReturn($share);
  203. $attributes = new ShareAttributes();
  204. $attributes->setAttribute('permissions', 'download', false);
  205. $share->expects($this->once())->method('getAttributes')->willReturn($attributes);
  206. $info = $this->getMockBuilder(FileInfo::class)
  207. ->disableOriginalConstructor()
  208. ->setMethods(['getStorage', 'getType'])
  209. ->getMock();
  210. $info->method('getStorage')->willReturn($storage);
  211. $info->method('getType')->willReturn(FileInfo::TYPE_FOLDER);
  212. $view = $this->getMockBuilder(View::class)
  213. ->disableOriginalConstructor()
  214. ->getMock();
  215. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  216. $this->invokePrivate($node, 'shareManager', [$shareManager]);
  217. $this->assertEquals($attributes->toArray(), $node->getShareAttributes());
  218. }
  219. public function testShareAttributesNonShare(): void {
  220. $storage = $this->getMockBuilder(Storage::class)
  221. ->disableOriginalConstructor()
  222. ->getMock();
  223. $shareManager = $this->getMockBuilder(IManager::class)->disableOriginalConstructor()->getMock();
  224. $info = $this->getMockBuilder(FileInfo::class)
  225. ->disableOriginalConstructor()
  226. ->setMethods(['getStorage', 'getType'])
  227. ->getMock();
  228. $info->method('getStorage')->willReturn($storage);
  229. $info->method('getType')->willReturn(FileInfo::TYPE_FOLDER);
  230. $view = $this->getMockBuilder(View::class)
  231. ->disableOriginalConstructor()
  232. ->getMock();
  233. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  234. $this->invokePrivate($node, 'shareManager', [$shareManager]);
  235. $this->assertEquals([], $node->getShareAttributes());
  236. }
  237. public function sanitizeMtimeProvider() {
  238. return [
  239. [123456789, 123456789],
  240. ['987654321', 987654321],
  241. ];
  242. }
  243. /**
  244. * @dataProvider sanitizeMtimeProvider
  245. */
  246. public function testSanitizeMtime($mtime, $expected): void {
  247. $view = $this->getMockBuilder(View::class)
  248. ->disableOriginalConstructor()
  249. ->getMock();
  250. $info = $this->getMockBuilder(FileInfo::class)
  251. ->disableOriginalConstructor()
  252. ->getMock();
  253. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  254. $result = $this->invokePrivate($node, 'sanitizeMtime', [$mtime]);
  255. $this->assertEquals($expected, $result);
  256. }
  257. public function invalidSanitizeMtimeProvider() {
  258. return [
  259. [-1337], [0], ['abcdef'], ['-1337'], ['0'], [12321], [24 * 60 * 60 - 1],
  260. ];
  261. }
  262. /**
  263. * @dataProvider invalidSanitizeMtimeProvider
  264. */
  265. public function testInvalidSanitizeMtime($mtime): void {
  266. $this->expectException(\InvalidArgumentException::class);
  267. $view = $this->getMockBuilder(View::class)
  268. ->disableOriginalConstructor()
  269. ->getMock();
  270. $info = $this->getMockBuilder(FileInfo::class)
  271. ->disableOriginalConstructor()
  272. ->getMock();
  273. $node = new \OCA\DAV\Connector\Sabre\File($view, $info);
  274. $result = $this->invokePrivate($node, 'sanitizeMtime', [$mtime]);
  275. }
  276. }