UpdaterLegacyTest.php 12 KB

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