ScannerTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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\Utils;
  9. use OC\Files\Filesystem;
  10. use OC\Files\Mount\MountPoint;
  11. use OC\Files\Storage\Temporary;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\Files\Config\IMountProvider;
  14. use OCP\Files\Storage\IStorageFactory;
  15. use OCP\IUser;
  16. class TestScanner extends \OC\Files\Utils\Scanner {
  17. /**
  18. * @var \OC\Files\Mount\MountPoint[] $mounts
  19. */
  20. private $mounts = [];
  21. /**
  22. * @param \OC\Files\Mount\MountPoint $mount
  23. */
  24. public function addMount($mount) {
  25. $this->mounts[] = $mount;
  26. }
  27. protected function getMounts($dir) {
  28. return $this->mounts;
  29. }
  30. }
  31. /**
  32. * Class ScannerTest
  33. *
  34. * @group DB
  35. *
  36. * @package Test\Files\Utils
  37. */
  38. class ScannerTest extends \Test\TestCase {
  39. /**
  40. * @var \Test\Util\User\Dummy
  41. */
  42. private $userBackend;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->userBackend = new \Test\Util\User\Dummy();
  46. \OC::$server->getUserManager()->registerBackend($this->userBackend);
  47. $this->loginAsUser();
  48. }
  49. protected function tearDown(): void {
  50. $this->logout();
  51. \OC::$server->getUserManager()->removeBackend($this->userBackend);
  52. parent::tearDown();
  53. }
  54. public function testReuseExistingRoot() {
  55. $storage = new Temporary([]);
  56. $mount = new MountPoint($storage, '');
  57. Filesystem::getMountManager()->addMount($mount);
  58. $cache = $storage->getCache();
  59. $storage->mkdir('folder');
  60. $storage->file_put_contents('foo.txt', 'qwerty');
  61. $storage->file_put_contents('folder/bar.txt', 'qwerty');
  62. $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $this->createMock(IEventDispatcher::class), \OC::$server->getLogger());
  63. $scanner->addMount($mount);
  64. $scanner->scan('');
  65. $this->assertTrue($cache->inCache('folder/bar.txt'));
  66. $oldRoot = $cache->get('');
  67. $scanner->scan('');
  68. $newRoot = $cache->get('');
  69. $this->assertEquals($oldRoot, $newRoot);
  70. }
  71. public function testReuseExistingFile() {
  72. $storage = new Temporary([]);
  73. $mount = new MountPoint($storage, '');
  74. Filesystem::getMountManager()->addMount($mount);
  75. $cache = $storage->getCache();
  76. $storage->mkdir('folder');
  77. $storage->file_put_contents('foo.txt', 'qwerty');
  78. $storage->file_put_contents('folder/bar.txt', 'qwerty');
  79. $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $this->createMock(IEventDispatcher::class), \OC::$server->getLogger());
  80. $scanner->addMount($mount);
  81. $scanner->scan('');
  82. $this->assertTrue($cache->inCache('folder/bar.txt'));
  83. $old = $cache->get('folder/bar.txt');
  84. $scanner->scan('');
  85. $new = $cache->get('folder/bar.txt');
  86. $this->assertEquals($old, $new);
  87. }
  88. public function testScanSubMount() {
  89. $uid = $this->getUniqueID();
  90. $this->userBackend->createUser($uid, 'test');
  91. $mountProvider = $this->createMock(IMountProvider::class);
  92. $storage = new Temporary([]);
  93. $mount = new MountPoint($storage, '/' . $uid . '/files/foo');
  94. $mountProvider->expects($this->any())
  95. ->method('getMountsForUser')
  96. ->willReturnCallback(function (IUser $user, IStorageFactory $storageFactory) use ($mount, $uid) {
  97. if ($user->getUID() === $uid) {
  98. return [$mount];
  99. } else {
  100. return [];
  101. }
  102. });
  103. \OC::$server->getMountProviderCollection()->registerProvider($mountProvider);
  104. $cache = $storage->getCache();
  105. $storage->mkdir('folder');
  106. $storage->file_put_contents('foo.txt', 'qwerty');
  107. $storage->file_put_contents('folder/bar.txt', 'qwerty');
  108. $scanner = new \OC\Files\Utils\Scanner($uid, \OC::$server->getDatabaseConnection(), \OC::$server->query(IEventDispatcher::class), \OC::$server->getLogger());
  109. $this->assertFalse($cache->inCache('folder/bar.txt'));
  110. $scanner->scan('/' . $uid . '/files/foo');
  111. $this->assertTrue($cache->inCache('folder/bar.txt'));
  112. }
  113. /**
  114. * @return array
  115. */
  116. public function invalidPathProvider() {
  117. return [
  118. [
  119. '../',
  120. ],
  121. [
  122. '..\\',
  123. ],
  124. [
  125. '../..\\../',
  126. ],
  127. ];
  128. }
  129. /**
  130. * @dataProvider invalidPathProvider
  131. * @param string $invalidPath
  132. */
  133. public function testInvalidPathScanning($invalidPath) {
  134. $this->expectException(\InvalidArgumentException::class);
  135. $this->expectExceptionMessage('Invalid path to scan');
  136. $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $this->createMock(IEventDispatcher::class), \OC::$server->getLogger());
  137. $scanner->scan($invalidPath);
  138. }
  139. public function testPropagateEtag() {
  140. $storage = new Temporary([]);
  141. $mount = new MountPoint($storage, '');
  142. Filesystem::getMountManager()->addMount($mount);
  143. $cache = $storage->getCache();
  144. $storage->mkdir('folder');
  145. $storage->file_put_contents('folder/bar.txt', 'qwerty');
  146. $storage->touch('folder/bar.txt', time() - 200);
  147. $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $this->createMock(IEventDispatcher::class), \OC::$server->getLogger());
  148. $scanner->addMount($mount);
  149. $scanner->scan('');
  150. $this->assertTrue($cache->inCache('folder/bar.txt'));
  151. $oldRoot = $cache->get('');
  152. $storage->file_put_contents('folder/bar.txt', 'qwerty');
  153. $scanner->scan('');
  154. $newRoot = $cache->get('');
  155. $this->assertNotEquals($oldRoot->getEtag(), $newRoot->getEtag());
  156. }
  157. public function testSkipLocalShares() {
  158. $sharedStorage = $this->createMock(SharedStorage::class);
  159. $sharedMount = new MountPoint($sharedStorage, '/share');
  160. Filesystem::getMountManager()->addMount($sharedMount);
  161. $sharedStorage->expects($this->any())
  162. ->method('instanceOfStorage')
  163. ->willReturnMap([
  164. [SharedStorage::class, true],
  165. ]);
  166. $sharedStorage->expects($this->never())
  167. ->method('getScanner');
  168. $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $this->createMock(IEventDispatcher::class), \OC::$server->getLogger());
  169. $scanner->addMount($sharedMount);
  170. $scanner->scan('');
  171. $scanner->backgroundScan('');
  172. }
  173. public function testShallow() {
  174. $storage = new Temporary([]);
  175. $mount = new MountPoint($storage, '');
  176. Filesystem::getMountManager()->addMount($mount);
  177. $cache = $storage->getCache();
  178. $storage->mkdir('folder');
  179. $storage->mkdir('folder/subfolder');
  180. $storage->file_put_contents('foo.txt', 'qwerty');
  181. $storage->file_put_contents('folder/bar.txt', 'qwerty');
  182. $storage->file_put_contents('folder/subfolder/foobar.txt', 'qwerty');
  183. $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $this->createMock(IEventDispatcher::class), \OC::$server->getLogger());
  184. $scanner->addMount($mount);
  185. $scanner->scan('', $recusive = false);
  186. $this->assertTrue($cache->inCache('folder'));
  187. $this->assertFalse($cache->inCache('folder/subfolder'));
  188. $this->assertTrue($cache->inCache('foo.txt'));
  189. $this->assertFalse($cache->inCache('folder/bar.txt'));
  190. $this->assertFalse($cache->inCache('folder/subfolder/foobar.txt'));
  191. $scanner->scan('folder', $recusive = false);
  192. $this->assertTrue($cache->inCache('folder'));
  193. $this->assertTrue($cache->inCache('folder/subfolder'));
  194. $this->assertTrue($cache->inCache('foo.txt'));
  195. $this->assertTrue($cache->inCache('folder/bar.txt'));
  196. $this->assertFalse($cache->inCache('folder/subfolder/foobar.txt'));
  197. }
  198. }