ScannerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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() {
  31. parent::setUp();
  32. $this->storage = new \OC\Files\Storage\Temporary(array());
  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() {
  37. if ($this->cache) {
  38. $this->cache->clear();
  39. }
  40. parent::tearDown();
  41. }
  42. 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. 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. 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. 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. 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. 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. 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 testReuseExisting() {
  161. $this->fillTestFolders();
  162. $this->scanner->scan('');
  163. $oldData = $this->cache->get('');
  164. $this->storage->unlink('folder/bar.txt');
  165. $this->cache->put('folder', array('mtime' => $this->storage->filemtime('folder'), 'storage_mtime' => $this->storage->filemtime('folder')));
  166. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_SIZE);
  167. $newData = $this->cache->get('');
  168. $this->assertInternalType('string', $oldData['etag']);
  169. $this->assertInternalType('string', $newData['etag']);
  170. $this->assertNotSame($oldData['etag'], $newData['etag']);
  171. $this->assertEquals($oldData['size'], $newData['size']);
  172. $oldData = $newData;
  173. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
  174. $newData = $this->cache->get('');
  175. $this->assertSame($oldData['etag'], $newData['etag']);
  176. $this->assertEquals(-1, $newData['size']);
  177. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE);
  178. $oldData = $this->cache->get('');
  179. $this->assertNotEquals(-1, $oldData['size']);
  180. $this->scanner->scanFile('', \OC\Files\Cache\Scanner::REUSE_ETAG + \OC\Files\Cache\Scanner::REUSE_SIZE);
  181. $newData = $this->cache->get('');
  182. $this->assertSame($oldData['etag'], $newData['etag']);
  183. $this->assertEquals($oldData['size'], $newData['size']);
  184. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG + \OC\Files\Cache\Scanner::REUSE_SIZE);
  185. $newData = $this->cache->get('');
  186. $this->assertSame($oldData['etag'], $newData['etag']);
  187. $this->assertEquals($oldData['size'], $newData['size']);
  188. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG + \OC\Files\Cache\Scanner::REUSE_SIZE);
  189. $newData = $this->cache->get('');
  190. $this->assertSame($oldData['etag'], $newData['etag']);
  191. $this->assertEquals($oldData['size'], $newData['size']);
  192. }
  193. public function testRemovedFile() {
  194. $this->fillTestFolders();
  195. $this->scanner->scan('');
  196. $this->assertTrue($this->cache->inCache('foo.txt'));
  197. $this->storage->unlink('foo.txt');
  198. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  199. $this->assertFalse($this->cache->inCache('foo.txt'));
  200. }
  201. public function testRemovedFolder() {
  202. $this->fillTestFolders();
  203. $this->scanner->scan('');
  204. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  205. $this->storage->rmdir('/folder');
  206. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  207. $this->assertFalse($this->cache->inCache('folder'));
  208. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  209. }
  210. public function testScanRemovedFile() {
  211. $this->fillTestFolders();
  212. $this->scanner->scan('');
  213. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  214. $this->storage->unlink('folder/bar.txt');
  215. $this->scanner->scanFile('folder/bar.txt');
  216. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  217. }
  218. public function testETagRecreation() {
  219. $this->fillTestFolders();
  220. $this->scanner->scan('folder/bar.txt');
  221. // manipulate etag to simulate an empty etag
  222. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
  223. /** @var CacheEntry $data0 */
  224. $data0 = $this->cache->get('folder/bar.txt');
  225. $this->assertInternalType('string', $data0['etag']);
  226. $data1 = $this->cache->get('folder');
  227. $this->assertInternalType('string', $data1['etag']);
  228. $data2 = $this->cache->get('');
  229. $this->assertInternalType('string', $data2['etag']);
  230. $data0['etag'] = '';
  231. $this->cache->put('folder/bar.txt', $data0->getData());
  232. // rescan
  233. $this->scanner->scan('folder/bar.txt', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
  234. // verify cache content
  235. $newData0 = $this->cache->get('folder/bar.txt');
  236. $this->assertInternalType('string', $newData0['etag']);
  237. $this->assertNotEmpty($newData0['etag']);
  238. }
  239. public function testRepairParent() {
  240. $this->fillTestFolders();
  241. $this->scanner->scan('');
  242. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  243. $oldFolderId = $this->cache->getId('folder');
  244. // delete the folder without removing the childs
  245. $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
  246. \OC_DB::executeAudited($sql, array($oldFolderId));
  247. $cachedData = $this->cache->get('folder/bar.txt');
  248. $this->assertEquals($oldFolderId, $cachedData['parent']);
  249. $this->assertFalse($this->cache->inCache('folder'));
  250. $this->scanner->scan('');
  251. $this->assertTrue($this->cache->inCache('folder'));
  252. $newFolderId = $this->cache->getId('folder');
  253. $this->assertNotEquals($oldFolderId, $newFolderId);
  254. $cachedData = $this->cache->get('folder/bar.txt');
  255. $this->assertEquals($newFolderId, $cachedData['parent']);
  256. }
  257. public function testRepairParentShallow() {
  258. $this->fillTestFolders();
  259. $this->scanner->scan('');
  260. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  261. $oldFolderId = $this->cache->getId('folder');
  262. // delete the folder without removing the childs
  263. $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
  264. \OC_DB::executeAudited($sql, array($oldFolderId));
  265. $cachedData = $this->cache->get('folder/bar.txt');
  266. $this->assertEquals($oldFolderId, $cachedData['parent']);
  267. $this->assertFalse($this->cache->inCache('folder'));
  268. $this->scanner->scan('folder', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  269. $this->assertTrue($this->cache->inCache('folder'));
  270. $newFolderId = $this->cache->getId('folder');
  271. $this->assertNotEquals($oldFolderId, $newFolderId);
  272. $cachedData = $this->cache->get('folder/bar.txt');
  273. $this->assertEquals($newFolderId, $cachedData['parent']);
  274. }
  275. /**
  276. * @dataProvider dataTestIsPartialFile
  277. *
  278. * @param string $path
  279. * @param bool $expected
  280. */
  281. public function testIsPartialFile($path, $expected) {
  282. $this->assertSame($expected,
  283. $this->scanner->isPartialFile($path)
  284. );
  285. }
  286. public function dataTestIsPartialFile() {
  287. return [
  288. ['foo.txt.part', true],
  289. ['/sub/folder/foo.txt.part', true],
  290. ['/sub/folder.part/foo.txt', true],
  291. ['foo.txt', false],
  292. ['/sub/folder/foo.txt', false],
  293. ];
  294. }
  295. }