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.
|
|
|
|
*/
|
|
|
|
|
2012-11-14 20:39:35 +04:00
|
|
|
require_once realpath( dirname(__FILE__).'/../../../lib/base.php' );
|
2012-12-12 21:39:43 +04: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' );
|
|
|
|
require_once realpath( dirname(__FILE__).'/../appinfo/app.php' );
|
2013-01-02 23:29:22 +04:00
|
|
|
|
2012-11-16 22:30:00 +04:00
|
|
|
use OCA\Encryption;
|
2012-11-14 20:39:35 +04:00
|
|
|
|
2013-05-20 00:28:48 +04:00
|
|
|
/**
|
|
|
|
* Class Test_Encryption_Util
|
|
|
|
*/
|
2013-05-18 23:37:00 +04:00
|
|
|
class Test_Encryption_Util extends \PHPUnit_Framework_TestCase {
|
2013-05-20 00:28:48 +04:00
|
|
|
|
|
|
|
public $userId;
|
|
|
|
public $encryptionDir;
|
|
|
|
public $publicKeyDir;
|
|
|
|
public $pass;
|
|
|
|
/**
|
|
|
|
* @var OC_FilesystemView
|
|
|
|
*/
|
|
|
|
public $view;
|
|
|
|
public $keyfilesPath;
|
|
|
|
public $publicKeyPath;
|
|
|
|
public $privateKeyPath;
|
|
|
|
/**
|
|
|
|
* @var \OCA\Encryption\Util
|
|
|
|
*/
|
|
|
|
public $util;
|
|
|
|
public $dataShort;
|
|
|
|
|
2012-07-25 15:38:40 +04:00
|
|
|
function setUp() {
|
2013-04-30 03:35:46 +04:00
|
|
|
// reset backend
|
|
|
|
\OC_User::useBackend('database');
|
|
|
|
|
|
|
|
\OC_User::setUserId( 'admin' );
|
|
|
|
$this->userId = 'admin';
|
|
|
|
$this->pass = 'admin';
|
|
|
|
|
|
|
|
// set content for encrypting / decrypting in tests
|
2012-11-20 23:10:10 +04:00
|
|
|
$this->dataUrl = realpath( dirname(__FILE__).'/../lib/crypt.php' );
|
|
|
|
$this->dataShort = 'hats';
|
|
|
|
$this->dataLong = file_get_contents( realpath( dirname(__FILE__).'/../lib/crypt.php' ) );
|
2012-07-25 15:38:40 +04:00
|
|
|
$this->legacyData = realpath( dirname(__FILE__).'/legacy-text.txt' );
|
|
|
|
$this->legacyEncryptedData = realpath( dirname(__FILE__).'/legacy-encrypted-text.txt' );
|
2013-04-30 03:35:46 +04:00
|
|
|
|
2012-11-20 23:10:10 +04:00
|
|
|
$keypair = Encryption\Crypt::createKeypair();
|
|
|
|
|
|
|
|
$this->genPublicKey = $keypair['publicKey'];
|
|
|
|
$this->genPrivateKey = $keypair['privateKey'];
|
|
|
|
|
2012-11-16 22:30:00 +04:00
|
|
|
$this->publicKeyDir = '/' . 'public-keys';
|
|
|
|
$this->encryptionDir = '/' . $this->userId . '/' . 'files_encryption';
|
|
|
|
$this->keyfilesPath = $this->encryptionDir . '/' . 'keyfiles';
|
|
|
|
$this->publicKeyPath = $this->publicKeyDir . '/' . $this->userId . '.public.key'; // e.g. data/public-keys/admin.public.key
|
|
|
|
$this->privateKeyPath = $this->encryptionDir . '/' . $this->userId . '.private.key'; // e.g. data/admin/admin.private.key
|
2013-05-15 04:38:08 +04:00
|
|
|
|
|
|
|
$this->view = new \OC_FilesystemView( '/' );
|
2013-04-30 03:35:46 +04:00
|
|
|
|
|
|
|
$userHome = \OC_User::getHome($this->userId);
|
|
|
|
$this->dataDir = str_replace('/'.$this->userId, '', $userHome);
|
|
|
|
|
2013-05-15 04:38:08 +04:00
|
|
|
// Filesystem related hooks
|
|
|
|
\OCA\Encryption\Helper::registerFilesystemHooks();
|
|
|
|
|
|
|
|
\OC_FileProxy::register(new OCA\Encryption\Proxy());
|
|
|
|
|
|
|
|
\OC_Util::tearDownFS();
|
|
|
|
\OC_User::setUserId('');
|
2013-05-15 12:21:56 +04:00
|
|
|
\OC\Files\Filesystem::tearDown();
|
2013-05-15 04:38:08 +04:00
|
|
|
\OC_Util::setupFS($this->userId);
|
|
|
|
\OC_User::setUserId($this->userId);
|
2013-04-30 03:35:46 +04:00
|
|
|
|
2013-05-01 03:43:56 +04:00
|
|
|
$params['uid'] = $this->userId;
|
|
|
|
$params['password'] = $this->pass;
|
|
|
|
OCA\Encryption\Hooks::login($params);
|
|
|
|
|
2013-05-18 23:37:00 +04:00
|
|
|
$this->util = new Encryption\Util( $this->view, $this->userId );
|
2012-07-25 15:38:40 +04:00
|
|
|
}
|
|
|
|
|
2012-11-16 22:30:00 +04:00
|
|
|
function tearDown(){
|
|
|
|
|
2013-05-16 02:36:40 +04:00
|
|
|
\OC_FileProxy::clearProxies();
|
2012-11-16 22:30:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test that paths set during User construction are correct
|
|
|
|
*/
|
|
|
|
function testKeyPaths() {
|
|
|
|
|
2013-05-18 23:37:00 +04:00
|
|
|
$util = new Encryption\Util( $this->view, $this->userId );
|
2012-11-16 22:30:00 +04:00
|
|
|
|
|
|
|
$this->assertEquals( $this->publicKeyDir, $util->getPath( 'publicKeyDir' ) );
|
|
|
|
$this->assertEquals( $this->encryptionDir, $util->getPath( 'encryptionDir' ) );
|
|
|
|
$this->assertEquals( $this->keyfilesPath, $util->getPath( 'keyfilesPath' ) );
|
|
|
|
$this->assertEquals( $this->publicKeyPath, $util->getPath( 'publicKeyPath' ) );
|
|
|
|
$this->assertEquals( $this->privateKeyPath, $util->getPath( 'privateKeyPath' ) );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-18 23:37:00 +04:00
|
|
|
* @brief test setup of encryption directories
|
2012-11-16 22:30:00 +04:00
|
|
|
*/
|
2013-05-18 23:37:00 +04:00
|
|
|
function testSetupServerSide() {
|
2012-11-16 22:30:00 +04:00
|
|
|
|
2013-05-18 23:37:00 +04:00
|
|
|
$this->assertEquals( true, $this->util->setupServerSide( $this->pass ) );
|
2012-11-16 22:30:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-05-18 23:37:00 +04:00
|
|
|
* @brief test checking whether account is ready for encryption,
|
2012-11-16 22:30:00 +04:00
|
|
|
*/
|
2013-05-18 23:37:00 +04:00
|
|
|
function testUserIsReady() {
|
2012-11-16 22:30:00 +04:00
|
|
|
|
2013-05-18 23:37:00 +04:00
|
|
|
$this->assertEquals( true, $this->util->ready() );
|
2013-01-23 23:24:26 +04:00
|
|
|
}
|
2013-03-20 22:26:59 +04:00
|
|
|
|
2013-05-01 21:18:31 +04:00
|
|
|
function testRecoveryEnabledForUser() {
|
2013-03-20 22:26:59 +04:00
|
|
|
|
|
|
|
$util = new Encryption\Util( $this->view, $this->userId );
|
|
|
|
|
|
|
|
// 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-03-20 22:26:59 +04:00
|
|
|
|
2013-05-01 21:18:31 +04:00
|
|
|
$this->assertTrue( $util->setRecoveryForUser( 1 ) );
|
2013-03-20 22:26:59 +04:00
|
|
|
|
2013-05-01 21:18:31 +04:00
|
|
|
$this->assertEquals( 1, $util->recoveryEnabledForUser() );
|
2013-03-20 22:26:59 +04:00
|
|
|
|
2013-05-01 21:18:31 +04:00
|
|
|
$this->assertTrue( $util->setRecoveryForUser( 0 ) );
|
2013-03-20 22:26:59 +04:00
|
|
|
|
2013-05-01 21:18:31 +04:00
|
|
|
$this->assertEquals( 0, $util->recoveryEnabledForUser() );
|
2013-03-20 22:26:59 +04:00
|
|
|
|
|
|
|
// Return the setting to it's previous state
|
2013-05-01 21:18:31 +04:00
|
|
|
$this->assertTrue( $util->setRecoveryForUser( $enabled ) );
|
2013-03-20 22:26:59 +04:00
|
|
|
|
|
|
|
}
|
2013-04-10 19:37:03 +04:00
|
|
|
|
|
|
|
function testGetUidAndFilename() {
|
|
|
|
|
|
|
|
\OC_User::setUserId( 'admin' );
|
2013-04-30 03:35:46 +04:00
|
|
|
|
|
|
|
$filename = 'tmp-'.time().'.test';
|
|
|
|
|
|
|
|
// Disable encryption proxy to prevent recursive calls
|
|
|
|
$proxyStatus = \OC_FileProxy::$enabled;
|
|
|
|
\OC_FileProxy::$enabled = false;
|
|
|
|
|
|
|
|
$this->view->file_put_contents($this->userId . '/files/' . $filename, $this->dataShort);
|
|
|
|
|
|
|
|
// Re-enable proxy - our work is done
|
|
|
|
\OC_FileProxy::$enabled = $proxyStatus;
|
|
|
|
|
|
|
|
$util = new Encryption\Util( $this->view, $this->userId );
|
|
|
|
|
|
|
|
list($fileOwnerUid, $file) = $util->getUidAndFilename( $filename );
|
|
|
|
|
|
|
|
$this->assertEquals('admin', $fileOwnerUid);
|
|
|
|
|
|
|
|
$this->assertEquals($file, $filename);
|
2013-04-10 19:37:03 +04:00
|
|
|
}
|
2012-07-25 15:38:40 +04:00
|
|
|
}
|