1
0

S3Test.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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) {
  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. protected function getInstance() {
  56. $config = \OC::$server->getConfig()->getSystemValue('objectstore');
  57. if (!is_array($config) || $config['class'] !== S3::class) {
  58. $this->markTestSkipped('objectstore not configured for s3');
  59. }
  60. return new S3($config['arguments']);
  61. }
  62. public function testUploadNonSeekable() {
  63. $s3 = $this->getInstance();
  64. $s3->writeObject('multiparttest', NonSeekableStream::wrap(fopen(__FILE__, 'r')));
  65. $result = $s3->readObject('multiparttest');
  66. $this->assertEquals(file_get_contents(__FILE__), stream_get_contents($result));
  67. }
  68. public function testSeek() {
  69. $data = file_get_contents(__FILE__);
  70. $instance = $this->getInstance();
  71. $instance->writeObject('seek', $this->stringToStream($data));
  72. $read = $instance->readObject('seek');
  73. $this->assertEquals(substr($data, 0, 100), fread($read, 100));
  74. fseek($read, 10);
  75. $this->assertEquals(substr($data, 10, 100), fread($read, 100));
  76. fseek($read, 100, SEEK_CUR);
  77. $this->assertEquals(substr($data, 210, 100), fread($read, 100));
  78. }
  79. }