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.

quota.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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, 'unencrypted_size' => 0)
  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, 'unencrypted_size' => 0)
  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, 'unencrypted_size' => 3)
  75. );
  76. $this->assertEquals(6, $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 testReturnFalseWhenFopenFailed() {
  87. $failStorage = $this->getMock(
  88. '\OC\Files\Storage\Local',
  89. array('fopen'),
  90. array(array('datadir' => $this->tmpDir)));
  91. $failStorage->expects($this->any())
  92. ->method('fopen')
  93. ->will($this->returnValue(false));
  94. $instance = new \OC\Files\Storage\Wrapper\Quota(array('storage' => $failStorage, 'quota' => 1000));
  95. $this->assertFalse($instance->fopen('failedfopen', 'r'));
  96. }
  97. public function testReturnRegularStreamOnRead() {
  98. $instance = $this->getLimitedStorage(9);
  99. // create test file first
  100. $stream = $instance->fopen('foo', 'w+');
  101. fwrite($stream, 'blablacontent');
  102. fclose($stream);
  103. $stream = $instance->fopen('foo', 'r');
  104. $meta = stream_get_meta_data($stream);
  105. $this->assertEquals('plainfile', $meta['wrapper_type']);
  106. fclose($stream);
  107. $stream = $instance->fopen('foo', 'rb');
  108. $meta = stream_get_meta_data($stream);
  109. $this->assertEquals('plainfile', $meta['wrapper_type']);
  110. fclose($stream);
  111. }
  112. public function testReturnQuotaStreamOnWrite() {
  113. $instance = $this->getLimitedStorage(9);
  114. $stream = $instance->fopen('foo', 'w+');
  115. $meta = stream_get_meta_data($stream);
  116. $this->assertEquals('user-space', $meta['wrapper_type']);
  117. fclose($stream);
  118. }
  119. public function testSpaceRoot() {
  120. $storage = $this->getMockBuilder('\OC\Files\Storage\Local')->disableOriginalConstructor()->getMock();
  121. $cache = $this->getMockBuilder('\OC\Files\Cache\Cache')->disableOriginalConstructor()->getMock();
  122. $storage->expects($this->once())
  123. ->method('getCache')
  124. ->will($this->returnValue($cache));
  125. $storage->expects($this->once())
  126. ->method('free_space')
  127. ->will($this->returnValue(2048));
  128. $cache->expects($this->once())
  129. ->method('get')
  130. ->with('files')
  131. ->will($this->returnValue(array('size' => 50)));
  132. $instance = new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => 1024, 'root' => 'files'));
  133. $this->assertEquals(1024 - 50, $instance->free_space(''));
  134. }
  135. public function testInstanceOfStorageWrapper() {
  136. $this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Local'));
  137. $this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Wrapper\Wrapper'));
  138. $this->assertTrue($this->instance->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota'));
  139. }
  140. }