2012-08-15 13:01:43 +00: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.
|
|
|
|
*/
|
|
|
|
|
2012-12-12 17:39:43 +00:00
|
|
|
//require_once "PHPUnit/Framework/TestCase.php";
|
2012-08-15 13:01:43 +00:00
|
|
|
require_once realpath( dirname(__FILE__).'/../../../lib/base.php' );
|
2012-12-12 17:39:43 +00:00
|
|
|
require_once realpath( dirname(__FILE__).'/../lib/crypt.php' );
|
|
|
|
require_once realpath( dirname(__FILE__).'/../lib/keymanager.php' );
|
|
|
|
require_once realpath( dirname(__FILE__).'/../lib/proxy.php' );
|
|
|
|
require_once realpath( dirname(__FILE__).'/../lib/stream.php' );
|
|
|
|
require_once realpath( dirname(__FILE__).'/../lib/util.php' );
|
2013-05-09 21:22:22 +00:00
|
|
|
require_once realpath( dirname(__FILE__).'/../lib/helper.php' );
|
2012-12-12 17:39:43 +00:00
|
|
|
require_once realpath( dirname(__FILE__).'/../appinfo/app.php' );
|
2012-08-15 13:01:43 +00:00
|
|
|
|
2012-11-16 18:31:37 +00:00
|
|
|
use OCA\Encryption;
|
|
|
|
|
2013-01-06 18:38:35 +00:00
|
|
|
// This has to go here because otherwise session errors arise, and the private
|
|
|
|
// encryption key needs to be saved in the session
|
2013-04-30 23:43:56 +00:00
|
|
|
//\OC_User::login( 'admin', 'admin' );
|
2013-01-06 18:38:35 +00:00
|
|
|
|
2012-08-15 13:01:43 +00:00
|
|
|
class Test_Keymanager extends \PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
function setUp() {
|
2013-04-29 21:41:49 +00:00
|
|
|
// reset backend
|
2013-05-15 00:38:08 +00:00
|
|
|
\OC_User::clearBackends();
|
2013-04-29 21:41:49 +00:00
|
|
|
\OC_User::useBackend('database');
|
|
|
|
|
2012-08-15 13:01:43 +00:00
|
|
|
\OC_FileProxy::$enabled = false;
|
2013-01-05 17:12:23 +00:00
|
|
|
|
2013-01-06 18:38:35 +00:00
|
|
|
// set content for encrypting / decrypting in tests
|
|
|
|
$this->dataLong = file_get_contents( realpath( dirname(__FILE__).'/../lib/crypt.php' ) );
|
|
|
|
$this->dataShort = 'hats';
|
|
|
|
$this->dataUrl = realpath( dirname(__FILE__).'/../lib/crypt.php' );
|
|
|
|
$this->legacyData = realpath( dirname(__FILE__).'/legacy-text.txt' );
|
|
|
|
$this->legacyEncryptedData = realpath( dirname(__FILE__).'/legacy-encrypted-text.txt' );
|
|
|
|
$this->randomKey = Encryption\Crypt::generateKey();
|
|
|
|
|
|
|
|
$keypair = Encryption\Crypt::createKeypair();
|
|
|
|
$this->genPublicKey = $keypair['publicKey'];
|
|
|
|
$this->genPrivateKey = $keypair['privateKey'];
|
2013-05-15 00:38:08 +00:00
|
|
|
|
|
|
|
$this->view = new \OC_FilesystemView( '/' );
|
|
|
|
|
|
|
|
\OC_User::setUserId( 'admin' );
|
|
|
|
$this->userId = 'admin';
|
|
|
|
$this->pass = 'admin';
|
2013-04-29 21:41:49 +00:00
|
|
|
|
|
|
|
$userHome = \OC_User::getHome($this->userId);
|
|
|
|
$this->dataDir = str_replace('/'.$this->userId, '', $userHome);
|
|
|
|
|
2013-05-15 00:38:08 +00:00
|
|
|
// Filesystem related hooks
|
|
|
|
\OCA\Encryption\Helper::registerFilesystemHooks();
|
|
|
|
|
|
|
|
\OC_FileProxy::register(new OCA\Encryption\Proxy());
|
|
|
|
|
2013-05-16 23:07:26 +00:00
|
|
|
// remember files_trashbin state
|
|
|
|
$this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
|
|
|
|
|
|
|
|
// we don't want to tests with app files_trashbin enabled
|
|
|
|
\OC_App::disable('files_trashbin');
|
|
|
|
|
2013-05-15 00:38:08 +00:00
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_User::setUserId('');
|
2013-05-15 08:21:56 +00:00
|
|
|
\OC\Files\Filesystem::tearDown();
|
2013-05-15 00:38:08 +00:00
|
|
|
\OC_Util::setupFS($this->userId);
|
|
|
|
\OC_User::setUserId($this->userId);
|
2013-04-30 23:43:56 +00:00
|
|
|
|
|
|
|
$params['uid'] = $this->userId;
|
|
|
|
$params['password'] = $this->pass;
|
|
|
|
OCA\Encryption\Hooks::login($params);
|
2012-08-15 13:01:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function tearDown(){
|
|
|
|
|
2012-08-15 13:13:03 +00:00
|
|
|
\OC_FileProxy::$enabled = true;
|
2013-05-15 22:36:40 +00:00
|
|
|
\OC_FileProxy::clearProxies();
|
2013-05-16 23:07:26 +00:00
|
|
|
|
|
|
|
// reset app files_trashbin
|
|
|
|
if ($this->stateFilesTrashbin) {
|
|
|
|
OC_App::enable('files_trashbin');
|
|
|
|
} else {
|
|
|
|
OC_App::disable('files_trashbin');
|
|
|
|
}
|
2012-08-15 13:01:43 +00:00
|
|
|
}
|
|
|
|
|
2013-01-05 17:12:23 +00:00
|
|
|
function testGetPrivateKey() {
|
2012-08-15 13:01:43 +00:00
|
|
|
|
2013-01-06 18:38:35 +00:00
|
|
|
$key = Encryption\Keymanager::getPrivateKey( $this->view, $this->userId );
|
2013-04-29 23:54:19 +00:00
|
|
|
|
|
|
|
$privateKey = Encryption\Crypt::symmetricDecryptFileContent( $key, $this->pass);
|
|
|
|
|
2013-01-06 18:38:35 +00:00
|
|
|
// Will this length vary? Perhaps we should use a range instead
|
2013-04-29 23:54:19 +00:00
|
|
|
$this->assertGreaterThan( 27, strlen( $privateKey ) );
|
|
|
|
|
|
|
|
$this->assertEquals( '-----BEGIN PRIVATE KEY-----', substr( $privateKey, 0, 27 ) );
|
2012-08-15 13:13:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-01-05 17:12:23 +00:00
|
|
|
function testGetPublicKey() {
|
|
|
|
|
2013-01-06 18:38:35 +00:00
|
|
|
$key = Encryption\Keymanager::getPublicKey( $this->view, $this->userId );
|
2013-01-05 17:12:23 +00:00
|
|
|
|
2013-04-29 23:54:19 +00:00
|
|
|
$this->assertGreaterThan( 26, strlen( $key ) );
|
2013-01-05 17:12:23 +00:00
|
|
|
|
|
|
|
$this->assertEquals( '-----BEGIN PUBLIC KEY-----', substr( $key, 0, 26 ) );
|
|
|
|
}
|
|
|
|
|
2012-08-23 15:43:10 +00:00
|
|
|
function testSetFileKey() {
|
|
|
|
|
2013-01-05 17:12:23 +00:00
|
|
|
# NOTE: This cannot be tested until we are able to break out
|
|
|
|
# of the FileSystemView data directory root
|
2012-08-23 15:43:10 +00:00
|
|
|
|
2013-01-14 19:07:28 +00:00
|
|
|
$key = Encryption\Crypt::symmetricEncryptFileContentKeyfile( $this->randomKey, 'hat' );
|
|
|
|
|
2013-04-29 21:41:49 +00:00
|
|
|
$file = 'unittest-'.time().'.txt';
|
|
|
|
|
|
|
|
// Disable encryption proxy to prevent recursive calls
|
|
|
|
$proxyStatus = \OC_FileProxy::$enabled;
|
|
|
|
\OC_FileProxy::$enabled = false;
|
|
|
|
|
|
|
|
$this->view->file_put_contents($this->userId . '/files/' . $file, $key['encrypted']);
|
|
|
|
|
|
|
|
// Re-enable proxy - our work is done
|
|
|
|
\OC_FileProxy::$enabled = $proxyStatus;
|
|
|
|
|
2013-01-14 19:07:28 +00:00
|
|
|
//$view = new \OC_FilesystemView( '/' . $this->userId . '/files_encryption/keyfiles' );
|
2013-04-29 21:41:49 +00:00
|
|
|
Encryption\Keymanager::setFileKey( $this->view, $file, $this->userId, $key['key'] );
|
2013-05-16 23:07:26 +00:00
|
|
|
|
|
|
|
// Disable encryption proxy to prevent recursive calls
|
|
|
|
$proxyStatus = \OC_FileProxy::$enabled;
|
|
|
|
\OC_FileProxy::$enabled = true;
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
$this->view->unlink('/'.$this->userId . '/files/' . $file);
|
|
|
|
|
|
|
|
// Re-enable proxy - our work is done
|
|
|
|
\OC_FileProxy::$enabled = $proxyStatus;
|
2012-08-23 15:43:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-01-06 18:38:35 +00:00
|
|
|
// /**
|
|
|
|
// * @depends testGetPrivateKey
|
|
|
|
// */
|
|
|
|
// function testGetPrivateKey_decrypt() {
|
|
|
|
//
|
|
|
|
// $key = Encryption\Keymanager::getPrivateKey( $this->view, $this->userId );
|
|
|
|
//
|
|
|
|
// # TODO: replace call to Crypt with a mock object?
|
|
|
|
// $decrypted = Encryption\Crypt::symmetricDecryptFileContent( $key, $this->passphrase );
|
|
|
|
//
|
|
|
|
// $this->assertEquals( 1704, strlen( $decrypted ) );
|
|
|
|
//
|
|
|
|
// $this->assertEquals( '-----BEGIN PRIVATE KEY-----', substr( $decrypted, 0, 27 ) );
|
|
|
|
//
|
|
|
|
// }
|
2012-08-15 13:01:43 +00:00
|
|
|
|
2013-01-05 17:12:23 +00:00
|
|
|
function testGetUserKeys() {
|
|
|
|
|
2013-01-06 18:38:35 +00:00
|
|
|
$keys = Encryption\Keymanager::getUserKeys( $this->view, $this->userId );
|
2013-01-05 17:12:23 +00:00
|
|
|
|
2013-04-29 23:54:19 +00:00
|
|
|
$this->assertGreaterThan( 26, strlen( $keys['publicKey'] ) );
|
|
|
|
|
2013-01-05 17:12:23 +00:00
|
|
|
$this->assertEquals( '-----BEGIN PUBLIC KEY-----', substr( $keys['publicKey'], 0, 26 ) );
|
2013-04-29 23:54:19 +00:00
|
|
|
|
|
|
|
$privateKey = Encryption\Crypt::symmetricDecryptFileContent( $keys['privateKey'], $this->pass);
|
|
|
|
|
|
|
|
$this->assertGreaterThan( 27, strlen( $keys['privateKey'] ) );
|
|
|
|
|
|
|
|
$this->assertEquals( '-----BEGIN PRIVATE KEY-----', substr( $privateKey, 0, 27 ) );
|
2012-08-23 15:43:10 +00:00
|
|
|
|
2013-01-05 17:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testGetPublicKeys() {
|
|
|
|
|
|
|
|
# TODO: write me
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testGetFileKey() {
|
|
|
|
|
2013-01-06 18:38:35 +00:00
|
|
|
// Encryption\Keymanager::getFileKey( $this->view, $this->userId, $this->filePath );
|
2013-01-05 17:12:23 +00:00
|
|
|
|
|
|
|
}
|
2012-08-23 15:43:10 +00:00
|
|
|
|
2012-08-15 13:01:43 +00:00
|
|
|
}
|