quota.php 6.0 KB

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