commit
753af3a3ff
@ -0,0 +1,271 @@ |
||||
<?php |
||||
/** |
||||
* ownCloud |
||||
* |
||||
* @author Bjoern Schiessle |
||||
* @copyright 2014 Bjoern Schiessle <schiessle@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/>. |
||||
* |
||||
*/ |
||||
|
||||
require_once __DIR__ . '/../../../lib/base.php'; |
||||
require_once __DIR__ . '/../lib/crypt.php'; |
||||
require_once __DIR__ . '/../lib/keymanager.php'; |
||||
require_once __DIR__ . '/../lib/stream.php'; |
||||
require_once __DIR__ . '/../lib/util.php'; |
||||
require_once __DIR__ . '/../appinfo/app.php'; |
||||
require_once __DIR__ . '/util.php'; |
||||
|
||||
use OCA\Encryption; |
||||
|
||||
/** |
||||
* Class Test_Encryption_Hooks |
||||
* @brief this class provide basic hook app tests |
||||
*/ |
||||
class Test_Encryption_Hooks extends \PHPUnit_Framework_TestCase { |
||||
|
||||
const TEST_ENCRYPTION_HOOKS_USER1 = "test-encryption-hooks-user1"; |
||||
const TEST_ENCRYPTION_HOOKS_USER2 = "test-encryption-hooks-user2"; |
||||
|
||||
/** |
||||
* @var \OC_FilesystemView |
||||
*/ |
||||
public $user1View; // view on /data/user1/files |
||||
public $user2View; // view on /data/user2/files |
||||
public $rootView; // view on /data/user |
||||
public $data; |
||||
public $filename; |
||||
|
||||
public static function setUpBeforeClass() { |
||||
// reset backend |
||||
\OC_User::clearBackends(); |
||||
\OC_User::useBackend('database'); |
||||
|
||||
\OC_Hook::clear('OC_Filesystem'); |
||||
\OC_Hook::clear('OC_User'); |
||||
|
||||
// clear share hooks |
||||
\OC_Hook::clear('OCP\\Share'); |
||||
\OC::registerShareHooks(); |
||||
\OCP\Util::connectHook('OC_Filesystem', 'setup', '\OC\Files\Storage\Shared', 'setup'); |
||||
|
||||
// Filesystem related hooks |
||||
\OCA\Encryption\Helper::registerFilesystemHooks(); |
||||
|
||||
// Sharing related hooks |
||||
\OCA\Encryption\Helper::registerShareHooks(); |
||||
|
||||
// clear and register proxies |
||||
\OC_FileProxy::clearProxies(); |
||||
\OC_FileProxy::register(new OCA\Encryption\Proxy()); |
||||
|
||||
// create test user |
||||
\Test_Encryption_Util::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1, true); |
||||
\Test_Encryption_Util::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2, true); |
||||
} |
||||
|
||||
function setUp() { |
||||
// set user id |
||||
\Test_Encryption_Util::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1); |
||||
\OC_User::setUserId(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1); |
||||
|
||||
// init filesystem view |
||||
$this->user1View = new \OC_FilesystemView('/'. \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1 . '/files'); |
||||
$this->user2View = new \OC_FilesystemView('/'. \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '/files'); |
||||
$this->rootView = new \OC_FilesystemView('/'); |
||||
|
||||
// init short data |
||||
$this->data = 'hats'; |
||||
$this->filename = 'enc_hooks_tests-' . uniqid() . '.txt'; |
||||
|
||||
} |
||||
|
||||
public static function tearDownAfterClass() { |
||||
// cleanup test user |
||||
\OC_User::deleteUser(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1); |
||||
\OC_User::deleteUser(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2); |
||||
} |
||||
|
||||
function testDeleteHooks() { |
||||
|
||||
// remember files_trashbin state |
||||
$stateFilesTrashbin = OC_App::isEnabled('files_trashbin'); |
||||
|
||||
// we want to tests with app files_trashbin disabled |
||||
\OC_App::disable('files_trashbin'); |
||||
|
||||
// make sure that the trash bin is disabled |
||||
$this->assertFalse(\OC_APP::isEnabled('files_trashbin')); |
||||
|
||||
$this->user1View->file_put_contents($this->filename, $this->data); |
||||
|
||||
// check if all keys are generated |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/share-keys/' |
||||
. $this->filename . '.' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keyfiles/' . $this->filename . '.key')); |
||||
|
||||
|
||||
\Test_Encryption_Util::logoutHelper(); |
||||
\Test_Encryption_Util::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2); |
||||
\OC_User::setUserId(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2); |
||||
|
||||
|
||||
$this->user2View->file_put_contents($this->filename, $this->data); |
||||
|
||||
// check if all keys are generated |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/share-keys/' |
||||
. $this->filename . '.' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keyfiles/' . $this->filename . '.key')); |
||||
|
||||
|
||||
// create a dummy file that we can delete something outside of data/user/files |
||||
// in this case no share or file keys should be deleted |
||||
$this->rootView->file_put_contents(self::TEST_ENCRYPTION_HOOKS_USER2 . "/" . $this->filename, $this->data); |
||||
|
||||
// delete dummy file outside of data/user/files |
||||
$this->rootView->unlink(self::TEST_ENCRYPTION_HOOKS_USER2 . "/" . $this->filename); |
||||
|
||||
// all keys should still exist |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/share-keys/' |
||||
. $this->filename . '.' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keyfiles/' . $this->filename . '.key')); |
||||
|
||||
|
||||
// delete the file in data/user/files |
||||
// now the correspondig share and file keys from user2 should be deleted |
||||
$this->user2View->unlink($this->filename); |
||||
|
||||
// check if keys from user2 are really deleted |
||||
$this->assertFalse($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/share-keys/' |
||||
. $this->filename . '.' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); |
||||
$this->assertFalse($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keyfiles/' . $this->filename . '.key')); |
||||
|
||||
// but user1 keys should still exist |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/share-keys/' |
||||
. $this->filename . '.' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keyfiles/' . $this->filename . '.key')); |
||||
|
||||
if ($stateFilesTrashbin) { |
||||
OC_App::enable('files_trashbin'); |
||||
} |
||||
else { |
||||
OC_App::disable('files_trashbin'); |
||||
} |
||||
} |
||||
|
||||
function testDeleteHooksForSharedFiles() { |
||||
|
||||
\Test_Encryption_Util::logoutHelper(); |
||||
\Test_Encryption_Util::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1); |
||||
\OC_User::setUserId(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1); |
||||
|
||||
// remember files_trashbin state |
||||
$stateFilesTrashbin = OC_App::isEnabled('files_trashbin'); |
||||
|
||||
// we want to tests with app files_trashbin disabled |
||||
\OC_App::disable('files_trashbin'); |
||||
|
||||
// make sure that the trash bin is disabled |
||||
$this->assertFalse(\OC_APP::isEnabled('files_trashbin')); |
||||
|
||||
$this->user1View->file_put_contents($this->filename, $this->data); |
||||
|
||||
// check if all keys are generated |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/share-keys/' |
||||
. $this->filename . '.' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keyfiles/' . $this->filename . '.key')); |
||||
|
||||
// get the file info from previous created file |
||||
$fileInfo = $this->user1View->getFileInfo($this->filename); |
||||
|
||||
// check if we have a valid file info |
||||
$this->assertTrue(is_array($fileInfo)); |
||||
|
||||
// share the file with user2 |
||||
\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_HOOKS_USER2, OCP\PERMISSION_ALL); |
||||
|
||||
// check if new share key exists |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/share-keys/' |
||||
. $this->filename . '.' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); |
||||
|
||||
\Test_Encryption_Util::logoutHelper(); |
||||
\Test_Encryption_Util::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2); |
||||
\OC_User::setUserId(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2); |
||||
|
||||
// user2 has a local file with the same name |
||||
$this->user2View->file_put_contents($this->filename, $this->data); |
||||
|
||||
// check if all keys are generated |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/share-keys/' |
||||
. $this->filename . '.' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keyfiles/' . $this->filename . '.key')); |
||||
|
||||
// delete the Shared file from user1 in data/user2/files/Shared |
||||
$this->user2View->unlink('/Shared/' . $this->filename); |
||||
|
||||
// now keys from user1s home should be gone |
||||
$this->assertFalse($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/share-keys/' |
||||
. $this->filename . '.' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey')); |
||||
$this->assertFalse($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/share-keys/' |
||||
. $this->filename . '.' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); |
||||
$this->assertFalse($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keyfiles/' . $this->filename . '.key')); |
||||
|
||||
// but user2 keys should still exist |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/share-keys/' |
||||
. $this->filename . '.' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey')); |
||||
$this->assertTrue($this->rootView->file_exists( |
||||
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keyfiles/' . $this->filename . '.key')); |
||||
|
||||
// cleanup |
||||
|
||||
$this->user2View->unlink($this->filename); |
||||
|
||||
\Test_Encryption_Util::logoutHelper(); |
||||
\Test_Encryption_Util::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1); |
||||
\OC_User::setUserId(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1); |
||||
|
||||
// unshare the file |
||||
\OCP\Share::unshare('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_HOOKS_USER2); |
||||
|
||||
$this->user1View->unlink($this->filename); |
||||
|
||||
if ($stateFilesTrashbin) { |
||||
OC_App::enable('files_trashbin'); |
||||
} |
||||
else { |
||||
OC_App::disable('files_trashbin'); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,134 @@ |
||||
<?php |
||||
/** |
||||
* ownCloud |
||||
* |
||||
* @author Vincent Petry |
||||
* @copyright 2014 Vincent Petry <pvince81@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/>. |
||||
* |
||||
*/ |
||||
require_once __DIR__ . '/base.php'; |
||||
|
||||
class Test_Files_Sharing_Cache extends Test_Files_Sharing_Base { |
||||
|
||||
function setUp() { |
||||
parent::setUp(); |
||||
|
||||
self::loginHelper(self::TEST_FILES_SHARING_API_USER1); |
||||
|
||||
// prepare user1's dir structure |
||||
$textData = "dummy file data\n"; |
||||
$this->view->mkdir('container'); |
||||
$this->view->mkdir('container/shareddir'); |
||||
$this->view->mkdir('container/shareddir/subdir'); |
||||
$this->view->mkdir('container/shareddir/emptydir'); |
||||
|
||||
$textData = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
||||
$this->view->file_put_contents('container/not shared.txt', $textData); |
||||
$this->view->file_put_contents('container/shared single file.txt', $textData); |
||||
$this->view->file_put_contents('container/shareddir/bar.txt', $textData); |
||||
$this->view->file_put_contents('container/shareddir/subdir/another.txt', $textData); |
||||
$this->view->file_put_contents('container/shareddir/subdir/another too.txt', $textData); |
||||
$this->view->file_put_contents('container/shareddir/subdir/not a text file.xml', '<xml></xml>'); |
||||
|
||||
list($this->ownerStorage, $internalPath) = $this->view->resolvePath(''); |
||||
$this->ownerCache = $this->ownerStorage->getCache(); |
||||
$this->ownerStorage->getScanner()->scan(''); |
||||
|
||||
// share "shareddir" with user2 |
||||
$fileinfo = $this->view->getFileInfo('container/shareddir'); |
||||
\OCP\Share::shareItem('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, |
||||
self::TEST_FILES_SHARING_API_USER2, 31); |
||||
|
||||
$fileinfo = $this->view->getFileInfo('container/shared single file.txt'); |
||||
\OCP\Share::shareItem('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, |
||||
self::TEST_FILES_SHARING_API_USER2, 31); |
||||
|
||||
// login as user2 |
||||
self::loginHelper(self::TEST_FILES_SHARING_API_USER2); |
||||
|
||||
// retrieve the shared storage |
||||
$secondView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2); |
||||
list($this->sharedStorage, $internalPath) = $secondView->resolvePath('files/Shared/shareddir'); |
||||
$this->sharedCache = $this->sharedStorage->getCache(); |
||||
} |
||||
|
||||
function tearDown() { |
||||
$this->sharedCache->clear(); |
||||
|
||||
self::loginHelper(self::TEST_FILES_SHARING_API_USER1); |
||||
|
||||
$fileinfo = $this->view->getFileInfo('container/shareddir'); |
||||
\OCP\Share::unshare('folder', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, |
||||
self::TEST_FILES_SHARING_API_USER2); |
||||
|
||||
$fileinfo = $this->view->getFileInfo('container/shared single file.txt'); |
||||
\OCP\Share::unshare('file', $fileinfo['fileid'], \OCP\Share::SHARE_TYPE_USER, |
||||
self::TEST_FILES_SHARING_API_USER2); |
||||
|
||||
$this->view->deleteAll('container'); |
||||
|
||||
$this->ownerCache->clear(); |
||||
|
||||
parent::tearDown(); |
||||
} |
||||
|
||||
/** |
||||
* Test searching by mime type |
||||
*/ |
||||
function testSearchByMime() { |
||||
$results = $this->sharedStorage->getCache()->searchByMime('text'); |
||||
$check = array( |
||||
array( |
||||
'name' => 'shared single file.txt', |
||||
'path' => 'shared single file.txt' |
||||
), |
||||
array( |
||||
'name' => 'bar.txt', |
||||
'path' => 'shareddir/bar.txt' |
||||
), |
||||
array( |
||||
'name' => 'another too.txt', |
||||
'path' => 'shareddir/subdir/another too.txt' |
||||
), |
||||
array( |
||||
'name' => 'another.txt', |
||||
'path' => 'shareddir/subdir/another.txt' |
||||
), |
||||
); |
||||
$this->verifyFiles($check, $results); |
||||
|
||||
$results2 = $this->sharedStorage->getCache()->searchByMime('text/plain'); |
||||
|
||||
$this->verifyFiles($check, $results); |
||||
} |
||||
|
||||
/** |
||||
* Checks that all provided attributes exist in the files list, |
||||
* only the values provided in $examples will be used to check against |
||||
* the file list. The files order also needs to be the same. |
||||
* |
||||
* @param array $examples array of example files |
||||
* @param array $files array of files |
||||
*/ |
||||
private function verifyFiles($examples, $files) { |
||||
$this->assertEquals(count($examples), count($files)); |
||||
foreach ($files as $i => $file) { |
||||
foreach ($examples[$i] as $key => $value) { |
||||
$this->assertEquals($value, $file[$key]); |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue