2014-02-01 02:37:21 +04:00
|
|
|
<?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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
use OCA\Encryption;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Test_Encryption_Hooks
|
2014-05-19 19:50:53 +04:00
|
|
|
* this class provide basic hook app tests
|
2014-02-01 02:37:21 +04:00
|
|
|
*/
|
2014-11-06 18:53:35 +03:00
|
|
|
class Test_Encryption_Hooks extends \OCA\Files_Encryption\Tests\TestCase {
|
2014-02-01 02:37:21 +04:00
|
|
|
|
2014-09-17 20:50:29 +04:00
|
|
|
const TEST_ENCRYPTION_HOOKS_USER1 = "test-encryption-hooks-user1.dot";
|
|
|
|
const TEST_ENCRYPTION_HOOKS_USER2 = "test-encryption-hooks-user2.dot";
|
2014-02-01 02:37:21 +04:00
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
/** @var \OC\Files\View */
|
2014-02-01 02:37:21 +04:00
|
|
|
public $user1View; // view on /data/user1/files
|
2014-11-06 18:53:35 +03:00
|
|
|
/** @var \OC\Files\View */
|
2014-02-01 02:37:21 +04:00
|
|
|
public $user2View; // view on /data/user2/files
|
2014-11-06 18:53:35 +03:00
|
|
|
/** @var \OC\Files\View */
|
2014-02-01 02:37:21 +04:00
|
|
|
public $rootView; // view on /data/user
|
|
|
|
public $data;
|
|
|
|
public $filename;
|
2014-02-24 19:33:57 +04:00
|
|
|
public $folder;
|
2014-02-01 02:37:21 +04:00
|
|
|
|
2014-09-17 20:50:29 +04:00
|
|
|
private static $testFiles;
|
|
|
|
|
2014-02-01 02:37:21 +04:00
|
|
|
public static function setUpBeforeClass() {
|
2014-11-06 18:53:35 +03:00
|
|
|
parent::setUpBeforeClass();
|
|
|
|
|
2014-09-17 20:50:29 +04:00
|
|
|
// note: not using a data provider because these
|
|
|
|
// files all need to coexist to make sure the
|
|
|
|
// share keys are found properly (pattern matching)
|
|
|
|
self::$testFiles = array(
|
|
|
|
't est.txt',
|
|
|
|
't est_.txt',
|
|
|
|
't est.doc.txt',
|
|
|
|
't est(.*).txt', // make sure the regexp is escaped
|
|
|
|
'multiple.dots.can.happen.too.txt',
|
|
|
|
't est.' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.txt',
|
|
|
|
't est_.' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey.txt',
|
|
|
|
'who would upload their.shareKey',
|
|
|
|
'user ones file.txt',
|
|
|
|
'user ones file.txt.backup',
|
|
|
|
'.t est.txt'
|
|
|
|
);
|
|
|
|
|
2014-02-01 02:37:21 +04:00
|
|
|
// create test user
|
2014-11-06 18:53:35 +03:00
|
|
|
self::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1, true);
|
|
|
|
self::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2, true);
|
2014-02-01 02:37:21 +04:00
|
|
|
}
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2014-02-01 02:37:21 +04:00
|
|
|
// set user id
|
2014-11-06 18:53:35 +03:00
|
|
|
self::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1);
|
2014-02-01 02:37:21 +04:00
|
|
|
\OC_User::setUserId(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1);
|
|
|
|
|
|
|
|
// init filesystem view
|
2014-05-12 18:30:39 +04:00
|
|
|
$this->user1View = new \OC\Files\View('/'. \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1 . '/files');
|
|
|
|
$this->user2View = new \OC\Files\View('/'. \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '/files');
|
|
|
|
$this->rootView = new \OC\Files\View('/');
|
2014-02-01 02:37:21 +04:00
|
|
|
|
|
|
|
// init short data
|
|
|
|
$this->data = 'hats';
|
2014-11-06 18:53:35 +03:00
|
|
|
$this->filename = 'enc_hooks_tests-' . $this->getUniqueID() . '.txt';
|
|
|
|
$this->folder = 'enc_hooks_tests_folder-' . $this->getUniqueID();
|
2014-02-01 02:37:21 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2014-10-10 17:34:19 +04:00
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
parent::tearDownAfterClass();
|
2014-02-01 02:37:21 +04:00
|
|
|
}
|
|
|
|
|
2014-05-06 21:20:49 +04:00
|
|
|
function testDisableHook() {
|
|
|
|
// encryption is enabled and running so we should have some user specific
|
|
|
|
// settings in oc_preferences
|
|
|
|
$query = \OC_DB::prepare('SELECT * FROM `*PREFIX*preferences` WHERE `appid` = ?');
|
|
|
|
$result = $query->execute(array('files_encryption'));
|
|
|
|
$row = $result->fetchRow();
|
|
|
|
$this->assertTrue(is_array($row));
|
|
|
|
|
|
|
|
// disabling the app should delete all user specific settings
|
|
|
|
\OCA\Encryption\Hooks::preDisable(array('app' => 'files_encryption'));
|
|
|
|
|
|
|
|
// check if user specific settings for the encryption app are really gone
|
|
|
|
$query = \OC_DB::prepare('SELECT * FROM `*PREFIX*preferences` WHERE `appid` = ?');
|
|
|
|
$result = $query->execute(array('files_encryption'));
|
|
|
|
$row = $result->fetchRow();
|
|
|
|
$this->assertFalse($row);
|
|
|
|
|
|
|
|
// relogin user to initialize the encryption again
|
|
|
|
$user = \OCP\User::getUser();
|
2014-11-06 18:53:35 +03:00
|
|
|
self::loginHelper($user);
|
2014-05-06 21:20:49 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-01 02:37:21 +04:00
|
|
|
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(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $this->filename . '/' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->filename . '/fileKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
self::logoutHelper();
|
|
|
|
self::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2);
|
2014-02-01 02:37:21 +04:00
|
|
|
\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(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/'
|
|
|
|
. $this->filename . '/' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/' . $this->filename . '/fileKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
|
|
|
|
|
|
|
|
// 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(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/'
|
|
|
|
. $this->filename . '/' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/' . $this->filename . '/fileKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
|
|
|
|
|
|
|
|
// 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(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/'
|
|
|
|
. $this->filename . '/' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
$this->assertFalse($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/' . $this->filename . '/fileKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
|
|
|
|
// but user1 keys should still exist
|
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $this->filename . '/' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->filename . '/fileKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
|
|
|
|
if ($stateFilesTrashbin) {
|
|
|
|
OC_App::enable('files_trashbin');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
OC_App::disable('files_trashbin');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDeleteHooksForSharedFiles() {
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
self::logoutHelper();
|
|
|
|
self::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1);
|
2014-02-01 02:37:21 +04:00
|
|
|
\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(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $this->filename . '/' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->filename . '/fileKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
|
|
|
|
// get the file info from previous created file
|
|
|
|
$fileInfo = $this->user1View->getFileInfo($this->filename);
|
|
|
|
|
|
|
|
// check if we have a valid file info
|
2014-02-14 21:05:59 +04:00
|
|
|
$this->assertTrue($fileInfo instanceof \OC\Files\FileInfo);
|
2014-02-01 02:37:21 +04:00
|
|
|
|
|
|
|
// share the file with user2
|
2014-11-25 18:28:41 +03:00
|
|
|
\OCP\Share::shareItem('file', $fileInfo['fileid'], \OCP\Share::SHARE_TYPE_USER, self::TEST_ENCRYPTION_HOOKS_USER2, \OCP\Constants::PERMISSION_ALL);
|
2014-02-01 02:37:21 +04:00
|
|
|
|
|
|
|
// check if new share key exists
|
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $this->filename . '/' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
self::logoutHelper();
|
|
|
|
self::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2);
|
2014-02-01 02:37:21 +04:00
|
|
|
\OC_User::setUserId(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2);
|
|
|
|
|
2014-04-09 19:52:24 +04:00
|
|
|
// user2 update the shared file
|
2014-02-01 02:37:21 +04:00
|
|
|
$this->user2View->file_put_contents($this->filename, $this->data);
|
|
|
|
|
2014-04-09 19:52:24 +04:00
|
|
|
// keys should be stored at user1s dir, not in user2s
|
|
|
|
$this->assertFalse($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/'
|
|
|
|
. $this->filename . '/' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey'));
|
2014-04-09 19:52:24 +04:00
|
|
|
$this->assertFalse($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER2 . '/files_encryption/keys/' . $this->filename . '/fileKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
|
|
|
|
// delete the Shared file from user1 in data/user2/files/Shared
|
2014-04-09 19:52:24 +04:00
|
|
|
$result = $this->user2View->unlink($this->filename);
|
|
|
|
|
|
|
|
$this->assertTrue($result);
|
2014-02-01 02:37:21 +04:00
|
|
|
|
2014-05-27 17:09:43 +04:00
|
|
|
// share key for user2 from user1s home should be gone, all other keys should still exists
|
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $this->filename . '/' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
$this->assertFalse($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $this->filename . '/' . \Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER2 . '.shareKey'));
|
2014-05-27 17:09:43 +04:00
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->filename . '/fileKey'));
|
2014-02-01 02:37:21 +04:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
self::logoutHelper();
|
|
|
|
self::loginHelper(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1);
|
2014-02-01 02:37:21 +04:00
|
|
|
\OC_User::setUserId(\Test_Encryption_Hooks::TEST_ENCRYPTION_HOOKS_USER1);
|
|
|
|
|
|
|
|
if ($stateFilesTrashbin) {
|
|
|
|
OC_App::enable('files_trashbin');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
OC_App::disable('files_trashbin');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-24 19:33:57 +04:00
|
|
|
function testRenameHook() {
|
2014-09-17 20:50:29 +04:00
|
|
|
// create all files to make sure all keys can coexist properly
|
|
|
|
foreach (self::$testFiles as $file) {
|
|
|
|
// save file with content
|
|
|
|
$cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $file, $this->data);
|
2014-02-24 19:33:57 +04:00
|
|
|
|
2014-09-17 20:50:29 +04:00
|
|
|
// test that data was successfully written
|
|
|
|
$this->assertTrue(is_int($cryptedFile));
|
|
|
|
}
|
2014-02-24 19:33:57 +04:00
|
|
|
|
2014-09-17 20:50:29 +04:00
|
|
|
foreach (self::$testFiles as $file) {
|
|
|
|
$this->doTestRenameHook($file);
|
|
|
|
}
|
|
|
|
}
|
2014-02-24 19:33:57 +04:00
|
|
|
|
2014-09-17 20:50:29 +04:00
|
|
|
/**
|
|
|
|
* test rename operation
|
|
|
|
*/
|
|
|
|
function doTestRenameHook($filename) {
|
2014-02-24 19:33:57 +04:00
|
|
|
// check if keys exists
|
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey'));
|
2014-02-24 19:33:57 +04:00
|
|
|
|
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $filename . '/fileKey'));
|
2014-02-24 19:33:57 +04:00
|
|
|
|
2014-06-11 19:21:08 +04:00
|
|
|
// make subfolder and sub-subfolder
|
2014-02-24 19:33:57 +04:00
|
|
|
$this->rootView->mkdir('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder);
|
2014-06-11 19:21:08 +04:00
|
|
|
$this->rootView->mkdir('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder . '/' . $this->folder);
|
2014-02-24 19:33:57 +04:00
|
|
|
|
2014-06-11 19:21:08 +04:00
|
|
|
$this->assertTrue($this->rootView->is_dir('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder . '/' . $this->folder));
|
2014-02-24 19:33:57 +04:00
|
|
|
|
2014-06-23 19:13:56 +04:00
|
|
|
// move the file to the sub-subfolder
|
2014-02-24 19:33:57 +04:00
|
|
|
$root = $this->rootView->getRoot();
|
|
|
|
$this->rootView->chroot('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/');
|
2014-09-17 20:50:29 +04:00
|
|
|
$this->rootView->rename($filename, '/' . $this->folder . '/' . $this->folder . '/' . $filename);
|
2014-02-24 19:33:57 +04:00
|
|
|
$this->rootView->chroot($root);
|
|
|
|
|
2014-09-17 20:50:29 +04:00
|
|
|
$this->assertFalse($this->rootView->file_exists('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $filename));
|
|
|
|
$this->assertTrue($this->rootView->file_exists('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder . '/' . $this->folder . '/' . $filename));
|
2014-02-24 19:33:57 +04:00
|
|
|
|
|
|
|
// keys should be renamed too
|
|
|
|
$this->assertFalse($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey'));
|
2014-02-24 19:33:57 +04:00
|
|
|
$this->assertFalse($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $filename . '/fileKey'));
|
2014-02-24 19:33:57 +04:00
|
|
|
|
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->folder . '/' . $this->folder . '/'
|
|
|
|
. $filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey'));
|
2014-02-24 19:33:57 +04:00
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->folder . '/' . $this->folder . '/'
|
|
|
|
. $filename . '/fileKey'));
|
2014-02-24 19:33:57 +04:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
$this->rootView->unlink('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder);
|
|
|
|
}
|
|
|
|
|
2014-07-24 18:49:38 +04:00
|
|
|
function testCopyHook() {
|
2014-09-17 20:50:29 +04:00
|
|
|
// create all files to make sure all keys can coexist properly
|
|
|
|
foreach (self::$testFiles as $file) {
|
|
|
|
// save file with content
|
|
|
|
$cryptedFile = file_put_contents('crypt:///' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $file, $this->data);
|
2014-07-24 18:49:38 +04:00
|
|
|
|
2014-09-17 20:50:29 +04:00
|
|
|
// test that data was successfully written
|
|
|
|
$this->assertTrue(is_int($cryptedFile));
|
|
|
|
}
|
2014-07-24 18:49:38 +04:00
|
|
|
|
2014-09-17 20:50:29 +04:00
|
|
|
foreach (self::$testFiles as $file) {
|
|
|
|
$this->doTestCopyHook($file);
|
|
|
|
}
|
|
|
|
}
|
2014-07-24 18:49:38 +04:00
|
|
|
|
2014-09-17 20:50:29 +04:00
|
|
|
/**
|
|
|
|
* test rename operation
|
|
|
|
*/
|
|
|
|
function doTestCopyHook($filename) {
|
2014-07-24 18:49:38 +04:00
|
|
|
// check if keys exists
|
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey'));
|
2014-07-24 18:49:38 +04:00
|
|
|
|
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $filename . '/fileKey'));
|
2014-07-24 18:49:38 +04:00
|
|
|
|
|
|
|
// make subfolder and sub-subfolder
|
|
|
|
$this->rootView->mkdir('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder);
|
|
|
|
$this->rootView->mkdir('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder . '/' . $this->folder);
|
|
|
|
|
|
|
|
$this->assertTrue($this->rootView->is_dir('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder . '/' . $this->folder));
|
|
|
|
|
|
|
|
// copy the file to the sub-subfolder
|
2014-09-17 20:50:29 +04:00
|
|
|
\OC\Files\Filesystem::copy($filename, '/' . $this->folder . '/' . $this->folder . '/' . $filename);
|
2014-07-24 18:49:38 +04:00
|
|
|
|
2014-09-17 20:50:29 +04:00
|
|
|
$this->assertTrue($this->rootView->file_exists('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $filename));
|
|
|
|
$this->assertTrue($this->rootView->file_exists('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder . '/' . $this->folder . '/' . $filename));
|
2014-07-24 18:49:38 +04:00
|
|
|
|
|
|
|
// keys should be copied too
|
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey'));
|
2014-07-24 18:49:38 +04:00
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/'
|
|
|
|
. $filename . '/fileKey'));
|
2014-07-24 18:49:38 +04:00
|
|
|
|
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->folder . '/' . $this->folder . '/'
|
|
|
|
. $filename . '/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '.shareKey'));
|
2014-07-24 18:49:38 +04:00
|
|
|
$this->assertTrue($this->rootView->file_exists(
|
2014-11-10 14:40:24 +03:00
|
|
|
'/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files_encryption/keys/' . $this->folder . '/' . $this->folder . '/'
|
|
|
|
. $filename . '/fileKey'));
|
2014-07-24 18:49:38 +04:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
$this->rootView->unlink('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder);
|
2014-09-17 20:50:29 +04:00
|
|
|
$this->rootView->unlink('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $filename);
|
2014-07-24 18:49:38 +04:00
|
|
|
}
|
|
|
|
|
2014-05-19 17:08:02 +04:00
|
|
|
/**
|
|
|
|
* @brief replacing encryption keys during password change should be allowed
|
|
|
|
* until the user logged in for the first time
|
|
|
|
*/
|
|
|
|
public function testSetPassphrase() {
|
|
|
|
|
|
|
|
$view = new \OC\Files\View();
|
|
|
|
|
|
|
|
// set user password for the first time
|
|
|
|
\OCA\Encryption\Hooks::postCreateUser(array('uid' => 'newUser', 'password' => 'newUserPassword'));
|
|
|
|
|
2014-11-18 19:25:36 +03:00
|
|
|
$this->assertTrue($view->file_exists(\OCA\Encryption\Keymanager::getPublicKeyPath() . '/newUser.publicKey'));
|
2014-11-14 19:30:38 +03:00
|
|
|
$this->assertTrue($view->file_exists('newUser/files_encryption/newUser.privateKey'));
|
2014-05-19 17:08:02 +04:00
|
|
|
|
|
|
|
// check if we are able to decrypt the private key
|
|
|
|
$encryptedKey = \OCA\Encryption\Keymanager::getPrivateKey($view, 'newUser');
|
|
|
|
$privateKey = \OCA\Encryption\Crypt::decryptPrivateKey($encryptedKey, 'newUserPassword');
|
|
|
|
$this->assertTrue(is_string($privateKey));
|
|
|
|
|
|
|
|
// change the password before the user logged-in for the first time,
|
|
|
|
// we can replace the encryption keys
|
|
|
|
\OCA\Encryption\Hooks::setPassphrase(array('uid' => 'newUser', 'password' => 'passwordChanged'));
|
|
|
|
|
|
|
|
$encryptedKey = \OCA\Encryption\Keymanager::getPrivateKey($view, 'newUser');
|
|
|
|
$privateKey = \OCA\Encryption\Crypt::decryptPrivateKey($encryptedKey, 'passwordChanged');
|
|
|
|
$this->assertTrue(is_string($privateKey));
|
|
|
|
|
|
|
|
// now create a files folder to simulate a already used account
|
|
|
|
$view->mkdir('/newUser/files');
|
|
|
|
|
|
|
|
// change the password after the user logged in, now the password should not change
|
|
|
|
\OCA\Encryption\Hooks::setPassphrase(array('uid' => 'newUser', 'password' => 'passwordChanged2'));
|
|
|
|
|
|
|
|
$encryptedKey = \OCA\Encryption\Keymanager::getPrivateKey($view, 'newUser');
|
|
|
|
$privateKey = \OCA\Encryption\Crypt::decryptPrivateKey($encryptedKey, 'passwordChanged2');
|
|
|
|
$this->assertFalse($privateKey);
|
|
|
|
|
|
|
|
$privateKey = \OCA\Encryption\Crypt::decryptPrivateKey($encryptedKey, 'passwordChanged');
|
|
|
|
$this->assertTrue(is_string($privateKey));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-02-01 02:37:21 +04:00
|
|
|
}
|