You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CacheTest.php 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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'), 10, 0, [], $user)));
  337. $this->assertCount(2, $this->cache->searchQuery(new SearchQuery(
  338. new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', 'foo%'), 10, 0, [], $user)));
  339. $this->assertCount(2, $this->cache->searchQuery(new SearchQuery(
  340. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', 'foo/file'), 10, 0, [], $user)));
  341. $this->assertCount(3, $this->cache->searchQuery(new SearchQuery(
  342. new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'foo/%'), 10, 0, [], $user)));
  343. $this->assertCount(1, $this->cache->searchQuery(new SearchQuery(
  344. new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN, 'size', 100), 10, 0, [], $user)));
  345. $this->assertCount(2, $this->cache->searchQuery(new SearchQuery(
  346. new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN_EQUAL, 'size', 100), 10, 0, [], $user)));
  347. }
  348. public function movePathProvider() {
  349. return [
  350. ['folder/foo', 'folder/foobar', ['1', '2']],
  351. ['folder/foo', 'foo', ['1', '2']],
  352. ['files/Индустрия_Инженерные системы ЦОД', 'files/Индустрия_Инженерные системы ЦОД1', ['1', '2']],
  353. ];
  354. }
  355. /**
  356. * @dataProvider movePathProvider
  357. */
  358. public function testMove($sourceFolder, $targetFolder, $children) {
  359. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/bar'];
  360. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  361. // create folders
  362. foreach ([$sourceFolder, $targetFolder] as $current) {
  363. while (strpos($current, '/') > 0) {
  364. $current = dirname($current);
  365. $this->cache->put($current, $folderData);
  366. $this->cache2->put($current, $folderData);
  367. }
  368. }
  369. $this->cache->put($sourceFolder, $folderData);
  370. $this->cache2->put($sourceFolder, $folderData);
  371. foreach ($children as $child) {
  372. $this->cache->put($sourceFolder . '/' . $child, $data);
  373. $this->cache2->put($sourceFolder . '/' . $child, $data);
  374. }
  375. $this->cache->move($sourceFolder, $targetFolder);
  376. $this->assertFalse($this->cache->inCache($sourceFolder));
  377. $this->assertTrue($this->cache2->inCache($sourceFolder));
  378. $this->assertTrue($this->cache->inCache($targetFolder));
  379. $this->assertFalse($this->cache2->inCache($targetFolder));
  380. foreach ($children as $child) {
  381. $this->assertFalse($this->cache->inCache($sourceFolder . '/' . $child));
  382. $this->assertTrue($this->cache2->inCache($sourceFolder . '/' . $child));
  383. $this->assertTrue($this->cache->inCache($targetFolder . '/' . $child));
  384. $this->assertFalse($this->cache2->inCache($targetFolder . '/' . $child));
  385. }
  386. }
  387. public function testGetIncomplete() {
  388. $file1 = 'folder1';
  389. $file2 = 'folder2';
  390. $file3 = 'folder3';
  391. $file4 = 'folder4';
  392. $data = ['size' => 10, 'mtime' => 50, 'mimetype' => 'foo/bar'];
  393. $this->cache->put($file1, $data);
  394. $data['size'] = -1;
  395. $this->cache->put($file2, $data);
  396. $this->cache->put($file3, $data);
  397. $data['size'] = 12;
  398. $this->cache->put($file4, $data);
  399. $this->assertEquals($file3, $this->cache->getIncomplete());
  400. }
  401. public function testNonExisting() {
  402. $this->assertFalse($this->cache->get('foo.txt'));
  403. $this->assertFalse($this->cache->get(-1));
  404. $this->assertEquals([], $this->cache->getFolderContents('foo'));
  405. }
  406. public function testGetById() {
  407. $storageId = $this->storage->getId();
  408. $data = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  409. $id = $this->cache->put('foo', $data);
  410. if (strlen($storageId) > 64) {
  411. $storageId = md5($storageId);
  412. }
  413. $this->assertEquals([$storageId, 'foo'], \OC\Files\Cache\Cache::getById($id));
  414. }
  415. public function testStorageMTime() {
  416. $data = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  417. $this->cache->put('foo', $data);
  418. $cachedData = $this->cache->get('foo');
  419. $this->assertEquals($data['mtime'], $cachedData['storage_mtime']); //if no storage_mtime is saved, mtime should be used
  420. $this->cache->put('foo', ['storage_mtime' => 30]); //when setting storage_mtime, mtime is also set
  421. $cachedData = $this->cache->get('foo');
  422. $this->assertEquals(30, $cachedData['storage_mtime']);
  423. $this->assertEquals(30, $cachedData['mtime']);
  424. $this->cache->put('foo', ['mtime' => 25]); //setting mtime does not change storage_mtime
  425. $cachedData = $this->cache->get('foo');
  426. $this->assertEquals(30, $cachedData['storage_mtime']);
  427. $this->assertEquals(25, $cachedData['mtime']);
  428. }
  429. public function testLongId() {
  430. $storage = new LongId([]);
  431. $cache = $storage->getCache();
  432. $storageId = $storage->getId();
  433. $data = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  434. $id = $cache->put('foo', $data);
  435. $this->assertEquals([md5($storageId), 'foo'], \OC\Files\Cache\Cache::getById($id));
  436. }
  437. /**
  438. * this test show the bug resulting if we have no normalizer installed
  439. */
  440. public function testWithoutNormalizer() {
  441. // folder name "Schön" with U+00F6 (normalized)
  442. $folderWith00F6 = "\x53\x63\x68\xc3\xb6\x6e";
  443. // folder name "Schön" with U+0308 (un-normalized)
  444. $folderWith0308 = "\x53\x63\x68\x6f\xcc\x88\x6e";
  445. /**
  446. * @var \OC\Files\Cache\Cache | \PHPUnit\Framework\MockObject\MockObject $cacheMock
  447. */
  448. $cacheMock = $this->getMockBuilder(Cache::class)
  449. ->setMethods(['normalize'])
  450. ->setConstructorArgs([$this->storage])
  451. ->getMock();
  452. $cacheMock->expects($this->any())
  453. ->method('normalize')
  454. ->willReturnArgument(0);
  455. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  456. // put root folder
  457. $this->assertFalse($cacheMock->get('folder'));
  458. $this->assertGreaterThan(0, $cacheMock->put('folder', $data));
  459. // put un-normalized folder
  460. $this->assertFalse($cacheMock->get('folder/' . $folderWith0308));
  461. $this->assertGreaterThan(0, $cacheMock->put('folder/' . $folderWith0308, $data));
  462. // get un-normalized folder by name
  463. $unNormalizedFolderName = $cacheMock->get('folder/' . $folderWith0308);
  464. // check if database layer normalized the folder name (this should not happen)
  465. $this->assertEquals($folderWith0308, $unNormalizedFolderName['name']);
  466. // put normalized folder
  467. $this->assertFalse($cacheMock->get('folder/' . $folderWith00F6));
  468. $this->assertGreaterThan(0, $cacheMock->put('folder/' . $folderWith00F6, $data));
  469. // this is our bug, we have two different hashes with the same name (Schön)
  470. $this->assertEquals(2, count($cacheMock->getFolderContents('folder')));
  471. }
  472. /**
  473. * this test shows that there is no bug if we use the normalizer
  474. */
  475. public function testWithNormalizer() {
  476. if (!class_exists('Patchwork\PHP\Shim\Normalizer')) {
  477. $this->markTestSkipped('The 3rdparty Normalizer extension is not available.');
  478. return;
  479. }
  480. // folder name "Schön" with U+00F6 (normalized)
  481. $folderWith00F6 = "\x53\x63\x68\xc3\xb6\x6e";
  482. // folder name "Schön" with U+0308 (un-normalized)
  483. $folderWith0308 = "\x53\x63\x68\x6f\xcc\x88\x6e";
  484. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  485. // put root folder
  486. $this->assertFalse($this->cache->get('folder'));
  487. $this->assertGreaterThan(0, $this->cache->put('folder', $data));
  488. // put un-normalized folder
  489. $this->assertFalse($this->cache->get('folder/' . $folderWith0308));
  490. $this->assertGreaterThan(0, $this->cache->put('folder/' . $folderWith0308, $data));
  491. // get un-normalized folder by name
  492. $unNormalizedFolderName = $this->cache->get('folder/' . $folderWith0308);
  493. // check if folder name was normalized
  494. $this->assertEquals($folderWith00F6, $unNormalizedFolderName['name']);
  495. // put normalized folder
  496. $this->assertInstanceOf('\OCP\Files\Cache\ICacheEntry', $this->cache->get('folder/' . $folderWith00F6));
  497. $this->assertGreaterThan(0, $this->cache->put('folder/' . $folderWith00F6, $data));
  498. // at this point we should have only one folder named "Schön"
  499. $this->assertEquals(1, count($this->cache->getFolderContents('folder')));
  500. }
  501. public function bogusPathNamesProvider() {
  502. return [
  503. ['/bogus.txt', 'bogus.txt'],
  504. ['//bogus.txt', 'bogus.txt'],
  505. ['bogus/', 'bogus'],
  506. ['bogus//', 'bogus'],
  507. ];
  508. }
  509. /**
  510. * Test bogus paths with leading or doubled slashes
  511. *
  512. * @dataProvider bogusPathNamesProvider
  513. */
  514. public function testBogusPaths($bogusPath, $fixedBogusPath) {
  515. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  516. // put root folder
  517. $this->assertFalse($this->cache->get(''));
  518. $parentId = $this->cache->put('', $data);
  519. $this->assertGreaterThan(0, $parentId);
  520. $this->assertGreaterThan(0, $this->cache->put($bogusPath, $data));
  521. $newData = $this->cache->get($fixedBogusPath);
  522. $this->assertNotFalse($newData);
  523. $this->assertEquals($fixedBogusPath, $newData['path']);
  524. // parent is the correct one, resolved properly (they used to not be)
  525. $this->assertEquals($parentId, $newData['parent']);
  526. $newDataFromBogus = $this->cache->get($bogusPath);
  527. // same entry
  528. $this->assertEquals($newData, $newDataFromBogus);
  529. }
  530. public function testNoReuseOfFileId() {
  531. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'];
  532. $this->cache->put('somefile.txt', $data1);
  533. $info = $this->cache->get('somefile.txt');
  534. $fileId = $info['fileid'];
  535. $this->cache->remove('somefile.txt');
  536. $data2 = ['size' => 200, 'mtime' => 100, 'mimetype' => 'text/plain'];
  537. $this->cache->put('anotherfile.txt', $data2);
  538. $info2 = $this->cache->get('anotherfile.txt');
  539. $fileId2 = $info2['fileid'];
  540. $this->assertNotEquals($fileId, $fileId2);
  541. }
  542. public function escapingProvider() {
  543. return [
  544. ['foo'],
  545. ['o%'],
  546. ['oth_r'],
  547. ];
  548. }
  549. /**
  550. * @param string $name
  551. * @dataProvider escapingProvider
  552. */
  553. public function testEscaping($name) {
  554. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'];
  555. $this->cache->put($name, $data);
  556. $this->assertTrue($this->cache->inCache($name));
  557. $retrievedData = $this->cache->get($name);
  558. foreach ($data as $key => $value) {
  559. $this->assertEquals($value, $retrievedData[$key]);
  560. }
  561. $this->cache->move($name, $name . 'asd');
  562. $this->assertFalse($this->cache->inCache($name));
  563. $this->assertTrue($this->cache->inCache($name . 'asd'));
  564. $this->cache->remove($name . 'asd');
  565. $this->assertFalse($this->cache->inCache($name . 'asd'));
  566. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  567. $this->cache->put($name, $folderData);
  568. $this->cache->put('other', $folderData);
  569. $childs = ['asd', 'bar', 'foo', 'sub/folder'];
  570. $this->cache->put($name . '/sub', $folderData);
  571. $this->cache->put('other/sub', $folderData);
  572. foreach ($childs as $child) {
  573. $this->cache->put($name . '/' . $child, $data);
  574. $this->cache->put('other/' . $child, $data);
  575. $this->assertTrue($this->cache->inCache($name . '/' . $child));
  576. }
  577. $this->cache->move($name, $name . 'asd');
  578. foreach ($childs as $child) {
  579. $this->assertTrue($this->cache->inCache($name . 'asd/' . $child));
  580. $this->assertTrue($this->cache->inCache('other/' . $child));
  581. }
  582. foreach ($childs as $child) {
  583. $this->cache->remove($name . 'asd/' . $child);
  584. $this->assertFalse($this->cache->inCache($name . 'asd/' . $child));
  585. $this->assertTrue($this->cache->inCache('other/' . $child));
  586. }
  587. }
  588. public function testExtended() {
  589. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  590. $this->cache->put("", $folderData);
  591. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'creation_time' => 20];
  592. $id1 = $this->cache->put("foo1", $data);
  593. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'upload_time' => 30];
  594. $this->cache->put("foo2", $data);
  595. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'metadata_etag' => 'foo'];
  596. $this->cache->put("foo3", $data);
  597. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'];
  598. $id4 = $this->cache->put("foo4", $data);
  599. $entry = $this->cache->get($id1);
  600. $this->assertEquals(20, $entry->getCreationTime());
  601. $this->assertEquals(0, $entry->getUploadTime());
  602. $this->assertEquals(null, $entry->getMetadataEtag());
  603. $entries = $this->cache->getFolderContents("");
  604. $this->assertCount(4, $entries);
  605. $this->assertEquals("foo1", $entries[0]->getName());
  606. $this->assertEquals("foo2", $entries[1]->getName());
  607. $this->assertEquals("foo3", $entries[2]->getName());
  608. $this->assertEquals("foo4", $entries[3]->getName());
  609. $this->assertEquals(20, $entries[0]->getCreationTime());
  610. $this->assertEquals(0, $entries[0]->getUploadTime());
  611. $this->assertEquals(null, $entries[0]->getMetadataEtag());
  612. $this->assertEquals(0, $entries[1]->getCreationTime());
  613. $this->assertEquals(30, $entries[1]->getUploadTime());
  614. $this->assertEquals(null, $entries[1]->getMetadataEtag());
  615. $this->assertEquals(0, $entries[2]->getCreationTime());
  616. $this->assertEquals(0, $entries[2]->getUploadTime());
  617. $this->assertEquals('foo', $entries[2]->getMetadataEtag());
  618. $this->assertEquals(0, $entries[3]->getCreationTime());
  619. $this->assertEquals(0, $entries[3]->getUploadTime());
  620. $this->assertEquals(null, $entries[3]->getMetadataEtag());
  621. $this->cache->update($id1, ['upload_time' => 25]);
  622. $entry = $this->cache->get($id1);
  623. $this->assertEquals(20, $entry->getCreationTime());
  624. $this->assertEquals(25, $entry->getUploadTime());
  625. $this->assertEquals(null, $entry->getMetadataEtag());
  626. $this->cache->put("sub", $folderData);
  627. $this->cache->move("foo1", "sub/foo1");
  628. $entries = $this->cache->getFolderContents("sub");
  629. $this->assertCount(1, $entries);
  630. $this->assertEquals(20, $entries[0]->getCreationTime());
  631. $this->assertEquals(25, $entries[0]->getUploadTime());
  632. $this->assertEquals(null, $entries[0]->getMetadataEtag());
  633. $this->cache->update($id4, ['upload_time' => 25]);
  634. $entry = $this->cache->get($id4);
  635. $this->assertEquals(0, $entry->getCreationTime());
  636. $this->assertEquals(25, $entry->getUploadTime());
  637. $this->assertEquals(null, $entry->getMetadataEtag());
  638. $this->cache->remove("sub");
  639. }
  640. protected function tearDown(): void {
  641. if ($this->cache) {
  642. $this->cache->clear();
  643. }
  644. parent::tearDown();
  645. }
  646. protected function setUp(): void {
  647. parent::setUp();
  648. $this->storage = new \OC\Files\Storage\Temporary([]);
  649. $this->storage2 = new \OC\Files\Storage\Temporary([]);
  650. $this->cache = new \OC\Files\Cache\Cache($this->storage);
  651. $this->cache2 = new \OC\Files\Cache\Cache($this->storage2);
  652. }
  653. }