You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
9.8 KiB

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Files\Cache;
  9. use OC\Files\Filesystem;
  10. use OC\Files\Storage\Temporary;
  11. use OC\Files\View;
  12. /**
  13. * Class UpdaterTest
  14. *
  15. * @group DB
  16. *
  17. * @package Test\Files\Cache
  18. */
  19. class UpdaterTest extends \Test\TestCase {
  20. /**
  21. * @var \OC\Files\Storage\Storage
  22. */
  23. protected $storage;
  24. /**
  25. * @var \OC\Files\Cache\Cache
  26. */
  27. protected $cache;
  28. /**
  29. * @var \OC\Files\View
  30. */
  31. protected $view;
  32. /**
  33. * @var \OC\Files\Cache\Updater
  34. */
  35. protected $updater;
  36. protected function setUp() {
  37. parent::setUp();
  38. $this->loginAsUser();
  39. $this->storage = new Temporary(array());
  40. $this->updater = $this->storage->getUpdater();
  41. $this->cache = $this->storage->getCache();
  42. }
  43. protected function tearDown() {
  44. Filesystem::clearMounts();
  45. $this->logout();
  46. parent::tearDown();
  47. }
  48. public function testNewFile() {
  49. $this->storage->file_put_contents('foo.txt', 'bar');
  50. $this->assertFalse($this->cache->inCache('foo.txt'));
  51. $this->updater->update('foo.txt');
  52. $this->assertTrue($this->cache->inCache('foo.txt'));
  53. $cached = $this->cache->get('foo.txt');
  54. $this->assertEquals(3, $cached['size']);
  55. $this->assertEquals('text/plain', $cached['mimetype']);
  56. }
  57. public function testUpdatedFile() {
  58. $this->storage->file_put_contents('foo.txt', 'bar');
  59. $this->updater->update('foo.txt');
  60. $cached = $this->cache->get('foo.txt');
  61. $this->assertEquals(3, $cached['size']);
  62. $this->assertEquals('text/plain', $cached['mimetype']);
  63. $this->storage->file_put_contents('foo.txt', 'qwerty');
  64. $cached = $this->cache->get('foo.txt');
  65. $this->assertEquals(3, $cached['size']);
  66. $this->updater->update('/foo.txt');
  67. $cached = $this->cache->get('foo.txt');
  68. $this->assertEquals(6, $cached['size']);
  69. }
  70. public function testParentSize() {
  71. $this->storage->getScanner()->scan('');
  72. $parentCached = $this->cache->get('');
  73. $this->assertEquals(0, $parentCached['size']);
  74. $this->storage->file_put_contents('foo.txt', 'bar');
  75. $parentCached = $this->cache->get('');
  76. $this->assertEquals(0, $parentCached['size']);
  77. $this->updater->update('foo.txt');
  78. $parentCached = $this->cache->get('');
  79. $this->assertEquals(3, $parentCached['size']);
  80. $this->storage->file_put_contents('foo.txt', 'qwerty');
  81. $parentCached = $this->cache->get('');
  82. $this->assertEquals(3, $parentCached['size']);
  83. $this->updater->update('foo.txt');
  84. $parentCached = $this->cache->get('');
  85. $this->assertEquals(6, $parentCached['size']);
  86. $this->storage->unlink('foo.txt');
  87. $parentCached = $this->cache->get('');
  88. $this->assertEquals(6, $parentCached['size']);
  89. $this->updater->remove('foo.txt');
  90. $parentCached = $this->cache->get('');
  91. $this->assertEquals(0, $parentCached['size']);
  92. }
  93. public function testMove() {
  94. $this->storage->file_put_contents('foo.txt', 'qwerty');
  95. $this->updater->update('foo.txt');
  96. $this->assertTrue($this->cache->inCache('foo.txt'));
  97. $this->assertFalse($this->cache->inCache('bar.txt'));
  98. $cached = $this->cache->get('foo.txt');
  99. $this->storage->rename('foo.txt', 'bar.txt');
  100. $this->assertTrue($this->cache->inCache('foo.txt'));
  101. $this->assertFalse($this->cache->inCache('bar.txt'));
  102. $this->updater->renameFromStorage($this->storage, 'foo.txt', 'bar.txt');
  103. $this->assertFalse($this->cache->inCache('foo.txt'));
  104. $this->assertTrue($this->cache->inCache('bar.txt'));
  105. $cachedTarget = $this->cache->get('bar.txt');
  106. $this->assertEquals($cached['etag'], $cachedTarget['etag']);
  107. $this->assertEquals($cached['mtime'], $cachedTarget['mtime']);
  108. $this->assertEquals($cached['size'], $cachedTarget['size']);
  109. $this->assertEquals($cached['fileid'], $cachedTarget['fileid']);
  110. }
  111. public function testMoveNonExistingOverwrite() {
  112. $this->storage->file_put_contents('bar.txt', 'qwerty');
  113. $this->updater->update('bar.txt');
  114. $cached = $this->cache->get('bar.txt');
  115. $this->updater->renameFromStorage($this->storage, 'foo.txt', 'bar.txt');
  116. $this->assertFalse($this->cache->inCache('foo.txt'));
  117. $this->assertTrue($this->cache->inCache('bar.txt'));
  118. $cachedTarget = $this->cache->get('bar.txt');
  119. $this->assertEquals($cached['etag'], $cachedTarget['etag']);
  120. $this->assertEquals($cached['mtime'], $cachedTarget['mtime']);
  121. $this->assertEquals($cached['size'], $cachedTarget['size']);
  122. $this->assertEquals($cached['fileid'], $cachedTarget['fileid']);
  123. }
  124. public function testUpdateStorageMTime() {
  125. $this->storage->mkdir('sub');
  126. $this->storage->mkdir('sub2');
  127. $this->storage->file_put_contents('sub/foo.txt', 'qwerty');
  128. $this->updater->update('sub');
  129. $this->updater->update('sub/foo.txt');
  130. $this->updater->update('sub2');
  131. $cachedSourceParent = $this->cache->get('sub');
  132. $cachedSource = $this->cache->get('sub/foo.txt');
  133. $this->storage->rename('sub/foo.txt', 'sub2/bar.txt');
  134. // simulate storage having a different mtime
  135. $testmtime = 1433323578;
  136. // source storage mtime change
  137. $this->storage->touch('sub', $testmtime);
  138. // target storage mtime change
  139. $this->storage->touch('sub2', $testmtime);
  140. // some storages (like Dropbox) change storage mtime on rename
  141. $this->storage->touch('sub2/bar.txt', $testmtime);
  142. $this->updater->renameFromStorage($this->storage, 'sub/foo.txt', 'sub2/bar.txt');
  143. $cachedTargetParent = $this->cache->get('sub2');
  144. $cachedTarget = $this->cache->get('sub2/bar.txt');
  145. $this->assertEquals($cachedSource['mtime'], $cachedTarget['mtime'], 'file mtime preserved');
  146. $this->assertNotEquals($cachedTarget['storage_mtime'], $cachedTarget['mtime'], 'mtime is not storage_mtime for moved file');
  147. $this->assertEquals($testmtime, $cachedTarget['storage_mtime'], 'target file storage_mtime propagated');
  148. $this->assertNotEquals($testmtime, $cachedTarget['mtime'], 'target file mtime changed, not from storage');
  149. $this->assertEquals($testmtime, $cachedTargetParent['storage_mtime'], 'target parent storage_mtime propagated');
  150. $this->assertNotEquals($testmtime, $cachedTargetParent['mtime'], 'target folder mtime changed, not from storage');
  151. }
  152. public function testNewFileDisabled() {
  153. $this->storage->file_put_contents('foo.txt', 'bar');
  154. $this->assertFalse($this->cache->inCache('foo.txt'));
  155. $this->updater->disable();
  156. $this->updater->update('/foo.txt');
  157. $this->assertFalse($this->cache->inCache('foo.txt'));
  158. }
  159. public function testMoveCrossStorage() {
  160. $storage2 = new Temporary(array());
  161. $cache2 = $storage2->getCache();
  162. Filesystem::mount($storage2, array(), '/bar');
  163. $this->storage->file_put_contents('foo.txt', 'qwerty');
  164. $this->updater->update('foo.txt');
  165. $this->assertTrue($this->cache->inCache('foo.txt'));
  166. $this->assertFalse($cache2->inCache('bar.txt'));
  167. $cached = $this->cache->get('foo.txt');
  168. // "rename"
  169. $storage2->file_put_contents('bar.txt', 'qwerty');
  170. $this->storage->unlink('foo.txt');
  171. $this->assertTrue($this->cache->inCache('foo.txt'));
  172. $this->assertFalse($cache2->inCache('bar.txt'));
  173. $storage2->getUpdater()->renameFromStorage($this->storage, 'foo.txt', 'bar.txt');
  174. $this->assertFalse($this->cache->inCache('foo.txt'));
  175. $this->assertTrue($cache2->inCache('bar.txt'));
  176. $cachedTarget = $cache2->get('bar.txt');
  177. $this->assertEquals($cached['mtime'], $cachedTarget['mtime']);
  178. $this->assertEquals($cached['size'], $cachedTarget['size']);
  179. $this->assertEquals($cached['etag'], $cachedTarget['etag']);
  180. $this->assertEquals($cached['fileid'], $cachedTarget['fileid']);
  181. }
  182. public function testMoveFolderCrossStorage() {
  183. $storage2 = new Temporary(array());
  184. $cache2 = $storage2->getCache();
  185. Filesystem::mount($storage2, array(), '/bar');
  186. $this->storage->mkdir('foo');
  187. $this->storage->mkdir('foo/bar');
  188. $this->storage->file_put_contents('foo/foo.txt', 'qwerty');
  189. $this->storage->file_put_contents('foo/bar.txt', 'foo');
  190. $this->storage->file_put_contents('foo/bar/bar.txt', 'qwertyuiop');
  191. $this->storage->getScanner()->scan('');
  192. $this->assertTrue($this->cache->inCache('foo'));
  193. $this->assertTrue($this->cache->inCache('foo/foo.txt'));
  194. $this->assertTrue($this->cache->inCache('foo/bar.txt'));
  195. $this->assertTrue($this->cache->inCache('foo/bar'));
  196. $this->assertTrue($this->cache->inCache('foo/bar/bar.txt'));
  197. $cached = [];
  198. $cached[] = $this->cache->get('foo');
  199. $cached[] = $this->cache->get('foo/foo.txt');
  200. $cached[] = $this->cache->get('foo/bar.txt');
  201. $cached[] = $this->cache->get('foo/bar');
  202. $cached[] = $this->cache->get('foo/bar/bar.txt');
  203. // add extension to trigger the possible mimetype change
  204. $storage2->moveFromStorage($this->storage, 'foo', 'foo.b');
  205. $storage2->getUpdater()->renameFromStorage($this->storage, 'foo', 'foo.b');
  206. $this->assertFalse($this->cache->inCache('foo'));
  207. $this->assertFalse($this->cache->inCache('foo/foo.txt'));
  208. $this->assertFalse($this->cache->inCache('foo/bar.txt'));
  209. $this->assertFalse($this->cache->inCache('foo/bar'));
  210. $this->assertFalse($this->cache->inCache('foo/bar/bar.txt'));
  211. $this->assertTrue($cache2->inCache('foo.b'));
  212. $this->assertTrue($cache2->inCache('foo.b/foo.txt'));
  213. $this->assertTrue($cache2->inCache('foo.b/bar.txt'));
  214. $this->assertTrue($cache2->inCache('foo.b/bar'));
  215. $this->assertTrue($cache2->inCache('foo.b/bar/bar.txt'));
  216. $cachedTarget = [];
  217. $cachedTarget[] = $cache2->get('foo.b');
  218. $cachedTarget[] = $cache2->get('foo.b/foo.txt');
  219. $cachedTarget[] = $cache2->get('foo.b/bar.txt');
  220. $cachedTarget[] = $cache2->get('foo.b/bar');
  221. $cachedTarget[] = $cache2->get('foo.b/bar/bar.txt');
  222. foreach ($cached as $i => $old) {
  223. $new = $cachedTarget[$i];
  224. $this->assertEquals($old['mtime'], $new['mtime']);
  225. $this->assertEquals($old['size'], $new['size']);
  226. $this->assertEquals($old['etag'], $new['etag']);
  227. $this->assertEquals($old['fileid'], $new['fileid']);
  228. $this->assertEquals($old['mimetype'], $new['mimetype']);
  229. }
  230. }
  231. }