CacheTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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\Cache\Cache;
  9. use OC\Files\Search\SearchComparison;
  10. use OC\Files\Search\SearchQuery;
  11. use OCP\EventDispatcher\IEventDispatcher;
  12. use OCP\Files\Search\ISearchComparison;
  13. use OCP\IDBConnection;
  14. use OCP\IUser;
  15. class LongId extends \OC\Files\Storage\Temporary {
  16. public function getId() {
  17. return 'long:' . str_repeat('foo', 50) . parent::getId();
  18. }
  19. }
  20. /**
  21. * Class CacheTest
  22. *
  23. * @group DB
  24. *
  25. * @package Test\Files\Cache
  26. */
  27. class CacheTest extends \Test\TestCase {
  28. /**
  29. * @var \OC\Files\Storage\Temporary $storage ;
  30. */
  31. protected $storage;
  32. /**
  33. * @var \OC\Files\Storage\Temporary $storage2 ;
  34. */
  35. protected $storage2;
  36. /**
  37. * @var \OC\Files\Cache\Cache $cache
  38. */
  39. protected $cache;
  40. /**
  41. * @var \OC\Files\Cache\Cache $cache2
  42. */
  43. protected $cache2;
  44. public function testGetNumericId() {
  45. $this->assertNotNull($this->cache->getNumericStorageId());
  46. }
  47. public function testSimple() {
  48. $file1 = 'foo';
  49. $file2 = 'foo/bar';
  50. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
  51. $data2 = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  52. $this->assertFalse($this->cache->inCache($file1));
  53. $this->assertEquals($this->cache->get($file1), null);
  54. $id1 = $this->cache->put($file1, $data1);
  55. $this->assertTrue($this->cache->inCache($file1));
  56. $cacheData1 = $this->cache->get($file1);
  57. foreach ($data1 as $key => $value) {
  58. $this->assertEquals($value, $cacheData1[$key]);
  59. }
  60. $this->assertEquals($cacheData1['mimepart'], 'foo');
  61. $this->assertEquals($cacheData1['fileid'], $id1);
  62. $this->assertEquals($id1, $this->cache->getId($file1));
  63. $this->assertFalse($this->cache->inCache($file2));
  64. $id2 = $this->cache->put($file2, $data2);
  65. $this->assertTrue($this->cache->inCache($file2));
  66. $cacheData2 = $this->cache->get($file2);
  67. foreach ($data2 as $key => $value) {
  68. $this->assertEquals($value, $cacheData2[$key]);
  69. }
  70. $this->assertEquals($cacheData1['fileid'], $cacheData2['parent']);
  71. $this->assertEquals($cacheData2['fileid'], $id2);
  72. $this->assertEquals($id2, $this->cache->getId($file2));
  73. $this->assertEquals($id1, $this->cache->getParentId($file2));
  74. $newSize = 1050;
  75. $newId2 = $this->cache->put($file2, ['size' => $newSize]);
  76. $cacheData2 = $this->cache->get($file2);
  77. $this->assertEquals($newId2, $id2);
  78. $this->assertEquals($cacheData2['size'], $newSize);
  79. $this->assertEquals($cacheData1, $this->cache->get($file1));
  80. $this->cache->remove($file2);
  81. $this->assertFalse($this->cache->inCache($file2));
  82. $this->assertEquals($this->cache->get($file2), null);
  83. $this->assertTrue($this->cache->inCache($file1));
  84. $this->assertEquals($cacheData1, $this->cache->get($id1));
  85. }
  86. public function testCacheEntryGetters() {
  87. $file1 = 'foo';
  88. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/file'];
  89. $id1 = $this->cache->put($file1, $data1);
  90. $entry = $this->cache->get($file1);
  91. $this->assertEquals($entry->getId(), $id1);
  92. $this->assertEquals($entry->getStorageId(), $this->cache->getNumericStorageId());
  93. $this->assertEquals($entry->getPath(), 'foo');
  94. $this->assertEquals($entry->getName(), 'foo');
  95. $this->assertEquals($entry->getMimeType(), 'foo/file');
  96. $this->assertEquals($entry->getMimePart(), 'foo');
  97. $this->assertEquals($entry->getSize(), 100);
  98. $this->assertEquals($entry->getMTime(), 50);
  99. $this->assertEquals($entry->getStorageMTime(), 50);
  100. $this->assertEquals($entry->getEtag(), null);
  101. $this->assertEquals($entry->getPermissions(), 0);
  102. $this->assertEquals($entry->isEncrypted(), false);
  103. $this->assertEquals($entry->getMetadataEtag(), null);
  104. $this->assertEquals($entry->getCreationTime(), null);
  105. $this->assertEquals($entry->getUploadTime(), null);
  106. $this->assertEquals($entry->getUnencryptedSize(), 100);
  107. }
  108. public function testPartial() {
  109. $file1 = 'foo';
  110. $this->cache->put($file1, ['size' => 10]);
  111. $this->assertEquals(['size' => 10], $this->cache->get($file1));
  112. $this->cache->put($file1, ['mtime' => 15]);
  113. $this->assertEquals(['size' => 10, 'mtime' => 15], $this->cache->get($file1));
  114. $this->cache->put($file1, ['size' => 12]);
  115. $this->assertEquals(['size' => 12, 'mtime' => 15], $this->cache->get($file1));
  116. }
  117. /**
  118. * @dataProvider folderDataProvider
  119. */
  120. public function testFolder($folder) {
  121. if (strpos($folder, 'F09F9890')) {
  122. // 4 byte UTF doesn't work on mysql
  123. $params = \OC::$server->get(\OC\DB\Connection::class)->getParams();
  124. if (\OC::$server->getDatabaseConnection()->getDatabaseProvider() === IDBConnection::PLATFORM_MYSQL && $params['charset'] !== 'utf8mb4') {
  125. $this->markTestSkipped('MySQL doesn\'t support 4 byte UTF-8');
  126. }
  127. }
  128. $file2 = $folder . '/bar';
  129. $file3 = $folder . '/foo';
  130. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  131. $fileData = [];
  132. $fileData['bar'] = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  133. $fileData['foo'] = ['size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file'];
  134. $this->cache->put($folder, $data1);
  135. $this->cache->put($file2, $fileData['bar']);
  136. $this->cache->put($file3, $fileData['foo']);
  137. $content = $this->cache->getFolderContents($folder);
  138. $this->assertEquals(count($content), 2);
  139. foreach ($content as $cachedData) {
  140. $data = $fileData[$cachedData['name']];
  141. foreach ($data as $name => $value) {
  142. $this->assertEquals($value, $cachedData[$name]);
  143. }
  144. }
  145. $file4 = $folder . '/unkownSize';
  146. $fileData['unkownSize'] = ['size' => -1, 'mtime' => 25, 'mimetype' => 'foo/file'];
  147. $this->cache->put($file4, $fileData['unkownSize']);
  148. $this->assertEquals(-1, $this->cache->calculateFolderSize($folder));
  149. $fileData['unkownSize'] = ['size' => 5, 'mtime' => 25, 'mimetype' => 'foo/file'];
  150. $this->cache->put($file4, $fileData['unkownSize']);
  151. $this->assertEquals(1025, $this->cache->calculateFolderSize($folder));
  152. $this->cache->remove($file2);
  153. $this->cache->remove($file3);
  154. $this->cache->remove($file4);
  155. $this->assertEquals(0, $this->cache->calculateFolderSize($folder));
  156. $this->cache->remove($folder);
  157. $this->assertFalse($this->cache->inCache($folder . '/foo'));
  158. $this->assertFalse($this->cache->inCache($folder . '/bar'));
  159. }
  160. public function testRemoveRecursive() {
  161. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  162. $fileData = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'text/plain'];
  163. $folders = ['folder', 'folder/subfolder', 'folder/sub2', 'folder/sub2/sub3'];
  164. $files = ['folder/foo.txt', 'folder/bar.txt', 'folder/subfolder/asd.txt', 'folder/sub2/qwerty.txt', 'folder/sub2/sub3/foo.txt'];
  165. foreach ($folders as $folder) {
  166. $this->cache->put($folder, $folderData);
  167. }
  168. foreach ($files as $file) {
  169. $this->cache->put($file, $fileData);
  170. }
  171. $this->cache->remove('folder');
  172. foreach ($files as $file) {
  173. $this->assertFalse($this->cache->inCache($file));
  174. }
  175. }
  176. public function folderDataProvider() {
  177. return [
  178. ['folder'],
  179. // that was too easy, try something harder
  180. ['☺, WHITE SMILING FACE, UTF-8 hex E298BA'],
  181. // what about 4 byte utf-8
  182. ['😐, NEUTRAL_FACE, UTF-8 hex F09F9890'],
  183. // now the crazy stuff
  184. [', UNASSIGNED PRIVATE USE, UTF-8 hex EF9890'],
  185. // and my favorite
  186. ['w͢͢͝h͡o͢͡ ̸͢k̵͟n̴͘ǫw̸̛s͘ ̀́w͘͢ḩ̵a҉̡͢t ̧̕h́o̵r͏̵rors̡ ̶͡͠lį̶e͟͟ ̶͝in͢ ͏t̕h̷̡͟e ͟͟d̛a͜r̕͡k̢̨ ͡h̴e͏a̷̢̡rt́͏ ̴̷͠ò̵̶f̸ u̧͘ní̛͜c͢͏o̷͏d̸͢e̡͝']
  187. ];
  188. }
  189. public function testEncryptedFolder() {
  190. $file1 = 'folder';
  191. $file2 = 'folder/bar';
  192. $file3 = 'folder/foo';
  193. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  194. $fileData = [];
  195. $fileData['bar'] = ['size' => 1000, 'encrypted' => 1, 'mtime' => 20, 'mimetype' => 'foo/file'];
  196. $fileData['foo'] = ['size' => 20, 'encrypted' => 1, 'mtime' => 25, 'mimetype' => 'foo/file'];
  197. $this->cache->put($file1, $data1);
  198. $this->cache->put($file2, $fileData['bar']);
  199. $this->cache->put($file3, $fileData['foo']);
  200. $content = $this->cache->getFolderContents($file1);
  201. $this->assertEquals(count($content), 2);
  202. foreach ($content as $cachedData) {
  203. $data = $fileData[$cachedData['name']];
  204. }
  205. $file4 = 'folder/unkownSize';
  206. $fileData['unkownSize'] = ['size' => -1, 'mtime' => 25, 'mimetype' => 'foo/file'];
  207. $this->cache->put($file4, $fileData['unkownSize']);
  208. $this->assertEquals(-1, $this->cache->calculateFolderSize($file1));
  209. $fileData['unkownSize'] = ['size' => 5, 'mtime' => 25, 'mimetype' => 'foo/file'];
  210. $this->cache->put($file4, $fileData['unkownSize']);
  211. $this->assertEquals(1025, $this->cache->calculateFolderSize($file1));
  212. // direct cache entry retrieval returns the original values
  213. $entry = $this->cache->get($file1);
  214. $this->assertEquals(1025, $entry['size']);
  215. $this->cache->remove($file2);
  216. $this->cache->remove($file3);
  217. $this->cache->remove($file4);
  218. $this->assertEquals(0, $this->cache->calculateFolderSize($file1));
  219. $this->cache->remove('folder');
  220. $this->assertFalse($this->cache->inCache('folder/foo'));
  221. $this->assertFalse($this->cache->inCache('folder/bar'));
  222. }
  223. public function testRootFolderSizeForNonHomeStorage() {
  224. $dir1 = 'knownsize';
  225. $dir2 = 'unknownsize';
  226. $fileData = [];
  227. $fileData[''] = ['size' => -1, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory'];
  228. $fileData[$dir1] = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory'];
  229. $fileData[$dir2] = ['size' => -1, 'mtime' => 25, 'mimetype' => 'httpd/unix-directory'];
  230. $this->cache->put('', $fileData['']);
  231. $this->cache->put($dir1, $fileData[$dir1]);
  232. $this->cache->put($dir2, $fileData[$dir2]);
  233. $this->assertTrue($this->cache->inCache($dir1));
  234. $this->assertTrue($this->cache->inCache($dir2));
  235. // check that root size ignored the unknown sizes
  236. $this->assertEquals(-1, $this->cache->calculateFolderSize(''));
  237. // clean up
  238. $this->cache->remove('');
  239. $this->cache->remove($dir1);
  240. $this->cache->remove($dir2);
  241. $this->assertFalse($this->cache->inCache($dir1));
  242. $this->assertFalse($this->cache->inCache($dir2));
  243. }
  244. public function testStatus() {
  245. $this->assertEquals(\OC\Files\Cache\Cache::NOT_FOUND, $this->cache->getStatus('foo'));
  246. $this->cache->put('foo', ['size' => -1]);
  247. $this->assertEquals(\OC\Files\Cache\Cache::PARTIAL, $this->cache->getStatus('foo'));
  248. $this->cache->put('foo', ['size' => -1, 'mtime' => 20, 'mimetype' => 'foo/file']);
  249. $this->assertEquals(\OC\Files\Cache\Cache::SHALLOW, $this->cache->getStatus('foo'));
  250. $this->cache->put('foo', ['size' => 10]);
  251. $this->assertEquals(\OC\Files\Cache\Cache::COMPLETE, $this->cache->getStatus('foo'));
  252. }
  253. public function putWithAllKindOfQuotesData() {
  254. return [
  255. ['`backtick`'],
  256. ['´forward´'],
  257. ['\'single\''],
  258. ];
  259. }
  260. /**
  261. * @dataProvider putWithAllKindOfQuotesData
  262. * @param $fileName
  263. */
  264. public function testPutWithAllKindOfQuotes($fileName) {
  265. $this->assertEquals(\OC\Files\Cache\Cache::NOT_FOUND, $this->cache->get($fileName));
  266. $this->cache->put($fileName, ['size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file', 'etag' => $fileName]);
  267. $cacheEntry = $this->cache->get($fileName);
  268. $this->assertEquals($fileName, $cacheEntry['etag']);
  269. $this->assertEquals($fileName, $cacheEntry['path']);
  270. }
  271. public function testSearch() {
  272. $file1 = 'folder';
  273. $file2 = 'folder/foobar';
  274. $file3 = 'folder/foo';
  275. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
  276. $fileData = [];
  277. $fileData['foobar'] = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  278. $fileData['foo'] = ['size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file'];
  279. $this->cache->put($file1, $data1);
  280. $this->cache->put($file2, $fileData['foobar']);
  281. $this->cache->put($file3, $fileData['foo']);
  282. $this->assertEquals(2, count($this->cache->search('%foo%')));
  283. $this->assertEquals(1, count($this->cache->search('foo')));
  284. $this->assertEquals(1, count($this->cache->search('%folder%')));
  285. $this->assertEquals(1, count($this->cache->search('folder%')));
  286. $this->assertEquals(3, count($this->cache->search('%')));
  287. // case insensitive search should match the same files
  288. $this->assertEquals(2, count($this->cache->search('%Foo%')));
  289. $this->assertEquals(1, count($this->cache->search('Foo')));
  290. $this->assertEquals(1, count($this->cache->search('%Folder%')));
  291. $this->assertEquals(1, count($this->cache->search('Folder%')));
  292. $this->assertEquals(3, count($this->cache->searchByMime('foo')));
  293. $this->assertEquals(2, count($this->cache->searchByMime('foo/file')));
  294. }
  295. public function testSearchQueryByTag() {
  296. $userId = static::getUniqueID('user');
  297. \OC::$server->getUserManager()->createUser($userId, $userId);
  298. static::loginAsUser($userId);
  299. $user = new \OC\User\User($userId, null, \OC::$server->get(IEventDispatcher::class));
  300. $file1 = 'folder';
  301. $file2 = 'folder/foobar';
  302. $file3 = 'folder/foo';
  303. $file4 = 'folder/foo2';
  304. $file5 = 'folder/foo3';
  305. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
  306. $fileData = [];
  307. $fileData['foobar'] = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  308. $fileData['foo'] = ['size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file'];
  309. $fileData['foo2'] = ['size' => 25, 'mtime' => 28, 'mimetype' => 'foo/file'];
  310. $fileData['foo3'] = ['size' => 88, 'mtime' => 34, 'mimetype' => 'foo/file'];
  311. $id1 = $this->cache->put($file1, $data1);
  312. $id2 = $this->cache->put($file2, $fileData['foobar']);
  313. $id3 = $this->cache->put($file3, $fileData['foo']);
  314. $id4 = $this->cache->put($file4, $fileData['foo2']);
  315. $id5 = $this->cache->put($file5, $fileData['foo3']);
  316. $tagManager = \OC::$server->getTagManager()->load('files', [], false, $userId);
  317. $this->assertTrue($tagManager->tagAs($id1, 'tag1'));
  318. $this->assertTrue($tagManager->tagAs($id1, 'tag2'));
  319. $this->assertTrue($tagManager->tagAs($id2, 'tag2'));
  320. $this->assertTrue($tagManager->tagAs($id3, 'tag1'));
  321. $this->assertTrue($tagManager->tagAs($id4, 'tag2'));
  322. $results = $this->cache->searchQuery(new SearchQuery(
  323. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'tagname', 'tag2'),
  324. 0, 0, [], $user
  325. ));
  326. $this->assertEquals(3, count($results));
  327. usort($results, function ($value1, $value2) {
  328. return $value1['name'] <=> $value2['name'];
  329. });
  330. $this->assertEquals('folder', $results[0]['name']);
  331. $this->assertEquals('foo2', $results[1]['name']);
  332. $this->assertEquals('foobar', $results[2]['name']);
  333. $tagManager->delete('tag1');
  334. $tagManager->delete('tag2');
  335. static::logout();
  336. $user = \OC::$server->getUserManager()->get($userId);
  337. if ($user !== null) {
  338. try {
  339. $user->delete();
  340. } catch (\Exception $e) {
  341. }
  342. }
  343. }
  344. public function testSearchByQuery() {
  345. $file1 = 'folder';
  346. $file2 = 'folder/foobar';
  347. $file3 = 'folder/foo';
  348. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
  349. $fileData = [];
  350. $fileData['foobar'] = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  351. $fileData['foo'] = ['size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file'];
  352. $this->cache->put($file1, $data1);
  353. $this->cache->put($file2, $fileData['foobar']);
  354. $this->cache->put($file3, $fileData['foo']);
  355. /** @var IUser $user */
  356. $user = $this->createMock(IUser::class);
  357. $this->assertCount(1, $this->cache->searchQuery(new SearchQuery(
  358. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', 'foo'), 10, 0, [], $user)));
  359. $this->assertCount(2, $this->cache->searchQuery(new SearchQuery(
  360. new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', 'foo%'), 10, 0, [], $user)));
  361. $this->assertCount(2, $this->cache->searchQuery(new SearchQuery(
  362. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', 'foo/file'), 10, 0, [], $user)));
  363. $this->assertCount(3, $this->cache->searchQuery(new SearchQuery(
  364. new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'foo/%'), 10, 0, [], $user)));
  365. $this->assertCount(1, $this->cache->searchQuery(new SearchQuery(
  366. new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN, 'size', 100), 10, 0, [], $user)));
  367. $this->assertCount(2, $this->cache->searchQuery(new SearchQuery(
  368. new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN_EQUAL, 'size', 100), 10, 0, [], $user)));
  369. }
  370. public function movePathProvider() {
  371. return [
  372. ['folder/foo', 'folder/foobar', ['1', '2']],
  373. ['folder/foo', 'foo', ['1', '2']],
  374. ['files/Индустрия_Инженерные системы ЦОД', 'files/Индустрия_Инженерные системы ЦОД1', ['1', '2']],
  375. ];
  376. }
  377. /**
  378. * @dataProvider movePathProvider
  379. */
  380. public function testMove($sourceFolder, $targetFolder, $children) {
  381. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/bar'];
  382. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  383. // create folders
  384. foreach ([$sourceFolder, $targetFolder] as $current) {
  385. while (strpos($current, '/') > 0) {
  386. $current = dirname($current);
  387. $this->cache->put($current, $folderData);
  388. $this->cache2->put($current, $folderData);
  389. }
  390. }
  391. $this->cache->put($sourceFolder, $folderData);
  392. $this->cache2->put($sourceFolder, $folderData);
  393. foreach ($children as $child) {
  394. $this->cache->put($sourceFolder . '/' . $child, $data);
  395. $this->cache2->put($sourceFolder . '/' . $child, $data);
  396. }
  397. $this->cache->move($sourceFolder, $targetFolder);
  398. $this->assertFalse($this->cache->inCache($sourceFolder));
  399. $this->assertTrue($this->cache2->inCache($sourceFolder));
  400. $this->assertTrue($this->cache->inCache($targetFolder));
  401. $this->assertFalse($this->cache2->inCache($targetFolder));
  402. foreach ($children as $child) {
  403. $this->assertFalse($this->cache->inCache($sourceFolder . '/' . $child));
  404. $this->assertTrue($this->cache2->inCache($sourceFolder . '/' . $child));
  405. $this->assertTrue($this->cache->inCache($targetFolder . '/' . $child));
  406. $this->assertFalse($this->cache2->inCache($targetFolder . '/' . $child));
  407. }
  408. }
  409. public function testGetIncomplete() {
  410. $file1 = 'folder1';
  411. $file2 = 'folder2';
  412. $file3 = 'folder3';
  413. $file4 = 'folder4';
  414. $data = ['size' => 10, 'mtime' => 50, 'mimetype' => 'foo/bar'];
  415. $this->cache->put($file1, $data);
  416. $data['size'] = -1;
  417. $this->cache->put($file2, $data);
  418. $this->cache->put($file3, $data);
  419. $data['size'] = 12;
  420. $this->cache->put($file4, $data);
  421. $this->assertEquals($file3, $this->cache->getIncomplete());
  422. }
  423. public function testNonExisting() {
  424. $this->assertFalse($this->cache->get('foo.txt'));
  425. $this->assertFalse($this->cache->get(-1));
  426. $this->assertEquals([], $this->cache->getFolderContents('foo'));
  427. }
  428. public function testGetById() {
  429. $storageId = $this->storage->getId();
  430. $data = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  431. $id = $this->cache->put('foo', $data);
  432. if (strlen($storageId) > 64) {
  433. $storageId = md5($storageId);
  434. }
  435. $this->assertEquals([$storageId, 'foo'], \OC\Files\Cache\Cache::getById($id));
  436. }
  437. public function testStorageMTime() {
  438. $data = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  439. $this->cache->put('foo', $data);
  440. $cachedData = $this->cache->get('foo');
  441. $this->assertEquals($data['mtime'], $cachedData['storage_mtime']); //if no storage_mtime is saved, mtime should be used
  442. $this->cache->put('foo', ['storage_mtime' => 30]); //when setting storage_mtime, mtime is also set
  443. $cachedData = $this->cache->get('foo');
  444. $this->assertEquals(30, $cachedData['storage_mtime']);
  445. $this->assertEquals(30, $cachedData['mtime']);
  446. $this->cache->put('foo', ['mtime' => 25]); //setting mtime does not change storage_mtime
  447. $cachedData = $this->cache->get('foo');
  448. $this->assertEquals(30, $cachedData['storage_mtime']);
  449. $this->assertEquals(25, $cachedData['mtime']);
  450. }
  451. public function testLongId() {
  452. $storage = new LongId([]);
  453. $cache = $storage->getCache();
  454. $storageId = $storage->getId();
  455. $data = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  456. $id = $cache->put('foo', $data);
  457. $this->assertEquals([md5($storageId), 'foo'], \OC\Files\Cache\Cache::getById($id));
  458. }
  459. /**
  460. * this test show the bug resulting if we have no normalizer installed
  461. */
  462. public function testWithoutNormalizer() {
  463. // folder name "Schön" with U+00F6 (normalized)
  464. $folderWith00F6 = "\x53\x63\x68\xc3\xb6\x6e";
  465. // folder name "Schön" with U+0308 (un-normalized)
  466. $folderWith0308 = "\x53\x63\x68\x6f\xcc\x88\x6e";
  467. /**
  468. * @var \OC\Files\Cache\Cache | \PHPUnit\Framework\MockObject\MockObject $cacheMock
  469. */
  470. $cacheMock = $this->getMockBuilder(Cache::class)
  471. ->setMethods(['normalize'])
  472. ->setConstructorArgs([$this->storage])
  473. ->getMock();
  474. $cacheMock->expects($this->any())
  475. ->method('normalize')
  476. ->willReturnArgument(0);
  477. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  478. // put root folder
  479. $this->assertFalse($cacheMock->get('folder'));
  480. $this->assertGreaterThan(0, $cacheMock->put('folder', $data));
  481. // put un-normalized folder
  482. $this->assertFalse($cacheMock->get('folder/' . $folderWith0308));
  483. $this->assertGreaterThan(0, $cacheMock->put('folder/' . $folderWith0308, $data));
  484. // get un-normalized folder by name
  485. $unNormalizedFolderName = $cacheMock->get('folder/' . $folderWith0308);
  486. // check if database layer normalized the folder name (this should not happen)
  487. $this->assertEquals($folderWith0308, $unNormalizedFolderName['name']);
  488. // put normalized folder
  489. $this->assertFalse($cacheMock->get('folder/' . $folderWith00F6));
  490. $this->assertGreaterThan(0, $cacheMock->put('folder/' . $folderWith00F6, $data));
  491. // this is our bug, we have two different hashes with the same name (Schön)
  492. $this->assertEquals(2, count($cacheMock->getFolderContents('folder')));
  493. }
  494. /**
  495. * this test shows that there is no bug if we use the normalizer
  496. */
  497. public function testWithNormalizer() {
  498. if (!class_exists('Patchwork\PHP\Shim\Normalizer')) {
  499. $this->markTestSkipped('The 3rdparty Normalizer extension is not available.');
  500. return;
  501. }
  502. // folder name "Schön" with U+00F6 (normalized)
  503. $folderWith00F6 = "\x53\x63\x68\xc3\xb6\x6e";
  504. // folder name "Schön" with U+0308 (un-normalized)
  505. $folderWith0308 = "\x53\x63\x68\x6f\xcc\x88\x6e";
  506. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  507. // put root folder
  508. $this->assertFalse($this->cache->get('folder'));
  509. $this->assertGreaterThan(0, $this->cache->put('folder', $data));
  510. // put un-normalized folder
  511. $this->assertFalse($this->cache->get('folder/' . $folderWith0308));
  512. $this->assertGreaterThan(0, $this->cache->put('folder/' . $folderWith0308, $data));
  513. // get un-normalized folder by name
  514. $unNormalizedFolderName = $this->cache->get('folder/' . $folderWith0308);
  515. // check if folder name was normalized
  516. $this->assertEquals($folderWith00F6, $unNormalizedFolderName['name']);
  517. // put normalized folder
  518. $this->assertInstanceOf('\OCP\Files\Cache\ICacheEntry', $this->cache->get('folder/' . $folderWith00F6));
  519. $this->assertGreaterThan(0, $this->cache->put('folder/' . $folderWith00F6, $data));
  520. // at this point we should have only one folder named "Schön"
  521. $this->assertEquals(1, count($this->cache->getFolderContents('folder')));
  522. }
  523. public function bogusPathNamesProvider() {
  524. return [
  525. ['/bogus.txt', 'bogus.txt'],
  526. ['//bogus.txt', 'bogus.txt'],
  527. ['bogus/', 'bogus'],
  528. ['bogus//', 'bogus'],
  529. ];
  530. }
  531. /**
  532. * Test bogus paths with leading or doubled slashes
  533. *
  534. * @dataProvider bogusPathNamesProvider
  535. */
  536. public function testBogusPaths($bogusPath, $fixedBogusPath) {
  537. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  538. // put root folder
  539. $this->assertFalse($this->cache->get(''));
  540. $parentId = $this->cache->put('', $data);
  541. $this->assertGreaterThan(0, $parentId);
  542. $this->assertGreaterThan(0, $this->cache->put($bogusPath, $data));
  543. $newData = $this->cache->get($fixedBogusPath);
  544. $this->assertNotFalse($newData);
  545. $this->assertEquals($fixedBogusPath, $newData['path']);
  546. // parent is the correct one, resolved properly (they used to not be)
  547. $this->assertEquals($parentId, $newData['parent']);
  548. $newDataFromBogus = $this->cache->get($bogusPath);
  549. // same entry
  550. $this->assertEquals($newData, $newDataFromBogus);
  551. }
  552. public function testNoReuseOfFileId() {
  553. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'];
  554. $this->cache->put('somefile.txt', $data1);
  555. $info = $this->cache->get('somefile.txt');
  556. $fileId = $info['fileid'];
  557. $this->cache->remove('somefile.txt');
  558. $data2 = ['size' => 200, 'mtime' => 100, 'mimetype' => 'text/plain'];
  559. $this->cache->put('anotherfile.txt', $data2);
  560. $info2 = $this->cache->get('anotherfile.txt');
  561. $fileId2 = $info2['fileid'];
  562. $this->assertNotEquals($fileId, $fileId2);
  563. }
  564. public function escapingProvider() {
  565. return [
  566. ['foo'],
  567. ['o%'],
  568. ['oth_r'],
  569. ];
  570. }
  571. /**
  572. * @param string $name
  573. * @dataProvider escapingProvider
  574. */
  575. public function testEscaping($name) {
  576. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'];
  577. $this->cache->put($name, $data);
  578. $this->assertTrue($this->cache->inCache($name));
  579. $retrievedData = $this->cache->get($name);
  580. foreach ($data as $key => $value) {
  581. $this->assertEquals($value, $retrievedData[$key]);
  582. }
  583. $this->cache->move($name, $name . 'asd');
  584. $this->assertFalse($this->cache->inCache($name));
  585. $this->assertTrue($this->cache->inCache($name . 'asd'));
  586. $this->cache->remove($name . 'asd');
  587. $this->assertFalse($this->cache->inCache($name . 'asd'));
  588. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  589. $this->cache->put($name, $folderData);
  590. $this->cache->put('other', $folderData);
  591. $childs = ['asd', 'bar', 'foo', 'sub/folder'];
  592. $this->cache->put($name . '/sub', $folderData);
  593. $this->cache->put('other/sub', $folderData);
  594. foreach ($childs as $child) {
  595. $this->cache->put($name . '/' . $child, $data);
  596. $this->cache->put('other/' . $child, $data);
  597. $this->assertTrue($this->cache->inCache($name . '/' . $child));
  598. }
  599. $this->cache->move($name, $name . 'asd');
  600. foreach ($childs as $child) {
  601. $this->assertTrue($this->cache->inCache($name . 'asd/' . $child));
  602. $this->assertTrue($this->cache->inCache('other/' . $child));
  603. }
  604. foreach ($childs as $child) {
  605. $this->cache->remove($name . 'asd/' . $child);
  606. $this->assertFalse($this->cache->inCache($name . 'asd/' . $child));
  607. $this->assertTrue($this->cache->inCache('other/' . $child));
  608. }
  609. }
  610. public function testExtended() {
  611. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  612. $this->cache->put("", $folderData);
  613. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'creation_time' => 20];
  614. $id1 = $this->cache->put("foo1", $data);
  615. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'upload_time' => 30];
  616. $this->cache->put("foo2", $data);
  617. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'metadata_etag' => 'foo'];
  618. $this->cache->put("foo3", $data);
  619. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'];
  620. $id4 = $this->cache->put("foo4", $data);
  621. $entry = $this->cache->get($id1);
  622. $this->assertEquals(20, $entry->getCreationTime());
  623. $this->assertEquals(0, $entry->getUploadTime());
  624. $this->assertEquals(null, $entry->getMetadataEtag());
  625. $entries = $this->cache->getFolderContents("");
  626. $this->assertCount(4, $entries);
  627. $this->assertEquals("foo1", $entries[0]->getName());
  628. $this->assertEquals("foo2", $entries[1]->getName());
  629. $this->assertEquals("foo3", $entries[2]->getName());
  630. $this->assertEquals("foo4", $entries[3]->getName());
  631. $this->assertEquals(20, $entries[0]->getCreationTime());
  632. $this->assertEquals(0, $entries[0]->getUploadTime());
  633. $this->assertEquals(null, $entries[0]->getMetadataEtag());
  634. $this->assertEquals(0, $entries[1]->getCreationTime());
  635. $this->assertEquals(30, $entries[1]->getUploadTime());
  636. $this->assertEquals(null, $entries[1]->getMetadataEtag());
  637. $this->assertEquals(0, $entries[2]->getCreationTime());
  638. $this->assertEquals(0, $entries[2]->getUploadTime());
  639. $this->assertEquals('foo', $entries[2]->getMetadataEtag());
  640. $this->assertEquals(0, $entries[3]->getCreationTime());
  641. $this->assertEquals(0, $entries[3]->getUploadTime());
  642. $this->assertEquals(null, $entries[3]->getMetadataEtag());
  643. $this->cache->update($id1, ['upload_time' => 25]);
  644. $entry = $this->cache->get($id1);
  645. $this->assertEquals(20, $entry->getCreationTime());
  646. $this->assertEquals(25, $entry->getUploadTime());
  647. $this->assertEquals(null, $entry->getMetadataEtag());
  648. $this->cache->put("sub", $folderData);
  649. $this->cache->move("foo1", "sub/foo1");
  650. $entries = $this->cache->getFolderContents("sub");
  651. $this->assertCount(1, $entries);
  652. $this->assertEquals(20, $entries[0]->getCreationTime());
  653. $this->assertEquals(25, $entries[0]->getUploadTime());
  654. $this->assertEquals(null, $entries[0]->getMetadataEtag());
  655. $this->cache->update($id4, ['upload_time' => 25]);
  656. $entry = $this->cache->get($id4);
  657. $this->assertEquals(0, $entry->getCreationTime());
  658. $this->assertEquals(25, $entry->getUploadTime());
  659. $this->assertEquals(null, $entry->getMetadataEtag());
  660. $this->cache->remove("sub");
  661. }
  662. protected function tearDown(): void {
  663. if ($this->cache) {
  664. $this->cache->clear();
  665. }
  666. parent::tearDown();
  667. }
  668. protected function setUp(): void {
  669. parent::setUp();
  670. $this->storage = new \OC\Files\Storage\Temporary([]);
  671. $this->storage2 = new \OC\Files\Storage\Temporary([]);
  672. $this->cache = new \OC\Files\Cache\Cache($this->storage);
  673. $this->cache2 = new \OC\Files\Cache\Cache($this->storage2);
  674. }
  675. }