summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2014-04-29 12:15:11 +0200
committerVincent Petry <pvince81@owncloud.com>2014-04-29 12:15:11 +0200
commit6b02126dc1c8ba9de5b87c6c8568feea5948d2a0 (patch)
tree90f8d6372232858ee0bba4b00275a6303677a4fc
parentb95aa43a5d1dac3e1ca8f3c55e050861d7a4938b (diff)
parent73a2d87ab4db9058c1e34539abe023c85a461584 (diff)
downloadnextcloud-server-6b02126dc1c8ba9de5b87c6c8568feea5948d2a0.tar.gz
nextcloud-server-6b02126dc1c8ba9de5b87c6c8568feea5948d2a0.zip
Merge pull request #8382 from owncloud/enc_fix_decrypt_all
fix shouldEncrypt() method and improved decryptAll() unit tests
-rw-r--r--apps/files_encryption/lib/proxy.php3
-rwxr-xr-xapps/files_encryption/tests/util.php59
2 files changed, 52 insertions, 10 deletions
diff --git a/apps/files_encryption/lib/proxy.php b/apps/files_encryption/lib/proxy.php
index ae2d8d63e23..03ddc795eae 100644
--- a/apps/files_encryption/lib/proxy.php
+++ b/apps/files_encryption/lib/proxy.php
@@ -53,10 +53,11 @@ class Proxy extends \OC_FileProxy {
private static function shouldEncrypt($path, $mode = 'w') {
$userId = Helper::getUser($path);
+ $session = new Session(new \OC\Files\View());
// don't call the crypt stream wrapper, if...
if (
- \OCP\App::isEnabled('files_encryption') === false // encryption is disabled
+ $session->getInitialized() !== Session::INIT_SUCCESSFUL // encryption successful initialized
|| Crypt::mode() !== 'server' // we are not in server-side-encryption mode
|| strpos($path, '/' . $userId . '/files') !== 0 // path is not in files/
|| substr($path, 0, 8) === 'crypt://' // we are already in crypt mode
diff --git a/apps/files_encryption/tests/util.php b/apps/files_encryption/tests/util.php
index a29ef831a52..88ded7ec40a 100755
--- a/apps/files_encryption/tests/util.php
+++ b/apps/files_encryption/tests/util.php
@@ -349,10 +349,12 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
$this->view->unlink($this->userId . '/files/' . $filename);
}
-
function testDecryptAll() {
$filename = "/decryptAll" . uniqid() . ".txt";
+ $datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data/');
+ $userdir = $datadir . '/' . $this->userId . '/files/';
+
$util = new Encryption\Util($this->view, $this->userId);
$this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort);
@@ -362,13 +364,47 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
$this->assertTrue($fileInfoEncrypted instanceof \OC\Files\FileInfo);
$this->assertEquals($fileInfoEncrypted['encrypted'], 1);
- // decrypt all encrypted files
- $result = $util->decryptAll('/' . $this->userId . '/' . 'files');
+ $encContent = file_get_contents($userdir . $filename);
- $this->assertTrue($result);
+ \OC_App::disable('files_encryption');
- $fileInfoUnencrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
+ $user = \OCP\User::getUser();
+ $this->logoutHelper();
+ $this->loginHelper($user, false, false, false);
+
+ $content = file_get_contents($userdir . $filename);
+
+ //content should be encrypted
+ $this->assertSame($encContent, $content);
+
+ // now we load the encryption app again
+ OC_App::loadApp('files_encryption');
+
+ // init encryption app
+ $params = array('uid' => \OCP\User::getUser(),
+ 'password' => \OCP\User::getUser());
+
+ $view = new OC_FilesystemView('/');
+ $util = new \OCA\Encryption\Util($view, \OCP\User::getUser());
+ $result = $util->initEncryption($params);
+
+ $this->assertTrue($result instanceof \OCA\Encryption\Session);
+
+ $successful = $util->decryptAll();
+
+ $this->assertTrue($successful);
+
+ $this->logoutHelper();
+ $this->loginHelper($user, false, false, false);
+
+ // file should be unencrypted and fileInfo should contain the correct values
+ $content = file_get_contents($userdir . $filename);
+
+ // now we should get the plain data
+ $this->assertSame($this->dataShort, $content);
+
+ $fileInfoUnencrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
$this->assertTrue($fileInfoUnencrypted instanceof \OC\Files\FileInfo);
// check if mtime and etags unchanged
@@ -377,10 +413,13 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
// file should no longer be encrypted
$this->assertEquals(0, $fileInfoUnencrypted['encrypted']);
+ // cleanup
$this->view->unlink($this->userId . '/files/' . $filename);
+ OC_App::enable('files_encryption');
}
+
function testDescryptAllWithBrokenFiles() {
$file1 = "/decryptAll1" . uniqid() . ".txt";
@@ -508,7 +547,7 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
* @param bool $create
* @param bool $password
*/
- public static function loginHelper($user, $create = false, $password = false) {
+ public static function loginHelper($user, $create = false, $password = false, $loadEncryption = true) {
if ($create) {
try {
\OC_User::createUser($user, $user);
@@ -527,9 +566,11 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
\OC_User::setUserId($user);
\OC_Util::setupFS($user);
- $params['uid'] = $user;
- $params['password'] = $password;
- OCA\Encryption\Hooks::login($params);
+ if ($loadEncryption) {
+ $params['uid'] = $user;
+ $params['password'] = $password;
+ OCA\Encryption\Hooks::login($params);
+ }
}
public static function logoutHelper() {