QuotaPluginTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  28. use OC\Files\View;
  29. use OCA\DAV\Connector\Sabre\Directory;
  30. use OCA\DAV\Connector\Sabre\QuotaPlugin;
  31. use OCA\DAV\Files\FilesHome;
  32. use OCP\Files\FileInfo;
  33. use Sabre\DAV\Exception\InsufficientStorage;
  34. use Sabre\DAV\Tree;
  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(null, null, $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(null, null, $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(null, null, $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(null, null, $headers);
  97. $result = $this->plugin->checkQuota('/sub/test.txt');
  98. $this->assertTrue($result);
  99. }
  100. public function quotaOkayProvider() {
  101. return array(
  102. array(1024, array()),
  103. array(1024, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  104. array(1024, array('CONTENT-LENGTH' => '512')),
  105. array(1024, array('OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512')),
  106. array(FileInfo::SPACE_UNKNOWN, array()),
  107. array(FileInfo::SPACE_UNKNOWN, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  108. array(FileInfo::SPACE_UNKNOWN, array('CONTENT-LENGTH' => '512')),
  109. array(FileInfo::SPACE_UNKNOWN, array('OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512')),
  110. array(FileInfo::SPACE_UNLIMITED, array()),
  111. array(FileInfo::SPACE_UNLIMITED, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  112. array(FileInfo::SPACE_UNLIMITED, array('CONTENT-LENGTH' => '512')),
  113. array(FileInfo::SPACE_UNLIMITED, array('OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512')),
  114. );
  115. }
  116. public function quotaExceededProvider() {
  117. return array(
  118. array(1023, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  119. array(511, array('CONTENT-LENGTH' => '512')),
  120. array(2047, array('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 array(
  140. array(1024, 0, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  141. array(1024, 0, array('CONTENT-LENGTH' => '512')),
  142. array(1024, 0, array('OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512')),
  143. // with existing chunks (allowed size = total length - chunk total size)
  144. array(400, 128, array('X-EXPECTED-ENTITY-LENGTH' => '512')),
  145. array(400, 128, array('CONTENT-LENGTH' => '512')),
  146. array(400, 128, array('OC-TOTAL-LENGTH' => '512', 'CONTENT-LENGTH' => '500')),
  147. // \OCP\Files\FileInfo::SPACE-UNKNOWN = -2
  148. array(-2, 0, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  149. array(-2, 0, array('CONTENT-LENGTH' => '512')),
  150. array(-2, 0, array('OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512')),
  151. array(-2, 128, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  152. array(-2, 128, array('CONTENT-LENGTH' => '512')),
  153. array(-2, 128, array('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. ->will($this->returnValue($chunkTotalSize));
  167. $this->plugin->expects($this->once())
  168. ->method('getFileChunking')
  169. ->will($this->returnValue($mockChunking));
  170. $headers['OC-CHUNKED'] = 1;
  171. $this->server->httpRequest = new \Sabre\HTTP\Request(null, null, $headers);
  172. $result = $this->plugin->checkQuota('/sub/test.txt-chunking-12345-3-1');
  173. $this->assertTrue($result);
  174. }
  175. public function quotaChunkedFailProvider() {
  176. return array(
  177. array(400, 0, array('X-EXPECTED-ENTITY-LENGTH' => '1024')),
  178. array(400, 0, array('CONTENT-LENGTH' => '512')),
  179. array(400, 0, array('OC-TOTAL-LENGTH' => '1024', 'CONTENT-LENGTH' => '512')),
  180. // with existing chunks (allowed size = total length - chunk total size)
  181. array(380, 128, array('X-EXPECTED-ENTITY-LENGTH' => '512')),
  182. array(380, 128, array('CONTENT-LENGTH' => '512')),
  183. array(380, 128, array('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. ->will($this->returnValue($chunkTotalSize));
  198. $this->plugin->expects($this->once())
  199. ->method('getFileChunking')
  200. ->will($this->returnValue($mockChunking));
  201. $headers['OC-CHUNKED'] = 1;
  202. $this->server->httpRequest = new \Sabre\HTTP\Request(null, null, $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. ->will($this->returnValue($quota));
  215. return $view;
  216. }
  217. }