2012-10-26 20:07:01 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud
|
|
|
|
*
|
|
|
|
* @author Robin Appelman
|
|
|
|
* @copyright 2012 Robin Appelman icewind@owncloud.com
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Test\Files;
|
|
|
|
|
2014-11-12 17:54:41 +03:00
|
|
|
class Filesystem extends \Test\TestCase {
|
2012-10-26 20:07:01 +04:00
|
|
|
/**
|
|
|
|
* @var array tmpDirs
|
|
|
|
*/
|
2013-12-06 21:26:38 +04:00
|
|
|
private $tmpDirs = array();
|
2012-10-26 20:07:01 +04:00
|
|
|
|
2014-11-12 17:54:41 +03:00
|
|
|
/** @var \OC\Files\Storage\Storage */
|
|
|
|
private $originalStorage;
|
|
|
|
|
2012-10-26 20:07:01 +04:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getStorageData() {
|
|
|
|
$dir = \OC_Helper::tmpFolder();
|
|
|
|
$this->tmpDirs[] = $dir;
|
|
|
|
return array('datadir' => $dir);
|
|
|
|
}
|
|
|
|
|
2014-11-12 17:54:41 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->originalStorage = \OC\Files\Filesystem::getStorage('/');
|
|
|
|
\OC_User::setUserId('');
|
|
|
|
\OC\Files\Filesystem::clearMounts();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown() {
|
2012-10-26 20:07:01 +04:00
|
|
|
foreach ($this->tmpDirs as $dir) {
|
|
|
|
\OC_Helper::rmdirr($dir);
|
|
|
|
}
|
2013-11-12 18:46:01 +04:00
|
|
|
\OC\Files\Filesystem::clearMounts();
|
2014-11-12 17:54:41 +03:00
|
|
|
\OC\Files\Filesystem::mount($this->originalStorage, array(), '/');
|
2013-11-12 18:46:01 +04:00
|
|
|
\OC_User::setUserId('');
|
2014-11-07 17:23:15 +03:00
|
|
|
|
|
|
|
parent::tearDown();
|
2012-10-26 20:07:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMount() {
|
2013-12-06 21:26:38 +04:00
|
|
|
\OC\Files\Filesystem::mount('\OC\Files\Storage\Local', self::getStorageData(), '/');
|
|
|
|
$this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/'));
|
|
|
|
$this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/some/folder'));
|
|
|
|
list(, $internalPath) = \OC\Files\Filesystem::resolvePath('/');
|
|
|
|
$this->assertEquals('', $internalPath);
|
|
|
|
list(, $internalPath) = \OC\Files\Filesystem::resolvePath('/some/folder');
|
|
|
|
$this->assertEquals('some/folder', $internalPath);
|
|
|
|
|
|
|
|
\OC\Files\Filesystem::mount('\OC\Files\Storage\Local', self::getStorageData(), '/some');
|
|
|
|
$this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/'));
|
|
|
|
$this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some/folder'));
|
|
|
|
$this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some/'));
|
|
|
|
$this->assertEquals('/some/', \OC\Files\Filesystem::getMountPoint('/some'));
|
|
|
|
list(, $internalPath) = \OC\Files\Filesystem::resolvePath('/some/folder');
|
|
|
|
$this->assertEquals('folder', $internalPath);
|
2012-10-26 20:07:01 +04:00
|
|
|
}
|
|
|
|
|
2014-11-17 18:52:45 +03:00
|
|
|
public function normalizePathData() {
|
|
|
|
return array(
|
|
|
|
array('/', ''),
|
|
|
|
array('/', '/'),
|
|
|
|
array('/', '//'),
|
|
|
|
array('/', '/', false),
|
|
|
|
array('/', '//', false),
|
|
|
|
|
|
|
|
array('/path', '/path/'),
|
|
|
|
array('/path/', '/path/', false),
|
|
|
|
array('/path', 'path'),
|
|
|
|
|
|
|
|
array('/foo/bar', '/foo//bar/'),
|
|
|
|
array('/foo/bar/', '/foo//bar/', false),
|
|
|
|
array('/foo/bar', '/foo////bar'),
|
|
|
|
array('/foo/bar', '/foo/////bar'),
|
|
|
|
array('/foo/bar', '/foo/bar/.'),
|
|
|
|
array('/foo/bar', '/foo/bar/./'),
|
|
|
|
array('/foo/bar/', '/foo/bar/./', false),
|
|
|
|
array('/foo/bar', '/foo/bar/./.'),
|
|
|
|
array('/foo/bar', '/foo/bar/././'),
|
|
|
|
array('/foo/bar/', '/foo/bar/././', false),
|
|
|
|
array('/foo/bar', '/foo/./bar/'),
|
|
|
|
array('/foo/bar/', '/foo/./bar/', false),
|
|
|
|
array('/foo/.bar', '/foo/.bar/'),
|
|
|
|
array('/foo/.bar/', '/foo/.bar/', false),
|
|
|
|
array('/foo/.bar/tee', '/foo/.bar/tee'),
|
|
|
|
|
|
|
|
// Windows paths
|
|
|
|
array('/', ''),
|
|
|
|
array('/', '\\'),
|
|
|
|
array('/', '\\', false),
|
|
|
|
array('/', '\\\\'),
|
|
|
|
array('/', '\\\\', false),
|
|
|
|
|
|
|
|
array('/path', '\\path'),
|
|
|
|
array('/path', '\\path', false),
|
|
|
|
array('/path', '\\path\\'),
|
|
|
|
array('/path/', '\\path\\', false),
|
|
|
|
|
|
|
|
array('/foo/bar', '\\foo\\\\bar\\'),
|
|
|
|
array('/foo/bar/', '\\foo\\\\bar\\', false),
|
|
|
|
array('/foo/bar', '\\foo\\\\\\\\bar'),
|
|
|
|
array('/foo/bar', '\\foo\\\\\\\\\\bar'),
|
|
|
|
array('/foo/bar', '\\foo\\bar\\.'),
|
|
|
|
array('/foo/bar', '\\foo\\bar\\.\\'),
|
|
|
|
array('/foo/bar/', '\\foo\\bar\\.\\', false),
|
|
|
|
array('/foo/bar', '\\foo\\bar\\.\\.'),
|
|
|
|
array('/foo/bar', '\\foo\\bar\\.\\.\\'),
|
|
|
|
array('/foo/bar/', '\\foo\\bar\\.\\.\\', false),
|
|
|
|
array('/foo/bar', '\\foo\\.\\bar\\'),
|
|
|
|
array('/foo/bar/', '\\foo\\.\\bar\\', false),
|
|
|
|
array('/foo/.bar', '\\foo\\.bar\\'),
|
|
|
|
array('/foo/.bar/', '\\foo\\.bar\\', false),
|
|
|
|
array('/foo/.bar/tee', '\\foo\\.bar\\tee'),
|
|
|
|
|
2014-11-17 18:57:15 +03:00
|
|
|
// Absolute windows paths NOT marked as absolute
|
|
|
|
array('/C:', 'C:\\'),
|
|
|
|
array('/C:/', 'C:\\', false),
|
|
|
|
array('/C:/tests', 'C:\\tests'),
|
|
|
|
array('/C:/tests', 'C:\\tests', false),
|
|
|
|
array('/C:/tests', 'C:\\tests\\'),
|
|
|
|
array('/C:/tests/', 'C:\\tests\\', false),
|
|
|
|
|
2014-11-17 18:52:45 +03:00
|
|
|
// normalize does not resolve '..' (by design)
|
|
|
|
array('/foo/..', '/foo/../'),
|
|
|
|
array('/foo/..', '\\foo\\..\\'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider normalizePathData
|
|
|
|
*/
|
2014-11-17 18:57:15 +03:00
|
|
|
public function testNormalizePath($expected, $path, $stripTrailingSlash = true) {
|
|
|
|
$this->assertEquals($expected, \OC\Files\Filesystem::normalizePath($path, $stripTrailingSlash));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function normalizePathWindowsAbsolutePathData() {
|
|
|
|
return array(
|
|
|
|
array('C:/', 'C:\\'),
|
|
|
|
array('C:/', 'C:\\', false),
|
|
|
|
array('C:/tests', 'C:\\tests'),
|
|
|
|
array('C:/tests', 'C:\\tests', false),
|
|
|
|
array('C:/tests', 'C:\\tests\\'),
|
|
|
|
array('C:/tests/', 'C:\\tests\\', false),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider normalizePathWindowsAbsolutePathData
|
|
|
|
*/
|
|
|
|
public function testNormalizePathWindowsAbsolutePath($expected, $path, $stripTrailingSlash = true) {
|
|
|
|
if (!\OC_Util::runningOnWindows()) {
|
|
|
|
$this->markTestSkipped('This test is Windows only');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals($expected, \OC\Files\Filesystem::normalizePath($path, $stripTrailingSlash, true));
|
2012-10-26 20:07:01 +04:00
|
|
|
}
|
|
|
|
|
2014-11-17 18:52:45 +03:00
|
|
|
public function testNormalizePathUTF8() {
|
|
|
|
if (!class_exists('Patchwork\PHP\Shim\Normalizer')) {
|
|
|
|
$this->markTestSkipped('UTF8 normalizer Patchwork was not found');
|
2013-11-14 16:15:03 +04:00
|
|
|
}
|
2014-11-17 18:52:45 +03:00
|
|
|
|
|
|
|
$this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("/foo/baru\xCC\x88"));
|
|
|
|
$this->assertEquals("/foo/bar\xC3\xBC", \OC\Files\Filesystem::normalizePath("\\foo\\baru\xCC\x88"));
|
2013-11-14 16:15:03 +04:00
|
|
|
}
|
|
|
|
|
2012-10-26 20:07:01 +04:00
|
|
|
public function testHooks() {
|
2013-12-06 21:26:38 +04:00
|
|
|
if (\OC\Files\Filesystem::getView()) {
|
2012-10-26 20:07:01 +04:00
|
|
|
$user = \OC_User::getUser();
|
2013-12-06 21:26:38 +04:00
|
|
|
} else {
|
|
|
|
$user = uniqid();
|
|
|
|
\OC\Files\Filesystem::init($user, '/' . $user . '/files');
|
2012-10-26 20:07:01 +04:00
|
|
|
}
|
|
|
|
\OC_Hook::clear('OC_Filesystem');
|
|
|
|
\OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook');
|
|
|
|
|
|
|
|
\OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/');
|
|
|
|
|
2013-12-06 21:26:38 +04:00
|
|
|
$rootView = new \OC\Files\View('');
|
|
|
|
$rootView->mkdir('/' . $user);
|
|
|
|
$rootView->mkdir('/' . $user . '/files');
|
2012-10-26 20:07:01 +04:00
|
|
|
|
2013-02-05 23:21:29 +04:00
|
|
|
// \OC\Files\Filesystem::file_put_contents('/foo', 'foo');
|
2012-10-26 20:07:01 +04:00
|
|
|
\OC\Files\Filesystem::mkdir('/bar');
|
2013-02-05 23:21:29 +04:00
|
|
|
// \OC\Files\Filesystem::file_put_contents('/bar//foo', 'foo');
|
2012-10-26 20:07:01 +04:00
|
|
|
|
|
|
|
$tmpFile = \OC_Helper::tmpFile();
|
|
|
|
file_put_contents($tmpFile, 'foo');
|
|
|
|
$fh = fopen($tmpFile, 'r');
|
2013-02-05 23:21:29 +04:00
|
|
|
// \OC\Files\Filesystem::file_put_contents('/bar//foo', $fh);
|
2012-10-26 20:07:01 +04:00
|
|
|
}
|
|
|
|
|
2013-11-12 18:46:01 +04:00
|
|
|
/**
|
|
|
|
* Tests that a local storage mount is used when passed user
|
|
|
|
* does not exist.
|
|
|
|
*/
|
|
|
|
public function testLocalMountWhenUserDoesNotExist() {
|
|
|
|
$datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data");
|
|
|
|
$userId = uniqid('user_');
|
|
|
|
|
|
|
|
\OC\Files\Filesystem::initMountPoints($userId);
|
|
|
|
|
|
|
|
$homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/');
|
|
|
|
|
2014-06-02 18:54:41 +04:00
|
|
|
$this->assertTrue($homeMount->instanceOfStorage('\OC\Files\Storage\Local'));
|
2013-11-12 18:46:01 +04:00
|
|
|
$this->assertEquals('local::' . $datadir . '/' . $userId . '/', $homeMount->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that the home storage is used for the user's mount point
|
|
|
|
*/
|
|
|
|
public function testHomeMount() {
|
|
|
|
$userId = uniqid('user_');
|
|
|
|
|
|
|
|
\OC_User::createUser($userId, $userId);
|
|
|
|
|
|
|
|
\OC\Files\Filesystem::initMountPoints($userId);
|
|
|
|
|
|
|
|
$homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/');
|
|
|
|
|
2014-06-02 18:54:41 +04:00
|
|
|
$this->assertTrue($homeMount->instanceOfStorage('\OC\Files\Storage\Home'));
|
2013-11-12 18:46:01 +04:00
|
|
|
$this->assertEquals('home::' . $userId, $homeMount->getId());
|
|
|
|
|
|
|
|
\OC_User::deleteUser($userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests that the home storage is used in legacy mode
|
|
|
|
* for the user's mount point
|
|
|
|
*/
|
|
|
|
public function testLegacyHomeMount() {
|
|
|
|
$datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data");
|
|
|
|
$userId = uniqid('user_');
|
|
|
|
|
|
|
|
// insert storage into DB by constructing it
|
|
|
|
// to make initMountsPoint find its existence
|
|
|
|
$localStorage = new \OC\Files\Storage\Local(array('datadir' => $datadir . '/' . $userId . '/'));
|
|
|
|
// this will trigger the insert
|
|
|
|
$cache = $localStorage->getCache();
|
|
|
|
|
|
|
|
\OC_User::createUser($userId, $userId);
|
|
|
|
\OC\Files\Filesystem::initMountPoints($userId);
|
|
|
|
|
|
|
|
$homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/');
|
|
|
|
|
2014-06-02 18:54:41 +04:00
|
|
|
$this->assertTrue($homeMount->instanceOfStorage('\OC\Files\Storage\Home'));
|
2013-12-06 21:26:38 +04:00
|
|
|
$this->assertEquals('local::' . $datadir . '/' . $userId . '/', $homeMount->getId());
|
2013-11-12 18:46:01 +04:00
|
|
|
|
|
|
|
\OC_User::deleteUser($userId);
|
|
|
|
// delete storage entry
|
|
|
|
$cache->clear();
|
|
|
|
}
|
|
|
|
|
2012-10-26 20:07:01 +04:00
|
|
|
public function dummyHook($arguments) {
|
|
|
|
$path = $arguments['path'];
|
|
|
|
$this->assertEquals($path, \OC\Files\Filesystem::normalizePath($path)); //the path passed to the hook should already be normalized
|
|
|
|
}
|
2014-03-24 17:32:04 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the default cache dir is part of the user's home
|
|
|
|
*/
|
|
|
|
public function testMountDefaultCacheDir() {
|
|
|
|
$userId = uniqid('user_');
|
|
|
|
$oldCachePath = \OC_Config::getValue('cache_path', '');
|
|
|
|
// no cache path configured
|
|
|
|
\OC_Config::setValue('cache_path', '');
|
|
|
|
|
|
|
|
\OC_User::createUser($userId, $userId);
|
|
|
|
\OC\Files\Filesystem::initMountPoints($userId);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'/' . $userId . '/',
|
|
|
|
\OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache')
|
|
|
|
);
|
|
|
|
list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache');
|
2014-06-02 18:54:41 +04:00
|
|
|
$this->assertTrue($storage->instanceOfStorage('\OC\Files\Storage\Home'));
|
2014-03-24 17:32:04 +04:00
|
|
|
$this->assertEquals('cache', $internalPath);
|
|
|
|
\OC_User::deleteUser($userId);
|
|
|
|
|
|
|
|
\OC_Config::setValue('cache_path', $oldCachePath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an external cache is mounted into
|
|
|
|
* the user's home
|
|
|
|
*/
|
|
|
|
public function testMountExternalCacheDir() {
|
|
|
|
$userId = uniqid('user_');
|
|
|
|
|
|
|
|
$oldCachePath = \OC_Config::getValue('cache_path', '');
|
|
|
|
// set cache path to temp dir
|
|
|
|
$cachePath = \OC_Helper::tmpFolder() . '/extcache';
|
|
|
|
\OC_Config::setValue('cache_path', $cachePath);
|
|
|
|
|
|
|
|
\OC_User::createUser($userId, $userId);
|
|
|
|
\OC\Files\Filesystem::initMountPoints($userId);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
'/' . $userId . '/cache/',
|
|
|
|
\OC\Files\Filesystem::getMountPoint('/' . $userId . '/cache')
|
|
|
|
);
|
|
|
|
list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath('/' . $userId . '/cache');
|
2014-06-02 18:54:41 +04:00
|
|
|
$this->assertTrue($storage->instanceOfStorage('\OC\Files\Storage\Local'));
|
2014-03-24 17:32:04 +04:00
|
|
|
$this->assertEquals('', $internalPath);
|
|
|
|
\OC_User::deleteUser($userId);
|
|
|
|
|
|
|
|
\OC_Config::setValue('cache_path', $oldCachePath);
|
|
|
|
}
|
2012-10-26 20:07:01 +04:00
|
|
|
}
|