123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace Test\Files\Utils;
- use OC\Files\Filesystem;
- use OC\Files\Mount\MountPoint;
- use OC\Files\Storage\Temporary;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\Files\Config\IMountProvider;
- use OCP\Files\Storage\IStorageFactory;
- use OCP\IUser;
- use Psr\Log\LoggerInterface;
- class TestScanner extends \OC\Files\Utils\Scanner {
- /**
- * @var \OC\Files\Mount\MountPoint[] $mounts
- */
- private $mounts = [];
- /**
- * @param \OC\Files\Mount\MountPoint $mount
- */
- public function addMount($mount) {
- $this->mounts[] = $mount;
- }
- protected function getMounts($dir) {
- return $this->mounts;
- }
- }
- /**
- * Class ScannerTest
- *
- * @group DB
- *
- * @package Test\Files\Utils
- */
- class ScannerTest extends \Test\TestCase {
- /**
- * @var \Test\Util\User\Dummy
- */
- private $userBackend;
- protected function setUp(): void {
- parent::setUp();
- $this->userBackend = new \Test\Util\User\Dummy();
- \OC::$server->getUserManager()->registerBackend($this->userBackend);
- $this->loginAsUser();
- }
- protected function tearDown(): void {
- $this->logout();
- \OC::$server->getUserManager()->removeBackend($this->userBackend);
- parent::tearDown();
- }
- public function testReuseExistingRoot() {
- $storage = new Temporary([]);
- $mount = new MountPoint($storage, '');
- Filesystem::getMountManager()->addMount($mount);
- $cache = $storage->getCache();
- $storage->mkdir('folder');
- $storage->file_put_contents('foo.txt', 'qwerty');
- $storage->file_put_contents('folder/bar.txt', 'qwerty');
- $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $this->createMock(IEventDispatcher::class), \OC::$server->get(LoggerInterface::class));
- $scanner->addMount($mount);
- $scanner->scan('');
- $this->assertTrue($cache->inCache('folder/bar.txt'));
- $oldRoot = $cache->get('');
- $scanner->scan('');
- $newRoot = $cache->get('');
- $this->assertEquals($oldRoot, $newRoot);
- }
- public function testReuseExistingFile() {
- $storage = new Temporary([]);
- $mount = new MountPoint($storage, '');
- Filesystem::getMountManager()->addMount($mount);
- $cache = $storage->getCache();
- $storage->mkdir('folder');
- $storage->file_put_contents('foo.txt', 'qwerty');
- $storage->file_put_contents('folder/bar.txt', 'qwerty');
- $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $this->createMock(IEventDispatcher::class), \OC::$server->get(LoggerInterface::class));
- $scanner->addMount($mount);
- $scanner->scan('');
- $this->assertTrue($cache->inCache('folder/bar.txt'));
- $old = $cache->get('folder/bar.txt');
- $scanner->scan('');
- $new = $cache->get('folder/bar.txt');
- $this->assertEquals($old, $new);
- }
- public function testScanSubMount() {
- $uid = $this->getUniqueID();
- $this->userBackend->createUser($uid, 'test');
- $mountProvider = $this->createMock(IMountProvider::class);
- $storage = new Temporary([]);
- $mount = new MountPoint($storage, '/' . $uid . '/files/foo');
- $mountProvider->expects($this->any())
- ->method('getMountsForUser')
- ->willReturnCallback(function (IUser $user, IStorageFactory $storageFactory) use ($mount, $uid) {
- if ($user->getUID() === $uid) {
- return [$mount];
- } else {
- return [];
- }
- });
- \OC::$server->getMountProviderCollection()->registerProvider($mountProvider);
- $cache = $storage->getCache();
- $storage->mkdir('folder');
- $storage->file_put_contents('foo.txt', 'qwerty');
- $storage->file_put_contents('folder/bar.txt', 'qwerty');
- $scanner = new \OC\Files\Utils\Scanner($uid, \OC::$server->getDatabaseConnection(), \OC::$server->query(IEventDispatcher::class), \OC::$server->get(LoggerInterface::class));
- $this->assertFalse($cache->inCache('folder/bar.txt'));
- $scanner->scan('/' . $uid . '/files/foo');
- $this->assertTrue($cache->inCache('folder/bar.txt'));
- }
- /**
- * @return array
- */
- public function invalidPathProvider() {
- return [
- [
- '../',
- ],
- [
- '..\\',
- ],
- [
- '../..\\../',
- ],
- ];
- }
- /**
- * @dataProvider invalidPathProvider
- * @param string $invalidPath
- */
- public function testInvalidPathScanning($invalidPath) {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid path to scan');
- $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $this->createMock(IEventDispatcher::class), \OC::$server->get(LoggerInterface::class));
- $scanner->scan($invalidPath);
- }
- public function testPropagateEtag() {
- $storage = new Temporary([]);
- $mount = new MountPoint($storage, '');
- Filesystem::getMountManager()->addMount($mount);
- $cache = $storage->getCache();
- $storage->mkdir('folder');
- $storage->file_put_contents('folder/bar.txt', 'qwerty');
- $storage->touch('folder/bar.txt', time() - 200);
- $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $this->createMock(IEventDispatcher::class), \OC::$server->get(LoggerInterface::class));
- $scanner->addMount($mount);
- $scanner->scan('');
- $this->assertTrue($cache->inCache('folder/bar.txt'));
- $oldRoot = $cache->get('');
- $storage->file_put_contents('folder/bar.txt', 'qwerty');
- $scanner->scan('');
- $newRoot = $cache->get('');
- $this->assertNotEquals($oldRoot->getEtag(), $newRoot->getEtag());
- }
- public function testShallow() {
- $storage = new Temporary([]);
- $mount = new MountPoint($storage, '');
- Filesystem::getMountManager()->addMount($mount);
- $cache = $storage->getCache();
- $storage->mkdir('folder');
- $storage->mkdir('folder/subfolder');
- $storage->file_put_contents('foo.txt', 'qwerty');
- $storage->file_put_contents('folder/bar.txt', 'qwerty');
- $storage->file_put_contents('folder/subfolder/foobar.txt', 'qwerty');
- $scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), $this->createMock(IEventDispatcher::class), \OC::$server->get(LoggerInterface::class));
- $scanner->addMount($mount);
- $scanner->scan('', $recusive = false);
- $this->assertTrue($cache->inCache('folder'));
- $this->assertFalse($cache->inCache('folder/subfolder'));
- $this->assertTrue($cache->inCache('foo.txt'));
- $this->assertFalse($cache->inCache('folder/bar.txt'));
- $this->assertFalse($cache->inCache('folder/subfolder/foobar.txt'));
- $scanner->scan('folder', $recusive = false);
- $this->assertTrue($cache->inCache('folder'));
- $this->assertTrue($cache->inCache('folder/subfolder'));
- $this->assertTrue($cache->inCache('foo.txt'));
- $this->assertTrue($cache->inCache('folder/bar.txt'));
- $this->assertFalse($cache->inCache('folder/subfolder/foobar.txt'));
- }
- }
|