1
0

QuotaTest.php 4.8 KB

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