1
0

UpdaterLegacyTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 as Filesystem;
  10. use OC\Files\View;
  11. use OCP\Files\Mount\IMountManager;
  12. /**
  13. * Class UpdaterLegacyTest
  14. *
  15. * @group DB
  16. *
  17. * @package Test\Files\Cache
  18. */
  19. class UpdaterLegacyTest extends \Test\TestCase {
  20. /**
  21. * @var \OC\Files\Storage\Storage $storage
  22. */
  23. private $storage;
  24. /**
  25. * @var \OC\Files\Cache\Scanner $scanner
  26. */
  27. private $scanner;
  28. /**
  29. * @var \OC\Files\Cache\Cache $cache
  30. */
  31. private $cache;
  32. private static $user;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->storage = new \OC\Files\Storage\Temporary([]);
  36. $textData = "dummy file data\n";
  37. $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo/logo.png');
  38. $this->storage->mkdir('folder');
  39. $this->storage->file_put_contents('foo.txt', $textData);
  40. $this->storage->file_put_contents('foo.png', $imgData);
  41. $this->storage->file_put_contents('folder/bar.txt', $textData);
  42. $this->storage->file_put_contents('folder/bar2.txt', $textData);
  43. $this->scanner = $this->storage->getScanner();
  44. $this->scanner->scan('');
  45. $this->cache = $this->storage->getCache();
  46. if (!self::$user) {
  47. self::$user = $this->getUniqueID();
  48. }
  49. \OC::$server->getUserManager()->createUser(self::$user, 'password');
  50. $this->loginAsUser(self::$user);
  51. Filesystem::init(self::$user, '/' . self::$user . '/files');
  52. /** @var IMountManager $manager */
  53. $manager = \OC::$server->get(IMountManager::class);
  54. $manager->removeMount('/' . self::$user);
  55. Filesystem::mount($this->storage, [], '/' . self::$user . '/files');
  56. \OC_Hook::clear('OC_Filesystem');
  57. }
  58. protected function tearDown(): void {
  59. if ($this->cache) {
  60. $this->cache->clear();
  61. }
  62. $result = false;
  63. $user = \OC::$server->getUserManager()->get(self::$user);
  64. if ($user !== null) {
  65. $result = $user->delete();
  66. }
  67. $this->assertTrue($result);
  68. $this->logout();
  69. parent::tearDown();
  70. }
  71. public function testWrite() {
  72. $textSize = strlen("dummy file data\n");
  73. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo/logo.png');
  74. $this->cache->put('foo.txt', ['mtime' => 100, 'storage_mtime' => 150]);
  75. $rootCachedData = $this->cache->get('');
  76. $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
  77. $fooCachedData = $this->cache->get('foo.txt');
  78. Filesystem::file_put_contents('foo.txt', 'asd');
  79. $cachedData = $this->cache->get('foo.txt');
  80. $this->assertEquals(3, $cachedData['size']);
  81. $this->assertIsString($fooCachedData['etag']);
  82. $this->assertIsString($cachedData['etag']);
  83. $this->assertNotSame($fooCachedData['etag'], $cachedData['etag']);
  84. $cachedData = $this->cache->get('');
  85. $this->assertEquals(2 * $textSize + $imageSize + 3, $cachedData['size']);
  86. $this->assertIsString($rootCachedData['etag']);
  87. $this->assertIsString($cachedData['etag']);
  88. $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
  89. $rootCachedData = $cachedData;
  90. $this->assertFalse($this->cache->inCache('bar.txt'));
  91. Filesystem::file_put_contents('bar.txt', 'asd');
  92. $this->assertTrue($this->cache->inCache('bar.txt'));
  93. $cachedData = $this->cache->get('bar.txt');
  94. $this->assertEquals(3, $cachedData['size']);
  95. $mtime = $cachedData['mtime'];
  96. $cachedData = $this->cache->get('');
  97. $this->assertEquals(2 * $textSize + $imageSize + 2 * 3, $cachedData['size']);
  98. $this->assertIsString($rootCachedData['etag']);
  99. $this->assertIsString($cachedData['etag']);
  100. $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
  101. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $mtime);
  102. }
  103. public function testWriteWithMountPoints() {
  104. $storage2 = new \OC\Files\Storage\Temporary([]);
  105. $storage2->getScanner()->scan(''); //initialize etags
  106. $cache2 = $storage2->getCache();
  107. Filesystem::mount($storage2, [], '/' . self::$user . '/files/folder/substorage');
  108. $view = new View('/' . self::$user . '/files');
  109. $folderCachedData = $view->getFileInfo('folder');
  110. $substorageCachedData = $cache2->get('');
  111. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  112. $this->assertTrue($cache2->inCache('foo.txt'));
  113. $cachedData = $cache2->get('foo.txt');
  114. $oldEtag = $substorageCachedData['etag'];
  115. $this->assertEquals(3, $cachedData['size']);
  116. $mtime = $cachedData['mtime'];
  117. $cachedData = $cache2->get('');
  118. $this->assertIsString($substorageCachedData['etag']);
  119. $this->assertIsString($cachedData['etag']);
  120. $this->assertNotSame($oldEtag, $cachedData['etag']);
  121. $cachedData = $view->getFileInfo('folder');
  122. $this->assertIsString($folderCachedData['etag']);
  123. $this->assertIsString($cachedData['etag']);
  124. $this->assertNotSame($oldEtag, $cachedData['etag']);
  125. }
  126. public function testDelete() {
  127. $textSize = strlen("dummy file data\n");
  128. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo/logo.png');
  129. $rootCachedData = $this->cache->get('');
  130. $oldEtag = $rootCachedData['etag'];
  131. $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
  132. $this->assertTrue($this->cache->inCache('foo.txt'));
  133. Filesystem::unlink('foo.txt');
  134. $this->assertFalse($this->cache->inCache('foo.txt'));
  135. $cachedData = $this->cache->get('');
  136. $this->assertEquals(2 * $textSize + $imageSize, $cachedData['size']);
  137. $this->assertIsString($rootCachedData['etag']);
  138. $this->assertIsString($cachedData['etag']);
  139. $this->assertNotSame($oldEtag, $cachedData['etag']);
  140. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
  141. $rootCachedData = $cachedData;
  142. Filesystem::mkdir('bar_folder');
  143. $this->assertTrue($this->cache->inCache('bar_folder'));
  144. $cachedData = $this->cache->get('');
  145. $this->assertIsString($rootCachedData['etag']);
  146. $this->assertIsString($cachedData['etag']);
  147. $this->assertNotSame($oldEtag, $cachedData['etag']);
  148. $rootCachedData = $cachedData;
  149. $oldEtag = $rootCachedData['etag'];
  150. Filesystem::rmdir('bar_folder');
  151. $this->assertFalse($this->cache->inCache('bar_folder'));
  152. $cachedData = $this->cache->get('');
  153. $this->assertIsString($rootCachedData['etag']);
  154. $this->assertIsString($cachedData['etag']);
  155. $this->assertNotSame($oldEtag, $cachedData['etag']);
  156. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
  157. }
  158. public function testDeleteWithMountPoints() {
  159. $storage2 = new \OC\Files\Storage\Temporary([]);
  160. $cache2 = $storage2->getCache();
  161. Filesystem::mount($storage2, [], '/' . self::$user . '/files/folder/substorage');
  162. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  163. $view = new View('/' . self::$user . '/files');
  164. $this->assertTrue($cache2->inCache('foo.txt'));
  165. $folderCachedData = $view->getFileInfo('folder');
  166. $substorageCachedData = $cache2->get('');
  167. $oldEtag = $folderCachedData['etag'];
  168. Filesystem::unlink('folder/substorage/foo.txt');
  169. $this->assertFalse($cache2->inCache('foo.txt'));
  170. $cachedData = $cache2->get('');
  171. $this->assertIsString($substorageCachedData['etag']);
  172. $this->assertIsString($cachedData['etag']);
  173. $this->assertNotSame($substorageCachedData, $cachedData['etag']);
  174. $this->assertGreaterThanOrEqual($substorageCachedData['mtime'], $cachedData['mtime']);
  175. $cachedData = $view->getFileInfo('folder');
  176. $this->assertIsString($folderCachedData['etag']);
  177. $this->assertIsString($cachedData['etag']);
  178. $this->assertNotSame($oldEtag, $cachedData['etag']);
  179. $this->assertGreaterThanOrEqual($folderCachedData['mtime'], $cachedData['mtime']);
  180. }
  181. public function testRename() {
  182. $textSize = strlen("dummy file data\n");
  183. $imageSize = filesize(\OC::$SERVERROOT . '/core/img/logo/logo.png');
  184. $rootCachedData = $this->cache->get('');
  185. $this->assertEquals(3 * $textSize + $imageSize, $rootCachedData['size']);
  186. $this->assertTrue($this->cache->inCache('foo.txt'));
  187. $fooCachedData = $this->cache->get('foo.txt');
  188. $this->assertFalse($this->cache->inCache('bar.txt'));
  189. Filesystem::rename('foo.txt', 'bar.txt');
  190. $this->assertFalse($this->cache->inCache('foo.txt'));
  191. $this->assertTrue($this->cache->inCache('bar.txt'));
  192. $cachedData = $this->cache->get('bar.txt');
  193. $this->assertEquals($fooCachedData['fileid'], $cachedData['fileid']);
  194. $mtime = $cachedData['mtime'];
  195. $cachedData = $this->cache->get('');
  196. $this->assertEquals(3 * $textSize + $imageSize, $cachedData['size']);
  197. $this->assertIsString($rootCachedData['etag']);
  198. $this->assertIsString($cachedData['etag']);
  199. $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
  200. }
  201. public function testRenameExtension() {
  202. $fooCachedData = $this->cache->get('foo.txt');
  203. $this->assertEquals('text/plain', $fooCachedData['mimetype']);
  204. Filesystem::rename('foo.txt', 'foo.abcd');
  205. $fooCachedData = $this->cache->get('foo.abcd');
  206. $this->assertEquals('application/octet-stream', $fooCachedData['mimetype']);
  207. }
  208. public function testRenameWithMountPoints() {
  209. $storage2 = new \OC\Files\Storage\Temporary([]);
  210. $cache2 = $storage2->getCache();
  211. Filesystem::mount($storage2, [], '/' . self::$user . '/files/folder/substorage');
  212. Filesystem::file_put_contents('folder/substorage/foo.txt', 'asd');
  213. $view = new View('/' . self::$user . '/files');
  214. $this->assertTrue($cache2->inCache('foo.txt'));
  215. $folderCachedData = $view->getFileInfo('folder');
  216. $oldEtag = $folderCachedData['etag'];
  217. $substorageCachedData = $cache2->get('');
  218. $fooCachedData = $cache2->get('foo.txt');
  219. Filesystem::rename('folder/substorage/foo.txt', 'folder/substorage/bar.txt');
  220. $this->assertFalse($cache2->inCache('foo.txt'));
  221. $this->assertTrue($cache2->inCache('bar.txt'));
  222. $cachedData = $cache2->get('bar.txt');
  223. $this->assertEquals($fooCachedData['fileid'], $cachedData['fileid']);
  224. $mtime = $cachedData['mtime'];
  225. $cachedData = $cache2->get('');
  226. $this->assertIsString($substorageCachedData['etag']);
  227. $this->assertIsString($cachedData['etag']);
  228. $this->assertNotSame($oldEtag, $cachedData['etag']);
  229. // rename can cause mtime change - invalid assert
  230. // $this->assertEquals($mtime, $cachedData['mtime']);
  231. $cachedData = $view->getFileInfo('folder');
  232. $this->assertIsString($folderCachedData['etag']);
  233. $this->assertIsString($cachedData['etag']);
  234. $this->assertNotSame($oldEtag, $cachedData['etag']);
  235. // rename can cause mtime change - invalid assert
  236. // $this->assertEquals($mtime, $cachedData['mtime']);
  237. }
  238. public function testTouch() {
  239. $rootCachedData = $this->cache->get('');
  240. $fooCachedData = $this->cache->get('foo.txt');
  241. Filesystem::touch('foo.txt');
  242. $cachedData = $this->cache->get('foo.txt');
  243. $this->assertIsString($fooCachedData['etag']);
  244. $this->assertIsString($cachedData['etag']);
  245. $this->assertGreaterThanOrEqual($fooCachedData['mtime'], $cachedData['mtime']);
  246. $cachedData = $this->cache->get('');
  247. $this->assertIsString($rootCachedData['etag']);
  248. $this->assertIsString($cachedData['etag']);
  249. $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
  250. $this->assertGreaterThanOrEqual($rootCachedData['mtime'], $cachedData['mtime']);
  251. $rootCachedData = $cachedData;
  252. $time = 1371006070;
  253. $barCachedData = $this->cache->get('folder/bar.txt');
  254. $folderCachedData = $this->cache->get('folder');
  255. $this->cache->put('', ['mtime' => $time - 100]);
  256. Filesystem::touch('folder/bar.txt', $time);
  257. $cachedData = $this->cache->get('folder/bar.txt');
  258. $this->assertIsString($barCachedData['etag']);
  259. $this->assertIsString($cachedData['etag']);
  260. $this->assertNotSame($barCachedData['etag'], $cachedData['etag']);
  261. $this->assertEquals($time, $cachedData['mtime']);
  262. $cachedData = $this->cache->get('folder');
  263. $this->assertIsString($folderCachedData['etag']);
  264. $this->assertIsString($cachedData['etag']);
  265. $this->assertNotSame($folderCachedData['etag'], $cachedData['etag']);
  266. $cachedData = $this->cache->get('');
  267. $this->assertIsString($rootCachedData['etag']);
  268. $this->assertIsString($cachedData['etag']);
  269. $this->assertNotSame($rootCachedData['etag'], $cachedData['etag']);
  270. $this->assertEquals($time, $cachedData['mtime']);
  271. }
  272. }