ObjectStoreStorageTest.php 10 KB

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