2013-08-12 13:37:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test\Files\Utils;
|
|
|
|
|
2014-06-02 13:17:00 +00:00
|
|
|
use OC\Files\Filesystem;
|
2014-11-24 14:54:42 +00:00
|
|
|
use OC\Files\Mount\MountPoint;
|
2013-08-12 13:37:15 +00:00
|
|
|
use OC\Files\Storage\Temporary;
|
2016-12-19 18:53:35 +00:00
|
|
|
use OCA\Files_Sharing\SharedStorage;
|
2016-09-12 19:29:38 +00:00
|
|
|
use OCP\Files\Config\IMountProvider;
|
2015-06-22 12:41:08 +00:00
|
|
|
use OCP\Files\Storage\IStorageFactory;
|
|
|
|
use OCP\IUser;
|
2013-08-12 13:37:15 +00:00
|
|
|
|
|
|
|
class TestScanner extends \OC\Files\Utils\Scanner {
|
|
|
|
/**
|
2014-11-24 14:54:42 +00:00
|
|
|
* @var \OC\Files\Mount\MountPoint[] $mounts
|
2013-08-12 13:37:15 +00:00
|
|
|
*/
|
|
|
|
private $mounts = array();
|
|
|
|
|
|
|
|
/**
|
2014-11-24 14:54:42 +00:00
|
|
|
* @param \OC\Files\Mount\MountPoint $mount
|
2013-08-12 13:37:15 +00:00
|
|
|
*/
|
|
|
|
public function addMount($mount) {
|
|
|
|
$this->mounts[] = $mount;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getMounts($dir) {
|
|
|
|
return $this->mounts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 00:52:41 +00:00
|
|
|
/**
|
2016-05-20 13:38:20 +00:00
|
|
|
* Class ScannerTest
|
2015-11-03 00:52:41 +00:00
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*
|
|
|
|
* @package Test\Files\Utils
|
|
|
|
*/
|
2016-05-20 13:38:20 +00:00
|
|
|
class ScannerTest extends \Test\TestCase {
|
2015-06-22 12:41:08 +00:00
|
|
|
/**
|
2015-09-21 22:56:36 +00:00
|
|
|
* @var \Test\Util\User\Dummy
|
2015-06-22 12:41:08 +00:00
|
|
|
*/
|
|
|
|
private $userBackend;
|
|
|
|
|
2014-11-12 14:54:41 +00:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2015-09-21 22:56:36 +00:00
|
|
|
$this->userBackend = new \Test\Util\User\Dummy();
|
2015-06-22 12:41:08 +00:00
|
|
|
\OC::$server->getUserManager()->registerBackend($this->userBackend);
|
2015-04-08 10:03:55 +00:00
|
|
|
$this->loginAsUser();
|
2014-11-12 14:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown() {
|
2015-04-08 10:03:55 +00:00
|
|
|
$this->logout();
|
2015-06-22 12:41:08 +00:00
|
|
|
\OC::$server->getUserManager()->removeBackend($this->userBackend);
|
2014-11-12 14:54:41 +00:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2013-08-12 13:37:15 +00:00
|
|
|
public function testReuseExistingRoot() {
|
|
|
|
$storage = new Temporary(array());
|
2014-11-24 14:54:42 +00:00
|
|
|
$mount = new MountPoint($storage, '');
|
2014-06-03 09:34:48 +00:00
|
|
|
Filesystem::getMountManager()->addMount($mount);
|
2013-08-12 13:37:15 +00:00
|
|
|
$cache = $storage->getCache();
|
|
|
|
|
|
|
|
$storage->mkdir('folder');
|
|
|
|
$storage->file_put_contents('foo.txt', 'qwerty');
|
|
|
|
$storage->file_put_contents('folder/bar.txt', 'qwerty');
|
|
|
|
|
2015-11-27 13:02:50 +00:00
|
|
|
$scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
|
2013-08-12 13:37:15 +00:00
|
|
|
$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(array());
|
2014-11-24 14:54:42 +00:00
|
|
|
$mount = new MountPoint($storage, '');
|
2014-06-03 09:34:48 +00:00
|
|
|
Filesystem::getMountManager()->addMount($mount);
|
2013-08-12 13:37:15 +00:00
|
|
|
$cache = $storage->getCache();
|
|
|
|
|
|
|
|
$storage->mkdir('folder');
|
|
|
|
$storage->file_put_contents('foo.txt', 'qwerty');
|
|
|
|
$storage->file_put_contents('folder/bar.txt', 'qwerty');
|
|
|
|
|
2015-11-27 13:02:50 +00:00
|
|
|
$scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
|
2013-08-12 13:37:15 +00:00
|
|
|
$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);
|
|
|
|
}
|
2014-06-02 13:17:00 +00:00
|
|
|
|
2015-06-22 12:41:08 +00:00
|
|
|
public function testScanSubMount() {
|
|
|
|
$uid = $this->getUniqueID();
|
|
|
|
$this->userBackend->createUser($uid, 'test');
|
|
|
|
|
2016-09-12 19:29:38 +00:00
|
|
|
$mountProvider = $this->createMock(IMountProvider::class);
|
2015-06-22 12:41:08 +00:00
|
|
|
|
|
|
|
$storage = new Temporary(array());
|
|
|
|
$mount = new MountPoint($storage, '/' . $uid . '/files/foo');
|
|
|
|
|
|
|
|
$mountProvider->expects($this->any())
|
|
|
|
->method('getMountsForUser')
|
|
|
|
->will($this->returnCallback(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');
|
|
|
|
|
2015-11-27 13:02:50 +00:00
|
|
|
$scanner = new \OC\Files\Utils\Scanner($uid, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
|
2015-06-22 12:41:08 +00:00
|
|
|
|
|
|
|
$this->assertFalse($cache->inCache('folder/bar.txt'));
|
|
|
|
$scanner->scan('/' . $uid . '/files/foo');
|
|
|
|
$this->assertTrue($cache->inCache('folder/bar.txt'));
|
|
|
|
}
|
|
|
|
|
2015-07-27 09:18:41 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function invalidPathProvider() {
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'../',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'..\\',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'../..\\../',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider invalidPathProvider
|
|
|
|
* @expectedException \InvalidArgumentException
|
|
|
|
* @expectedExceptionMessage Invalid path to scan
|
|
|
|
* @param string $invalidPath
|
|
|
|
*/
|
|
|
|
public function testInvalidPathScanning($invalidPath) {
|
2015-11-27 13:02:50 +00:00
|
|
|
$scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
|
2015-07-27 09:18:41 +00:00
|
|
|
$scanner->scan($invalidPath);
|
|
|
|
}
|
2016-04-25 15:23:48 +00:00
|
|
|
|
|
|
|
public function testPropagateEtag() {
|
|
|
|
$storage = new Temporary(array());
|
|
|
|
$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(), \OC::$server->getLogger());
|
|
|
|
$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());
|
|
|
|
}
|
2016-11-10 14:44:18 +00:00
|
|
|
|
|
|
|
public function testSkipLocalShares() {
|
2016-12-19 18:53:35 +00:00
|
|
|
$sharedStorage = $this->createMock(SharedStorage::class);
|
2016-11-10 14:44:18 +00:00
|
|
|
$sharedMount = new MountPoint($sharedStorage, '/share');
|
|
|
|
Filesystem::getMountManager()->addMount($sharedMount);
|
|
|
|
|
|
|
|
$sharedStorage->expects($this->any())
|
|
|
|
->method('instanceOfStorage')
|
|
|
|
->will($this->returnValueMap([
|
2016-12-19 18:53:35 +00:00
|
|
|
[SharedStorage::class, true],
|
2016-11-10 14:44:18 +00:00
|
|
|
]));
|
|
|
|
$sharedStorage->expects($this->never())
|
|
|
|
->method('getScanner');
|
|
|
|
|
|
|
|
$scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
|
|
|
|
$scanner->addMount($sharedMount);
|
|
|
|
$scanner->scan('');
|
|
|
|
|
|
|
|
$scanner->backgroundScan('');
|
|
|
|
}
|
2018-05-18 21:51:08 +00:00
|
|
|
|
|
|
|
public function testShallow() {
|
|
|
|
$storage = new Temporary(array());
|
|
|
|
$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(), \OC::$server->getLogger());
|
|
|
|
$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'));
|
|
|
|
}
|
|
|
|
|
2013-08-12 13:37:15 +00:00
|
|
|
}
|