2013-08-12 17:37:15 +04: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 17:17:00 +04:00
|
|
|
use OC\Files\Filesystem;
|
2014-11-24 17:54:42 +03:00
|
|
|
use OC\Files\Mount\MountPoint;
|
2013-08-12 17:37:15 +04:00
|
|
|
use OC\Files\Storage\Temporary;
|
2015-06-22 15:41:08 +03:00
|
|
|
use OCP\Files\Storage\IStorageFactory;
|
|
|
|
use OCP\IUser;
|
2013-08-12 17:37:15 +04:00
|
|
|
|
|
|
|
class TestScanner extends \OC\Files\Utils\Scanner {
|
|
|
|
/**
|
2014-11-24 17:54:42 +03:00
|
|
|
* @var \OC\Files\Mount\MountPoint[] $mounts
|
2013-08-12 17:37:15 +04:00
|
|
|
*/
|
|
|
|
private $mounts = array();
|
|
|
|
|
|
|
|
/**
|
2014-11-24 17:54:42 +03:00
|
|
|
* @param \OC\Files\Mount\MountPoint $mount
|
2013-08-12 17:37:15 +04:00
|
|
|
*/
|
|
|
|
public function addMount($mount) {
|
|
|
|
$this->mounts[] = $mount;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getMounts($dir) {
|
|
|
|
return $this->mounts;
|
|
|
|
}
|
2014-06-02 17:17:00 +04:00
|
|
|
|
|
|
|
public function getPropagator() {
|
|
|
|
return $this->propagator;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPropagator($propagator) {
|
|
|
|
$this->propagator = $propagator;
|
|
|
|
}
|
2013-08-12 17:37:15 +04:00
|
|
|
}
|
|
|
|
|
2014-11-12 17:54:41 +03:00
|
|
|
class Scanner extends \Test\TestCase {
|
2015-06-22 15:41:08 +03:00
|
|
|
/**
|
2015-09-22 01:56:36 +03:00
|
|
|
* @var \Test\Util\User\Dummy
|
2015-06-22 15:41:08 +03:00
|
|
|
*/
|
|
|
|
private $userBackend;
|
|
|
|
|
2014-11-12 17:54:41 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2015-09-22 01:56:36 +03:00
|
|
|
$this->userBackend = new \Test\Util\User\Dummy();
|
2015-06-22 15:41:08 +03:00
|
|
|
\OC::$server->getUserManager()->registerBackend($this->userBackend);
|
2015-04-08 13:03:55 +03:00
|
|
|
$this->loginAsUser();
|
2014-11-12 17:54:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown() {
|
2015-04-08 13:03:55 +03:00
|
|
|
$this->logout();
|
2015-06-22 15:41:08 +03:00
|
|
|
\OC::$server->getUserManager()->removeBackend($this->userBackend);
|
2014-11-12 17:54:41 +03:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2013-08-12 17:37:15 +04:00
|
|
|
public function testReuseExistingRoot() {
|
|
|
|
$storage = new Temporary(array());
|
2014-11-24 17:54:42 +03:00
|
|
|
$mount = new MountPoint($storage, '');
|
2014-06-03 13:34:48 +04:00
|
|
|
Filesystem::getMountManager()->addMount($mount);
|
2013-08-12 17:37:15 +04: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 16:02:50 +03:00
|
|
|
$scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
|
2013-08-12 17:37:15 +04: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 17:54:42 +03:00
|
|
|
$mount = new MountPoint($storage, '');
|
2014-06-03 13:34:48 +04:00
|
|
|
Filesystem::getMountManager()->addMount($mount);
|
2013-08-12 17:37:15 +04: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 16:02:50 +03:00
|
|
|
$scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
|
2013-08-12 17:37:15 +04: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 17:17:00 +04:00
|
|
|
|
2015-06-22 15:41:08 +03:00
|
|
|
public function testScanSubMount() {
|
|
|
|
$uid = $this->getUniqueID();
|
|
|
|
$this->userBackend->createUser($uid, 'test');
|
|
|
|
|
|
|
|
$mountProvider = $this->getMock('\OCP\Files\Config\IMountProvider');
|
|
|
|
|
|
|
|
$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 16:02:50 +03:00
|
|
|
$scanner = new \OC\Files\Utils\Scanner($uid, \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
|
2015-06-22 15:41:08 +03:00
|
|
|
|
|
|
|
$this->assertFalse($cache->inCache('folder/bar.txt'));
|
|
|
|
$scanner->scan('/' . $uid . '/files/foo');
|
|
|
|
$this->assertTrue($cache->inCache('folder/bar.txt'));
|
|
|
|
}
|
|
|
|
|
2014-06-02 17:17:00 +04:00
|
|
|
public function testChangePropagator() {
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Cache\ChangePropagator $propagator
|
|
|
|
*/
|
|
|
|
$propagator = $this->getMock('\OC\Files\Cache\ChangePropagator', array('propagateChanges'), array(), '', false);
|
|
|
|
|
|
|
|
$storage = new Temporary(array());
|
2014-11-24 17:54:42 +03:00
|
|
|
$mount = new MountPoint($storage, '/foo');
|
2014-06-02 17:17:00 +04:00
|
|
|
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');
|
|
|
|
|
2015-11-27 16:02:50 +03:00
|
|
|
$scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
|
2014-06-02 17:17:00 +04:00
|
|
|
$originalPropagator = $scanner->getPropagator();
|
|
|
|
$scanner->setPropagator($propagator);
|
|
|
|
$scanner->addMount($mount);
|
|
|
|
|
|
|
|
$scanner->scan('');
|
|
|
|
|
2014-06-04 12:01:50 +04:00
|
|
|
$changes = $propagator->getChanges();
|
|
|
|
$parents = $propagator->getAllParents();
|
|
|
|
sort($changes);
|
|
|
|
sort($parents);
|
|
|
|
$this->assertEquals(array('/foo', '/foo/folder', '/foo/folder/bar.txt', '/foo/foo.txt'), $changes);
|
|
|
|
$this->assertEquals(array('/', '/foo', '/foo/folder'), $parents);
|
2014-06-02 17:17:00 +04:00
|
|
|
|
2014-06-05 13:31:55 +04:00
|
|
|
$cache->put('foo.txt', array('storage_mtime' => time() - 50));
|
2014-06-02 17:17:00 +04:00
|
|
|
|
|
|
|
$propagator = $this->getMock('\OC\Files\Cache\ChangePropagator', array('propagateChanges'), array(), '', false);
|
|
|
|
$scanner->setPropagator($propagator);
|
|
|
|
$storage->file_put_contents('foo.txt', 'asdasd');
|
|
|
|
|
|
|
|
$scanner->scan('');
|
|
|
|
|
2014-06-04 12:01:50 +04:00
|
|
|
$changes = $propagator->getChanges();
|
|
|
|
$parents = $propagator->getAllParents();
|
|
|
|
$this->assertEquals(array('/foo/foo.txt'), $changes);
|
|
|
|
$this->assertEquals(array('/', '/foo'), $parents);
|
2014-06-02 17:17:00 +04:00
|
|
|
|
|
|
|
$scanner->setPropagator($originalPropagator);
|
|
|
|
|
|
|
|
$oldInfo = $cache->get('');
|
2014-06-05 13:31:55 +04:00
|
|
|
$cache->put('foo.txt', array('storage_mtime' => time() - 70));
|
2014-06-02 17:17:00 +04:00
|
|
|
$storage->file_put_contents('foo.txt', 'asdasd');
|
|
|
|
|
|
|
|
$scanner->scan('');
|
|
|
|
$newInfo = $cache->get('');
|
|
|
|
$this->assertNotEquals($oldInfo['etag'], $newInfo['etag']);
|
|
|
|
}
|
2015-07-27 12:18:41 +03: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 16:02:50 +03:00
|
|
|
$scanner = new TestScanner('', \OC::$server->getDatabaseConnection(), \OC::$server->getLogger());
|
2015-07-27 12:18:41 +03:00
|
|
|
$scanner->scan($invalidPath);
|
|
|
|
}
|
2013-08-12 17:37:15 +04:00
|
|
|
}
|