scanner.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 \PHPUnit_Framework_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. function setUp() {
  23. $this->storage = new \OC\Files\Storage\Temporary(array());
  24. $this->scanner = new \OC\Files\Cache\Scanner($this->storage);
  25. $this->cache = new \OC\Files\Cache\Cache($this->storage);
  26. }
  27. function tearDown() {
  28. if ($this->cache) {
  29. $ids = $this->cache->getAll();
  30. $permissionsCache = $this->storage->getPermissionsCache();
  31. $permissionsCache->removeMultiple($ids, \OC_User::getUser());
  32. $this->cache->clear();
  33. }
  34. }
  35. function testFile() {
  36. $data = "dummy file data\n";
  37. $this->storage->file_put_contents('foo.txt', $data);
  38. $this->scanner->scanFile('foo.txt');
  39. $this->assertEquals($this->cache->inCache('foo.txt'), true);
  40. $cachedData = $this->cache->get('foo.txt');
  41. $this->assertEquals($cachedData['size'], strlen($data));
  42. $this->assertEquals($cachedData['mimetype'], 'text/plain');
  43. $this->assertNotEquals($cachedData['parent'], -1); //parent folders should be scanned automatically
  44. $data = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
  45. $this->storage->file_put_contents('foo.png', $data);
  46. $this->scanner->scanFile('foo.png');
  47. $this->assertEquals($this->cache->inCache('foo.png'), true);
  48. $cachedData = $this->cache->get('foo.png');
  49. $this->assertEquals($cachedData['size'], strlen($data));
  50. $this->assertEquals($cachedData['mimetype'], 'image/png');
  51. }
  52. private function fillTestFolders() {
  53. $textData = "dummy file data\n";
  54. $imgData = file_get_contents(\OC::$SERVERROOT . '/core/img/logo.png');
  55. $this->storage->mkdir('folder');
  56. $this->storage->file_put_contents('foo.txt', $textData);
  57. $this->storage->file_put_contents('foo.png', $imgData);
  58. $this->storage->file_put_contents('folder/bar.txt', $textData);
  59. }
  60. function testFolder() {
  61. $this->fillTestFolders();
  62. $this->scanner->scan('');
  63. $this->assertEquals($this->cache->inCache(''), true);
  64. $this->assertEquals($this->cache->inCache('foo.txt'), true);
  65. $this->assertEquals($this->cache->inCache('foo.png'), true);
  66. $this->assertEquals($this->cache->inCache('folder'), true);
  67. $this->assertEquals($this->cache->inCache('folder/bar.txt'), true);
  68. $cachedDataText = $this->cache->get('foo.txt');
  69. $cachedDataText2 = $this->cache->get('foo.txt');
  70. $cachedDataImage = $this->cache->get('foo.png');
  71. $cachedDataFolder = $this->cache->get('');
  72. $cachedDataFolder2 = $this->cache->get('folder');
  73. $this->assertEquals($cachedDataImage['parent'], $cachedDataText['parent']);
  74. $this->assertEquals($cachedDataFolder['fileid'], $cachedDataImage['parent']);
  75. $this->assertEquals($cachedDataFolder['size'], $cachedDataImage['size'] + $cachedDataText['size'] + $cachedDataText2['size']);
  76. $this->assertEquals($cachedDataFolder2['size'], $cachedDataText2['size']);
  77. }
  78. function testShallow() {
  79. $this->fillTestFolders();
  80. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  81. $this->assertEquals($this->cache->inCache(''), true);
  82. $this->assertEquals($this->cache->inCache('foo.txt'), true);
  83. $this->assertEquals($this->cache->inCache('foo.png'), true);
  84. $this->assertEquals($this->cache->inCache('folder'), true);
  85. $this->assertEquals($this->cache->inCache('folder/bar.txt'), false);
  86. $cachedDataFolder = $this->cache->get('');
  87. $cachedDataFolder2 = $this->cache->get('folder');
  88. $this->assertEquals(-1, $cachedDataFolder['size']);
  89. $this->assertEquals(-1, $cachedDataFolder2['size']);
  90. $this->scanner->scan('folder', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  91. $cachedDataFolder2 = $this->cache->get('folder');
  92. $this->assertNotEquals($cachedDataFolder2['size'], -1);
  93. $this->cache->correctFolderSize('folder');
  94. $cachedDataFolder = $this->cache->get('');
  95. $this->assertNotEquals($cachedDataFolder['size'], -1);
  96. }
  97. function testBackgroundScan() {
  98. $this->fillTestFolders();
  99. $this->storage->mkdir('folder2');
  100. $this->storage->file_put_contents('folder2/bar.txt', 'foobar');
  101. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  102. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  103. $this->assertFalse($this->cache->inCache('folder/2bar.txt'));
  104. $cachedData = $this->cache->get('');
  105. $this->assertEquals(-1, $cachedData['size']);
  106. $this->scanner->backgroundScan();
  107. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  108. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  109. $cachedData = $this->cache->get('');
  110. $this->assertnotEquals(-1, $cachedData['size']);
  111. $this->assertFalse($this->cache->getIncomplete());
  112. }
  113. public function testReuseExisting() {
  114. $this->fillTestFolders();
  115. $this->scanner->scan('');
  116. $oldData = $this->cache->get('');
  117. $this->storage->unlink('folder/bar.txt');
  118. $this->cache->put('folder', array('mtime' => $this->storage->filemtime('folder')));
  119. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_SIZE);
  120. $newData = $this->cache->get('');
  121. $this->assertNotEquals($oldData['etag'], $newData['etag']);
  122. $this->assertEquals($oldData['size'], $newData['size']);
  123. $oldData = $newData;
  124. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
  125. $newData = $this->cache->get('');
  126. $this->assertEquals($oldData['etag'], $newData['etag']);
  127. $this->assertEquals(-1, $newData['size']);
  128. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE);
  129. $oldData = $this->cache->get('');
  130. $this->assertNotEquals(-1, $oldData['size']);
  131. $this->scanner->scanFile('', \OC\Files\Cache\Scanner::REUSE_ETAG + \OC\Files\Cache\Scanner::REUSE_SIZE);
  132. $newData = $this->cache->get('');
  133. $this->assertEquals($oldData['etag'], $newData['etag']);
  134. $this->assertEquals($oldData['size'], $newData['size']);
  135. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE, \OC\Files\Cache\Scanner::REUSE_ETAG + \OC\Files\Cache\Scanner::REUSE_SIZE);
  136. $newData = $this->cache->get('');
  137. $this->assertEquals($oldData['etag'], $newData['etag']);
  138. $this->assertEquals($oldData['size'], $newData['size']);
  139. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG + \OC\Files\Cache\Scanner::REUSE_SIZE);
  140. $newData = $this->cache->get('');
  141. $this->assertEquals($oldData['etag'], $newData['etag']);
  142. $this->assertEquals($oldData['size'], $newData['size']);
  143. }
  144. public function testRemovedFile() {
  145. $this->fillTestFolders();
  146. $this->scanner->scan('');
  147. $this->assertTrue($this->cache->inCache('foo.txt'));
  148. $this->storage->unlink('foo.txt');
  149. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  150. $this->assertFalse($this->cache->inCache('foo.txt'));
  151. }
  152. public function testRemovedFolder() {
  153. $this->fillTestFolders();
  154. $this->scanner->scan('');
  155. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  156. $this->storage->unlink('/folder');
  157. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW);
  158. $this->assertFalse($this->cache->inCache('folder'));
  159. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  160. }
  161. public function testScanRemovedFile(){
  162. $this->fillTestFolders();
  163. $this->scanner->scan('');
  164. $this->assertTrue($this->cache->inCache('folder/bar.txt'));
  165. $this->storage->unlink('folder/bar.txt');
  166. $this->scanner->scanFile('folder/bar.txt');
  167. $this->assertFalse($this->cache->inCache('folder/bar.txt'));
  168. }
  169. public function testETagRecreation() {
  170. $this->fillTestFolders();
  171. $this->scanner->scan('folder/bar.txt');
  172. // manipulate etag to simulate an empty etag
  173. $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
  174. $data0 = $this->cache->get('folder/bar.txt');
  175. $data1 = $this->cache->get('folder');
  176. $data2 = $this->cache->get('');
  177. $data0['etag'] = '';
  178. $this->cache->put('folder/bar.txt', $data0);
  179. // rescan
  180. $this->scanner->scan('folder/bar.txt', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
  181. // verify cache content
  182. $newData0 = $this->cache->get('folder/bar.txt');
  183. $newData1 = $this->cache->get('folder');
  184. $newData2 = $this->cache->get('');
  185. $this->assertNotEmpty($newData0['etag']);
  186. $this->assertNotEquals($data1['etag'], $newData1['etag']);
  187. $this->assertNotEquals($data2['etag'], $newData2['etag']);
  188. }
  189. }