2013-11-12 13:24:10 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program 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, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2013-11-12 13:24:10 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-12-03 19:20:04 +03:00
|
|
|
namespace OCA\Files_Encryption\Tests;
|
2013-11-12 13:24:10 +04:00
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
use OCA\Files_Encryption;
|
|
|
|
use OCA\Files_Encryption\Helper;
|
|
|
|
|
2013-11-12 13:24:10 +04:00
|
|
|
/**
|
2014-12-03 19:20:04 +03:00
|
|
|
* Class Helper
|
2013-11-12 13:24:10 +04:00
|
|
|
*/
|
2015-01-08 22:57:49 +03:00
|
|
|
class TestHelper extends TestCase {
|
2013-11-12 13:24:10 +04:00
|
|
|
|
2013-12-19 14:15:59 +04:00
|
|
|
const TEST_ENCRYPTION_HELPER_USER1 = "test-helper-user1";
|
2014-05-22 17:43:42 +04:00
|
|
|
const TEST_ENCRYPTION_HELPER_USER2 = "test-helper-user2";
|
2013-12-19 14:15:59 +04:00
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
protected function setUpUsers() {
|
2013-12-19 14:15:59 +04:00
|
|
|
// create test user
|
2014-12-03 19:03:11 +03:00
|
|
|
self::loginHelper(self::TEST_ENCRYPTION_HELPER_USER2, true);
|
|
|
|
self::loginHelper(self::TEST_ENCRYPTION_HELPER_USER1, true);
|
2013-12-19 14:15:59 +04:00
|
|
|
}
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
protected function cleanUpUsers() {
|
2013-12-19 14:15:59 +04:00
|
|
|
// cleanup test user
|
2014-12-03 19:03:11 +03:00
|
|
|
\OC_User::deleteUser(self::TEST_ENCRYPTION_HELPER_USER1);
|
|
|
|
\OC_User::deleteUser(self::TEST_ENCRYPTION_HELPER_USER2);
|
2014-10-10 17:34:19 +04:00
|
|
|
}
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
public static function setupHooks() {
|
|
|
|
// Filesystem related hooks
|
2015-01-08 22:57:49 +03:00
|
|
|
Helper::registerFilesystemHooks();
|
2014-11-06 18:53:35 +03:00
|
|
|
|
|
|
|
// clear and register hooks
|
|
|
|
\OC_FileProxy::clearProxies();
|
2015-01-08 22:57:49 +03:00
|
|
|
\OC_FileProxy::register(new Files_Encryption\Proxy());
|
2014-11-06 18:53:35 +03:00
|
|
|
}
|
2014-10-10 17:34:19 +04:00
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
public static function tearDownAfterClass() {
|
|
|
|
parent::tearDownAfterClass();
|
2013-12-19 14:15:59 +04:00
|
|
|
}
|
|
|
|
|
2013-11-12 13:24:10 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2013-11-12 19:48:24 +04:00
|
|
|
function testStripPartialFileExtension() {
|
2013-11-12 13:24:10 +04:00
|
|
|
|
|
|
|
$partFilename = 'testfile.txt.part';
|
|
|
|
$filename = 'testfile.txt';
|
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
$this->assertTrue(Helper::isPartialFilePath($partFilename));
|
2013-11-12 13:24:10 +04:00
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
$this->assertEquals('testfile.txt', Helper::stripPartialFileExtension($partFilename));
|
2013-11-12 13:24:10 +04:00
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
$this->assertFalse(Helper::isPartialFilePath($filename));
|
2013-11-12 13:24:10 +04:00
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
$this->assertEquals('testfile.txt', Helper::stripPartialFileExtension($filename));
|
2013-11-12 13:24:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2013-11-12 19:48:24 +04:00
|
|
|
function testStripPartialFileExtensionWithTransferIdPath() {
|
2013-11-12 13:24:10 +04:00
|
|
|
|
|
|
|
$partFilename = 'testfile.txt.ocTransferId643653835.part';
|
|
|
|
$filename = 'testfile.txt';
|
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
$this->assertTrue(Helper::isPartialFilePath($partFilename));
|
|
|
|
|
|
|
|
$this->assertEquals('testfile.txt', Helper::stripPartialFileExtension($partFilename));
|
2013-11-12 13:24:10 +04:00
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
$this->assertFalse(Helper::isPartialFilePath($filename));
|
2013-11-12 13:24:10 +04:00
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
$this->assertEquals('testfile.txt', Helper::stripPartialFileExtension($filename));
|
|
|
|
}
|
2013-11-12 13:24:10 +04:00
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataVersionsPathPositive
|
|
|
|
*/
|
|
|
|
function testGetPathFromVersionPositive($path, $expected) {
|
|
|
|
$result = Helper::getPathFromVersion($path);
|
|
|
|
$this->assertSame($expected, $result);
|
2013-11-12 13:24:10 +04:00
|
|
|
}
|
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
function dataVersionsPathPositive() {
|
|
|
|
return array(
|
|
|
|
array('/user/files_versions/foo/bar/test.txt.v456756835', 'foo/bar/test.txt'),
|
|
|
|
array('user/files_versions/foo/bar/test.txt.v456756835', 'foo/bar/test.txt'),
|
|
|
|
array('user/files_versions//foo/bar/test.txt.v456756835', 'foo/bar/test.txt'),
|
|
|
|
array('user/files_versions/test.txt.v456756835', 'test.txt'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataVersionsPathNegative
|
|
|
|
* @expectedException \OCA\Files_Encryption\Exception\EncryptionException
|
|
|
|
*/
|
|
|
|
function testGetPathFromVersionNegative($path) {
|
|
|
|
Helper::getPathFromVersion($path);
|
|
|
|
}
|
2013-11-15 19:49:50 +04:00
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
function dataVersionsPathNegative() {
|
|
|
|
return array(
|
|
|
|
array('/user/files_versions/'),
|
|
|
|
array('/user/files_versions'),
|
|
|
|
);
|
|
|
|
}
|
2013-11-15 19:49:50 +04:00
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataPathsCachedFilePositive
|
|
|
|
*/
|
|
|
|
function testGetPathFromCachedFilePositive($path, $expected) {
|
|
|
|
$result = Helper::getPathFromCachedFile($path);
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
2013-11-15 19:49:50 +04:00
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
function dataPathsCachedFilePositive() {
|
|
|
|
return array(
|
|
|
|
array('/user/cache/transferid636483/foo/bar/test.txt', 'foo/bar/test.txt'),
|
|
|
|
array('/user/cache/transferid636483//test.txt', 'test.txt'),
|
|
|
|
array('user/cache/transferid636483//test.txt', 'test.txt'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataPathsCachedFileNegative
|
|
|
|
* @expectedException \OCA\Files_Encryption\Exception\EncryptionException
|
|
|
|
*/
|
|
|
|
function testGetPathFromCachedFileNegative($path) {
|
|
|
|
Helper::getPathFromCachedFile($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataPathsCachedFileNegative() {
|
|
|
|
return array(
|
|
|
|
array('/user/cache/transferid636483/'),
|
|
|
|
array('/user/cache/transferid636483'),
|
|
|
|
array('/user/cache/transferid636483//'),
|
|
|
|
array('/user/cache'),
|
|
|
|
);
|
2013-11-15 19:49:50 +04:00
|
|
|
}
|
|
|
|
|
2013-12-19 14:15:59 +04:00
|
|
|
function testGetUser() {
|
2014-11-06 18:53:35 +03:00
|
|
|
self::setUpUsers();
|
2013-12-19 14:15:59 +04:00
|
|
|
|
|
|
|
$path1 = "/" . self::TEST_ENCRYPTION_HELPER_USER1 . "/files/foo/bar.txt";
|
|
|
|
$path2 = "/" . self::TEST_ENCRYPTION_HELPER_USER1 . "/cache/foo/bar.txt";
|
2014-05-22 17:43:42 +04:00
|
|
|
$path3 = "/" . self::TEST_ENCRYPTION_HELPER_USER2 . "/thumbnails/foo";
|
2013-12-19 14:15:59 +04:00
|
|
|
$path4 ="/" . "/" . self::TEST_ENCRYPTION_HELPER_USER1;
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
self::loginHelper(self::TEST_ENCRYPTION_HELPER_USER1);
|
2014-05-22 17:43:42 +04:00
|
|
|
|
2013-12-19 14:15:59 +04:00
|
|
|
// if we are logged-in every path should return the currently logged-in user
|
2015-01-08 22:57:49 +03:00
|
|
|
$this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path1));
|
|
|
|
$this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path2));
|
|
|
|
$this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path3));
|
|
|
|
$this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path4));
|
2013-12-19 14:15:59 +04:00
|
|
|
|
|
|
|
// now log out
|
2014-11-06 18:53:35 +03:00
|
|
|
self::logoutHelper();
|
2013-12-19 14:15:59 +04:00
|
|
|
|
|
|
|
// now we should only get the user from /user/files and user/cache paths
|
2015-01-08 22:57:49 +03:00
|
|
|
$this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path1));
|
|
|
|
$this->assertEquals(self::TEST_ENCRYPTION_HELPER_USER1, Helper::getUser($path2));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->assertFalse(Helper::getUser($path3));
|
|
|
|
$this->assertFalse(true, '"OCA\Files_Encryption\Exception\EncryptionException: Could not determine user expected"');
|
|
|
|
} catch (Files_Encryption\Exception\EncryptionException $e) {
|
|
|
|
$this->assertSame('Could not determine user', $e->getMessage());
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$this->assertFalse(Helper::getUser($path4));
|
|
|
|
$this->assertFalse(true, '"OCA\Files_Encryption\Exception\EncryptionException: Could not determine user expected"');
|
|
|
|
} catch (Files_Encryption\Exception\EncryptionException $e) {
|
|
|
|
$this->assertSame('Could not determine user', $e->getMessage());
|
|
|
|
}
|
2013-12-19 14:15:59 +04:00
|
|
|
|
|
|
|
// Log-in again
|
2014-12-03 19:03:11 +03:00
|
|
|
self::loginHelper(self::TEST_ENCRYPTION_HELPER_USER1);
|
2014-11-06 18:53:35 +03:00
|
|
|
self::cleanUpUsers();
|
2013-12-19 14:15:59 +04:00
|
|
|
}
|
|
|
|
|
2015-01-08 22:57:49 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataStripUserFilesPath
|
|
|
|
*/
|
|
|
|
function testStripUserFilesPath($path, $expected) {
|
|
|
|
$result = Helper::stripUserFilesPath($path);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataStripUserFilesPath() {
|
|
|
|
return array(
|
|
|
|
array('/user/files/foo.txt', 'foo.txt'),
|
|
|
|
array('//user/files/foo.txt', 'foo.txt'),
|
|
|
|
array('user//files/foo/bar.txt', 'foo/bar.txt'),
|
|
|
|
array('user//files/', false),
|
|
|
|
array('/user', false),
|
|
|
|
array('', false),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataStripUserFilesPathPositive
|
|
|
|
*/
|
|
|
|
function testGetUserFromPathPositive($path, $expected) {
|
|
|
|
self::setUpUsers();
|
|
|
|
$result = Helper::getUserFromPath($path);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
self::cleanUpUsers();
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataStripUserFilesPathPositive() {
|
|
|
|
return array(
|
|
|
|
array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/files/foo.txt', self::TEST_ENCRYPTION_HELPER_USER1),
|
|
|
|
array('//' . self::TEST_ENCRYPTION_HELPER_USER2 . '/files_versions/foo.txt', self::TEST_ENCRYPTION_HELPER_USER2),
|
2015-01-30 16:39:09 +03:00
|
|
|
array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/files_trashbin/', self::TEST_ENCRYPTION_HELPER_USER1),
|
2015-01-08 22:57:49 +03:00
|
|
|
array(self::TEST_ENCRYPTION_HELPER_USER1 . '//cache/foo/bar.txt', self::TEST_ENCRYPTION_HELPER_USER1),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataStripUserFilesPathNegative
|
|
|
|
* @expectedException \OCA\Files_Encryption\Exception\EncryptionException
|
|
|
|
*/
|
|
|
|
function testGetUserFromPathNegative($path) {
|
|
|
|
Helper::getUserFromPath($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataStripUserFilesPathNegative() {
|
|
|
|
return array(
|
|
|
|
array('/unknown_user/files/foo.txt'),
|
|
|
|
array('/' . self::TEST_ENCRYPTION_HELPER_USER2 . '/unknown_folder/foo.txt'),
|
|
|
|
array('/' . self::TEST_ENCRYPTION_HELPER_USER1),
|
|
|
|
array(''),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataPaths
|
|
|
|
*/
|
|
|
|
function testMkdirr($path, $expected) {
|
|
|
|
self::setUpUsers();
|
|
|
|
Helper::mkdirr($path, new \OC\Files\View('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/files'));
|
|
|
|
// ignore the filename because we only check for the directories
|
|
|
|
$dirParts = array_slice($expected, 0, -1);
|
|
|
|
$expectedPath = implode('/', $dirParts);
|
|
|
|
$this->assertTrue(\OC\Files\Filesystem::is_dir($expectedPath));
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
\OC\Files\Filesystem::unlink('/' . $expected[0]);
|
|
|
|
self::cleanUpUsers();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataDetectFileTypePositive
|
|
|
|
*/
|
|
|
|
function testDetectFileTypePositive($path, $expected) {
|
|
|
|
$result = Helper::detectFileType($path);
|
|
|
|
$this->assertSame($expected, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataDetectFileTypePositive() {
|
|
|
|
return array(
|
|
|
|
array(self::TEST_ENCRYPTION_HELPER_USER1 . '/files', Files_Encryption\Util::FILE_TYPE_FILE),
|
|
|
|
array(self::TEST_ENCRYPTION_HELPER_USER1 . '/files/foo/bar', Files_Encryption\Util::FILE_TYPE_FILE),
|
|
|
|
array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/files/foo/bar', Files_Encryption\Util::FILE_TYPE_FILE),
|
|
|
|
array(self::TEST_ENCRYPTION_HELPER_USER1 . '/files_versions', Files_Encryption\Util::FILE_TYPE_VERSION),
|
|
|
|
array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '//files_versions/foo/bar', Files_Encryption\Util::FILE_TYPE_VERSION),
|
|
|
|
array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '//cache/foo/bar', Files_Encryption\Util::FILE_TYPE_CACHE),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataDetectFileTypeNegative
|
|
|
|
* @expectedException \OCA\Files_Encryption\Exception\EncryptionException
|
|
|
|
*/
|
|
|
|
function testDetectFileTypeNegative($path) {
|
|
|
|
Helper::detectFileType($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataDetectFileTypeNegative() {
|
|
|
|
return array(
|
|
|
|
array('/files'),
|
|
|
|
array('/' . self::TEST_ENCRYPTION_HELPER_USER1 . '/unsuported_dir/foo/bar'),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataPaths
|
|
|
|
*/
|
|
|
|
function testSplitPath($path, $expected) {
|
|
|
|
$result = Helper::splitPath($path);
|
|
|
|
$this->compareArray($result, $expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataPaths() {
|
|
|
|
return array(
|
|
|
|
array('foo/bar/test.txt', array('', 'foo', 'bar', 'test.txt')),
|
|
|
|
array('/foo/bar/test.txt', array('', 'foo', 'bar', 'test.txt')),
|
|
|
|
array('/foo/bar//test.txt', array('', 'foo', 'bar', 'test.txt')),
|
|
|
|
array('//foo/bar/test.txt', array('', 'foo', 'bar', 'test.txt')),
|
|
|
|
array('foo', array('', 'foo')),
|
|
|
|
array('/foo', array('', 'foo')),
|
|
|
|
array('//foo', array('', 'foo')),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function compareArray($result, $expected) {
|
|
|
|
$this->assertSame(count($expected), count($result));
|
|
|
|
|
|
|
|
foreach ($expected as $key => $value) {
|
|
|
|
$this->assertArrayHasKey($key, $result);
|
|
|
|
$this->assertSame($value, $result[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-15 19:49:50 +04:00
|
|
|
}
|