<commit_before><?php
/**
 * ownCloud
 *
 * @author <NAME>
 * @copyright 2012 <NAME> <EMAIL>
 *
 * 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;

class Filesystem extends \Test\TestCase {
	/**
	 * @var array tmpDirs
	 */
	private $tmpDirs = array();

	/**
	 * @return array
	 */
	private function getStorageData() {
		$dir = \OC_Helper::tmpFolder();
		$this->tmpDirs[] = $dir;
		return array('datadir' => $dir);
	}

	protected function setUp() {
		parent::setUp();
		$this->loginAsUser();
	}

	protected function tearDown() {
		foreach ($this->tmpDirs as $dir) {
			\OC_Helper::rmdirr($dir);
		}

		$this->logout();
		parent::tearDown();
	}

	public function testMount() {
		\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);
	}

	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'),

			// 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),

			// normalize does not resolve '..' (by design)
			array('/foo/..', '/foo/../'),
			array('/foo/..', '\\foo\\..\\'),
		);
	}

	/**
	 * @dataProvider normalizePathData
	 */
	public function testNormalizePath($expected, $path, $stripTrailingSlash = true) {
		$this->assertEquals($expected, \OC\Files\Filesystem::normalizePath($path, $stripTrailingSlash));
	}

	public function isValidPathData() {
		return array(
			array('/', true),
			array('/path', true),
			array('/foo/bar', true),
			array('/foo//bar/', true),
			array('/foo////bar', true),
			array('/foo//\///bar', true),
			array('/foo/bar/.', true),
			array('/foo/bar/./', true),
			array('/foo/bar/./.', true),
			array('/foo/bar/././', true),
			array('/foo/bar/././..bar', true),
			array('/foo/bar/././..bar/a', true),
			array('/foo/bar/././..', false),
			array('/foo/bar/././../', false),
			array('/foo/bar/.././', false),
			array('/foo/bar/../../', false),
			array('/foo/bar/../..\\', false),
			array('..', false),
			array('../', false),
			array('../foo/bar', false),
			array('..\foo/bar', false),
		);
	}

	/**
	 * @dataProvider isValidPathData
	 */
	public function testIsValidPath($path, $expected) {
		$this->assertSame($expected, \OC\Files\Filesystem::isValidPath($path));
	}

	public function isFileBlacklistedData() {
		return array(
			array('/etc/foo/bar/foo.txt', false),
			array('\etc\foo/bar\foo.txt', false),
			array('.htaccess', true),
			array('.htaccess/', true),
			array('.htaccess\\', true),
			array('/etc/foo\bar/.htaccess\\', true),
			array('/etc/foo\bar/.htaccess/', true),
			array('/etc/foo\bar/.htaccess/foo', false),
			array('//foo//bar/\.htaccess/', true),
			array('\foo\bar\.HTAccess', true),
		);
	}

	/**
	 * @dataProvider isFileBlacklistedData
	 */
	public function testIsFileBlacklisted($path, $expected) {
		$this->assertSame($expected, \OC\Files\Filesystem::isFileBlacklisted($path));
	}

	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));
	}

	public function testNormalizePathUTF8() {
		if (!class_exists('Patchwork\PHP\Shim\Normalizer')) {
			$this->markTestSkipped('UTF8 normalizer Patchwork was not found');
		}

		$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"));
	}

	public function testHooks() {
		if (\OC\Files\Filesystem::getView()) {
			$user = \OC_User::getUser();
		} else {
			$user = $this->getUniqueID();
			\OC\Files\Filesystem::init($user, '/' . $user . '/files');
		}
		\OC_Hook::clear('OC_Filesystem');
		\OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook');

		\OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/');

		$rootView = new \OC\Files\View('');
		$rootView->mkdir('/' . $user);
		$rootView->mkdir('/' . $user . '/files');

//		\OC\Files\Filesystem::file_put_contents('/foo', 'foo');
		\OC\Files\Filesystem::mkdir('/bar');
//		\OC\Files\Filesystem::file_put_contents('/bar//foo', 'foo');

		$tmpFile = \OC_Helper::tmpFile();
		file_put_contents($tmpFile, 'foo');
		$fh = fopen($tmpFile, 'r');
//		\OC\Files\Filesystem::file_put_contents('/bar//foo', $fh);
	}

	/**
	 * 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 = $this->getUniqueID('user_');

		\OC\Files\Filesystem::initMountPoints($userId);

		$homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/');

		$this->assertTrue($homeMount->instanceOfStorage('\OC\Files\Storage\Local'));
		$this->assertEquals('local::' . $datadir . '/' . $userId . '/', $homeMount->getId());
	}

	/**
	 * Tests that the home storage is used for the user's mount point
	 */
	public function testHomeMount() {
		$userId = $this->getUniqueID('user_');

		\OC_User::createUser($userId, $userId);

		\OC\Files\Filesystem::initMountPoints($userId);

		$homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/');

		$this->assertTrue($homeMount->instanceOfStorage('\OC\Files\Storage\Home'));
		$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");<commit_msg>fix filesystem and encryption tests<commit_after><?php
/**
 * ownCloud
 *
 * @author <NAME>
 * @copyright 2012 <NAME> <EMAIL>
 *
 * 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;

use OC\User\NoUserException;

class Filesystem extends \Test\TestCase {
	/**
	 * @var array tmpDirs
	 */
	private $tmpDirs = array();

	/**
	 * @return array
	 */
	private function getStorageData() {
		$dir = \OC_Helper::tmpFolder();
		$this->tmpDirs[] = $dir;
		return array('datadir' => $dir);
	}

	protected function setUp() {
		parent::setUp();
		$this->loginAsUser();
	}

	protected function tearDown() {
		foreach ($this->tmpDirs as $dir) {
			\OC_Helper::rmdirr($dir);
		}

		$this->logout();
		parent::tearDown();
	}

	public function testMount() {
		\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);
	}

	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'),

			// 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),

			// normalize does not resolve '..' (by design)
			array('/foo/..', '/foo/../'),
			array('/foo/..', '\\foo\\..\\'),
		);
	}

	/**
	 * @dataProvider normalizePathData
	 */
	public function testNormalizePath($expected, $path, $stripTrailingSlash = true) {
		$this->assertEquals($expected, \OC\Files\Filesystem::normalizePath($path, $stripTrailingSlash));
	}

	public function isValidPathData() {
		return array(
			array('/', true),
			array('/path', true),
			array('/foo/bar', true),
			array('/foo//bar/', true),
			array('/foo////bar', true),
			array('/foo//\///bar', true),
			array('/foo/bar/.', true),
			array('/foo/bar/./', true),
			array('/foo/bar/./.', true),
			array('/foo/bar/././', true),
			array('/foo/bar/././..bar', true),
			array('/foo/bar/././..bar/a', true),
			array('/foo/bar/././..', false),
			array('/foo/bar/././../', false),
			array('/foo/bar/.././', false),
			array('/foo/bar/../../', false),
			array('/foo/bar/../..\\', false),
			array('..', false),
			array('../', false),
			array('../foo/bar', false),
			array('..\foo/bar', false),
		);
	}

	/**
	 * @dataProvider isValidPathData
	 */
	public function testIsValidPath($path, $expected) {
		$this->assertSame($expected, \OC\Files\Filesystem::isValidPath($path));
	}

	public function isFileBlacklistedData() {
		return array(
			array('/etc/foo/bar/foo.txt', false),
			array('\etc\foo/bar\foo.txt', false),
			array('.htaccess', true),
			array('.htaccess/', true),
			array('.htaccess\\', true),
			array('/etc/foo\bar/.htaccess\\', true),
			array('/etc/foo\bar/.htaccess/', true),
			array('/etc/foo\bar/.htaccess/foo', false),
			array('//foo//bar/\.htaccess/', true),
			array('\foo\bar\.HTAccess', true),
		);
	}

	/**
	 * @dataProvider isFileBlacklistedData
	 */
	public function testIsFileBlacklisted($path, $expected) {
		$this->assertSame($expected, \OC\Files\Filesystem::isFileBlacklisted($path));
	}

	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));
	}

	public function testNormalizePathUTF8() {
		if (!class_exists('Patchwork\PHP\Shim\Normalizer')) {
			$this->markTestSkipped('UTF8 normalizer Patchwork was not found');
		}

		$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"));
	}

	public function testHooks() {
		if (\OC\Files\Filesystem::getView()) {
			$user = \OC_User::getUser();
		} else {
			$user = $this->getUniqueID();
			\OC\Files\Filesystem::init($user, '/' . $user . '/files');
		}
		\OC_Hook::clear('OC_Filesystem');
		\OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook');

		\OC\Files\Filesystem::mount('OC\Files\Storage\Temporary', array(), '/');

		$rootView = new \OC\Files\View('');
		$rootView->mkdir('/' . $user);
		$rootView->mkdir('/' . $user . '/files');

//		\OC\Files\Filesystem::file_put_contents('/foo', 'foo');
		\OC\Files\Filesystem::mkdir('/bar');
//		\OC\Files\Filesystem::file_put_contents('/bar//foo', 'foo');

		$tmpFile = \OC_Helper::tmpFile();
		file_put_contents($tmpFile, 'foo');
		$fh = fopen($tmpFile, 'r');
//		\OC\Files\Filesystem::file_put_contents('/bar//foo', $fh);
	}

	/**
	 * Tests that an exception is thrown when passed user does not exist.
	 * @expectedException \OC\User\NoUserException
	 */
	public function testLocalMountWhenUserDoesNotExist() {
		$datadir = \OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data");
		$userId = $this->getUniqueID('user_');

		\OC\Files\Filesystem::initMountPoints($userId);
	}

	/**
	 * Tests that the home storage is used for the user's mount point
	 */
	public function testHomeMount() {
		$userId = $this->getUniqueID('user_');

		\OC_User::createUser($userId, $userId);

		\OC\Files\Filesystem::initMountPoints($userId);

		$homeMount = \OC\Files\Filesystem::getStorage('/' . $userId . '/');

		$this->assertTrue($homeMount->instanceOfStorage('\OC\Files\Storage\Home'));
		$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");
{'fraction_non_alphanumeric': 0.25561321753865707, 'fraction_numerical': 0.0013768269434441855, 'mean_word_length': 3.3018223234624147, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '496903', 'n_tokens_mistral': 7150, 'n_tokens_neox': 6451, 'n_words': 1195}