2012-07-25 15:38:40 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Sam Tuke <samtuke@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2014-12-03 18:52:44 +03:00
|
|
|
use OCA\Files_Encryption\Crypt;
|
|
|
|
use OCA\Files_Encryption\Keymanager;
|
|
|
|
use OCA\Files_Encryption\Util;
|
2012-11-14 20:39:35 +04:00
|
|
|
|
2013-05-20 00:28:48 +04:00
|
|
|
/**
|
|
|
|
* Class Test_Encryption_Util
|
|
|
|
*/
|
2014-11-06 18:53:35 +03:00
|
|
|
class Test_Encryption_Util extends \OCA\Files_Encryption\Tests\TestCase {
|
2013-05-26 22:44:15 +04:00
|
|
|
|
|
|
|
const TEST_ENCRYPTION_UTIL_USER1 = "test-util-user1";
|
2014-08-05 17:55:28 +04:00
|
|
|
const TEST_ENCRYPTION_UTIL_USER2 = "test-util-user2";
|
|
|
|
const TEST_ENCRYPTION_UTIL_GROUP1 = "test-util-group1";
|
|
|
|
const TEST_ENCRYPTION_UTIL_GROUP2 = "test-util-group2";
|
2013-05-26 22:44:15 +04:00
|
|
|
const TEST_ENCRYPTION_UTIL_LEGACY_USER = "test-legacy-user";
|
2013-05-20 00:28:48 +04:00
|
|
|
|
|
|
|
public $userId;
|
|
|
|
public $encryptionDir;
|
|
|
|
public $publicKeyDir;
|
|
|
|
public $pass;
|
|
|
|
/**
|
2014-05-12 18:30:39 +04:00
|
|
|
* @var OC\Files\View
|
2013-05-20 00:28:48 +04:00
|
|
|
*/
|
|
|
|
public $view;
|
|
|
|
public $keyfilesPath;
|
|
|
|
public $publicKeyPath;
|
|
|
|
public $privateKeyPath;
|
|
|
|
/**
|
2014-12-03 18:52:44 +03:00
|
|
|
* @var \OCA\Files_Encryption\Util
|
2013-05-20 00:28:48 +04:00
|
|
|
*/
|
|
|
|
public $util;
|
|
|
|
public $dataShort;
|
2013-05-20 23:19:28 +04:00
|
|
|
public $legacyEncryptedData;
|
|
|
|
public $legacyEncryptedDataKey;
|
2013-05-26 22:44:15 +04:00
|
|
|
public $legacyKey;
|
|
|
|
public $stateFilesTrashbin;
|
2013-05-20 00:28:48 +04:00
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
public static function setUpBeforeClass() {
|
2014-11-06 18:53:35 +03:00
|
|
|
parent::setUpBeforeClass();
|
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
// create test user
|
2014-11-06 18:53:35 +03:00
|
|
|
self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1, true);
|
|
|
|
self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER2, true);
|
|
|
|
self::loginHelper(self::TEST_ENCRYPTION_UTIL_LEGACY_USER, true);
|
2014-08-05 17:55:28 +04:00
|
|
|
|
|
|
|
// create groups
|
|
|
|
\OC_Group::createGroup(self::TEST_ENCRYPTION_UTIL_GROUP1);
|
|
|
|
\OC_Group::createGroup(self::TEST_ENCRYPTION_UTIL_GROUP2);
|
|
|
|
|
|
|
|
// add user 1 to group1
|
|
|
|
\OC_Group::addToGroup(self::TEST_ENCRYPTION_UTIL_USER1, self::TEST_ENCRYPTION_UTIL_GROUP1);
|
2013-05-26 22:44:15 +04:00
|
|
|
}
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
2013-05-26 22:44:15 +04:00
|
|
|
|
2014-02-10 20:23:54 +04:00
|
|
|
// login user
|
2014-11-06 18:53:35 +03:00
|
|
|
self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1);
|
|
|
|
\OC_User::setUserId(self::TEST_ENCRYPTION_UTIL_USER1);
|
|
|
|
$this->userId = self::TEST_ENCRYPTION_UTIL_USER1;
|
|
|
|
$this->pass = self::TEST_ENCRYPTION_UTIL_USER1;
|
2013-04-30 03:35:46 +04:00
|
|
|
|
2013-05-20 03:24:36 +04:00
|
|
|
// set content for encrypting / decrypting in tests
|
2013-08-21 12:59:31 +04:00
|
|
|
$this->dataUrl = __DIR__ . '/../lib/crypt.php';
|
2012-11-20 23:10:10 +04:00
|
|
|
$this->dataShort = 'hats';
|
2013-08-21 12:59:31 +04:00
|
|
|
$this->dataLong = file_get_contents(__DIR__ . '/../lib/crypt.php');
|
|
|
|
$this->legacyData = __DIR__ . '/legacy-text.txt';
|
|
|
|
$this->legacyEncryptedData = __DIR__ . '/legacy-encrypted-text.txt';
|
|
|
|
$this->legacyEncryptedDataKey = __DIR__ . '/encryption.key';
|
2013-05-31 03:55:48 +04:00
|
|
|
$this->legacyKey = "30943623843030686906\0\0\0\0";
|
2013-04-30 03:35:46 +04:00
|
|
|
|
2014-12-03 18:52:44 +03:00
|
|
|
$keypair = Crypt::createKeypair();
|
2013-05-20 03:24:36 +04:00
|
|
|
|
|
|
|
$this->genPublicKey = $keypair['publicKey'];
|
2012-11-20 23:10:10 +04:00
|
|
|
$this->genPrivateKey = $keypair['privateKey'];
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2014-12-03 18:52:44 +03:00
|
|
|
$this->publicKeyDir = Keymanager::getPublicKeyPath();
|
2013-05-20 03:24:36 +04:00
|
|
|
$this->encryptionDir = '/' . $this->userId . '/' . 'files_encryption';
|
2014-11-10 14:40:24 +03:00
|
|
|
$this->keysPath = $this->encryptionDir . '/' . 'keys';
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->publicKeyPath =
|
2014-11-14 19:30:38 +03:00
|
|
|
$this->publicKeyDir . '/' . $this->userId . '.publicKey'; // e.g. data/public-keys/admin.publicKey
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->privateKeyPath =
|
2014-11-14 19:30:38 +03:00
|
|
|
$this->encryptionDir . '/' . $this->userId . '.privateKey'; // e.g. data/admin/admin.privateKey
|
2013-05-15 04:38:08 +04:00
|
|
|
|
2014-05-12 18:30:39 +04:00
|
|
|
$this->view = new \OC\Files\View('/');
|
2013-05-15 04:38:08 +04:00
|
|
|
|
2014-12-03 18:52:44 +03:00
|
|
|
$this->util = new Util($this->view, $this->userId);
|
2013-04-30 03:35:46 +04:00
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
// remember files_trashbin state
|
|
|
|
$this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
|
2013-05-01 03:43:56 +04:00
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
// we don't want to tests with app files_trashbin enabled
|
|
|
|
\OC_App::disable('files_trashbin');
|
2012-07-25 15:38:40 +04:00
|
|
|
}
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
protected function tearDown() {
|
2013-05-26 22:44:15 +04:00
|
|
|
// reset app files_trashbin
|
|
|
|
if ($this->stateFilesTrashbin) {
|
|
|
|
OC_App::enable('files_trashbin');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
OC_App::disable('files_trashbin');
|
|
|
|
}
|
2014-11-06 18:53:35 +03:00
|
|
|
|
|
|
|
parent::tearDown();
|
2013-05-26 22:44:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function tearDownAfterClass() {
|
|
|
|
// cleanup test user
|
2014-11-06 18:53:35 +03:00
|
|
|
\OC_User::deleteUser(self::TEST_ENCRYPTION_UTIL_USER1);
|
|
|
|
\OC_User::deleteUser(self::TEST_ENCRYPTION_UTIL_USER2);
|
|
|
|
\OC_User::deleteUser(self::TEST_ENCRYPTION_UTIL_LEGACY_USER);
|
2014-10-10 17:34:19 +04:00
|
|
|
|
2014-08-05 17:55:28 +04:00
|
|
|
//cleanup groups
|
|
|
|
\OC_Group::deleteGroup(self::TEST_ENCRYPTION_UTIL_GROUP1);
|
|
|
|
\OC_Group::deleteGroup(self::TEST_ENCRYPTION_UTIL_GROUP2);
|
2014-10-10 17:34:19 +04:00
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
parent::tearDownAfterClass();
|
2012-11-16 22:30:00 +04:00
|
|
|
}
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2012-11-16 22:30:00 +04:00
|
|
|
/**
|
2013-06-10 11:31:22 +04:00
|
|
|
* @medium
|
2014-05-19 19:50:53 +04:00
|
|
|
* test that paths set during User construction are correct
|
2012-11-16 22:30:00 +04:00
|
|
|
*/
|
2013-05-26 05:22:16 +04:00
|
|
|
function testKeyPaths() {
|
2014-12-03 18:52:44 +03:00
|
|
|
$util = new Util($this->view, $this->userId);
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->assertEquals($this->publicKeyDir, $util->getPath('publicKeyDir'));
|
|
|
|
$this->assertEquals($this->encryptionDir, $util->getPath('encryptionDir'));
|
2014-11-10 14:40:24 +03:00
|
|
|
$this->assertEquals($this->keysPath, $util->getPath('keysPath'));
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->assertEquals($this->publicKeyPath, $util->getPath('publicKeyPath'));
|
|
|
|
$this->assertEquals($this->privateKeyPath, $util->getPath('privateKeyPath'));
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2012-11-16 22:30:00 +04:00
|
|
|
}
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2013-12-18 19:39:34 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
2014-05-19 19:50:53 +04:00
|
|
|
* test detection of encrypted files
|
2013-12-18 19:39:34 +04:00
|
|
|
*/
|
|
|
|
function testIsEncryptedPath() {
|
|
|
|
|
2014-12-03 18:52:44 +03:00
|
|
|
$util = new Util($this->view, $this->userId);
|
2013-12-18 19:39:34 +04:00
|
|
|
|
|
|
|
self::loginHelper($this->userId);
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
$unencryptedFile = '/tmpUnencrypted-' . $this->getUniqueID() . '.txt';
|
|
|
|
$encryptedFile = '/tmpEncrypted-' . $this->getUniqueID() . '.txt';
|
2013-12-18 19:39:34 +04:00
|
|
|
|
|
|
|
// Disable encryption proxy to write a unencrypted file
|
|
|
|
$proxyStatus = \OC_FileProxy::$enabled;
|
|
|
|
\OC_FileProxy::$enabled = false;
|
|
|
|
|
|
|
|
$this->view->file_put_contents($this->userId . '/files/' . $unencryptedFile, $this->dataShort);
|
|
|
|
|
|
|
|
// Re-enable proxy - our work is done
|
|
|
|
\OC_FileProxy::$enabled = $proxyStatus;
|
|
|
|
|
|
|
|
// write a encrypted file
|
|
|
|
$this->view->file_put_contents($this->userId . '/files/' . $encryptedFile, $this->dataShort);
|
|
|
|
|
|
|
|
// test if both files are detected correctly
|
|
|
|
$this->assertFalse($util->isEncryptedPath($this->userId . '/files/' . $unencryptedFile));
|
|
|
|
$this->assertTrue($util->isEncryptedPath($this->userId . '/files/' . $encryptedFile));
|
|
|
|
|
|
|
|
// cleanup
|
2014-06-18 15:26:42 +04:00
|
|
|
$this->view->unlink($this->userId . '/files/' . $unencryptedFile);
|
|
|
|
$this->view->unlink($this->userId . '/files/' . $encryptedFile);
|
2013-12-18 19:39:34 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-16 22:30:00 +04:00
|
|
|
/**
|
2013-06-10 11:31:22 +04:00
|
|
|
* @medium
|
2014-05-19 19:50:53 +04:00
|
|
|
* test setup of encryption directories
|
2012-11-16 22:30:00 +04:00
|
|
|
*/
|
2013-05-26 05:22:16 +04:00
|
|
|
function testSetupServerSide() {
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->assertEquals(true, $this->util->setupServerSide($this->pass));
|
2012-11-16 22:30:00 +04:00
|
|
|
}
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2012-11-16 22:30:00 +04:00
|
|
|
/**
|
2013-06-10 11:31:22 +04:00
|
|
|
* @medium
|
2014-05-19 19:50:53 +04:00
|
|
|
* test checking whether account is ready for encryption,
|
2012-11-16 22:30:00 +04:00
|
|
|
*/
|
2013-05-26 05:22:16 +04:00
|
|
|
function testUserIsReady() {
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->assertEquals(true, $this->util->ready());
|
2013-01-23 23:24:26 +04:00
|
|
|
}
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2013-05-20 23:19:28 +04:00
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* test checking whether account is not ready for encryption,
|
2013-05-20 23:19:28 +04:00
|
|
|
*/
|
2013-05-26 22:44:15 +04:00
|
|
|
// function testUserIsNotReady() {
|
|
|
|
// $this->view->unlink($this->publicKeyDir);
|
|
|
|
//
|
|
|
|
// $params['uid'] = $this->userId;
|
|
|
|
// $params['password'] = $this->pass;
|
2014-12-03 18:52:44 +03:00
|
|
|
// $this->assertFalse(OCA\Files_Encryption\Hooks::login($params));
|
2013-05-26 22:44:15 +04:00
|
|
|
//
|
|
|
|
// $this->view->unlink($this->privateKeyPath);
|
|
|
|
// }
|
2013-05-20 23:19:28 +04:00
|
|
|
|
2013-06-10 11:31:22 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2013-05-26 05:22:16 +04:00
|
|
|
function testRecoveryEnabledForUser() {
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2014-12-03 18:52:44 +03:00
|
|
|
$util = new Util($this->view, $this->userId);
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2013-03-20 22:26:59 +04:00
|
|
|
// Record the value so we can return it to it's original state later
|
2013-05-01 21:18:31 +04:00
|
|
|
$enabled = $util->recoveryEnabledForUser();
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2014-05-06 21:20:49 +04:00
|
|
|
$this->assertTrue($util->setRecoveryForUser(!$enabled));
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2014-05-06 21:20:49 +04:00
|
|
|
$this->assertEquals(!$enabled, $util->recoveryEnabledForUser());
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2014-05-06 21:20:49 +04:00
|
|
|
$this->assertTrue($util->setRecoveryForUser($enabled));
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2014-05-06 21:20:49 +04:00
|
|
|
$this->assertEquals($enabled, $util->recoveryEnabledForUser());
|
2013-05-20 03:24:36 +04:00
|
|
|
|
|
|
|
|
2013-03-20 22:26:59 +04:00
|
|
|
}
|
2013-04-30 03:35:46 +04:00
|
|
|
|
2013-06-10 11:31:22 +04:00
|
|
|
/**
|
|
|
|
* @medium
|
|
|
|
*/
|
2013-05-26 05:22:16 +04:00
|
|
|
function testGetUidAndFilename() {
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
\OC_User::setUserId(self::TEST_ENCRYPTION_UTIL_USER1);
|
2013-05-20 03:24:36 +04:00
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
$filename = '/tmp-' . $this->getUniqueID() . '.test';
|
2013-04-30 03:35:46 +04:00
|
|
|
|
2013-05-20 03:24:36 +04:00
|
|
|
// Disable encryption proxy to prevent recursive calls
|
|
|
|
$proxyStatus = \OC_FileProxy::$enabled;
|
|
|
|
\OC_FileProxy::$enabled = false;
|
2013-04-30 03:35:46 +04:00
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort);
|
2013-04-30 03:35:46 +04:00
|
|
|
|
2013-05-20 03:24:36 +04:00
|
|
|
// Re-enable proxy - our work is done
|
|
|
|
\OC_FileProxy::$enabled = $proxyStatus;
|
2013-04-30 03:35:46 +04:00
|
|
|
|
2014-12-03 18:52:44 +03:00
|
|
|
$util = new Util($this->view, $this->userId);
|
2013-04-30 03:35:46 +04:00
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
list($fileOwnerUid, $file) = $util->getUidAndFilename($filename);
|
2013-04-30 03:35:46 +04:00
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
$this->assertEquals(self::TEST_ENCRYPTION_UTIL_USER1, $fileOwnerUid);
|
2013-04-30 03:35:46 +04:00
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->assertEquals($file, $filename);
|
2013-05-26 05:22:16 +04:00
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
$this->view->unlink($this->userId . '/files/' . $filename);
|
2013-04-10 19:37:03 +04:00
|
|
|
}
|
2013-05-22 02:55:16 +04:00
|
|
|
|
2013-10-05 20:00:46 +04:00
|
|
|
/**
|
2014-07-11 16:27:43 +04:00
|
|
|
* Test that data that is read by the crypto stream wrapper
|
2013-10-05 20:00:46 +04:00
|
|
|
*/
|
|
|
|
function testGetFileSize() {
|
2014-11-06 18:53:35 +03:00
|
|
|
self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1);
|
2013-10-05 20:00:46 +04:00
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
$filename = 'tmp-' . $this->getUniqueID();
|
2013-10-05 20:00:46 +04:00
|
|
|
$externalFilename = '/' . $this->userId . '/files/' . $filename;
|
|
|
|
|
|
|
|
// Test for 0 byte files
|
|
|
|
$problematicFileSizeData = "";
|
|
|
|
$cryptedFile = $this->view->file_put_contents($externalFilename, $problematicFileSizeData);
|
|
|
|
$this->assertTrue(is_int($cryptedFile));
|
|
|
|
$this->assertEquals($this->util->getFileSize($externalFilename), 0);
|
|
|
|
$decrypt = $this->view->file_get_contents($externalFilename);
|
|
|
|
$this->assertEquals($problematicFileSizeData, $decrypt);
|
|
|
|
$this->view->unlink($this->userId . '/files/' . $filename);
|
|
|
|
|
|
|
|
// Test a file with 18377 bytes as in https://github.com/owncloud/mirall/issues/1009
|
|
|
|
$problematicFileSizeData = str_pad("", 18377, "abc");
|
|
|
|
$cryptedFile = $this->view->file_put_contents($externalFilename, $problematicFileSizeData);
|
|
|
|
$this->assertTrue(is_int($cryptedFile));
|
|
|
|
$this->assertEquals($this->util->getFileSize($externalFilename), 18377);
|
|
|
|
$decrypt = $this->view->file_get_contents($externalFilename);
|
|
|
|
$this->assertEquals($problematicFileSizeData, $decrypt);
|
|
|
|
$this->view->unlink($this->userId . '/files/' . $filename);
|
|
|
|
}
|
|
|
|
|
2013-11-14 15:11:21 +04:00
|
|
|
function testEncryptAll() {
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
$filename = "/encryptAll" . $this->getUniqueID() . ".txt";
|
2014-12-03 18:52:44 +03:00
|
|
|
$util = new Util($this->view, $this->userId);
|
2013-11-14 15:11:21 +04:00
|
|
|
|
|
|
|
// disable encryption to upload a unencrypted file
|
|
|
|
\OC_App::disable('files_encryption');
|
|
|
|
|
|
|
|
$this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort);
|
|
|
|
|
|
|
|
$fileInfoUnencrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
|
|
|
|
|
2014-01-17 17:38:14 +04:00
|
|
|
$this->assertTrue($fileInfoUnencrypted instanceof \OC\Files\FileInfo);
|
2013-11-14 15:11:21 +04:00
|
|
|
|
|
|
|
// enable file encryption again
|
|
|
|
\OC_App::enable('files_encryption');
|
|
|
|
|
|
|
|
// encrypt all unencrypted files
|
|
|
|
$util->encryptAll('/' . $this->userId . '/' . 'files');
|
|
|
|
|
|
|
|
$fileInfoEncrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
|
|
|
|
|
2014-01-17 17:38:14 +04:00
|
|
|
$this->assertTrue($fileInfoEncrypted instanceof \OC\Files\FileInfo);
|
2013-11-14 15:11:21 +04:00
|
|
|
|
|
|
|
// check if mtime and etags unchanged
|
|
|
|
$this->assertEquals($fileInfoEncrypted['mtime'], $fileInfoUnencrypted['mtime']);
|
2014-02-27 12:39:34 +04:00
|
|
|
$this->assertSame($fileInfoEncrypted['etag'], $fileInfoUnencrypted['etag']);
|
2013-11-14 15:11:21 +04:00
|
|
|
|
|
|
|
$this->view->unlink($this->userId . '/files/' . $filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDecryptAll() {
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
$filename = "/decryptAll" . $this->getUniqueID() . ".txt";
|
2014-04-28 16:40:10 +04:00
|
|
|
$datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data/');
|
|
|
|
$userdir = $datadir . '/' . $this->userId . '/files/';
|
|
|
|
|
2013-11-14 15:11:21 +04:00
|
|
|
$this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort);
|
|
|
|
|
|
|
|
$fileInfoEncrypted = $this->view->getFileInfo($this->userId . '/files/' . $filename);
|
|
|
|
|
2014-01-17 17:38:14 +04:00
|
|
|
$this->assertTrue($fileInfoEncrypted instanceof \OC\Files\FileInfo);
|
2014-02-10 20:23:54 +04:00
|
|
|
$this->assertEquals($fileInfoEncrypted['encrypted'], 1);
|
2013-11-14 15:11:21 +04:00
|
|
|
|
2014-04-28 16:40:10 +04:00
|
|
|
$encContent = file_get_contents($userdir . $filename);
|
2014-02-10 20:23:54 +04:00
|
|
|
|
2014-04-28 16:40:10 +04:00
|
|
|
\OC_App::disable('files_encryption');
|
2013-11-14 15:11:21 +04:00
|
|
|
|
2014-04-28 16:40:10 +04:00
|
|
|
$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());
|
|
|
|
|
2014-05-12 18:30:39 +04:00
|
|
|
$view = new OC\Files\View('/');
|
2014-12-03 18:52:44 +03:00
|
|
|
$util = new Util($view, \OCP\User::getUser());
|
2013-11-14 15:11:21 +04:00
|
|
|
|
2014-04-28 16:40:10 +04:00
|
|
|
$result = $util->initEncryption($params);
|
|
|
|
|
2014-12-03 18:52:44 +03:00
|
|
|
$this->assertTrue($result instanceof \OCA\Files_Encryption\Session);
|
2014-04-28 16:40:10 +04:00
|
|
|
|
|
|
|
$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);
|
2014-01-17 17:38:14 +04:00
|
|
|
$this->assertTrue($fileInfoUnencrypted instanceof \OC\Files\FileInfo);
|
2013-11-14 15:11:21 +04:00
|
|
|
|
|
|
|
// check if mtime and etags unchanged
|
|
|
|
$this->assertEquals($fileInfoEncrypted['mtime'], $fileInfoUnencrypted['mtime']);
|
2014-02-27 12:39:34 +04:00
|
|
|
$this->assertSame($fileInfoEncrypted['etag'], $fileInfoUnencrypted['etag']);
|
2014-02-10 20:23:54 +04:00
|
|
|
// file should no longer be encrypted
|
|
|
|
$this->assertEquals(0, $fileInfoUnencrypted['encrypted']);
|
2013-11-14 15:11:21 +04:00
|
|
|
|
2014-11-10 14:40:24 +03:00
|
|
|
$backupPath = $this->getBackupPath('decryptAll');
|
|
|
|
|
2014-04-29 17:00:57 +04:00
|
|
|
// check if the keys where moved to the backup location
|
2014-11-10 14:40:24 +03:00
|
|
|
$this->assertTrue($this->view->is_dir($backupPath . '/keys'));
|
|
|
|
$this->assertTrue($this->view->file_exists($backupPath . '/keys/' . $filename . '/fileKey'));
|
|
|
|
$this->assertTrue($this->view->file_exists($backupPath . '/keys/' . $filename . '/' . $user . '.shareKey'));
|
2014-11-14 19:30:38 +03:00
|
|
|
$this->assertTrue($this->view->file_exists($backupPath . '/' . $user . '.privateKey'));
|
|
|
|
$this->assertTrue($this->view->file_exists($backupPath . '/' . $user . '.publicKey'));
|
2014-04-29 17:00:57 +04:00
|
|
|
|
2014-04-28 16:40:10 +04:00
|
|
|
// cleanup
|
2013-11-14 15:11:21 +04:00
|
|
|
$this->view->unlink($this->userId . '/files/' . $filename);
|
2014-11-10 14:40:24 +03:00
|
|
|
$this->view->deleteAll($backupPath);
|
2014-04-28 16:40:10 +04:00
|
|
|
OC_App::enable('files_encryption');
|
2013-11-14 15:11:21 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-09-16 17:16:27 +04:00
|
|
|
/**
|
|
|
|
* test if all keys get moved to the backup folder correctly
|
|
|
|
*/
|
|
|
|
function testBackupAllKeys() {
|
|
|
|
self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1);
|
|
|
|
|
|
|
|
// create some dummy key files
|
|
|
|
$encPath = '/' . self::TEST_ENCRYPTION_UTIL_USER1 . '/files_encryption';
|
2014-11-10 14:40:24 +03:00
|
|
|
$this->view->mkdir($encPath . '/keys/foo');
|
|
|
|
$this->view->file_put_contents($encPath . '/keys/foo/fileKey', 'key');
|
|
|
|
$this->view->file_put_contents($encPath . '/keys/foo/user1.shareKey', 'share key');
|
2014-09-16 17:16:27 +04:00
|
|
|
|
2014-12-03 18:52:44 +03:00
|
|
|
$util = new Util($this->view, self::TEST_ENCRYPTION_UTIL_USER1);
|
2014-09-16 17:16:27 +04:00
|
|
|
|
2014-11-10 14:40:24 +03:00
|
|
|
$util->backupAllKeys('testBackupAllKeys');
|
2014-09-16 17:16:27 +04:00
|
|
|
|
2014-11-10 14:40:24 +03:00
|
|
|
$backupPath = $this->getBackupPath('testBackupAllKeys');
|
2014-09-16 17:16:27 +04:00
|
|
|
|
|
|
|
// check backupDir Content
|
2014-11-10 14:40:24 +03:00
|
|
|
$this->assertTrue($this->view->is_dir($backupPath . '/keys'));
|
|
|
|
$this->assertTrue($this->view->is_dir($backupPath . '/keys/foo'));
|
|
|
|
$this->assertTrue($this->view->file_exists($backupPath . '/keys/foo/fileKey'));
|
|
|
|
$this->assertTrue($this->view->file_exists($backupPath . '/keys/foo/user1.shareKey'));
|
2014-11-14 19:30:38 +03:00
|
|
|
$this->assertTrue($this->view->file_exists($backupPath . '/' . self::TEST_ENCRYPTION_UTIL_USER1 . '.privateKey'));
|
|
|
|
$this->assertTrue($this->view->file_exists($backupPath . '/' . self::TEST_ENCRYPTION_UTIL_USER1 . '.publicKey'));
|
2014-09-16 17:16:27 +04:00
|
|
|
|
|
|
|
//cleanup
|
|
|
|
$this->view->deleteAll($backupPath);
|
2014-11-10 14:40:24 +03:00
|
|
|
$this->view->unlink($encPath . '/keys/foo/fileKey');
|
|
|
|
$this->view->unlink($encPath . '/keys/foo/user1.shareKey');
|
2014-09-16 17:16:27 +04:00
|
|
|
}
|
|
|
|
|
2014-04-28 16:40:10 +04:00
|
|
|
|
2014-02-10 20:23:54 +04:00
|
|
|
function testDescryptAllWithBrokenFiles() {
|
|
|
|
|
2014-11-06 18:53:35 +03:00
|
|
|
$file1 = "/decryptAll1" . $this->getUniqueID() . ".txt";
|
|
|
|
$file2 = "/decryptAll2" . $this->getUniqueID() . ".txt";
|
2014-02-10 20:23:54 +04:00
|
|
|
|
2014-12-03 18:52:44 +03:00
|
|
|
$util = new Util($this->view, $this->userId);
|
2014-02-10 20:23:54 +04:00
|
|
|
|
|
|
|
$this->view->file_put_contents($this->userId . '/files/' . $file1, $this->dataShort);
|
|
|
|
$this->view->file_put_contents($this->userId . '/files/' . $file2, $this->dataShort);
|
|
|
|
|
|
|
|
$fileInfoEncrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1);
|
|
|
|
$fileInfoEncrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2);
|
|
|
|
|
|
|
|
$this->assertTrue($fileInfoEncrypted1 instanceof \OC\Files\FileInfo);
|
|
|
|
$this->assertTrue($fileInfoEncrypted2 instanceof \OC\Files\FileInfo);
|
|
|
|
$this->assertEquals($fileInfoEncrypted1['encrypted'], 1);
|
|
|
|
$this->assertEquals($fileInfoEncrypted2['encrypted'], 1);
|
|
|
|
|
|
|
|
// rename keyfile for file1 so that the decryption for file1 fails
|
|
|
|
// Expected behaviour: decryptAll() returns false, file2 gets decrypted anyway
|
2014-11-10 14:40:24 +03:00
|
|
|
$this->view->rename($this->userId . '/files_encryption/keys/' . $file1 . '/fileKey',
|
|
|
|
$this->userId . '/files_encryption/keys/' . $file1 . '/fileKey.moved');
|
2014-02-10 20:23:54 +04:00
|
|
|
|
|
|
|
// decrypt all encrypted files
|
2014-06-18 15:26:42 +04:00
|
|
|
$result = $util->decryptAll();
|
2014-02-10 20:23:54 +04:00
|
|
|
|
|
|
|
$this->assertFalse($result);
|
|
|
|
|
|
|
|
$fileInfoUnencrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1);
|
|
|
|
$fileInfoUnencrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2);
|
|
|
|
|
|
|
|
$this->assertTrue($fileInfoUnencrypted1 instanceof \OC\Files\FileInfo);
|
|
|
|
$this->assertTrue($fileInfoUnencrypted2 instanceof \OC\Files\FileInfo);
|
|
|
|
|
|
|
|
// file1 should be still encrypted; file2 should be decrypted
|
|
|
|
$this->assertEquals(1, $fileInfoUnencrypted1['encrypted']);
|
|
|
|
$this->assertEquals(0, $fileInfoUnencrypted2['encrypted']);
|
|
|
|
|
|
|
|
// keyfiles and share keys should still exist
|
2014-11-10 14:40:24 +03:00
|
|
|
$this->assertTrue($this->view->is_dir($this->userId . '/files_encryption/keys/'));
|
|
|
|
$this->assertTrue($this->view->file_exists($this->userId . '/files_encryption/keys/' . $file1 . '/fileKey.moved'));
|
|
|
|
$this->assertTrue($this->view->file_exists($this->userId . '/files_encryption/keys/' . $file1 . '/' . $this->userId . '.shareKey'));
|
2014-02-10 20:23:54 +04:00
|
|
|
|
|
|
|
// rename the keyfile for file1 back
|
2014-11-10 14:40:24 +03:00
|
|
|
$this->view->rename($this->userId . '/files_encryption/keys/' . $file1 . '/fileKey.moved',
|
|
|
|
$this->userId . '/files_encryption/keys/' . $file1 . '/fileKey');
|
2014-02-10 20:23:54 +04:00
|
|
|
|
|
|
|
// try again to decrypt all encrypted files
|
2014-06-18 15:26:42 +04:00
|
|
|
$result = $util->decryptAll();
|
2014-02-10 20:23:54 +04:00
|
|
|
|
|
|
|
$this->assertTrue($result);
|
|
|
|
|
|
|
|
$fileInfoUnencrypted1 = $this->view->getFileInfo($this->userId . '/files/' . $file1);
|
|
|
|
$fileInfoUnencrypted2 = $this->view->getFileInfo($this->userId . '/files/' . $file2);
|
|
|
|
|
|
|
|
$this->assertTrue($fileInfoUnencrypted1 instanceof \OC\Files\FileInfo);
|
|
|
|
$this->assertTrue($fileInfoUnencrypted2 instanceof \OC\Files\FileInfo);
|
|
|
|
|
|
|
|
// now both files should be decrypted
|
|
|
|
$this->assertEquals(0, $fileInfoUnencrypted1['encrypted']);
|
|
|
|
$this->assertEquals(0, $fileInfoUnencrypted2['encrypted']);
|
|
|
|
|
|
|
|
// keyfiles and share keys should be deleted
|
2014-11-10 14:40:24 +03:00
|
|
|
$this->assertFalse($this->view->is_dir($this->userId . '/files_encryption/keys/'));
|
2014-02-10 20:23:54 +04:00
|
|
|
|
2014-04-29 17:00:57 +04:00
|
|
|
//cleanup
|
2014-11-10 14:40:24 +03:00
|
|
|
$backupPath = $this->getBackupPath('decryptAll');
|
2014-02-10 20:23:54 +04:00
|
|
|
$this->view->unlink($this->userId . '/files/' . $file1);
|
|
|
|
$this->view->unlink($this->userId . '/files/' . $file2);
|
2014-11-10 14:40:24 +03:00
|
|
|
$this->view->deleteAll($backupPath);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function getBackupPath($extension) {
|
|
|
|
$encPath = '/' . self::TEST_ENCRYPTION_UTIL_USER1 . '/files_encryption';
|
|
|
|
$encFolderContent = $this->view->getDirectoryContent($encPath);
|
|
|
|
|
|
|
|
$backupPath = '';
|
|
|
|
foreach ($encFolderContent as $c) {
|
|
|
|
$name = $c['name'];
|
|
|
|
if (substr($name, 0, strlen('backup.' . $extension)) === 'backup.' . $extension) {
|
|
|
|
$backupPath = $encPath . '/'. $c['name'];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 20:23:54 +04:00
|
|
|
|
2014-11-10 14:40:24 +03:00
|
|
|
return $backupPath;
|
2014-02-10 20:23:54 +04:00
|
|
|
}
|
|
|
|
|
2014-08-05 17:55:28 +04:00
|
|
|
/**
|
|
|
|
* @dataProvider dataProviderFortestIsMountPointApplicableToUser
|
|
|
|
*/
|
|
|
|
function testIsMountPointApplicableToUser($mount, $expectedResult) {
|
|
|
|
self::loginHelper(self::TEST_ENCRYPTION_UTIL_USER1);
|
|
|
|
$dummyClass = new DummyUtilClass($this->view, self::TEST_ENCRYPTION_UTIL_USER1);
|
|
|
|
$result = $dummyClass->testIsMountPointApplicableToUser($mount);
|
|
|
|
|
|
|
|
$this->assertSame($expectedResult, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dataProviderFortestIsMountPointApplicableToUser() {
|
|
|
|
return array(
|
|
|
|
array(array('applicable' => array('groups' => array(), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER1))), true),
|
|
|
|
array(array('applicable' => array('groups' => array(), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER2))), false),
|
|
|
|
array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP1), 'users' => array())), true),
|
|
|
|
array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP1), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER2))), true),
|
|
|
|
array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP2), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER2))), false),
|
|
|
|
array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP2), 'users' => array(self::TEST_ENCRYPTION_UTIL_USER2, 'all'))), true),
|
|
|
|
array(array('applicable' => array('groups' => array(self::TEST_ENCRYPTION_UTIL_GROUP2), 'users' => array('all'))), true),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-11-12 15:33:41 +03:00
|
|
|
/**
|
|
|
|
* Tests that filterShareReadyUsers() returns the correct list of
|
|
|
|
* users that are ready or not ready for encryption
|
|
|
|
*/
|
|
|
|
public function testFilterShareReadyUsers() {
|
|
|
|
$appConfig = \OC::$server->getAppConfig();
|
|
|
|
|
|
|
|
$publicShareKeyId = $appConfig->getValue('files_encryption', 'publicShareKeyId');
|
|
|
|
$recoveryKeyId = $appConfig->getValue('files_encryption', 'recoveryKeyId');
|
|
|
|
|
|
|
|
$usersToTest = array(
|
|
|
|
'readyUser',
|
|
|
|
'notReadyUser',
|
|
|
|
'nonExistingUser',
|
|
|
|
$publicShareKeyId,
|
|
|
|
$recoveryKeyId,
|
|
|
|
);
|
|
|
|
\Test_Encryption_Util::loginHelper('readyUser', true);
|
|
|
|
\Test_Encryption_Util::loginHelper('notReadyUser', true);
|
|
|
|
// delete encryption dir to make it not ready
|
|
|
|
$this->view->unlink('notReadyUser/files_encryption/');
|
|
|
|
|
|
|
|
// login as user1
|
|
|
|
\Test_Encryption_Util::loginHelper(\Test_Encryption_Util::TEST_ENCRYPTION_UTIL_USER1);
|
|
|
|
|
|
|
|
$result = $this->util->filterShareReadyUsers($usersToTest);
|
|
|
|
$this->assertEquals(
|
|
|
|
array('readyUser', $publicShareKeyId, $recoveryKeyId),
|
|
|
|
$result['ready']
|
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
array('notReadyUser', 'nonExistingUser'),
|
|
|
|
$result['unready']
|
|
|
|
);
|
|
|
|
\OC_User::deleteUser('readyUser');
|
|
|
|
}
|
|
|
|
|
2013-05-26 22:44:15 +04:00
|
|
|
/**
|
2014-05-13 15:29:25 +04:00
|
|
|
* @param string $user
|
2013-05-26 22:44:15 +04:00
|
|
|
* @param bool $create
|
|
|
|
* @param bool $password
|
|
|
|
*/
|
2014-04-28 16:40:10 +04:00
|
|
|
public static function loginHelper($user, $create = false, $password = false, $loadEncryption = true) {
|
2013-05-26 22:44:15 +04:00
|
|
|
if ($create) {
|
2014-03-05 16:19:08 +04:00
|
|
|
try {
|
|
|
|
\OC_User::createUser($user, $user);
|
|
|
|
} catch(\Exception $e) { // catch username is already being used from previous aborted runs
|
|
|
|
|
|
|
|
}
|
2013-05-26 22:44:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($password === false) {
|
|
|
|
$password = $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_User::setUserId('');
|
|
|
|
\OC\Files\Filesystem::tearDown();
|
|
|
|
\OC_User::setUserId($user);
|
2014-04-09 19:52:24 +04:00
|
|
|
\OC_Util::setupFS($user);
|
2013-05-26 22:44:15 +04:00
|
|
|
|
2014-04-28 16:40:10 +04:00
|
|
|
if ($loadEncryption) {
|
|
|
|
$params['uid'] = $user;
|
|
|
|
$params['password'] = $password;
|
2014-12-03 18:52:44 +03:00
|
|
|
\OCA\Files_Encryption\Hooks::login($params);
|
2014-04-28 16:40:10 +04:00
|
|
|
}
|
2013-05-22 02:55:16 +04:00
|
|
|
}
|
2013-06-12 16:00:53 +04:00
|
|
|
|
2013-12-19 14:15:59 +04:00
|
|
|
public static function logoutHelper() {
|
|
|
|
\OC_Util::tearDownFS();
|
2014-07-22 01:36:20 +04:00
|
|
|
\OC_User::setUserId(false);
|
2013-12-19 14:15:59 +04:00
|
|
|
\OC\Files\Filesystem::tearDown();
|
|
|
|
}
|
|
|
|
|
2013-06-13 12:35:30 +04:00
|
|
|
/**
|
|
|
|
* helper function to set migration status to the right value
|
|
|
|
* to be able to test the migration path
|
2013-10-05 20:00:46 +04:00
|
|
|
*
|
2014-02-06 19:30:58 +04:00
|
|
|
* @param integer $status needed migration status for test
|
2014-05-13 15:29:25 +04:00
|
|
|
* @param string $user for which user the status should be set
|
2013-06-13 12:35:30 +04:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2013-06-12 16:00:53 +04:00
|
|
|
private function setMigrationStatus($status, $user) {
|
2014-12-04 18:48:07 +03:00
|
|
|
\OC::$server->getConfig()->setUserValue($user, 'files_encryption', 'migration_status', (string)$status);
|
|
|
|
// the update will definitely be executed -> return value is always true
|
|
|
|
return true;
|
2013-06-12 16:00:53 +04:00
|
|
|
}
|
|
|
|
|
2013-05-28 03:04:09 +04:00
|
|
|
}
|
2014-08-05 17:55:28 +04:00
|
|
|
|
|
|
|
/**
|
2014-12-03 18:52:44 +03:00
|
|
|
* dummy class extends \OCA\Files_Encryption\Util to access protected methods for testing
|
2014-08-05 17:55:28 +04:00
|
|
|
*/
|
2014-12-03 18:52:44 +03:00
|
|
|
class DummyUtilClass extends \OCA\Files_Encryption\Util {
|
2014-08-05 17:55:28 +04:00
|
|
|
public function testIsMountPointApplicableToUser($mount) {
|
|
|
|
return $this->isMountPointApplicableToUser($mount);
|
|
|
|
}
|
|
|
|
}
|