S3Test.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\Files\ObjectStore;
  22. use Icewind\Streams\Wrapper;
  23. use OC\Files\ObjectStore\S3;
  24. class MultiPartUploadS3 extends S3 {
  25. public function writeObject($urn, $stream, ?string $mimetype = null) {
  26. $this->getConnection()->upload($this->bucket, $urn, $stream, 'private', [
  27. 'mup_threshold' => 1,
  28. ]);
  29. }
  30. }
  31. class NonSeekableStream extends Wrapper {
  32. public static function wrap($source) {
  33. $context = stream_context_create([
  34. 'nonseek' => [
  35. 'source' => $source,
  36. ],
  37. ]);
  38. return Wrapper::wrapSource($source, $context, 'nonseek', self::class);
  39. }
  40. public function dir_opendir($path, $options) {
  41. return false;
  42. }
  43. public function stream_open($path, $mode, $options, &$opened_path) {
  44. $this->loadContext('nonseek');
  45. return true;
  46. }
  47. public function stream_seek($offset, $whence = SEEK_SET) {
  48. return false;
  49. }
  50. }
  51. /**
  52. * @group PRIMARY-s3
  53. */
  54. class S3Test extends ObjectStoreTest {
  55. public function setUp(): void {
  56. parent::setUp();
  57. $s3 = $this->getInstance();
  58. $s3->deleteObject('multiparttest');
  59. }
  60. protected function getInstance() {
  61. $config = \OC::$server->getConfig()->getSystemValue('objectstore');
  62. if (!is_array($config) || $config['class'] !== S3::class) {
  63. $this->markTestSkipped('objectstore not configured for s3');
  64. }
  65. return new S3($config['arguments']);
  66. }
  67. public function testUploadNonSeekable() {
  68. $this->cleanupAfter('multiparttest');
  69. $s3 = $this->getInstance();
  70. $s3->writeObject('multiparttest', NonSeekableStream::wrap(fopen(__FILE__, 'r')));
  71. $result = $s3->readObject('multiparttest');
  72. $this->assertEquals(file_get_contents(__FILE__), stream_get_contents($result));
  73. }
  74. public function testSeek() {
  75. $this->cleanupAfter('seek');
  76. $data = file_get_contents(__FILE__);
  77. $instance = $this->getInstance();
  78. $instance->writeObject('seek', $this->stringToStream($data));
  79. $read = $instance->readObject('seek');
  80. $this->assertEquals(substr($data, 0, 100), fread($read, 100));
  81. fseek($read, 10);
  82. $this->assertEquals(substr($data, 10, 100), fread($read, 100));
  83. fseek($read, 100, SEEK_CUR);
  84. $this->assertEquals(substr($data, 210, 100), fread($read, 100));
  85. }
  86. public function assertNoUpload($objectUrn) {
  87. /** @var \OC\Files\ObjectStore\S3 */
  88. $s3 = $this->getInstance();
  89. $s3client = $s3->getConnection();
  90. $uploads = $s3client->listMultipartUploads([
  91. 'Bucket' => $s3->getBucket(),
  92. 'Prefix' => $objectUrn,
  93. ]);
  94. $this->assertArrayNotHasKey('Uploads', $uploads, 'Assert is not uploaded');
  95. }
  96. public function testEmptyUpload() {
  97. $s3 = $this->getInstance();
  98. $emptyStream = fopen("php://memory", "r");
  99. fwrite($emptyStream, '');
  100. $s3->writeObject('emptystream', $emptyStream);
  101. $this->assertNoUpload('emptystream');
  102. $this->assertTrue($s3->objectExists('emptystream'), 'Object exists on S3');
  103. $thrown = false;
  104. try {
  105. self::assertFalse($s3->readObject('emptystream'), 'Reading empty stream object should return false');
  106. } catch (\Exception $e) {
  107. // An exception is expected here since 0 byte files are wrapped
  108. // to be read from an empty memory stream in the ObjectStoreStorage
  109. $thrown = true;
  110. }
  111. self::assertTrue($thrown, 'readObject with range requests are not expected to work on empty objects');
  112. $s3->deleteObject('emptystream');
  113. }
  114. /** File size to upload in bytes */
  115. public function dataFileSizes() {
  116. return [
  117. [1000000], [2000000], [5242879], [5242880], [5242881], [10000000]
  118. ];
  119. }
  120. /** @dataProvider dataFileSizes */
  121. public function testFileSizes($size) {
  122. $this->cleanupAfter('testfilesizes');
  123. $s3 = $this->getInstance();
  124. $sourceStream = fopen('php://memory', 'wb+');
  125. $writeChunkSize = 1024;
  126. $chunkCount = $size / $writeChunkSize;
  127. for ($i = 0; $i < $chunkCount; $i++) {
  128. fwrite($sourceStream, str_repeat('A',
  129. ($i < $chunkCount - 1) ? $writeChunkSize : $size - ($i * $writeChunkSize)
  130. ));
  131. }
  132. rewind($sourceStream);
  133. $s3->writeObject('testfilesizes', $sourceStream);
  134. $this->assertNoUpload('testfilesizes');
  135. self::assertTrue($s3->objectExists('testfilesizes'), 'Object exists on S3');
  136. $result = $s3->readObject('testfilesizes');
  137. // compare first 100 bytes
  138. self::assertEquals(str_repeat('A', 100), fread($result, 100), 'Compare first 100 bytes');
  139. // compare last 100 bytes
  140. fseek($result, $size - 100);
  141. self::assertEquals(str_repeat('A', 100), fread($result, 100), 'Compare last 100 bytes');
  142. // end of file reached
  143. fseek($result, $size);
  144. self::assertTrue(feof($result), 'End of file reached');
  145. $this->assertNoUpload('testfilesizes');
  146. }
  147. }