1
0

CacheTest.php 30 KB

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