cache.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 PHPUnit_Framework_MockObject_MockObject;
  10. class LongId extends \OC\Files\Storage\Temporary {
  11. public function getId() {
  12. return 'long:' . str_repeat('foo', 50) . parent::getId();
  13. }
  14. }
  15. class Cache extends \PHPUnit_Framework_TestCase {
  16. /**
  17. * @var \OC\Files\Storage\Temporary $storage ;
  18. */
  19. private $storage;
  20. /**
  21. * @var \OC\Files\Storage\Temporary $storage2 ;
  22. */
  23. private $storage2;
  24. /**
  25. * @var \OC\Files\Cache\Cache $cache
  26. */
  27. private $cache;
  28. /**
  29. * @var \OC\Files\Cache\Cache $cache2
  30. */
  31. private $cache2;
  32. public function testSimple() {
  33. $file1 = 'foo';
  34. $file2 = 'foo/bar';
  35. $data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder');
  36. $data2 = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file');
  37. $this->assertFalse($this->cache->inCache($file1));
  38. $this->assertEquals($this->cache->get($file1), null);
  39. $id1 = $this->cache->put($file1, $data1);
  40. $this->assertTrue($this->cache->inCache($file1));
  41. $cacheData1 = $this->cache->get($file1);
  42. foreach ($data1 as $key => $value) {
  43. $this->assertEquals($value, $cacheData1[$key]);
  44. }
  45. $this->assertEquals($cacheData1['mimepart'], 'foo');
  46. $this->assertEquals($cacheData1['fileid'], $id1);
  47. $this->assertEquals($id1, $this->cache->getId($file1));
  48. $this->assertFalse($this->cache->inCache($file2));
  49. $id2 = $this->cache->put($file2, $data2);
  50. $this->assertTrue($this->cache->inCache($file2));
  51. $cacheData2 = $this->cache->get($file2);
  52. foreach ($data2 as $key => $value) {
  53. $this->assertEquals($value, $cacheData2[$key]);
  54. }
  55. $this->assertEquals($cacheData1['fileid'], $cacheData2['parent']);
  56. $this->assertEquals($cacheData2['fileid'], $id2);
  57. $this->assertEquals($id2, $this->cache->getId($file2));
  58. $this->assertEquals($id1, $this->cache->getParentId($file2));
  59. $newSize = 1050;
  60. $newId2 = $this->cache->put($file2, array('size' => $newSize));
  61. $cacheData2 = $this->cache->get($file2);
  62. $this->assertEquals($newId2, $id2);
  63. $this->assertEquals($cacheData2['size'], $newSize);
  64. $this->assertEquals($cacheData1, $this->cache->get($file1));
  65. $this->cache->remove($file2);
  66. $this->assertFalse($this->cache->inCache($file2));
  67. $this->assertEquals($this->cache->get($file2), null);
  68. $this->assertTrue($this->cache->inCache($file1));
  69. $this->assertEquals($cacheData1, $this->cache->get($id1));
  70. }
  71. public function testPartial() {
  72. $file1 = 'foo';
  73. $this->cache->put($file1, array('size' => 10));
  74. $this->assertEquals(array('size' => 10), $this->cache->get($file1));
  75. $this->cache->put($file1, array('mtime' => 15));
  76. $this->assertEquals(array('size' => 10, 'mtime' => 15), $this->cache->get($file1));
  77. $this->cache->put($file1, array('size' => 12));
  78. $this->assertEquals(array('size' => 12, 'mtime' => 15), $this->cache->get($file1));
  79. }
  80. public function testFolder() {
  81. $file1 = 'folder';
  82. $file2 = 'folder/bar';
  83. $file3 = 'folder/foo';
  84. $data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
  85. $fileData = array();
  86. $fileData['bar'] = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file');
  87. $fileData['foo'] = array('size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file');
  88. $this->cache->put($file1, $data1);
  89. $this->cache->put($file2, $fileData['bar']);
  90. $this->cache->put($file3, $fileData['foo']);
  91. $content = $this->cache->getFolderContents($file1);
  92. $this->assertEquals(count($content), 2);
  93. foreach ($content as $cachedData) {
  94. $data = $fileData[$cachedData['name']];
  95. foreach ($data as $name => $value) {
  96. $this->assertEquals($value, $cachedData[$name]);
  97. }
  98. }
  99. $file4 = 'folder/unkownSize';
  100. $fileData['unkownSize'] = array('size' => -1, 'mtime' => 25, 'mimetype' => 'foo/file');
  101. $this->cache->put($file4, $fileData['unkownSize']);
  102. $this->assertEquals(-1, $this->cache->calculateFolderSize($file1));
  103. $fileData['unkownSize'] = array('size' => 5, 'mtime' => 25, 'mimetype' => 'foo/file');
  104. $this->cache->put($file4, $fileData['unkownSize']);
  105. $this->assertEquals(1025, $this->cache->calculateFolderSize($file1));
  106. $this->cache->remove($file2);
  107. $this->cache->remove($file3);
  108. $this->cache->remove($file4);
  109. $this->assertEquals(0, $this->cache->calculateFolderSize($file1));
  110. $this->cache->remove('folder');
  111. $this->assertFalse($this->cache->inCache('folder/foo'));
  112. $this->assertFalse($this->cache->inCache('folder/bar'));
  113. }
  114. public function testRootFolderSizeForNonHomeStorage() {
  115. $dir1 = 'knownsize';
  116. $dir2 = 'unknownsize';
  117. $fileData = array();
  118. $fileData[''] = array('size' => -1, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory');
  119. $fileData[$dir1] = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'httpd/unix-directory');
  120. $fileData[$dir2] = array('size' => -1, 'mtime' => 25, 'mimetype' => 'httpd/unix-directory');
  121. $this->cache->put('', $fileData['']);
  122. $this->cache->put($dir1, $fileData[$dir1]);
  123. $this->cache->put($dir2, $fileData[$dir2]);
  124. $this->assertTrue($this->cache->inCache($dir1));
  125. $this->assertTrue($this->cache->inCache($dir2));
  126. // check that root size ignored the unknown sizes
  127. $this->assertEquals(-1, $this->cache->calculateFolderSize(''));
  128. // clean up
  129. $this->cache->remove('');
  130. $this->cache->remove($dir1);
  131. $this->cache->remove($dir2);
  132. $this->assertFalse($this->cache->inCache($dir1));
  133. $this->assertFalse($this->cache->inCache($dir2));
  134. }
  135. function testStatus() {
  136. $this->assertEquals(\OC\Files\Cache\Cache::NOT_FOUND, $this->cache->getStatus('foo'));
  137. $this->cache->put('foo', array('size' => -1));
  138. $this->assertEquals(\OC\Files\Cache\Cache::PARTIAL, $this->cache->getStatus('foo'));
  139. $this->cache->put('foo', array('size' => -1, 'mtime' => 20, 'mimetype' => 'foo/file'));
  140. $this->assertEquals(\OC\Files\Cache\Cache::SHALLOW, $this->cache->getStatus('foo'));
  141. $this->cache->put('foo', array('size' => 10));
  142. $this->assertEquals(\OC\Files\Cache\Cache::COMPLETE, $this->cache->getStatus('foo'));
  143. }
  144. function testSearch() {
  145. $file1 = 'folder';
  146. $file2 = 'folder/foobar';
  147. $file3 = 'folder/foo';
  148. $data1 = array('size' => 100, 'mtime' => 50, 'mimetype' => 'foo/folder');
  149. $fileData = array();
  150. $fileData['foobar'] = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file');
  151. $fileData['foo'] = array('size' => 20, 'mtime' => 25, 'mimetype' => 'foo/file');
  152. $this->cache->put($file1, $data1);
  153. $this->cache->put($file2, $fileData['foobar']);
  154. $this->cache->put($file3, $fileData['foo']);
  155. $this->assertEquals(2, count($this->cache->search('%foo%')));
  156. $this->assertEquals(1, count($this->cache->search('foo')));
  157. $this->assertEquals(1, count($this->cache->search('%folder%')));
  158. $this->assertEquals(1, count($this->cache->search('folder%')));
  159. $this->assertEquals(3, count($this->cache->search('%')));
  160. $this->assertEquals(3, count($this->cache->searchByMime('foo')));
  161. $this->assertEquals(2, count($this->cache->searchByMime('foo/file')));
  162. }
  163. function testMove() {
  164. $file1 = 'folder';
  165. $file2 = 'folder/bar';
  166. $file3 = 'folder/foo';
  167. $file4 = 'folder/foo/1';
  168. $file5 = 'folder/foo/2';
  169. $data = array('size' => 100, 'mtime' => 50, 'mimetype' => 'foo/bar');
  170. $folderData = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
  171. $this->cache->put($file1, $folderData);
  172. $this->cache->put($file2, $folderData);
  173. $this->cache->put($file3, $folderData);
  174. $this->cache->put($file4, $data);
  175. $this->cache->put($file5, $data);
  176. /* simulate a second user with a different storage id but the same folder structure */
  177. $this->cache2->put($file1, $folderData);
  178. $this->cache2->put($file2, $folderData);
  179. $this->cache2->put($file3, $folderData);
  180. $this->cache2->put($file4, $data);
  181. $this->cache2->put($file5, $data);
  182. $this->cache->move('folder/foo', 'folder/foobar');
  183. $this->assertFalse($this->cache->inCache('folder/foo'));
  184. $this->assertFalse($this->cache->inCache('folder/foo/1'));
  185. $this->assertFalse($this->cache->inCache('folder/foo/2'));
  186. $this->assertTrue($this->cache->inCache('folder/bar'));
  187. $this->assertTrue($this->cache->inCache('folder/foobar'));
  188. $this->assertTrue($this->cache->inCache('folder/foobar/1'));
  189. $this->assertTrue($this->cache->inCache('folder/foobar/2'));
  190. /* the folder structure of the second user must not change! */
  191. $this->assertTrue($this->cache2->inCache('folder/bar'));
  192. $this->assertTrue($this->cache2->inCache('folder/foo'));
  193. $this->assertTrue($this->cache2->inCache('folder/foo/1'));
  194. $this->assertTrue($this->cache2->inCache('folder/foo/2'));
  195. $this->assertFalse($this->cache2->inCache('folder/foobar'));
  196. $this->assertFalse($this->cache2->inCache('folder/foobar/1'));
  197. $this->assertFalse($this->cache2->inCache('folder/foobar/2'));
  198. }
  199. function testGetIncomplete() {
  200. $file1 = 'folder1';
  201. $file2 = 'folder2';
  202. $file3 = 'folder3';
  203. $file4 = 'folder4';
  204. $data = array('size' => 10, 'mtime' => 50, 'mimetype' => 'foo/bar');
  205. $this->cache->put($file1, $data);
  206. $data['size'] = -1;
  207. $this->cache->put($file2, $data);
  208. $this->cache->put($file3, $data);
  209. $data['size'] = 12;
  210. $this->cache->put($file4, $data);
  211. $this->assertEquals($file3, $this->cache->getIncomplete());
  212. }
  213. function testNonExisting() {
  214. $this->assertFalse($this->cache->get('foo.txt'));
  215. $this->assertEquals(array(), $this->cache->getFolderContents('foo'));
  216. }
  217. function testGetById() {
  218. $storageId = $this->storage->getId();
  219. $data = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file');
  220. $id = $this->cache->put('foo', $data);
  221. $this->assertEquals(array($storageId, 'foo'), \OC\Files\Cache\Cache::getById($id));
  222. }
  223. function testStorageMTime() {
  224. $data = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file');
  225. $this->cache->put('foo', $data);
  226. $cachedData = $this->cache->get('foo');
  227. $this->assertEquals($data['mtime'], $cachedData['storage_mtime']); //if no storage_mtime is saved, mtime should be used
  228. $this->cache->put('foo', array('storage_mtime' => 30)); //when setting storage_mtime, mtime is also set
  229. $cachedData = $this->cache->get('foo');
  230. $this->assertEquals(30, $cachedData['storage_mtime']);
  231. $this->assertEquals(30, $cachedData['mtime']);
  232. $this->cache->put('foo', array('mtime' => 25)); //setting mtime does not change storage_mtime
  233. $cachedData = $this->cache->get('foo');
  234. $this->assertEquals(30, $cachedData['storage_mtime']);
  235. $this->assertEquals(25, $cachedData['mtime']);
  236. }
  237. function testLongId() {
  238. $storage = new LongId(array());
  239. $cache = $storage->getCache();
  240. $storageId = $storage->getId();
  241. $data = array('size' => 1000, 'mtime' => 20, 'mimetype' => 'foo/file');
  242. $id = $cache->put('foo', $data);
  243. $this->assertEquals(array(md5($storageId), 'foo'), \OC\Files\Cache\Cache::getById($id));
  244. }
  245. /**
  246. * @brief this test show the bug resulting if we have no normalizer installed
  247. */
  248. public function testWithoutNormalizer() {
  249. // folder name "Schön" with U+00F6 (normalized)
  250. $folderWith00F6 = "\x53\x63\x68\xc3\xb6\x6e";
  251. // folder name "Schön" with U+0308 (un-normalized)
  252. $folderWith0308 = "\x53\x63\x68\x6f\xcc\x88\x6e";
  253. /**
  254. * @var \OC\Files\Cache\Cache | PHPUnit_Framework_MockObject_MockObject $cacheMock
  255. */
  256. $cacheMock = $this->getMock('\OC\Files\Cache\Cache', array('normalize'), array($this->storage), '', true);
  257. $cacheMock->expects($this->any())
  258. ->method('normalize')
  259. ->will($this->returnArgument(0));
  260. $data = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
  261. // put root folder
  262. $this->assertFalse($cacheMock->get('folder'));
  263. $this->assertGreaterThan(0, $cacheMock->put('folder', $data));
  264. // put un-normalized folder
  265. $this->assertFalse($cacheMock->get('folder/' . $folderWith0308));
  266. $this->assertGreaterThan(0, $cacheMock->put('folder/' . $folderWith0308, $data));
  267. // get un-normalized folder by name
  268. $unNormalizedFolderName = $cacheMock->get('folder/' . $folderWith0308);
  269. // check if database layer normalized the folder name (this should not happen)
  270. $this->assertEquals($folderWith0308, $unNormalizedFolderName['name']);
  271. // put normalized folder
  272. $this->assertFalse($cacheMock->get('folder/' . $folderWith00F6));
  273. $this->assertGreaterThan(0, $cacheMock->put('folder/' . $folderWith00F6, $data));
  274. // this is our bug, we have two different hashes with the same name (Schön)
  275. $this->assertEquals(2, count($cacheMock->getFolderContents('folder')));
  276. }
  277. /**
  278. * @brief this test shows that there is no bug if we use the normalizer
  279. */
  280. public function testWithNormalizer() {
  281. if (!class_exists('Patchwork\PHP\Shim\Normalizer')) {
  282. $this->markTestSkipped('The 3rdparty Normalizer extension is not available.');
  283. return;
  284. }
  285. // folder name "Schön" with U+00F6 (normalized)
  286. $folderWith00F6 = "\x53\x63\x68\xc3\xb6\x6e";
  287. // folder name "Schön" with U+0308 (un-normalized)
  288. $folderWith0308 = "\x53\x63\x68\x6f\xcc\x88\x6e";
  289. $data = array('size' => 100, 'mtime' => 50, 'mimetype' => 'httpd/unix-directory');
  290. // put root folder
  291. $this->assertFalse($this->cache->get('folder'));
  292. $this->assertGreaterThan(0, $this->cache->put('folder', $data));
  293. // put un-normalized folder
  294. $this->assertFalse($this->cache->get('folder/' . $folderWith0308));
  295. $this->assertGreaterThan(0, $this->cache->put('folder/' . $folderWith0308, $data));
  296. // get un-normalized folder by name
  297. $unNormalizedFolderName = $this->cache->get('folder/' . $folderWith0308);
  298. // check if folder name was normalized
  299. $this->assertEquals($folderWith00F6, $unNormalizedFolderName['name']);
  300. // put normalized folder
  301. $this->assertTrue(is_array($this->cache->get('folder/' . $folderWith00F6)));
  302. $this->assertGreaterThan(0, $this->cache->put('folder/' . $folderWith00F6, $data));
  303. // at this point we should have only one folder named "Schön"
  304. $this->assertEquals(1, count($this->cache->getFolderContents('folder')));
  305. }
  306. public function tearDown() {
  307. if ($this->cache) {
  308. $this->cache->clear();
  309. }
  310. }
  311. public function setUp() {
  312. $this->storage = new \OC\Files\Storage\Temporary(array());
  313. $this->storage2 = new \OC\Files\Storage\Temporary(array());
  314. $this->cache = new \OC\Files\Cache\Cache($this->storage);
  315. $this->cache2 = new \OC\Files\Cache\Cache($this->storage2);
  316. }
  317. }