您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

QuotaPluginTest.php 8.2KB

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