ScannerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 OCA\Files_Sharing\SharedStorage;
  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 = array();
  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() {
  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() {
  50. $this->logout();
  51. \OC::$server->getUserManager()->removeBackend($this->userBackend);
  52. parent::tearDown();
  53. }
  54. public function testReuseExistingRoot() {
  55. $storage = new Temporary(array());
  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(), \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(array());
  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(), \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(array());
  93. $mount = new MountPoint($storage, '/' . $uid . '/files/foo');
  94. $mountProvider->expects($this->any())
  95. ->method('getMountsForUser')
  96. ->will($this->returnCallback(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->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. * @expectedException \InvalidArgumentException
  132. * @expectedExceptionMessage Invalid path to scan
  133. * @param string $invalidPath
  134. */
  135. public function testInvalidPathScanning($invalidPath) {
  136. $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
  137. $scanner->scan($invalidPath);
  138. }
  139. public function testPropagateEtag() {
  140. $storage = new Temporary(array());
  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(), \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. ->will($this->returnValueMap([
  164. [SharedStorage::class, true],
  165. ]));
  166. $sharedStorage->expects($this->never())
  167. ->method('getScanner');
  168. $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
  169. $scanner->addMount($sharedMount);
  170. $scanner->scan('');
  171. $scanner->backgroundScan('');
  172. }
  173. }