1
0

ScannerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files\Utils;
  8. use OC\Files\Filesystem;
  9. use OC\Files\Mount\MountPoint;
  10. use OC\Files\Storage\Temporary;
  11. use OCP\EventDispatcher\IEventDispatcher;
  12. use OCP\Files\Config\IMountProvider;
  13. use OCP\Files\Storage\IStorageFactory;
  14. use OCP\IUser;
  15. use Psr\Log\LoggerInterface;
  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(): void {
  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->get(LoggerInterface::class));
  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(): void {
  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->get(LoggerInterface::class));
  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(): void {
  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->get(LoggerInterface::class));
  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): void {
  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->get(LoggerInterface::class));
  137. $scanner->scan($invalidPath);
  138. }
  139. public function testPropagateEtag(): void {
  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->get(LoggerInterface::class));
  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 testShallow(): void {
  158. $storage = new Temporary([]);
  159. $mount = new MountPoint($storage, '');
  160. Filesystem::getMountManager()->addMount($mount);
  161. $cache = $storage->getCache();
  162. $storage->mkdir('folder');
  163. $storage->mkdir('folder/subfolder');
  164. $storage->file_put_contents('foo.txt', 'qwerty');
  165. $storage->file_put_contents('folder/bar.txt', 'qwerty');
  166. $storage->file_put_contents('folder/subfolder/foobar.txt', 'qwerty');
  167. $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $this->createMock(IEventDispatcher::class), \OC::$server->get(LoggerInterface::class));
  168. $scanner->addMount($mount);
  169. $scanner->scan('', $recusive = false);
  170. $this->assertTrue($cache->inCache('folder'));
  171. $this->assertFalse($cache->inCache('folder/subfolder'));
  172. $this->assertTrue($cache->inCache('foo.txt'));
  173. $this->assertFalse($cache->inCache('folder/bar.txt'));
  174. $this->assertFalse($cache->inCache('folder/subfolder/foobar.txt'));
  175. $scanner->scan('folder', $recusive = false);
  176. $this->assertTrue($cache->inCache('folder'));
  177. $this->assertTrue($cache->inCache('folder/subfolder'));
  178. $this->assertTrue($cache->inCache('foo.txt'));
  179. $this->assertTrue($cache->inCache('folder/bar.txt'));
  180. $this->assertFalse($cache->inCache('folder/subfolder/foobar.txt'));
  181. }
  182. }