1
0

CacheTest.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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\EventDispatcher\IEventDispatcher;
  14. use OCP\Files\Search\ISearchComparison;
  15. use OCP\IUser;
  16. class LongId extends \OC\Files\Storage\Temporary {
  17. public function getId() {
  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() {
  46. $this->assertNotNull($this->cache->getNumericStorageId());
  47. }
  48. public function testSimple() {
  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() {
  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() {
  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) {
  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()->getDatabasePlatform() instanceof MySqlPlatform && $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' => 'httpd/unix-directory'];
  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() {
  162. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  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() {
  191. $file1 = 'folder';
  192. $file2 = 'folder/bar';
  193. $file3 = 'folder/foo';
  194. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  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() {
  225. $dir1 = 'knownsize';
  226. $dir2 = 'unknownsize';
  227. $fileData = [];
  228. $fileData[''] = ['size' => -1, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory'];
  229. $fileData[$dir1] = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory'];
  230. $fileData[$dir2] = ['size' => -1, 'mtime' => 25, 'mimetype' => 'httpd/unix-directory'];
  231. $this->cache->put('', $fileData['']);
  232. $this->cache->put($dir1, $fileData[$dir1]);
  233. $this->cache->put($dir2, $fileData[$dir2]);
  234. $this->assertTrue($this->cache->inCache($dir1));
  235. $this->assertTrue($this->cache->inCache($dir2));
  236. // check that root size ignored the unknown sizes
  237. $this->assertEquals(-1, $this->cache->calculateFolderSize(''));
  238. // clean up
  239. $this->cache->remove('');
  240. $this->cache->remove($dir1);
  241. $this->cache->remove($dir2);
  242. $this->assertFalse($this->cache->inCache($dir1));
  243. $this->assertFalse($this->cache->inCache($dir2));
  244. }
  245. public function testStatus() {
  246. $this->assertEquals(\OC\Files\Cache\Cache::NOT_FOUND, $this->cache->getStatus('foo'));
  247. $this->cache->put('foo', ['size' => -1]);
  248. $this->assertEquals(\OC\Files\Cache\Cache::PARTIAL, $this->cache->getStatus('foo'));
  249. $this->cache->put('foo', ['size' => -1, 'mtime' => 20, 'mimetype' => 'foo/file']);
  250. $this->assertEquals(\OC\Files\Cache\Cache::SHALLOW, $this->cache->getStatus('foo'));
  251. $this->cache->put('foo', ['size' => 10]);
  252. $this->assertEquals(\OC\Files\Cache\Cache::COMPLETE, $this->cache->getStatus('foo'));
  253. }
  254. public function putWithAllKindOfQuotesData() {
  255. return [
  256. ['`backtick`'],
  257. ['´forward´'],
  258. ['\'single\''],
  259. ];
  260. }
  261. /**
  262. * @dataProvider putWithAllKindOfQuotesData
  263. * @param $fileName
  264. */
  265. public function testPutWithAllKindOfQuotes($fileName) {
  266. $this->assertEquals(\OC\Files\Cache\Cache::NOT_FOUND, $this->cache->get($fileName));
  267. $this->cache->put($fileName, ['size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file', 'etag' => $fileName]);
  268. $cacheEntry = $this->cache->get($fileName);
  269. $this->assertEquals($fileName, $cacheEntry['etag']);
  270. $this->assertEquals($fileName, $cacheEntry['path']);
  271. }
  272. public function testSearch() {
  273. $file1 = 'folder';
  274. $file2 = 'folder/foobar';
  275. $file3 = 'folder/foo';
  276. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
  277. $fileData = [];
  278. $fileData['foobar'] = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  279. $fileData['foo'] = ['size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file'];
  280. $this->cache->put($file1, $data1);
  281. $this->cache->put($file2, $fileData['foobar']);
  282. $this->cache->put($file3, $fileData['foo']);
  283. $this->assertEquals(2, count($this->cache->search('%foo%')));
  284. $this->assertEquals(1, count($this->cache->search('foo')));
  285. $this->assertEquals(1, count($this->cache->search('%folder%')));
  286. $this->assertEquals(1, count($this->cache->search('folder%')));
  287. $this->assertEquals(3, count($this->cache->search('%')));
  288. // case insensitive search should match the same files
  289. $this->assertEquals(2, count($this->cache->search('%Foo%')));
  290. $this->assertEquals(1, count($this->cache->search('Foo')));
  291. $this->assertEquals(1, count($this->cache->search('%Folder%')));
  292. $this->assertEquals(1, count($this->cache->search('Folder%')));
  293. $this->assertEquals(3, count($this->cache->searchByMime('foo')));
  294. $this->assertEquals(2, count($this->cache->searchByMime('foo/file')));
  295. }
  296. public function testSearchQueryByTag() {
  297. $userId = static::getUniqueID('user');
  298. \OC::$server->getUserManager()->createUser($userId, $userId);
  299. static::loginAsUser($userId);
  300. $user = new \OC\User\User($userId, null, \OC::$server->get(IEventDispatcher::class));
  301. $file1 = 'folder';
  302. $file2 = 'folder/foobar';
  303. $file3 = 'folder/foo';
  304. $file4 = 'folder/foo2';
  305. $file5 = 'folder/foo3';
  306. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
  307. $fileData = [];
  308. $fileData['foobar'] = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  309. $fileData['foo'] = ['size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file'];
  310. $fileData['foo2'] = ['size' => 25, 'mtime' => 28, 'mimetype' => 'foo/file'];
  311. $fileData['foo3'] = ['size' => 88, 'mtime' => 34, 'mimetype' => 'foo/file'];
  312. $id1 = $this->cache->put($file1, $data1);
  313. $id2 = $this->cache->put($file2, $fileData['foobar']);
  314. $id3 = $this->cache->put($file3, $fileData['foo']);
  315. $id4 = $this->cache->put($file4, $fileData['foo2']);
  316. $id5 = $this->cache->put($file5, $fileData['foo3']);
  317. $tagManager = \OC::$server->getTagManager()->load('files', [], false, $userId);
  318. $this->assertTrue($tagManager->tagAs($id1, 'tag1'));
  319. $this->assertTrue($tagManager->tagAs($id1, 'tag2'));
  320. $this->assertTrue($tagManager->tagAs($id2, 'tag2'));
  321. $this->assertTrue($tagManager->tagAs($id3, 'tag1'));
  322. $this->assertTrue($tagManager->tagAs($id4, 'tag2'));
  323. $results = $this->cache->searchQuery(new SearchQuery(
  324. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'tagname', 'tag2'),
  325. 0, 0, [], $user
  326. ));
  327. $this->assertEquals(3, count($results));
  328. usort($results, function ($value1, $value2) {
  329. return $value1['name'] <=> $value2['name'];
  330. });
  331. $this->assertEquals('folder', $results[0]['name']);
  332. $this->assertEquals('foo2', $results[1]['name']);
  333. $this->assertEquals('foobar', $results[2]['name']);
  334. $tagManager->delete('tag1');
  335. $tagManager->delete('tag2');
  336. static::logout();
  337. $user = \OC::$server->getUserManager()->get($userId);
  338. if ($user !== null) {
  339. try {
  340. $user->delete();
  341. } catch (\Exception $e) {
  342. }
  343. }
  344. }
  345. public function testSearchByQuery() {
  346. $file1 = 'folder';
  347. $file2 = 'folder/foobar';
  348. $file3 = 'folder/foo';
  349. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder'];
  350. $fileData = [];
  351. $fileData['foobar'] = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  352. $fileData['foo'] = ['size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file'];
  353. $this->cache->put($file1, $data1);
  354. $this->cache->put($file2, $fileData['foobar']);
  355. $this->cache->put($file3, $fileData['foo']);
  356. /** @var IUser $user */
  357. $user = $this->createMock(IUser::class);
  358. $this->assertCount(1, $this->cache->searchQuery(new SearchQuery(
  359. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'name', 'foo'), 10, 0, [], $user)));
  360. $this->assertCount(2, $this->cache->searchQuery(new SearchQuery(
  361. new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', 'foo%'), 10, 0, [], $user)));
  362. $this->assertCount(2, $this->cache->searchQuery(new SearchQuery(
  363. new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', 'foo/file'), 10, 0, [], $user)));
  364. $this->assertCount(3, $this->cache->searchQuery(new SearchQuery(
  365. new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', 'foo/%'), 10, 0, [], $user)));
  366. $this->assertCount(1, $this->cache->searchQuery(new SearchQuery(
  367. new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN, 'size', 100), 10, 0, [], $user)));
  368. $this->assertCount(2, $this->cache->searchQuery(new SearchQuery(
  369. new SearchComparison(ISearchComparison::COMPARE_GREATER_THAN_EQUAL, 'size', 100), 10, 0, [], $user)));
  370. }
  371. public function movePathProvider() {
  372. return [
  373. ['folder/foo', 'folder/foobar', ['1', '2']],
  374. ['folder/foo', 'foo', ['1', '2']],
  375. ['files/Индустрия_Инженерные системы ЦОД', 'files/Индустрия_Инженерные системы ЦОД1', ['1', '2']],
  376. ];
  377. }
  378. /**
  379. * @dataProvider movePathProvider
  380. */
  381. public function testMove($sourceFolder, $targetFolder, $children) {
  382. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'foo/bar'];
  383. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  384. // create folders
  385. foreach ([$sourceFolder, $targetFolder] as $current) {
  386. while (strpos($current, '/') > 0) {
  387. $current = dirname($current);
  388. $this->cache->put($current, $folderData);
  389. $this->cache2->put($current, $folderData);
  390. }
  391. }
  392. $this->cache->put($sourceFolder, $folderData);
  393. $this->cache2->put($sourceFolder, $folderData);
  394. foreach ($children as $child) {
  395. $this->cache->put($sourceFolder . '/' . $child, $data);
  396. $this->cache2->put($sourceFolder . '/' . $child, $data);
  397. }
  398. $this->cache->move($sourceFolder, $targetFolder);
  399. $this->assertFalse($this->cache->inCache($sourceFolder));
  400. $this->assertTrue($this->cache2->inCache($sourceFolder));
  401. $this->assertTrue($this->cache->inCache($targetFolder));
  402. $this->assertFalse($this->cache2->inCache($targetFolder));
  403. foreach ($children as $child) {
  404. $this->assertFalse($this->cache->inCache($sourceFolder . '/' . $child));
  405. $this->assertTrue($this->cache2->inCache($sourceFolder . '/' . $child));
  406. $this->assertTrue($this->cache->inCache($targetFolder . '/' . $child));
  407. $this->assertFalse($this->cache2->inCache($targetFolder . '/' . $child));
  408. }
  409. }
  410. public function testGetIncomplete() {
  411. $file1 = 'folder1';
  412. $file2 = 'folder2';
  413. $file3 = 'folder3';
  414. $file4 = 'folder4';
  415. $data = ['size' => 10, 'mtime' => 50, 'mimetype' => 'foo/bar'];
  416. $this->cache->put($file1, $data);
  417. $data['size'] = -1;
  418. $this->cache->put($file2, $data);
  419. $this->cache->put($file3, $data);
  420. $data['size'] = 12;
  421. $this->cache->put($file4, $data);
  422. $this->assertEquals($file3, $this->cache->getIncomplete());
  423. }
  424. public function testNonExisting() {
  425. $this->assertFalse($this->cache->get('foo.txt'));
  426. $this->assertFalse($this->cache->get(-1));
  427. $this->assertEquals([], $this->cache->getFolderContents('foo'));
  428. }
  429. public function testGetById() {
  430. $storageId = $this->storage->getId();
  431. $data = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  432. $id = $this->cache->put('foo', $data);
  433. if (strlen($storageId) > 64) {
  434. $storageId = md5($storageId);
  435. }
  436. $this->assertEquals([$storageId, 'foo'], \OC\Files\Cache\Cache::getById($id));
  437. }
  438. public function testStorageMTime() {
  439. $data = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  440. $this->cache->put('foo', $data);
  441. $cachedData = $this->cache->get('foo');
  442. $this->assertEquals($data['mtime'], $cachedData['storage_mtime']); //if no storage_mtime is saved, mtime should be used
  443. $this->cache->put('foo', ['storage_mtime' => 30]); //when setting storage_mtime, mtime is also set
  444. $cachedData = $this->cache->get('foo');
  445. $this->assertEquals(30, $cachedData['storage_mtime']);
  446. $this->assertEquals(30, $cachedData['mtime']);
  447. $this->cache->put('foo', ['mtime' => 25]); //setting mtime does not change storage_mtime
  448. $cachedData = $this->cache->get('foo');
  449. $this->assertEquals(30, $cachedData['storage_mtime']);
  450. $this->assertEquals(25, $cachedData['mtime']);
  451. }
  452. public function testLongId() {
  453. $storage = new LongId([]);
  454. $cache = $storage->getCache();
  455. $storageId = $storage->getId();
  456. $data = ['size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file'];
  457. $id = $cache->put('foo', $data);
  458. $this->assertEquals([md5($storageId), 'foo'], \OC\Files\Cache\Cache::getById($id));
  459. }
  460. /**
  461. * this test show the bug resulting if we have no normalizer installed
  462. */
  463. public function testWithoutNormalizer() {
  464. // folder name "Schön" with U+00F6 (normalized)
  465. $folderWith00F6 = "\x53\x63\x68\xc3\xb6\x6e";
  466. // folder name "Schön" with U+0308 (un-normalized)
  467. $folderWith0308 = "\x53\x63\x68\x6f\xcc\x88\x6e";
  468. /**
  469. * @var \OC\Files\Cache\Cache | \PHPUnit\Framework\MockObject\MockObject $cacheMock
  470. */
  471. $cacheMock = $this->getMockBuilder(Cache::class)
  472. ->setMethods(['normalize'])
  473. ->setConstructorArgs([$this->storage])
  474. ->getMock();
  475. $cacheMock->expects($this->any())
  476. ->method('normalize')
  477. ->willReturnArgument(0);
  478. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  479. // put root folder
  480. $this->assertFalse($cacheMock->get('folder'));
  481. $this->assertGreaterThan(0, $cacheMock->put('folder', $data));
  482. // put un-normalized folder
  483. $this->assertFalse($cacheMock->get('folder/' . $folderWith0308));
  484. $this->assertGreaterThan(0, $cacheMock->put('folder/' . $folderWith0308, $data));
  485. // get un-normalized folder by name
  486. $unNormalizedFolderName = $cacheMock->get('folder/' . $folderWith0308);
  487. // check if database layer normalized the folder name (this should not happen)
  488. $this->assertEquals($folderWith0308, $unNormalizedFolderName['name']);
  489. // put normalized folder
  490. $this->assertFalse($cacheMock->get('folder/' . $folderWith00F6));
  491. $this->assertGreaterThan(0, $cacheMock->put('folder/' . $folderWith00F6, $data));
  492. // this is our bug, we have two different hashes with the same name (Schön)
  493. $this->assertEquals(2, count($cacheMock->getFolderContents('folder')));
  494. }
  495. /**
  496. * this test shows that there is no bug if we use the normalizer
  497. */
  498. public function testWithNormalizer() {
  499. if (!class_exists('Patchwork\PHP\Shim\Normalizer')) {
  500. $this->markTestSkipped('The 3rdparty Normalizer extension is not available.');
  501. return;
  502. }
  503. // folder name "Schön" with U+00F6 (normalized)
  504. $folderWith00F6 = "\x53\x63\x68\xc3\xb6\x6e";
  505. // folder name "Schön" with U+0308 (un-normalized)
  506. $folderWith0308 = "\x53\x63\x68\x6f\xcc\x88\x6e";
  507. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  508. // put root folder
  509. $this->assertFalse($this->cache->get('folder'));
  510. $this->assertGreaterThan(0, $this->cache->put('folder', $data));
  511. // put un-normalized folder
  512. $this->assertFalse($this->cache->get('folder/' . $folderWith0308));
  513. $this->assertGreaterThan(0, $this->cache->put('folder/' . $folderWith0308, $data));
  514. // get un-normalized folder by name
  515. $unNormalizedFolderName = $this->cache->get('folder/' . $folderWith0308);
  516. // check if folder name was normalized
  517. $this->assertEquals($folderWith00F6, $unNormalizedFolderName['name']);
  518. // put normalized folder
  519. $this->assertInstanceOf('\OCP\Files\Cache\ICacheEntry', $this->cache->get('folder/' . $folderWith00F6));
  520. $this->assertGreaterThan(0, $this->cache->put('folder/' . $folderWith00F6, $data));
  521. // at this point we should have only one folder named "Schön"
  522. $this->assertEquals(1, count($this->cache->getFolderContents('folder')));
  523. }
  524. public function bogusPathNamesProvider() {
  525. return [
  526. ['/bogus.txt', 'bogus.txt'],
  527. ['//bogus.txt', 'bogus.txt'],
  528. ['bogus/', 'bogus'],
  529. ['bogus//', 'bogus'],
  530. ];
  531. }
  532. /**
  533. * Test bogus paths with leading or doubled slashes
  534. *
  535. * @dataProvider bogusPathNamesProvider
  536. */
  537. public function testBogusPaths($bogusPath, $fixedBogusPath) {
  538. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  539. // put root folder
  540. $this->assertFalse($this->cache->get(''));
  541. $parentId = $this->cache->put('', $data);
  542. $this->assertGreaterThan(0, $parentId);
  543. $this->assertGreaterThan(0, $this->cache->put($bogusPath, $data));
  544. $newData = $this->cache->get($fixedBogusPath);
  545. $this->assertNotFalse($newData);
  546. $this->assertEquals($fixedBogusPath, $newData['path']);
  547. // parent is the correct one, resolved properly (they used to not be)
  548. $this->assertEquals($parentId, $newData['parent']);
  549. $newDataFromBogus = $this->cache->get($bogusPath);
  550. // same entry
  551. $this->assertEquals($newData, $newDataFromBogus);
  552. }
  553. public function testNoReuseOfFileId() {
  554. $data1 = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'];
  555. $this->cache->put('somefile.txt', $data1);
  556. $info = $this->cache->get('somefile.txt');
  557. $fileId = $info['fileid'];
  558. $this->cache->remove('somefile.txt');
  559. $data2 = ['size' => 200, 'mtime' => 100, 'mimetype' => 'text/plain'];
  560. $this->cache->put('anotherfile.txt', $data2);
  561. $info2 = $this->cache->get('anotherfile.txt');
  562. $fileId2 = $info2['fileid'];
  563. $this->assertNotEquals($fileId, $fileId2);
  564. }
  565. public function escapingProvider() {
  566. return [
  567. ['foo'],
  568. ['o%'],
  569. ['oth_r'],
  570. ];
  571. }
  572. /**
  573. * @param string $name
  574. * @dataProvider escapingProvider
  575. */
  576. public function testEscaping($name) {
  577. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'];
  578. $this->cache->put($name, $data);
  579. $this->assertTrue($this->cache->inCache($name));
  580. $retrievedData = $this->cache->get($name);
  581. foreach ($data as $key => $value) {
  582. $this->assertEquals($value, $retrievedData[$key]);
  583. }
  584. $this->cache->move($name, $name . 'asd');
  585. $this->assertFalse($this->cache->inCache($name));
  586. $this->assertTrue($this->cache->inCache($name . 'asd'));
  587. $this->cache->remove($name . 'asd');
  588. $this->assertFalse($this->cache->inCache($name . 'asd'));
  589. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  590. $this->cache->put($name, $folderData);
  591. $this->cache->put('other', $folderData);
  592. $childs = ['asd', 'bar', 'foo', 'sub/folder'];
  593. $this->cache->put($name . '/sub', $folderData);
  594. $this->cache->put('other/sub', $folderData);
  595. foreach ($childs as $child) {
  596. $this->cache->put($name . '/' . $child, $data);
  597. $this->cache->put('other/' . $child, $data);
  598. $this->assertTrue($this->cache->inCache($name . '/' . $child));
  599. }
  600. $this->cache->move($name, $name . 'asd');
  601. foreach ($childs as $child) {
  602. $this->assertTrue($this->cache->inCache($name . 'asd/' . $child));
  603. $this->assertTrue($this->cache->inCache('other/' . $child));
  604. }
  605. foreach ($childs as $child) {
  606. $this->cache->remove($name . 'asd/' . $child);
  607. $this->assertFalse($this->cache->inCache($name . 'asd/' . $child));
  608. $this->assertTrue($this->cache->inCache('other/' . $child));
  609. }
  610. }
  611. public function testExtended() {
  612. $folderData = ['size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory'];
  613. $this->cache->put("", $folderData);
  614. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'creation_time' => 20];
  615. $id1 = $this->cache->put("foo1", $data);
  616. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'upload_time' => 30];
  617. $this->cache->put("foo2", $data);
  618. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain', 'metadata_etag' => 'foo'];
  619. $this->cache->put("foo3", $data);
  620. $data = ['size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'];
  621. $id4 = $this->cache->put("foo4", $data);
  622. $entry = $this->cache->get($id1);
  623. $this->assertEquals(20, $entry->getCreationTime());
  624. $this->assertEquals(0, $entry->getUploadTime());
  625. $this->assertEquals(null, $entry->getMetadataEtag());
  626. $entries = $this->cache->getFolderContents("");
  627. $this->assertCount(4, $entries);
  628. $this->assertEquals("foo1", $entries[0]->getName());
  629. $this->assertEquals("foo2", $entries[1]->getName());
  630. $this->assertEquals("foo3", $entries[2]->getName());
  631. $this->assertEquals("foo4", $entries[3]->getName());
  632. $this->assertEquals(20, $entries[0]->getCreationTime());
  633. $this->assertEquals(0, $entries[0]->getUploadTime());
  634. $this->assertEquals(null, $entries[0]->getMetadataEtag());
  635. $this->assertEquals(0, $entries[1]->getCreationTime());
  636. $this->assertEquals(30, $entries[1]->getUploadTime());
  637. $this->assertEquals(null, $entries[1]->getMetadataEtag());
  638. $this->assertEquals(0, $entries[2]->getCreationTime());
  639. $this->assertEquals(0, $entries[2]->getUploadTime());
  640. $this->assertEquals('foo', $entries[2]->getMetadataEtag());
  641. $this->assertEquals(0, $entries[3]->getCreationTime());
  642. $this->assertEquals(0, $entries[3]->getUploadTime());
  643. $this->assertEquals(null, $entries[3]->getMetadataEtag());
  644. $this->cache->update($id1, ['upload_time' => 25]);
  645. $entry = $this->cache->get($id1);
  646. $this->assertEquals(20, $entry->getCreationTime());
  647. $this->assertEquals(25, $entry->getUploadTime());
  648. $this->assertEquals(null, $entry->getMetadataEtag());
  649. $this->cache->put("sub", $folderData);
  650. $this->cache->move("foo1", "sub/foo1");
  651. $entries = $this->cache->getFolderContents("sub");
  652. $this->assertCount(1, $entries);
  653. $this->assertEquals(20, $entries[0]->getCreationTime());
  654. $this->assertEquals(25, $entries[0]->getUploadTime());
  655. $this->assertEquals(null, $entries[0]->getMetadataEtag());
  656. $this->cache->update($id4, ['upload_time' => 25]);
  657. $entry = $this->cache->get($id4);
  658. $this->assertEquals(0, $entry->getCreationTime());
  659. $this->assertEquals(25, $entry->getUploadTime());
  660. $this->assertEquals(null, $entry->getMetadataEtag());
  661. $this->cache->remove("sub");
  662. }
  663. protected function tearDown(): void {
  664. if ($this->cache) {
  665. $this->cache->clear();
  666. }
  667. parent::tearDown();
  668. }
  669. protected function setUp(): void {
  670. parent::setUp();
  671. $this->storage = new \OC\Files\Storage\Temporary([]);
  672. $this->storage2 = new \OC\Files\Storage\Temporary([]);
  673. $this->cache = new \OC\Files\Cache\Cache($this->storage);
  674. $this->cache2 = new \OC\Files\Cache\Cache($this->storage2);
  675. }
  676. }