1
0

AssemblyStreamTest.php 3.3 KB

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