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.

244 lines
8.5 KiB

10 years ago
9 years ago
9 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  27. use OC\Files\View;
  28. use OCA\DAV\Connector\Sabre\Directory;
  29. use OCA\DAV\Connector\Sabre\QuotaPlugin;
  30. use OCA\DAV\Files\FilesHome;
  31. use OCP\Files\FileInfo;
  32. use Sabre\DAV\Exception\InsufficientStorage;
  33. use Sabre\DAV\Tree;
  34. use Test\TestCase;
  35. /**
  36. * Copyright (c) 2013 Thomas Müller <thomas.mueller@tmit.eu>
  37. * This file is licensed under the Affero General Public License version 3 or
  38. * later.
  39. * See the COPYING-README file.
  40. */
  41. class QuotaPluginTest extends TestCase {
  42. /** @var \Sabre\DAV\Server | \PHPUnit_Framework_MockObject_MockObject */
  43. private $server;
  44. /** @var \OCA\DAV\Connector\Sabre\QuotaPlugin | \PHPUnit_Framework_MockObject_MockObject */
  45. private $plugin;
  46. private function init($quota, $checkedPath = '') {
  47. $view = $this->buildFileViewMock($quota, $checkedPath);
  48. $this->server = new \Sabre\DAV\Server();
  49. $this->plugin = $this->getMockBuilder(QuotaPlugin::class)
  50. ->setConstructorArgs([$view])
  51. ->setMethods(['getFileChunking'])
  52. ->getMock();
  53. $this->plugin->initialize($this->server);
  54. }
  55. /**
  56. * @dataProvider lengthProvider
  57. */
  58. public function testLength($expected, $headers) {
  59. $this->init(0);
  60. $this->plugin->expects($this->never())
  61. ->method('getFileChunking');
  62. $this->server->httpRequest = new \Sabre\HTTP\Request(null, null, $headers);
  63. $length = $this->plugin->getLength();
  64. $this->assertEquals($expected, $length);
  65. }
  66. /**
  67. * @dataProvider quotaOkayProvider
  68. */
  69. public function testCheckQuota($quota, $headers) {
  70. $this->init($quota);
  71. $this->plugin->expects($this->never())
  72. ->method('getFileChunking');
  73. $this->server->httpRequest = new \Sabre\HTTP\Request(null, null, $headers);
  74. $result = $this->plugin->checkQuota('');
  75. $this->assertTrue($result);
  76. }
  77. /**
  78. * @expectedException \Sabre\DAV\Exception\InsufficientStorage
  79. * @dataProvider quotaExceededProvider
  80. */
  81. public function testCheckExceededQuota($quota, $headers) {
  82. $this->init($quota);
  83. $this->plugin->expects($this->never())
  84. ->method('getFileChunking');
  85. $this->server->httpRequest = new \Sabre\HTTP\Request(null, null, $headers);
  86. $this->plugin->checkQuota('');
  87. }
  88. /**
  89. * @dataProvider quotaOkayProvider
  90. */
  91. public function testCheckQuotaOnPath($quota, $headers) {
  92. $this->init($quota, 'sub/test.txt');
  93. $this->plugin->expects($this->never())
  94. ->method('getFileChunking');
  95. $this->server->httpRequest = new \Sabre\HTTP\Request(null, null, $headers);
  96. $result = $this->plugin->checkQuota('/sub/test.txt');
  97. $this->assertTrue($result);
  98. }
  99. public function quotaOkayProvider() {
  100. return array(
  101. array(1024, array()),
  102. array(1024, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  103. array(1024, array('CONTENT-LENGTH' => '512')),
  104. array(1024, array('OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512')),
  105. array(FileInfo::SPACE_UNKNOWN, array()),
  106. array(FileInfo::SPACE_UNKNOWN, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  107. array(FileInfo::SPACE_UNKNOWN, array('CONTENT-LENGTH' => '512')),
  108. array(FileInfo::SPACE_UNKNOWN, array('OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512')),
  109. array(FileInfo::SPACE_UNLIMITED, array()),
  110. array(FileInfo::SPACE_UNLIMITED, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  111. array(FileInfo::SPACE_UNLIMITED, array('CONTENT-LENGTH' => '512')),
  112. array(FileInfo::SPACE_UNLIMITED, array('OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512')),
  113. );
  114. }
  115. public function quotaExceededProvider() {
  116. return array(
  117. array(1023, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  118. array(511, array('CONTENT-LENGTH' => '512')),
  119. array(2047, array('OC-TOTAL-LENGTH' => '2048', 'CONTENT-LENGTH' => '1024')),
  120. );
  121. }
  122. public function lengthProvider() {
  123. return [
  124. [null, []],
  125. [1024, ['X-EXPECTED-ENTITY-LENGTH' => '1024']],
  126. [512, ['CONTENT-LENGTH' => '512']],
  127. [2048, ['OC-TOTAL-LENGTH' => '2048', 'CONTENT-LENGTH' => '1024']],
  128. [4096, ['OC-TOTAL-LENGTH' => '2048', 'X-EXPECTED-ENTITY-LENGTH' => '4096']],
  129. [null, ['X-EXPECTED-ENTITY-LENGTH' => 'A']],
  130. [null, ['CONTENT-LENGTH' => 'A']],
  131. [1024, ['OC-TOTAL-LENGTH' => 'A', 'CONTENT-LENGTH' => '1024']],
  132. [1024, ['OC-TOTAL-LENGTH' => 'A', 'X-EXPECTED-ENTITY-LENGTH' => '1024']],
  133. [null, ['OC-TOTAL-LENGTH' => '2048', 'X-EXPECTED-ENTITY-LENGTH' => 'A']],
  134. [null, ['OC-TOTAL-LENGTH' => '2048', 'CONTENT-LENGTH' => 'A']],
  135. ];
  136. }
  137. public function quotaChunkedOkProvider() {
  138. return array(
  139. array(1024, 0, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  140. array(1024, 0, array('CONTENT-LENGTH' => '512')),
  141. array(1024, 0, array('OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512')),
  142. // with existing chunks (allowed size = total length - chunk total size)
  143. array(400, 128, array('X-EXPECTED-ENTITY-LENGTH' => '512')),
  144. array(400, 128, array('CONTENT-LENGTH' => '512')),
  145. array(400, 128, array('OC-TOTAL-LENGTH' => '512', 'CONTENT-LENGTH' => '500')),
  146. // \OCP\Files\FileInfo::SPACE-UNKNOWN = -2
  147. array(-2, 0, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  148. array(-2, 0, array('CONTENT-LENGTH' => '512')),
  149. array(-2, 0, array('OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512')),
  150. array(-2, 128, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  151. array(-2, 128, array('CONTENT-LENGTH' => '512')),
  152. array(-2, 128, array('OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512')),
  153. );
  154. }
  155. /**
  156. * @dataProvider quotaChunkedOkProvider
  157. */
  158. public function testCheckQuotaChunkedOk($quota, $chunkTotalSize, $headers) {
  159. $this->init($quota, 'sub/test.txt');
  160. $mockChunking = $this->getMockBuilder(\OC_FileChunking::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $mockChunking->expects($this->once())
  164. ->method('getCurrentSize')
  165. ->will($this->returnValue($chunkTotalSize));
  166. $this->plugin->expects($this->once())
  167. ->method('getFileChunking')
  168. ->will($this->returnValue($mockChunking));
  169. $headers['OC-CHUNKED'] = 1;
  170. $this->server->httpRequest = new \Sabre\HTTP\Request(null, null, $headers);
  171. $result = $this->plugin->checkQuota('/sub/test.txt-chunking-12345-3-1');
  172. $this->assertTrue($result);
  173. }
  174. public function quotaChunkedFailProvider() {
  175. return array(
  176. array(400, 0, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  177. array(400, 0, array('CONTENT-LENGTH' => '512')),
  178. array(400, 0, array('OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512')),
  179. // with existing chunks (allowed size = total length - chunk total size)
  180. array(380, 128, array('X-EXPECTED-ENTITY-LENGTH' => '512')),
  181. array(380, 128, array('CONTENT-LENGTH' => '512')),
  182. array(380, 128, array('OC-TOTAL-LENGTH' => '512', 'CONTENT-LENGTH' => '500')),
  183. );
  184. }
  185. /**
  186. * @dataProvider quotaChunkedFailProvider
  187. * @expectedException \Sabre\DAV\Exception\InsufficientStorage
  188. */
  189. public function testCheckQuotaChunkedFail($quota, $chunkTotalSize, $headers) {
  190. $this->init($quota, 'sub/test.txt');
  191. $mockChunking = $this->getMockBuilder(\OC_FileChunking::class)
  192. ->disableOriginalConstructor()
  193. ->getMock();
  194. $mockChunking->expects($this->once())
  195. ->method('getCurrentSize')
  196. ->will($this->returnValue($chunkTotalSize));
  197. $this->plugin->expects($this->once())
  198. ->method('getFileChunking')
  199. ->will($this->returnValue($mockChunking));
  200. $headers['OC-CHUNKED'] = 1;
  201. $this->server->httpRequest = new \Sabre\HTTP\Request(null, null, $headers);
  202. $this->plugin->checkQuota('/sub/test.txt-chunking-12345-3-1');
  203. }
  204. private function buildFileViewMock($quota, $checkedPath) {
  205. // mock filesysten
  206. $view = $this->getMockBuilder(View::class)
  207. ->setMethods(['free_space'])
  208. ->disableOriginalConstructor()
  209. ->getMock();
  210. $view->expects($this->any())
  211. ->method('free_space')
  212. ->with($this->identicalTo($checkedPath))
  213. ->will($this->returnValue($quota));
  214. return $view;
  215. }
  216. }