quota.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\Stream;
  9. class Quota extends \PHPUnit_Framework_TestCase {
  10. public function tearDown() {
  11. \OC\Files\Stream\Quota::clear();
  12. }
  13. protected function getStream($mode, $limit) {
  14. $source = fopen('php://temp', $mode);
  15. return \OC\Files\Stream\Quota::wrap($source, $limit);
  16. }
  17. public function testWriteEnoughSpace() {
  18. $stream = $this->getStream('w+', 100);
  19. $this->assertEquals(6, fwrite($stream, 'foobar'));
  20. rewind($stream);
  21. $this->assertEquals('foobar', fread($stream, 100));
  22. }
  23. public function testWriteNotEnoughSpace() {
  24. $stream = $this->getStream('w+', 3);
  25. $this->assertEquals(3, fwrite($stream, 'foobar'));
  26. rewind($stream);
  27. $this->assertEquals('foo', fread($stream, 100));
  28. }
  29. public function testWriteNotEnoughSpaceSecondTime() {
  30. $stream = $this->getStream('w+', 9);
  31. $this->assertEquals(6, fwrite($stream, 'foobar'));
  32. $this->assertEquals(3, fwrite($stream, 'qwerty'));
  33. rewind($stream);
  34. $this->assertEquals('foobarqwe', fread($stream, 100));
  35. }
  36. public function testWriteEnoughSpaceRewind() {
  37. $stream = $this->getStream('w+', 6);
  38. $this->assertEquals(6, fwrite($stream, 'foobar'));
  39. rewind($stream);
  40. $this->assertEquals(3, fwrite($stream, 'qwe'));
  41. rewind($stream);
  42. $this->assertEquals('qwebar', fread($stream, 100));
  43. }
  44. public function testWriteNotEnoughSpaceRead() {
  45. $stream = $this->getStream('w+', 6);
  46. $this->assertEquals(6, fwrite($stream, 'foobar'));
  47. rewind($stream);
  48. $this->assertEquals('foobar', fread($stream, 6));
  49. $this->assertEquals(0, fwrite($stream, 'qwe'));
  50. }
  51. public function testWriteNotEnoughSpaceExistingStream() {
  52. $source = fopen('php://temp', 'w+');
  53. fwrite($source, 'foobar');
  54. $stream = \OC\Files\Stream\Quota::wrap($source, 3);
  55. $this->assertEquals(3, fwrite($stream, 'foobar'));
  56. rewind($stream);
  57. $this->assertEquals('foobarfoo', fread($stream, 100));
  58. }
  59. public function testWriteNotEnoughSpaceExistingStreamRewind() {
  60. $source = fopen('php://temp', 'w+');
  61. fwrite($source, 'foobar');
  62. $stream = \OC\Files\Stream\Quota::wrap($source, 3);
  63. rewind($stream);
  64. $this->assertEquals(6, fwrite($stream, 'qwerty'));
  65. rewind($stream);
  66. $this->assertEquals('qwerty', fread($stream, 100));
  67. }
  68. public function testFseekReturnsSuccess() {
  69. $stream = $this->getStream('w+', 100);
  70. fwrite($stream, '0123456789');
  71. $this->assertEquals(0, fseek($stream, 3, SEEK_SET));
  72. $this->assertEquals(0, fseek($stream, -1, SEEK_CUR));
  73. $this->assertEquals(0, fseek($stream, -4, SEEK_END));
  74. }
  75. public function testWriteAfterSeekEndWithEnoughSpace() {
  76. $stream = $this->getStream('w+', 100);
  77. fwrite($stream, '0123456789');
  78. fseek($stream, -3, SEEK_END);
  79. $this->assertEquals(11, fwrite($stream, 'abcdefghijk'));
  80. rewind($stream);
  81. $this->assertEquals('0123456abcdefghijk', fread($stream, 100));
  82. }
  83. public function testWriteAfterSeekEndWithNotEnoughSpace() {
  84. $stream = $this->getStream('w+', 13);
  85. fwrite($stream, '0123456789');
  86. // seek forward first to potentially week out
  87. // potential limit calculation errors
  88. fseek($stream, 4, SEEK_SET);
  89. // seek to the end
  90. fseek($stream, -3, SEEK_END);
  91. $this->assertEquals(6, fwrite($stream, 'abcdefghijk'));
  92. rewind($stream);
  93. $this->assertEquals('0123456abcdef', fread($stream, 100));
  94. }
  95. public function testWriteAfterSeekSetWithEnoughSpace() {
  96. $stream = $this->getStream('w+', 100);
  97. fwrite($stream, '0123456789');
  98. fseek($stream, 7, SEEK_SET);
  99. $this->assertEquals(11, fwrite($stream, 'abcdefghijk'));
  100. rewind($stream);
  101. $this->assertEquals('0123456abcdefghijk', fread($stream, 100));
  102. }
  103. public function testWriteAfterSeekSetWithNotEnoughSpace() {
  104. $stream = $this->getStream('w+', 13);
  105. fwrite($stream, '0123456789');
  106. fseek($stream, 7, SEEK_SET);
  107. $this->assertEquals(6, fwrite($stream, 'abcdefghijk'));
  108. rewind($stream);
  109. $this->assertEquals('0123456abcdef', fread($stream, 100));
  110. }
  111. public function testWriteAfterSeekCurWithEnoughSpace() {
  112. $stream = $this->getStream('w+', 100);
  113. fwrite($stream, '0123456789');
  114. rewind($stream);
  115. fseek($stream, 3, SEEK_CUR);
  116. fseek($stream, 5, SEEK_CUR);
  117. fseek($stream, -1, SEEK_CUR);
  118. $this->assertEquals(11, fwrite($stream, 'abcdefghijk'));
  119. rewind($stream);
  120. $this->assertEquals('0123456abcdefghijk', fread($stream, 100));
  121. }
  122. public function testWriteAfterSeekCurWithNotEnoughSpace() {
  123. $stream = $this->getStream('w+', 13);
  124. fwrite($stream, '0123456789');
  125. rewind($stream);
  126. fseek($stream, 3, SEEK_CUR);
  127. fseek($stream, 5, SEEK_CUR);
  128. fseek($stream, -1, SEEK_CUR);
  129. $this->assertEquals(6, fwrite($stream, 'abcdefghijk'));
  130. rewind($stream);
  131. $this->assertEquals('0123456abcdef', fread($stream, 100));
  132. }
  133. }