ObjectStoreStorageTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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-or-later
  6. */
  7. namespace Test\Files\ObjectStore;
  8. use OC\Files\ObjectStore\StorageObjectStore;
  9. use OC\Files\Storage\Temporary;
  10. use OC\Files\Storage\Wrapper\Jail;
  11. use OCP\Files\ObjectStore\IObjectStore;
  12. use Test\Files\Storage\Storage;
  13. /**
  14. * @group DB
  15. */
  16. class ObjectStoreStorageTest extends Storage {
  17. /** @var ObjectStoreStorageOverwrite */
  18. protected $instance;
  19. /**
  20. * @var IObjectStore
  21. */
  22. private $objectStorage;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $baseStorage = new Temporary();
  26. $this->objectStorage = new StorageObjectStore($baseStorage);
  27. $config['objectstore'] = $this->objectStorage;
  28. $this->instance = new ObjectStoreStorageOverwrite($config);
  29. }
  30. protected function tearDown(): void {
  31. if (is_null($this->instance)) {
  32. return;
  33. }
  34. $this->instance->getCache()->clear();
  35. parent::tearDown();
  36. }
  37. public function testStat() {
  38. $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
  39. $ctimeStart = time();
  40. $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
  41. $this->assertTrue($this->instance->isReadable('/lorem.txt'));
  42. $ctimeEnd = time();
  43. $mTime = $this->instance->filemtime('/lorem.txt');
  44. // check that ($ctimeStart - 5) <= $mTime <= ($ctimeEnd + 1)
  45. $this->assertGreaterThanOrEqual(($ctimeStart - 5), $mTime);
  46. $this->assertLessThanOrEqual(($ctimeEnd + 1), $mTime);
  47. $this->assertEquals(filesize($textFile), $this->instance->filesize('/lorem.txt'));
  48. $stat = $this->instance->stat('/lorem.txt');
  49. //only size and mtime are required in the result
  50. $this->assertEquals($stat['size'], $this->instance->filesize('/lorem.txt'));
  51. $this->assertEquals($stat['mtime'], $mTime);
  52. if ($this->instance->touch('/lorem.txt', 100) !== false) {
  53. $mTime = $this->instance->filemtime('/lorem.txt');
  54. $this->assertEquals($mTime, 100);
  55. }
  56. }
  57. public function testCheckUpdate() {
  58. $this->markTestSkipped('Detecting external changes is not supported on object storages');
  59. }
  60. /**
  61. * @dataProvider copyAndMoveProvider
  62. */
  63. public function testMove($source, $target) {
  64. $this->initSourceAndTarget($source);
  65. $sourceId = $this->instance->getCache()->getId(ltrim('/', $source));
  66. $this->assertNotEquals(-1, $sourceId);
  67. $this->instance->rename($source, $target);
  68. $this->assertTrue($this->instance->file_exists($target), $target.' was not created');
  69. $this->assertFalse($this->instance->file_exists($source), $source.' still exists');
  70. $this->assertSameAsLorem($target);
  71. $targetId = $this->instance->getCache()->getId(ltrim('/', $target));
  72. $this->assertSame($sourceId, $targetId, 'fileid must be stable on move or shares will break');
  73. }
  74. public function testRenameDirectory() {
  75. $this->instance->mkdir('source');
  76. $this->instance->file_put_contents('source/test1.txt', 'foo');
  77. $this->instance->file_put_contents('source/test2.txt', 'qwerty');
  78. $this->instance->mkdir('source/subfolder');
  79. $this->instance->file_put_contents('source/subfolder/test.txt', 'bar');
  80. $sourceId = $this->instance->getCache()->getId('source');
  81. $this->assertNotEquals(-1, $sourceId);
  82. $this->instance->rename('source', 'target');
  83. $this->assertFalse($this->instance->file_exists('source'));
  84. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  85. $this->assertFalse($this->instance->file_exists('source/test2.txt'));
  86. $this->assertFalse($this->instance->file_exists('source/subfolder'));
  87. $this->assertFalse($this->instance->file_exists('source/subfolder/test.txt'));
  88. $this->assertTrue($this->instance->file_exists('target'));
  89. $this->assertTrue($this->instance->file_exists('target/test1.txt'));
  90. $this->assertTrue($this->instance->file_exists('target/test2.txt'));
  91. $this->assertTrue($this->instance->file_exists('target/subfolder'));
  92. $this->assertTrue($this->instance->file_exists('target/subfolder/test.txt'));
  93. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  94. $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt'));
  95. $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt'));
  96. $targetId = $this->instance->getCache()->getId('target');
  97. $this->assertSame($sourceId, $targetId, 'fileid must be stable on move or shares will break');
  98. }
  99. public function testRenameOverWriteDirectory() {
  100. $this->instance->mkdir('source');
  101. $this->instance->file_put_contents('source/test1.txt', 'foo');
  102. $sourceId = $this->instance->getCache()->getId('source');
  103. $this->assertNotEquals(-1, $sourceId);
  104. $this->instance->mkdir('target');
  105. $this->instance->file_put_contents('target/test1.txt', 'bar');
  106. $this->instance->file_put_contents('target/test2.txt', 'bar');
  107. $this->instance->rename('source', 'target');
  108. $this->assertFalse($this->instance->file_exists('source'));
  109. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  110. $this->assertFalse($this->instance->file_exists('target/test2.txt'));
  111. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  112. $targetId = $this->instance->getCache()->getId('target');
  113. $this->assertSame($sourceId, $targetId, 'fileid must be stable on move or shares will break');
  114. }
  115. public function testRenameOverWriteDirectoryOverFile() {
  116. $this->instance->mkdir('source');
  117. $this->instance->file_put_contents('source/test1.txt', 'foo');
  118. $sourceId = $this->instance->getCache()->getId('source');
  119. $this->assertNotEquals(-1, $sourceId);
  120. $this->instance->file_put_contents('target', 'bar');
  121. $this->instance->rename('source', 'target');
  122. $this->assertFalse($this->instance->file_exists('source'));
  123. $this->assertFalse($this->instance->file_exists('source/test1.txt'));
  124. $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt'));
  125. $targetId = $this->instance->getCache()->getId('target');
  126. $this->assertSame($sourceId, $targetId, 'fileid must be stable on move or shares will break');
  127. }
  128. public function testWriteObjectSilentFailure() {
  129. $objectStore = $this->instance->getObjectStore();
  130. $this->instance->setObjectStore(new FailWriteObjectStore($objectStore));
  131. try {
  132. $this->instance->file_put_contents('test.txt', 'foo');
  133. $this->fail('expected exception');
  134. } catch (\Exception $e) {
  135. $this->assertStringStartsWith('Object not found after writing', $e->getMessage());
  136. }
  137. $this->assertFalse($this->instance->file_exists('test.txt'));
  138. }
  139. public function testWriteObjectSilentFailureNoCheck() {
  140. $objectStore = $this->instance->getObjectStore();
  141. $this->instance->setObjectStore(new FailWriteObjectStore($objectStore));
  142. $this->instance->setValidateWrites(false);
  143. $this->instance->file_put_contents('test.txt', 'foo');
  144. $this->assertTrue($this->instance->file_exists('test.txt'));
  145. }
  146. public function testDeleteObjectFailureKeepCache() {
  147. $objectStore = $this->instance->getObjectStore();
  148. $this->instance->setObjectStore(new FailDeleteObjectStore($objectStore));
  149. $cache = $this->instance->getCache();
  150. $this->instance->file_put_contents('test.txt', 'foo');
  151. $this->assertTrue($cache->inCache('test.txt'));
  152. $this->assertFalse($this->instance->unlink('test.txt'));
  153. $this->assertTrue($cache->inCache('test.txt'));
  154. $this->instance->mkdir('foo');
  155. $this->instance->file_put_contents('foo/test.txt', 'foo');
  156. $this->assertTrue($cache->inCache('foo'));
  157. $this->assertTrue($cache->inCache('foo/test.txt'));
  158. $this->instance->rmdir('foo');
  159. $this->assertTrue($cache->inCache('foo'));
  160. $this->assertTrue($cache->inCache('foo/test.txt'));
  161. }
  162. public function testCopyBetweenJails() {
  163. $this->instance->mkdir('a');
  164. $this->instance->mkdir('b');
  165. $jailA = new Jail([
  166. 'storage' => $this->instance,
  167. 'root' => 'a'
  168. ]);
  169. $jailB = new Jail([
  170. 'storage' => $this->instance,
  171. 'root' => 'b'
  172. ]);
  173. $jailA->mkdir('sub');
  174. $jailA->file_put_contents('1.txt', '1');
  175. $jailA->file_put_contents('sub/2.txt', '2');
  176. $jailA->file_put_contents('sub/3.txt', '3');
  177. $jailB->copyFromStorage($jailA, '', 'target');
  178. $this->assertEquals('1', $this->instance->file_get_contents('b/target/1.txt'));
  179. $this->assertEquals('2', $this->instance->file_get_contents('b/target/sub/2.txt'));
  180. $this->assertEquals('3', $this->instance->file_get_contents('b/target/sub/3.txt'));
  181. }
  182. public function testCopyPreservesPermissions() {
  183. $cache = $this->instance->getCache();
  184. $this->instance->file_put_contents('test.txt', 'foo');
  185. $this->assertTrue($cache->inCache('test.txt'));
  186. $cache->update($cache->getId('test.txt'), ['permissions' => \OCP\Constants::PERMISSION_READ]);
  187. $this->assertEquals(\OCP\Constants::PERMISSION_READ, $this->instance->getPermissions('test.txt'));
  188. $this->assertTrue($this->instance->copy('test.txt', 'new.txt'));
  189. $this->assertTrue($cache->inCache('new.txt'));
  190. $this->assertEquals(\OCP\Constants::PERMISSION_READ, $this->instance->getPermissions('new.txt'));
  191. }
  192. /**
  193. * Test that copying files will drop permissions like local storage does
  194. * TODO: Drop this and fix local storage
  195. */
  196. public function testCopyGrantsPermissions() {
  197. $config['objectstore'] = $this->objectStorage;
  198. $config['handleCopiesAsOwned'] = true;
  199. $instance = new ObjectStoreStorageOverwrite($config);
  200. $cache = $instance->getCache();
  201. $instance->file_put_contents('test.txt', 'foo');
  202. $this->assertTrue($cache->inCache('test.txt'));
  203. $cache->update($cache->getId('test.txt'), ['permissions' => \OCP\Constants::PERMISSION_READ]);
  204. $this->assertEquals(\OCP\Constants::PERMISSION_READ, $instance->getPermissions('test.txt'));
  205. $this->assertTrue($instance->copy('test.txt', 'new.txt'));
  206. $this->assertTrue($cache->inCache('new.txt'));
  207. $this->assertEquals(\OCP\Constants::PERMISSION_ALL, $instance->getPermissions('new.txt'));
  208. }
  209. }