2012-07-25 11:38:40 +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.
|
|
|
|
*/
|
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
require_once realpath( dirname( __FILE__ ) . '/../../../lib/base.php' );
|
|
|
|
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 19:29:22 +00:00
|
|
|
|
2012-11-16 18:30:00 +00:00
|
|
|
use OCA\Encryption;
|
2012-11-14 16:39:35 +00:00
|
|
|
|
2013-05-19 20:28:48 +00:00
|
|
|
/**
|
|
|
|
* Class Test_Encryption_Util
|
|
|
|
*/
|
2013-05-19 23:24:36 +00:00
|
|
|
class Test_Encryption_Util extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2013-05-19 20:28:48 +00: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;
|
2013-05-20 19:19:28 +00:00
|
|
|
public $legacyEncryptedData;
|
|
|
|
public $legacyEncryptedDataKey;
|
|
|
|
public $lagacyKey;
|
2013-05-19 20:28:48 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
function setUp() {
|
2013-05-19 23:24:36 +00:00
|
|
|
// reset backend
|
2013-05-26 01:22:16 +00:00
|
|
|
\OC_User::useBackend( 'database' );
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
\OC_User::setUserId( 'admin' );
|
2013-05-19 23:24:36 +00:00
|
|
|
$this->userId = 'admin';
|
|
|
|
$this->pass = 'admin';
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2013-05-19 23:24:36 +00:00
|
|
|
// set content for encrypting / decrypting in tests
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->dataUrl = realpath( dirname( __FILE__ ) . '/../lib/crypt.php' );
|
2012-11-20 19:10:10 +00:00
|
|
|
$this->dataShort = 'hats';
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->dataLong = file_get_contents( realpath( dirname( __FILE__ ) . '/../lib/crypt.php' ) );
|
|
|
|
$this->legacyData = realpath( dirname( __FILE__ ) . '/legacy-text.txt' );
|
|
|
|
$this->legacyEncryptedData = realpath( dirname( __FILE__ ) . '/legacy-encrypted-text.txt' );
|
|
|
|
$this->legacyEncryptedDataKey = realpath( dirname( __FILE__ ) . '/encryption.key' );
|
2013-05-20 19:19:28 +00:00
|
|
|
$this->lagacyKey = '62829813025828180801';
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2012-11-20 19:10:10 +00:00
|
|
|
$keypair = Encryption\Crypt::createKeypair();
|
2013-05-19 23:24:36 +00:00
|
|
|
|
|
|
|
$this->genPublicKey = $keypair['publicKey'];
|
2012-11-20 19:10:10 +00:00
|
|
|
$this->genPrivateKey = $keypair['privateKey'];
|
2013-05-19 23:24:36 +00:00
|
|
|
|
|
|
|
$this->publicKeyDir = '/' . 'public-keys';
|
|
|
|
$this->encryptionDir = '/' . $this->userId . '/' . 'files_encryption';
|
2012-11-16 18:30:00 +00:00
|
|
|
$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 00:38:08 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->view = new \OC_FilesystemView( '/' );
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$userHome = \OC_User::getHome( $this->userId );
|
|
|
|
$this->dataDir = str_replace( '/' . $this->userId, '', $userHome );
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2013-05-19 23:24:36 +00:00
|
|
|
// Filesystem related hooks
|
|
|
|
\OCA\Encryption\Helper::registerFilesystemHooks();
|
2013-05-15 00:38:08 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
// clear and register hooks
|
|
|
|
\OC_FileProxy::clearProxies();
|
|
|
|
\OC_FileProxy::register( new OCA\Encryption\Proxy() );
|
2013-05-15 00:38:08 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
// setup filesystem
|
2013-05-19 23:24:36 +00:00
|
|
|
\OC_Util::tearDownFS();
|
2013-05-26 01:22:16 +00:00
|
|
|
\OC_User::setUserId( '' );
|
2013-05-19 23:24:36 +00:00
|
|
|
\OC\Files\Filesystem::tearDown();
|
2013-05-26 01:22:16 +00:00
|
|
|
\OC_Util::setupFS( $this->userId );
|
|
|
|
\OC_User::setUserId( $this->userId );
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
// login admin
|
2013-05-19 23:24:36 +00:00
|
|
|
$params['uid'] = $this->userId;
|
|
|
|
$params['password'] = $this->pass;
|
2013-05-26 01:22:16 +00:00
|
|
|
OCA\Encryption\Hooks::login( $params );
|
2013-04-30 23:43:56 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->util = new Encryption\Util( $this->view, $this->userId );
|
2012-07-25 11:38:40 +00:00
|
|
|
}
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
function tearDown() {
|
|
|
|
// clear and register hooks
|
2013-05-15 22:36:40 +00:00
|
|
|
\OC_FileProxy::clearProxies();
|
2012-11-16 18:30:00 +00:00
|
|
|
}
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2012-11-16 18:30:00 +00:00
|
|
|
/**
|
|
|
|
* @brief test that paths set during User construction are correct
|
|
|
|
*/
|
2013-05-26 01:22:16 +00:00
|
|
|
function testKeyPaths() {
|
|
|
|
$util = new Encryption\Util( $this->view, $this->userId );
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2013-05-26 01:22:16 +00: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-19 23:24:36 +00:00
|
|
|
|
2012-11-16 18:30:00 +00:00
|
|
|
}
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2012-11-16 18:30:00 +00:00
|
|
|
/**
|
2013-05-18 19:37:00 +00:00
|
|
|
* @brief test setup of encryption directories
|
2012-11-16 18:30:00 +00:00
|
|
|
*/
|
2013-05-26 01:22:16 +00:00
|
|
|
function testSetupServerSide() {
|
|
|
|
$this->assertEquals( true, $this->util->setupServerSide( $this->pass ) );
|
2012-11-16 18:30:00 +00:00
|
|
|
}
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2012-11-16 18:30:00 +00:00
|
|
|
/**
|
2013-05-18 19:37:00 +00:00
|
|
|
* @brief test checking whether account is ready for encryption,
|
2012-11-16 18:30:00 +00:00
|
|
|
*/
|
2013-05-26 01:22:16 +00:00
|
|
|
function testUserIsReady() {
|
|
|
|
$this->assertEquals( true, $this->util->ready() );
|
2013-01-23 19:24:26 +00:00
|
|
|
}
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2013-05-20 19:19:28 +00:00
|
|
|
/**
|
|
|
|
* @brief test checking whether account is not ready for encryption,
|
|
|
|
*/
|
2013-05-26 01:22:16 +00:00
|
|
|
function testUserIsNotReady() {
|
|
|
|
$this->view->unlink( $this->publicKeyDir );
|
2013-05-20 19:19:28 +00:00
|
|
|
|
|
|
|
$params['uid'] = $this->userId;
|
|
|
|
$params['password'] = $this->pass;
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertFalse( OCA\Encryption\Hooks::login( $params ) );
|
2013-05-20 19:19:28 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->view->unlink( $this->privateKeyPath );
|
2013-05-20 19:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test checking whether account is not ready for encryption,
|
|
|
|
*/
|
2013-05-26 01:22:16 +00:00
|
|
|
function testIsLagacyUser() {
|
2013-05-20 19:19:28 +00:00
|
|
|
$userView = new \OC_FilesystemView( '/' . $this->userId );
|
|
|
|
|
|
|
|
// Disable encryption proxy to prevent recursive calls
|
|
|
|
$proxyStatus = \OC_FileProxy::$enabled;
|
|
|
|
\OC_FileProxy::$enabled = false;
|
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$encryptionKeyContent = file_get_contents( $this->legacyEncryptedDataKey );
|
|
|
|
$userView->file_put_contents( '/encryption.key', $encryptionKeyContent );
|
2013-05-20 19:19:28 +00:00
|
|
|
|
|
|
|
\OC_FileProxy::$enabled = $proxyStatus;
|
|
|
|
|
|
|
|
$params['uid'] = $this->userId;
|
|
|
|
$params['password'] = $this->pass;
|
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$util = new Encryption\Util( $this->view, $this->userId );
|
|
|
|
$util->setMigrationStatus( 0 );
|
2013-05-20 19:19:28 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertTrue( OCA\Encryption\Hooks::login( $params ) );
|
2013-05-20 19:19:28 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertEquals( $this->lagacyKey, $_SESSION['legacyKey'] );
|
2013-05-20 19:19:28 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
function testRecoveryEnabledForUser() {
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$util = new Encryption\Util( $this->view, $this->userId );
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2013-03-20 18:26:59 +00:00
|
|
|
// Record the value so we can return it to it's original state later
|
2013-05-01 17:18:31 +00:00
|
|
|
$enabled = $util->recoveryEnabledForUser();
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertTrue( $util->setRecoveryForUser( 1 ) );
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertEquals( 1, $util->recoveryEnabledForUser() );
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertTrue( $util->setRecoveryForUser( 0 ) );
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertEquals( 0, $util->recoveryEnabledForUser() );
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2013-03-20 18:26:59 +00:00
|
|
|
// Return the setting to it's previous state
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertTrue( $util->setRecoveryForUser( $enabled ) );
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2013-03-20 18:26:59 +00:00
|
|
|
}
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
function testGetUidAndFilename() {
|
2013-05-19 23:24:36 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
\OC_User::setUserId( 'admin' );
|
2013-05-19 23:24:36 +00:00
|
|
|
|
|
|
|
$filename = 'tmp-' . time() . '.test';
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2013-05-19 23:24:36 +00:00
|
|
|
// Disable encryption proxy to prevent recursive calls
|
|
|
|
$proxyStatus = \OC_FileProxy::$enabled;
|
|
|
|
\OC_FileProxy::$enabled = false;
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->view->file_put_contents( $this->userId . '/files/' . $filename, $this->dataShort );
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2013-05-19 23:24:36 +00:00
|
|
|
// Re-enable proxy - our work is done
|
|
|
|
\OC_FileProxy::$enabled = $proxyStatus;
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$util = new Encryption\Util( $this->view, $this->userId );
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
list( $fileOwnerUid, $file ) = $util->getUidAndFilename( $filename );
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertEquals( 'admin', $fileOwnerUid );
|
2013-04-29 23:35:46 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertEquals( $file, $filename );
|
|
|
|
|
|
|
|
$this->view->unlink( $this->userId . '/files/' . $filename );
|
2013-04-10 15:37:03 +00:00
|
|
|
}
|
2013-05-21 22:55:16 +00:00
|
|
|
|
|
|
|
function testIsSharedPath() {
|
|
|
|
$sharedPath = '/user1/files/Shared/test';
|
|
|
|
$path = '/user1/files/test';
|
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertTrue( $this->util->isSharedPath( $sharedPath ) );
|
2013-05-21 22:55:16 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertFalse( $this->util->isSharedPath( $path ) );
|
2013-05-21 22:55:16 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
function testEncryptLagacyFiles() {
|
|
|
|
// login admin
|
|
|
|
$params['uid'] = $this->userId;
|
|
|
|
$params['password'] = $this->pass;
|
|
|
|
OCA\Encryption\Hooks::login( $params );
|
|
|
|
|
|
|
|
$userView = new \OC_FilesystemView( '/' . $this->userId );
|
2013-05-21 22:55:16 +00:00
|
|
|
$view = new \OC_FilesystemView( '/' . $this->userId . '/files' );
|
|
|
|
|
|
|
|
// Disable encryption proxy to prevent recursive calls
|
|
|
|
$proxyStatus = \OC_FileProxy::$enabled;
|
|
|
|
\OC_FileProxy::$enabled = false;
|
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$encryptionKeyContent = file_get_contents( $this->legacyEncryptedDataKey );
|
|
|
|
$userView->file_put_contents( '/encryption.key', $encryptionKeyContent );
|
2013-05-21 22:55:16 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$legacyEncryptedData = file_get_contents( $this->legacyEncryptedData );
|
|
|
|
$view->mkdir( '/test/' );
|
|
|
|
$view->mkdir( '/test/subtest/' );
|
|
|
|
$view->file_put_contents( '/test/subtest/legacy-encrypted-text.txt', $legacyEncryptedData );
|
2013-05-21 22:55:16 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$fileInfo = $view->getFileInfo( '/test/subtest/legacy-encrypted-text.txt' );
|
2013-05-21 22:55:16 +00:00
|
|
|
$fileInfo['encrypted'] = true;
|
2013-05-26 01:22:16 +00:00
|
|
|
$view->putFileInfo( '/test/subtest/legacy-encrypted-text.txt', $fileInfo );
|
2013-05-21 22:55:16 +00:00
|
|
|
|
|
|
|
\OC_FileProxy::$enabled = $proxyStatus;
|
|
|
|
|
|
|
|
$params['uid'] = $this->userId;
|
|
|
|
$params['password'] = $this->pass;
|
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$util = new Encryption\Util( $this->view, $this->userId );
|
|
|
|
$util->setMigrationStatus( 0 );
|
2013-05-21 22:55:16 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertTrue( OCA\Encryption\Hooks::login( $params ) );
|
2013-05-21 22:55:16 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertEquals( $this->lagacyKey, $_SESSION['legacyKey'] );
|
2013-05-21 22:55:16 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$files = $util->findEncFiles( '/' . $this->userId . '/files/' );
|
2013-05-21 22:55:16 +00:00
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertTrue( is_array( $files ) );
|
2013-05-21 22:55:16 +00:00
|
|
|
|
|
|
|
$found = false;
|
2013-05-26 01:22:16 +00:00
|
|
|
foreach ( $files['encrypted'] as $encryptedFile ) {
|
|
|
|
if ( $encryptedFile['name'] === 'legacy-encrypted-text.txt' ) {
|
2013-05-21 22:55:16 +00:00
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-26 01:22:16 +00:00
|
|
|
$this->assertTrue( $found );
|
2013-05-21 22:55:16 +00:00
|
|
|
}
|
2012-07-25 11:38:40 +00:00
|
|
|
}
|