1
0

QuotaTest.php 4.7 KB

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