AssemblyStreamTest.php 4.2 KB

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