QuotaPluginTest.php 7.5 KB

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