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.

QuotaPluginTest.php 8.3KB

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