CacheTest.php 28 KB

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