AssemblyStreamTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Markus Goetz <markus@woboq.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\DAV\Tests\unit\Upload;
  30. use Sabre\DAV\File;
  31. class AssemblyStreamTest extends \Test\TestCase {
  32. /**
  33. * @dataProvider providesNodes()
  34. */
  35. public function testGetContents($expected, $nodes): void {
  36. $stream = \OCA\DAV\Upload\AssemblyStream::wrap($nodes);
  37. $content = stream_get_contents($stream);
  38. $this->assertEquals($expected, $content);
  39. }
  40. /**
  41. * @dataProvider providesNodes()
  42. */
  43. public function testGetContentsFread($expected, $nodes): void {
  44. $stream = \OCA\DAV\Upload\AssemblyStream::wrap($nodes);
  45. $content = '';
  46. while (!feof($stream)) {
  47. $content .= fread($stream, 3);
  48. }
  49. $this->assertEquals($expected, $content);
  50. }
  51. /**
  52. * @dataProvider providesNodes()
  53. */
  54. public function testSeek($expected, $nodes): void {
  55. $stream = \OCA\DAV\Upload\AssemblyStream::wrap($nodes);
  56. $offset = floor(strlen($expected) * 0.6);
  57. if (fseek($stream, $offset) === -1) {
  58. $this->fail('fseek failed');
  59. }
  60. $content = stream_get_contents($stream);
  61. $this->assertEquals(substr($expected, $offset), $content);
  62. }
  63. public function providesNodes() {
  64. $data8k = $this->makeData(8192);
  65. $dataLess8k = $this->makeData(8191);
  66. $tonofnodes = [];
  67. $tonofdata = "";
  68. for ($i = 0; $i < 101; $i++) {
  69. $thisdata = rand(0,100); // variable length and content
  70. $tonofdata .= $thisdata;
  71. array_push($tonofnodes, $this->buildNode($i,$thisdata));
  72. }
  73. return[
  74. 'one node zero bytes' => [
  75. '', [
  76. $this->buildNode('0', '')
  77. ]],
  78. 'one node only' => [
  79. '1234567890', [
  80. $this->buildNode('0', '1234567890')
  81. ]],
  82. 'one node buffer boundary' => [
  83. $data8k, [
  84. $this->buildNode('0', $data8k)
  85. ]],
  86. 'two nodes' => [
  87. '1234567890', [
  88. $this->buildNode('1', '67890'),
  89. $this->buildNode('0', '12345')
  90. ]],
  91. 'two nodes end on buffer boundary' => [
  92. $data8k . $data8k, [
  93. $this->buildNode('1', $data8k),
  94. $this->buildNode('0', $data8k)
  95. ]],
  96. 'two nodes with one on buffer boundary' => [
  97. $data8k . $dataLess8k, [
  98. $this->buildNode('1', $dataLess8k),
  99. $this->buildNode('0', $data8k)
  100. ]],
  101. 'two nodes on buffer boundary plus one byte' => [
  102. $data8k . 'X' . $data8k, [
  103. $this->buildNode('1', $data8k),
  104. $this->buildNode('0', $data8k . 'X')
  105. ]],
  106. 'two nodes on buffer boundary plus one byte at the end' => [
  107. $data8k . $data8k . 'X', [
  108. $this->buildNode('1', $data8k . 'X'),
  109. $this->buildNode('0', $data8k)
  110. ]],
  111. 'a ton of nodes' => [
  112. $tonofdata, $tonofnodes
  113. ]
  114. ];
  115. }
  116. private function makeData($count) {
  117. $data = '';
  118. $base = '1234567890';
  119. $j = 0;
  120. for ($i = 0; $i < $count; $i++) {
  121. $data .= $base[$j];
  122. $j++;
  123. if (!isset($base[$j])) {
  124. $j = 0;
  125. }
  126. }
  127. return $data;
  128. }
  129. private function buildNode($name, $data) {
  130. $node = $this->getMockBuilder(File::class)
  131. ->setMethods(['getName', 'get', 'getSize'])
  132. ->getMockForAbstractClass();
  133. $node->expects($this->any())
  134. ->method('getName')
  135. ->willReturn($name);
  136. $node->expects($this->any())
  137. ->method('get')
  138. ->willReturn($data);
  139. $node->expects($this->any())
  140. ->method('getSize')
  141. ->willReturn(strlen($data));
  142. return $node;
  143. }
  144. }