1
0

ScannerTest.php 6.6 KB

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