QuotaTest.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Files\Storage\Wrapper;
  9. //ensure the constants are loaded
  10. use OC\Files\Cache\CacheEntry;
  11. use OC\Files\Storage\Local;
  12. \OC::$loader->load('\OC\Files\Filesystem');
  13. /**
  14. * Class QuotaTest
  15. *
  16. * @group DB
  17. *
  18. * @package Test\Files\Storage\Wrapper
  19. */
  20. class QuotaTest extends \Test\Files\Storage\Storage {
  21. /**
  22. * @var string tmpDir
  23. */
  24. private $tmpDir;
  25. protected function setUp() {
  26. parent::setUp();
  27. $this->tmpDir = \OC::$server->getTempManager()->getTemporaryFolder();
  28. $storage = new \OC\Files\Storage\Local(array('datadir' => $this->tmpDir));
  29. $this->instance = new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => 10000000));
  30. }
  31. protected function tearDown() {
  32. \OC_Helper::rmdirr($this->tmpDir);
  33. parent::tearDown();
  34. }
  35. /**
  36. * @param integer $limit
  37. */
  38. protected function getLimitedStorage($limit) {
  39. $storage = new \OC\Files\Storage\Local(array('datadir' => $this->tmpDir));
  40. $storage->mkdir('files');
  41. $storage->getScanner()->scan('');
  42. return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $limit));
  43. }
  44. public function testFilePutContentsNotEnoughSpace() {
  45. $instance = $this->getLimitedStorage(3);
  46. $this->assertFalse($instance->file_put_contents('files/foo', 'foobar'));
  47. }
  48. public function testCopyNotEnoughSpace() {
  49. $instance = $this->getLimitedStorage(9);
  50. $this->assertEquals(6, $instance->file_put_contents('files/foo', 'foobar'));
  51. $instance->getScanner()->scan('');
  52. $this->assertFalse($instance->copy('files/foo', 'files/bar'));
  53. }
  54. public function testFreeSpace() {
  55. $instance = $this->getLimitedStorage(9);
  56. $this->assertEquals(9, $instance->free_space(''));
  57. }
  58. public function testFreeSpaceWithUsedSpace() {
  59. $instance = $this->getLimitedStorage(9);
  60. $instance->getCache()->put(
  61. '', array('size' => 3)
  62. );
  63. $this->assertEquals(6, $instance->free_space(''));
  64. }
  65. public function testFreeSpaceWithUnknownDiskSpace() {
  66. $storage = $this->getMockBuilder(Local::class)
  67. ->setMethods(['free_space'])
  68. ->setConstructorArgs([['datadir' => $this->tmpDir]])
  69. ->getMock();
  70. $storage->expects($this->any())
  71. ->method('free_space')
  72. ->will($this->returnValue(-2));
  73. $storage->getScanner()->scan('');
  74. $instance = new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => 9));
  75. $instance->getCache()->put(
  76. '', array('size' => 3)
  77. );
  78. $this->assertEquals(6, $instance->free_space(''));
  79. }
  80. public function testFreeSpaceWithUsedSpaceAndEncryption() {
  81. $instance = $this->getLimitedStorage(9);
  82. $instance->getCache()->put(
  83. '', array('size' => 7)
  84. );
  85. $this->assertEquals(2, $instance->free_space(''));
  86. }
  87. public function testFWriteNotEnoughSpace() {
  88. $instance = $this->getLimitedStorage(9);
  89. $stream = $instance->fopen('files/foo', 'w+');
  90. $this->assertEquals(6, fwrite($stream, 'foobar'));
  91. $this->assertEquals(3, fwrite($stream, 'qwerty'));
  92. fclose($stream);
  93. $this->assertEquals('foobarqwe', $instance->file_get_contents('files/foo'));
  94. }
  95. public function testStreamCopyWithEnoughSpace() {
  96. $instance = $this->getLimitedStorage(16);
  97. $inputStream = fopen('data://text/plain,foobarqwerty', 'r');
  98. $outputStream = $instance->fopen('files/foo', 'w+');
  99. list($count, $result) = \OC_Helper::streamCopy($inputStream, $outputStream);
  100. $this->assertEquals(12, $count);
  101. $this->assertTrue($result);
  102. fclose($inputStream);
  103. fclose($outputStream);
  104. }
  105. public function testStreamCopyNotEnoughSpace() {
  106. $instance = $this->getLimitedStorage(9);
  107. $inputStream = fopen('data://text/plain,foobarqwerty', 'r');
  108. $outputStream = $instance->fopen('files/foo', 'w+');
  109. list($count, $result) = \OC_Helper::streamCopy($inputStream, $outputStream);
  110. $this->assertEquals(9, $count);
  111. $this->assertFalse($result);
  112. fclose($inputStream);
  113. fclose($outputStream);
  114. }
  115. public function testReturnFalseWhenFopenFailed() {
  116. $failStorage = $this->getMockBuilder(Local::class)
  117. ->setMethods(['fopen'])
  118. ->setConstructorArgs([['datadir' => $this->tmpDir]])
  119. ->getMock();
  120. $failStorage->expects($this->any())
  121. ->method('fopen')
  122. ->will($this->returnValue(false));
  123. $instance = new \OC\Files\Storage\Wrapper\Quota(array('storage' => $failStorage, 'quota' => 1000));
  124. $this->assertFalse($instance->fopen('failedfopen', 'r'));
  125. }
  126. public function testReturnRegularStreamOnRead() {
  127. $instance = $this->getLimitedStorage(9);
  128. // create test file first
  129. $stream = $instance->fopen('files/foo', 'w+');
  130. fwrite($stream, 'blablacontent');
  131. fclose($stream);
  132. $stream = $instance->fopen('files/foo', 'r');
  133. $meta = stream_get_meta_data($stream);
  134. $this->assertEquals('plainfile', $meta['wrapper_type']);
  135. fclose($stream);
  136. $stream = $instance->fopen('files/foo', 'rb');
  137. $meta = stream_get_meta_data($stream);
  138. $this->assertEquals('plainfile', $meta['wrapper_type']);
  139. fclose($stream);
  140. }
  141. public function testReturnRegularStreamWhenOutsideFiles() {
  142. $instance = $this->getLimitedStorage(9);
  143. $instance->mkdir('files_other');
  144. // create test file first
  145. $stream = $instance->fopen('files_other/foo', 'w+');
  146. $meta = stream_get_meta_data($stream);
  147. $this->assertEquals('plainfile', $meta['wrapper_type']);
  148. fclose($stream);
  149. }
  150. public function testReturnQuotaStreamOnWrite() {
  151. $instance = $this->getLimitedStorage(9);
  152. $stream = $instance->fopen('files/foo', 'w+');
  153. $meta = stream_get_meta_data($stream);
  154. $expected_type = defined('HHVM_VERSION') ? 'File' : 'user-space';
  155. $this->assertEquals($expected_type, $meta['wrapper_type']);
  156. fclose($stream);
  157. }
  158. public function testSpaceRoot() {
  159. $storage = $this->getMockBuilder('\OC\Files\Storage\Local')->disableOriginalConstructor()->getMock();
  160. $cache = $this->getMockBuilder('\OC\Files\Cache\Cache')->disableOriginalConstructor()->getMock();
  161. $storage->expects($this->once())
  162. ->method('getCache')
  163. ->will($this->returnValue($cache));
  164. $storage->expects($this->once())
  165. ->method('free_space')
  166. ->will($this->returnValue(2048));
  167. $cache->expects($this->once())
  168. ->method('get')
  169. ->with('files')
  170. ->will($this->returnValue(new CacheEntry(['size' => 50])));
  171. $instance = new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => 1024, 'root' => 'files'));
  172. $this->assertEquals(1024 - 50, $instance->free_space(''));
  173. }
  174. public function testInstanceOfStorageWrapper() {
  175. $this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Local'));
  176. $this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Wrapper\Wrapper'));
  177. $this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota'));
  178. }
  179. }