ScannerTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 OC;
  10. use OC\Files\Cache\Cache;
  11. use OC\Files\Cache\CacheEntry;
  12. use OC\Files\Cache\Scanner;
  13. use OC\Files\Storage\Storage;
  14. use OC\Files\Storage\Temporary;
  15. use OCP\Files\Cache\IScanner;
  16. use Test\TestCase;
  17. /**
  18. * Class ScannerTest
  19. *
  20. * @group DB
  21. *
  22. * @package Test\Files\Cache
  23. */
  24. class ScannerTest extends TestCase {
  25. private Storage $storage;
  26. private Scanner $scanner;
  27. private Cache $cache;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->storage = new Temporary([]);
  31. $this->scanner = new Scanner($this->storage);
  32. $this->cache = new Cache($this->storage);
  33. }
  34. protected function tearDown(): void {
  35. $this->cache->clear();
  36. parent::tearDown();
  37. }
  38. public function testFile() {
  39. $data = "dummy file data\n";
  40. $this->storage->file_put_contents('foo.txt', $data);
  41. $this->scanner->scanFile('foo.txt');
  42. $this->assertEquals($this->cache->inCache('foo.txt'), true);
  43. $cachedData = $this->cache->get('foo.txt');
  44. $this->assertEquals($cachedData['size'], strlen($data));
  45. $this->assertEquals($cachedData['mimetype'], 'text/plain');
  46. $this->assertNotEquals($cachedData['parent'], -1); //parent folders should be scanned automatically
  47. $data = file_get_contents(OC::$SERVERROOT . '/core/img/logo/logo.png');
  48. $this->storage->file_put_contents('foo.png', $data);
  49. $this->scanner->scanFile('foo.png');
  50. $this->assertEquals($this->cache->inCache('foo.png'), true);
  51. $cachedData = $this->cache->get('foo.png');
  52. $this->assertEquals($cachedData['size'], strlen($data));
  53. $this->assertEquals($cachedData['mimetype'], 'image/png');
  54. }
  55. public function testFile4Byte() {
  56. $data = "dummy file data\n";
  57. $this->storage->file_put_contents('foo🙈.txt', $data);
  58. if (OC::$server->getDatabaseConnection()->supports4ByteText()) {
  59. $this->assertNotNull($this->scanner->scanFile('foo🙈.txt'));
  60. $this->assertTrue($this->cache->inCache('foo🙈.txt'), true);
  61. $cachedData = $this->cache->get('foo🙈.txt');
  62. $this->assertEquals(strlen($data), $cachedData['size']);
  63. $this->assertEquals('text/plain', $cachedData['mimetype']);
  64. $this->assertNotEquals(-1, $cachedData['parent']); //parent folders should be scanned automatically
  65. } else {
  66. $this->assertNull($this->scanner->scanFile('foo🙈.txt'));
  67. $this->assertFalse($this->cache->inCache('foo🙈.txt'), true);
  68. }
  69. }
  70. public function testFileInvalidChars() {
  71. $data = "dummy file data\n";
  72. $this->storage->file_put_contents("foo\nbar.txt", $data);
  73. $this->assertNull($this->scanner->scanFile("foo\nbar.txt"));
  74. $this->assertFalse($this->cache->inCache("foo\nbar.txt"), true);
  75. }
  76. private function fillTestFolders() {
  77. $textData = "dummy file data\n";
  78. $imgData = file_get_contents(OC::$SERVERROOT . '/core/img/logo/logo.png');
  79. $this->storage->mkdir('folder');
  80. $this->storage->file_put_contents('foo.txt', $textData);
  81. $this->storage->file_put_contents('foo.png', $imgData);
  82. $this->storage->file_put_contents('folder/bar.txt', $textData);
  83. }
  84. public function testFolder() {
  85. $this->fillTestFolders();
  86. $this->scanner->scan('');
  87. $this->assertEquals($this->cache->inCache(''), true);
  88. $this->assertEquals($this->cache->inCache('foo.txt'), true);
  89. $this->assertEquals($this->cache->inCache('foo.png'), true);
  90. $this->assertEquals($this->cache->inCache('folder'), true);
  91. $this->assertEquals($this->cache->inCache('folder/bar.txt'), true);
  92. $cachedDataText = $this->cache->get('foo.txt');
  93. $cachedDataText2 = $this->cache->get('foo.txt');
  94. $cachedDataImage = $this->cache->get('foo.png');
  95. $cachedDataFolder = $this->cache->get('');
  96. $cachedDataFolder2 = $this->cache->get('folder');
  97. $this->assertEquals($cachedDataImage['parent'], $cachedDataText['parent']);
  98. $this->assertEquals($cachedDataFolder['fileid'], $cachedDataImage['parent']);
  99. $this->assertEquals($cachedDataFolder['size'], $cachedDataImage['size'] + $cachedDataText['size'] + $cachedDataText2['size']);
  100. $this->assertEquals($cachedDataFolder2['size'], $cachedDataText2['size']);
  101. }
  102. public function testShallow() {
  103. $this->fillTestFolders();
  104. $this->scanner->scan('', IScanner::SCAN_SHALLOW);
  105. $this->assertEquals($this->cache->inCache(''), true);
  106. $this->assertEquals($this->cache->inCache('foo.txt'), true);
  107. $this->assertEquals($this->cache->inCache('foo.png'), true);
  108. $this->assertEquals($this->cache->inCache('folder'), true);
  109. $this->assertEquals($this->cache->inCache('folder/bar.txt'), false);
  110. $cachedDataFolder = $this->cache->get('');
  111. $cachedDataFolder2 = $this->cache->get('folder');
  112. $this->assertEquals(-1, $cachedDataFolder['size']);
  113. $this->assertEquals(-1, $cachedDataFolder2['size']);
  114. $this->scanner->scan('folder', IScanner::SCAN_SHALLOW);
  115. $cachedDataFolder2 = $this->cache->get('folder');
  116. $this->assertNotEquals($cachedDataFolder2['size'], -1);
  117. $this->cache->correctFolderSize('folder');
  118. $cachedDataFolder = $this->cache->get('');
  119. $this->assertNotEquals($cachedDataFolder['size'], -1);
  120. }
  121. public function testBackgroundScan() {
  122. $this->fillTestFolders();
  123. $this->storage->mkdir('folder2');
  124. $this->storage->file_put_contents('folder2/bar.txt', 'foobar');
  125. $this->scanner->scan('', IScanner::SCAN_SHALLOW);
  126. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  127. $this->assertFalse($this->cache->inCache('folder/2bar.txt'));
  128. $cachedData = $this->cache->get('');
  129. $this->assertEquals(-1, $cachedData['size']);
  130. $this->scanner->backgroundScan();
  131. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  132. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  133. $cachedData = $this->cache->get('');
  134. $this->assertnotEquals(-1, $cachedData['size']);
  135. $this->assertFalse($this->cache->getIncomplete());
  136. }
  137. public function testBackgroundScanOnlyRecurseIncomplete() {
  138. $this->fillTestFolders();
  139. $this->storage->mkdir('folder2');
  140. $this->storage->file_put_contents('folder2/bar.txt', 'foobar');
  141. $this->scanner->scan('', IScanner::SCAN_SHALLOW);
  142. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  143. $this->assertFalse($this->cache->inCache('folder/2bar.txt'));
  144. $this->assertFalse($this->cache->inCache('folder2/bar.txt'));
  145. $this->cache->put('folder2', ['size' => 1]); // mark as complete
  146. $cachedData = $this->cache->get('');
  147. $this->assertEquals(-1, $cachedData['size']);
  148. $this->scanner->scan('', IScanner::SCAN_RECURSIVE_INCOMPLETE, IScanner::REUSE_ETAG | IScanner::REUSE_SIZE);
  149. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  150. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  151. $this->assertFalse($this->cache->inCache('folder2/bar.txt'));
  152. $cachedData = $this->cache->get('');
  153. $this->assertNotEquals(-1, $cachedData['size']);
  154. $this->assertFalse($this->cache->getIncomplete());
  155. }
  156. public function testBackgroundScanNestedIncompleteFolders() {
  157. $this->storage->mkdir('folder');
  158. $this->scanner->backgroundScan();
  159. $this->storage->mkdir('folder/subfolder1');
  160. $this->storage->mkdir('folder/subfolder2');
  161. $this->storage->mkdir('folder/subfolder1/subfolder3');
  162. $this->cache->put('folder', ['size' => -1]);
  163. $this->cache->put('folder/subfolder1', ['size' => -1]);
  164. // do a scan to get the folders into the cache.
  165. $this->scanner->backgroundScan();
  166. $this->assertTrue($this->cache->inCache('folder/subfolder1/subfolder3'));
  167. $this->storage->file_put_contents('folder/subfolder1/bar1.txt', 'foobar');
  168. $this->storage->file_put_contents('folder/subfolder1/subfolder3/bar3.txt', 'foobar');
  169. $this->storage->file_put_contents('folder/subfolder2/bar2.txt', 'foobar');
  170. //mark folders as incomplete.
  171. $this->cache->put('folder/subfolder1', ['size' => -1]);
  172. $this->cache->put('folder/subfolder2', ['size' => -1]);
  173. $this->cache->put('folder/subfolder1/subfolder3', ['size' => -1]);
  174. $this->scanner->backgroundScan();
  175. $this->assertTrue($this->cache->inCache('folder/subfolder1/bar1.txt'));
  176. $this->assertTrue($this->cache->inCache('folder/subfolder2/bar2.txt'));
  177. $this->assertTrue($this->cache->inCache('folder/subfolder1/subfolder3/bar3.txt'));
  178. //check if folder sizes are correct.
  179. $this->assertEquals(18, $this->cache->get('folder')['size']);
  180. $this->assertEquals(12, $this->cache->get('folder/subfolder1')['size']);
  181. $this->assertEquals(6, $this->cache->get('folder/subfolder1/subfolder3')['size']);
  182. $this->assertEquals(6, $this->cache->get('folder/subfolder2')['size']);
  183. }
  184. public function testReuseExisting() {
  185. $this->fillTestFolders();
  186. $this->scanner->scan('');
  187. $oldData = $this->cache->get('');
  188. $this->storage->unlink('folder/bar.txt');
  189. $this->cache->put('folder', ['mtime' => $this->storage->filemtime('folder'), 'storage_mtime' => $this->storage->filemtime('folder')]);
  190. $this->scanner->scan('', IScanner::SCAN_SHALLOW, IScanner::REUSE_SIZE);
  191. $newData = $this->cache->get('');
  192. $this->assertIsString($oldData['etag']);
  193. $this->assertIsString($newData['etag']);
  194. $this->assertNotSame($oldData['etag'], $newData['etag']);
  195. $this->assertEquals($oldData['size'], $newData['size']);
  196. $oldData = $newData;
  197. $this->scanner->scan('', IScanner::SCAN_SHALLOW, IScanner::REUSE_ETAG);
  198. $newData = $this->cache->get('');
  199. $this->assertSame($oldData['etag'], $newData['etag']);
  200. $this->assertEquals(-1, $newData['size']);
  201. $this->scanner->scan('', IScanner::SCAN_RECURSIVE);
  202. $oldData = $this->cache->get('');
  203. $this->assertNotEquals(-1, $oldData['size']);
  204. $this->scanner->scanFile('', IScanner::REUSE_ETAG + IScanner::REUSE_SIZE);
  205. $newData = $this->cache->get('');
  206. $this->assertSame($oldData['etag'], $newData['etag']);
  207. $this->assertEquals($oldData['size'], $newData['size']);
  208. $this->scanner->scan('', IScanner::SCAN_RECURSIVE, IScanner::REUSE_ETAG + IScanner::REUSE_SIZE);
  209. $newData = $this->cache->get('');
  210. $this->assertSame($oldData['etag'], $newData['etag']);
  211. $this->assertEquals($oldData['size'], $newData['size']);
  212. $this->scanner->scan('', IScanner::SCAN_SHALLOW, IScanner::REUSE_ETAG + IScanner::REUSE_SIZE);
  213. $newData = $this->cache->get('');
  214. $this->assertSame($oldData['etag'], $newData['etag']);
  215. $this->assertEquals($oldData['size'], $newData['size']);
  216. }
  217. public function testRemovedFile() {
  218. $this->fillTestFolders();
  219. $this->scanner->scan('');
  220. $this->assertTrue($this->cache->inCache('foo.txt'));
  221. $this->storage->unlink('foo.txt');
  222. $this->scanner->scan('', IScanner::SCAN_SHALLOW);
  223. $this->assertFalse($this->cache->inCache('foo.txt'));
  224. }
  225. public function testRemovedFolder() {
  226. $this->fillTestFolders();
  227. $this->scanner->scan('');
  228. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  229. $this->storage->rmdir('/folder');
  230. $this->scanner->scan('', IScanner::SCAN_SHALLOW);
  231. $this->assertFalse($this->cache->inCache('folder'));
  232. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  233. }
  234. public function testScanRemovedFile() {
  235. $this->fillTestFolders();
  236. $this->scanner->scan('');
  237. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  238. $this->storage->unlink('folder/bar.txt');
  239. $this->scanner->scanFile('folder/bar.txt');
  240. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  241. }
  242. public function testETagRecreation() {
  243. $this->fillTestFolders();
  244. $this->scanner->scan('folder/bar.txt');
  245. // manipulate etag to simulate an empty etag
  246. $this->scanner->scan('', IScanner::SCAN_SHALLOW, IScanner::REUSE_ETAG);
  247. /** @var CacheEntry $data0 */
  248. $data0 = $this->cache->get('folder/bar.txt');
  249. $this->assertIsString($data0['etag']);
  250. $data1 = $this->cache->get('folder');
  251. $this->assertIsString($data1['etag']);
  252. $data2 = $this->cache->get('');
  253. $this->assertIsString($data2['etag']);
  254. $data0['etag'] = '';
  255. $this->cache->put('folder/bar.txt', $data0->getData());
  256. // rescan
  257. $this->scanner->scan('folder/bar.txt', IScanner::SCAN_SHALLOW, IScanner::REUSE_ETAG);
  258. // verify cache content
  259. $newData0 = $this->cache->get('folder/bar.txt');
  260. $this->assertIsString($newData0['etag']);
  261. $this->assertNotEmpty($newData0['etag']);
  262. }
  263. public function testRepairParent() {
  264. $this->fillTestFolders();
  265. $this->scanner->scan('');
  266. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  267. $oldFolderId = $this->cache->getId('folder');
  268. // delete the folder without removing the children
  269. $query = OC::$server->getDatabaseConnection()->getQueryBuilder();
  270. $query->delete('filecache')
  271. ->where($query->expr()->eq('fileid', $query->createNamedParameter($oldFolderId)));
  272. $query->execute();
  273. $cachedData = $this->cache->get('folder/bar.txt');
  274. $this->assertEquals($oldFolderId, $cachedData['parent']);
  275. $this->assertFalse($this->cache->inCache('folder'));
  276. $this->scanner->scan('');
  277. $this->assertTrue($this->cache->inCache('folder'));
  278. $newFolderId = $this->cache->getId('folder');
  279. $this->assertNotEquals($oldFolderId, $newFolderId);
  280. $cachedData = $this->cache->get('folder/bar.txt');
  281. $this->assertEquals($newFolderId, $cachedData['parent']);
  282. }
  283. public function testRepairParentShallow() {
  284. $this->fillTestFolders();
  285. $this->scanner->scan('');
  286. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  287. $oldFolderId = $this->cache->getId('folder');
  288. // delete the folder without removing the children
  289. $query = OC::$server->getDatabaseConnection()->getQueryBuilder();
  290. $query->delete('filecache')
  291. ->where($query->expr()->eq('fileid', $query->createNamedParameter($oldFolderId)));
  292. $query->execute();
  293. $cachedData = $this->cache->get('folder/bar.txt');
  294. $this->assertEquals($oldFolderId, $cachedData['parent']);
  295. $this->assertFalse($this->cache->inCache('folder'));
  296. $this->scanner->scan('folder', IScanner::SCAN_SHALLOW);
  297. $this->assertTrue($this->cache->inCache('folder'));
  298. $newFolderId = $this->cache->getId('folder');
  299. $this->assertNotEquals($oldFolderId, $newFolderId);
  300. $cachedData = $this->cache->get('folder/bar.txt');
  301. $this->assertEquals($newFolderId, $cachedData['parent']);
  302. }
  303. /**
  304. * @dataProvider dataTestIsPartialFile
  305. *
  306. * @param string $path
  307. * @param bool $expected
  308. */
  309. public function testIsPartialFile($path, $expected) {
  310. $this->assertSame($expected,
  311. $this->scanner->isPartialFile($path)
  312. );
  313. }
  314. public function dataTestIsPartialFile() {
  315. return [
  316. ['foo.txt.part', true],
  317. ['/sub/folder/foo.txt.part', true],
  318. ['/sub/folder.part/foo.txt', true],
  319. ['foo.txt', false],
  320. ['/sub/folder/foo.txt', false],
  321. ];
  322. }
  323. }