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

NodeTest.php 10.0KB

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