AssemblyStreamTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Upload;
  8. use Sabre\DAV\File;
  9. class AssemblyStreamTest extends \Test\TestCase {
  10. /**
  11. * @dataProvider providesNodes()
  12. */
  13. public function testGetContents($expected, $nodes): void {
  14. $stream = \OCA\DAV\Upload\AssemblyStream::wrap($nodes);
  15. $content = stream_get_contents($stream);
  16. $this->assertEquals($expected, $content);
  17. }
  18. /**
  19. * @dataProvider providesNodes()
  20. */
  21. public function testGetContentsFread($expected, $nodes): void {
  22. $stream = \OCA\DAV\Upload\AssemblyStream::wrap($nodes);
  23. $content = '';
  24. while (!feof($stream)) {
  25. $content .= fread($stream, 3);
  26. }
  27. $this->assertEquals($expected, $content);
  28. }
  29. /**
  30. * @dataProvider providesNodes()
  31. */
  32. public function testSeek($expected, $nodes): void {
  33. $stream = \OCA\DAV\Upload\AssemblyStream::wrap($nodes);
  34. $offset = floor(strlen($expected) * 0.6);
  35. if (fseek($stream, $offset) === -1) {
  36. $this->fail('fseek failed');
  37. }
  38. $content = stream_get_contents($stream);
  39. $this->assertEquals(substr($expected, $offset), $content);
  40. }
  41. public function providesNodes() {
  42. $data8k = $this->makeData(8192);
  43. $dataLess8k = $this->makeData(8191);
  44. $tonofnodes = [];
  45. $tonofdata = "";
  46. for ($i = 0; $i < 101; $i++) {
  47. $thisdata = rand(0, 100); // variable length and content
  48. $tonofdata .= $thisdata;
  49. array_push($tonofnodes, $this->buildNode($i, $thisdata));
  50. }
  51. return[
  52. 'one node zero bytes' => [
  53. '', [
  54. $this->buildNode('0', '')
  55. ]],
  56. 'one node only' => [
  57. '1234567890', [
  58. $this->buildNode('0', '1234567890')
  59. ]],
  60. 'one node buffer boundary' => [
  61. $data8k, [
  62. $this->buildNode('0', $data8k)
  63. ]],
  64. 'two nodes' => [
  65. '1234567890', [
  66. $this->buildNode('1', '67890'),
  67. $this->buildNode('0', '12345')
  68. ]],
  69. 'two nodes end on buffer boundary' => [
  70. $data8k . $data8k, [
  71. $this->buildNode('1', $data8k),
  72. $this->buildNode('0', $data8k)
  73. ]],
  74. 'two nodes with one on buffer boundary' => [
  75. $data8k . $dataLess8k, [
  76. $this->buildNode('1', $dataLess8k),
  77. $this->buildNode('0', $data8k)
  78. ]],
  79. 'two nodes on buffer boundary plus one byte' => [
  80. $data8k . 'X' . $data8k, [
  81. $this->buildNode('1', $data8k),
  82. $this->buildNode('0', $data8k . 'X')
  83. ]],
  84. 'two nodes on buffer boundary plus one byte at the end' => [
  85. $data8k . $data8k . 'X', [
  86. $this->buildNode('1', $data8k . 'X'),
  87. $this->buildNode('0', $data8k)
  88. ]],
  89. 'a ton of nodes' => [
  90. $tonofdata, $tonofnodes
  91. ]
  92. ];
  93. }
  94. private function makeData($count) {
  95. $data = '';
  96. $base = '1234567890';
  97. $j = 0;
  98. for ($i = 0; $i < $count; $i++) {
  99. $data .= $base[$j];
  100. $j++;
  101. if (!isset($base[$j])) {
  102. $j = 0;
  103. }
  104. }
  105. return $data;
  106. }
  107. private function buildNode($name, $data) {
  108. $node = $this->getMockBuilder(File::class)
  109. ->setMethods(['getName', 'get', 'getSize'])
  110. ->getMockForAbstractClass();
  111. $node->expects($this->any())
  112. ->method('getName')
  113. ->willReturn($name);
  114. $node->expects($this->any())
  115. ->method('get')
  116. ->willReturn($data);
  117. $node->expects($this->any())
  118. ->method('getSize')
  119. ->willReturn(strlen($data));
  120. return $node;
  121. }
  122. }