scanner.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. class Scanner extends \Test\TestCase {
  10. /**
  11. * @var \OC\Files\Storage\Storage $storage
  12. */
  13. private $storage;
  14. /**
  15. * @var \OC\Files\Cache\Scanner $scanner
  16. */
  17. private $scanner;
  18. /**
  19. * @var \OC\Files\Cache\Cache $cache
  20. */
  21. private $cache;
  22. protected function setUp() {
  23. parent::setUp();
  24. $this->storage = new \OC\Files\Storage\Temporary(array());
  25. $this->scanner = new \OC\Files\Cache\Scanner($this->storage);
  26. $this->cache = new \OC\Files\Cache\Cache($this->storage);
  27. }
  28. protected function tearDown() {
  29. if ($this->cache) {
  30. $this->cache->clear();
  31. }
  32. parent::tearDown();
  33. }
  34. function testFile() {
  35. $data = "dummy file data\n";
  36. $this->storage->file_put_contents('foo.txt', $data);
  37. $this->scanner->scanFile('foo.txt');
  38. $this->assertEquals($this->cache->inCache('foo.txt'), true);
  39. $cachedData = $this->cache->get('foo.txt');
  40. $this->assertEquals($cachedData['size'], strlen($data));
  41. $this->assertEquals($cachedData['mimetype'], 'text/plain');
  42. $this->assertNotEquals($cachedData['parent'], -1); //parent folders should be scanned automatically
  43. $data = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
  44. $this->storage->file_put_contents('foo.png', $data);
  45. $this->scanner->scanFile('foo.png');
  46. $this->assertEquals($this->cache->inCache('foo.png'), true);
  47. $cachedData = $this->cache->get('foo.png');
  48. $this->assertEquals($cachedData['size'], strlen($data));
  49. $this->assertEquals($cachedData['mimetype'], 'image/png');
  50. }
  51. private function fillTestFolders() {
  52. $textData = "dummy file data\n";
  53. $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
  54. $this->storage->mkdir('folder');
  55. $this->storage->file_put_contents('foo.txt', $textData);
  56. $this->storage->file_put_contents('foo.png', $imgData);
  57. $this->storage->file_put_contents('folder/bar.txt', $textData);
  58. }
  59. function testFolder() {
  60. $this->fillTestFolders();
  61. $this->scanner->scan('');
  62. $this->assertEquals($this->cache->inCache(''), true);
  63. $this->assertEquals($this->cache->inCache('foo.txt'), true);
  64. $this->assertEquals($this->cache->inCache('foo.png'), true);
  65. $this->assertEquals($this->cache->inCache('folder'), true);
  66. $this->assertEquals($this->cache->inCache('folder/bar.txt'), true);
  67. $cachedDataText = $this->cache->get('foo.txt');
  68. $cachedDataText2 = $this->cache->get('foo.txt');
  69. $cachedDataImage = $this->cache->get('foo.png');
  70. $cachedDataFolder = $this->cache->get('');
  71. $cachedDataFolder2 = $this->cache->get('folder');
  72. $this->assertEquals($cachedDataImage['parent'], $cachedDataText['parent']);
  73. $this->assertEquals($cachedDataFolder['fileid'], $cachedDataImage['parent']);
  74. $this->assertEquals($cachedDataFolder['size'], $cachedDataImage['size'] + $cachedDataText['size'] + $cachedDataText2['size']);
  75. $this->assertEquals($cachedDataFolder2['size'], $cachedDataText2['size']);
  76. }
  77. function testShallow() {
  78. $this->fillTestFolders();
  79. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  80. $this->assertEquals($this->cache->inCache(''), true);
  81. $this->assertEquals($this->cache->inCache('foo.txt'), true);
  82. $this->assertEquals($this->cache->inCache('foo.png'), true);
  83. $this->assertEquals($this->cache->inCache('folder'), true);
  84. $this->assertEquals($this->cache->inCache('folder/bar.txt'), false);
  85. $cachedDataFolder = $this->cache->get('');
  86. $cachedDataFolder2 = $this->cache->get('folder');
  87. $this->assertEquals(-1, $cachedDataFolder['size']);
  88. $this->assertEquals(-1, $cachedDataFolder2['size']);
  89. $this->scanner->scan('folder', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  90. $cachedDataFolder2 = $this->cache->get('folder');
  91. $this->assertNotEquals($cachedDataFolder2['size'], -1);
  92. $this->cache->correctFolderSize('folder');
  93. $cachedDataFolder = $this->cache->get('');
  94. $this->assertNotEquals($cachedDataFolder['size'], -1);
  95. }
  96. function testBackgroundScan() {
  97. $this->fillTestFolders();
  98. $this->storage->mkdir('folder2');
  99. $this->storage->file_put_contents('folder2/bar.txt', 'foobar');
  100. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  101. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  102. $this->assertFalse($this->cache->inCache('folder/2bar.txt'));
  103. $cachedData = $this->cache->get('');
  104. $this->assertEquals(-1, $cachedData['size']);
  105. $this->scanner->backgroundScan();
  106. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  107. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  108. $cachedData = $this->cache->get('');
  109. $this->assertnotEquals(-1, $cachedData['size']);
  110. $this->assertFalse($this->cache->getIncomplete());
  111. }
  112. public function testReuseExisting() {
  113. $this->fillTestFolders();
  114. $this->scanner->scan('');
  115. $oldData = $this->cache->get('');
  116. $this->storage->unlink('folder/bar.txt');
  117. $this->cache->put('folder', array('mtime' => $this->storage->filemtime('folder'), 'storage_mtime' => $this->storage->filemtime('folder')));
  118. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_SIZE);
  119. $newData = $this->cache->get('');
  120. $this->assertInternalType('string', $oldData['etag']);
  121. $this->assertInternalType('string', $newData['etag']);
  122. $this->assertNotSame($oldData['etag'], $newData['etag']);
  123. $this->assertEquals($oldData['size'], $newData['size']);
  124. $oldData = $newData;
  125. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
  126. $newData = $this->cache->get('');
  127. $this->assertSame($oldData['etag'], $newData['etag']);
  128. $this->assertEquals(-1, $newData['size']);
  129. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE);
  130. $oldData = $this->cache->get('');
  131. $this->assertNotEquals(-1, $oldData['size']);
  132. $this->scanner->scanFile('', \OC\Files\Cache\Scanner::REUSE_ETAG + \OC\Files\Cache\Scanner::REUSE_SIZE);
  133. $newData = $this->cache->get('');
  134. $this->assertSame($oldData['etag'], $newData['etag']);
  135. $this->assertEquals($oldData['size'], $newData['size']);
  136. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG + \OC\Files\Cache\Scanner::REUSE_SIZE);
  137. $newData = $this->cache->get('');
  138. $this->assertSame($oldData['etag'], $newData['etag']);
  139. $this->assertEquals($oldData['size'], $newData['size']);
  140. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG + \OC\Files\Cache\Scanner::REUSE_SIZE);
  141. $newData = $this->cache->get('');
  142. $this->assertSame($oldData['etag'], $newData['etag']);
  143. $this->assertEquals($oldData['size'], $newData['size']);
  144. }
  145. public function testRemovedFile() {
  146. $this->fillTestFolders();
  147. $this->scanner->scan('');
  148. $this->assertTrue($this->cache->inCache('foo.txt'));
  149. $this->storage->unlink('foo.txt');
  150. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  151. $this->assertFalse($this->cache->inCache('foo.txt'));
  152. }
  153. public function testRemovedFolder() {
  154. $this->fillTestFolders();
  155. $this->scanner->scan('');
  156. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  157. $this->storage->rmdir('/folder');
  158. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  159. $this->assertFalse($this->cache->inCache('folder'));
  160. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  161. }
  162. public function testScanRemovedFile() {
  163. $this->fillTestFolders();
  164. $this->scanner->scan('');
  165. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  166. $this->storage->unlink('folder/bar.txt');
  167. $this->scanner->scanFile('folder/bar.txt');
  168. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  169. }
  170. public function testETagRecreation() {
  171. $this->fillTestFolders();
  172. $this->scanner->scan('folder/bar.txt');
  173. // manipulate etag to simulate an empty etag
  174. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
  175. $data0 = $this->cache->get('folder/bar.txt');
  176. $this->assertInternalType('string', $data0['etag']);
  177. $data1 = $this->cache->get('folder');
  178. $this->assertInternalType('string', $data1['etag']);
  179. $data2 = $this->cache->get('');
  180. $this->assertInternalType('string', $data2['etag']);
  181. $data0['etag'] = '';
  182. $this->cache->put('folder/bar.txt', $data0);
  183. // rescan
  184. $this->scanner->scan('folder/bar.txt', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
  185. // verify cache content
  186. $newData0 = $this->cache->get('folder/bar.txt');
  187. $this->assertInternalType('string', $newData0['etag']);
  188. $this->assertNotEmpty($newData0['etag']);
  189. }
  190. public function testRepairParent() {
  191. $this->fillTestFolders();
  192. $this->scanner->scan('');
  193. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  194. $oldFolderId = $this->cache->getId('folder');
  195. // delete the folder without removing the childs
  196. $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
  197. \OC_DB::executeAudited($sql, array($oldFolderId));
  198. $cachedData = $this->cache->get('folder/bar.txt');
  199. $this->assertEquals($oldFolderId, $cachedData['parent']);
  200. $this->assertFalse($this->cache->inCache('folder'));
  201. $this->scanner->scan('');
  202. $this->assertTrue($this->cache->inCache('folder'));
  203. $newFolderId = $this->cache->getId('folder');
  204. $this->assertNotEquals($oldFolderId, $newFolderId);
  205. $cachedData = $this->cache->get('folder/bar.txt');
  206. $this->assertEquals($newFolderId, $cachedData['parent']);
  207. }
  208. public function testRepairParentShallow() {
  209. $this->fillTestFolders();
  210. $this->scanner->scan('');
  211. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  212. $oldFolderId = $this->cache->getId('folder');
  213. // delete the folder without removing the childs
  214. $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?';
  215. \OC_DB::executeAudited($sql, array($oldFolderId));
  216. $cachedData = $this->cache->get('folder/bar.txt');
  217. $this->assertEquals($oldFolderId, $cachedData['parent']);
  218. $this->assertFalse($this->cache->inCache('folder'));
  219. $this->scanner->scan('folder', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  220. $this->assertTrue($this->cache->inCache('folder'));
  221. $newFolderId = $this->cache->getId('folder');
  222. $this->assertNotEquals($oldFolderId, $newFolderId);
  223. $cachedData = $this->cache->get('folder/bar.txt');
  224. $this->assertEquals($newFolderId, $cachedData['parent']);
  225. }
  226. }