ScannerTest.php 15 KB

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