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-12-12 21:39:43 +04:00
|
|
|
//require_once "PHPUnit/Framework/TestCase.php";
|
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
|
|
|
|
|
|
|
// Load mockery files
|
|
|
|
require_once 'Mockery/Loader.php';
|
|
|
|
require_once 'Hamcrest/Hamcrest.php';
|
|
|
|
$loader = new \Mockery\Loader;
|
|
|
|
$loader->register();
|
2012-11-16 22:30:00 +04:00
|
|
|
|
|
|
|
use \Mockery as m;
|
|
|
|
use OCA\Encryption;
|
2012-11-14 20:39:35 +04:00
|
|
|
|
2013-01-06 22:38:35 +04:00
|
|
|
class Test_Enc_Util extends \PHPUnit_Framework_TestCase {
|
2012-07-25 15:38:40 +04:00
|
|
|
|
|
|
|
function setUp() {
|
2013-01-06 22:38:35 +04:00
|
|
|
|
|
|
|
\OC_Filesystem::mount( 'OC_Filestorage_Local', array(), '/' );
|
2012-07-25 15:38:40 +04:00
|
|
|
|
|
|
|
// 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' );
|
|
|
|
|
2012-11-16 22:30:00 +04:00
|
|
|
$this->userId = 'admin';
|
|
|
|
$this->pass = 'admin';
|
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-01-23 23:24:26 +04:00
|
|
|
$this->view = new \OC_FilesystemView( '/' );
|
2012-11-20 23:10:10 +04:00
|
|
|
|
|
|
|
$this->mockView = m::mock('OC_FilesystemView');
|
|
|
|
$this->util = new Encryption\Util( $this->mockView, $this->userId );
|
2012-07-25 15:38:40 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-16 22:30:00 +04:00
|
|
|
function tearDown(){
|
|
|
|
|
|
|
|
m::close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test that paths set during User construction are correct
|
|
|
|
*/
|
|
|
|
function testKeyPaths() {
|
|
|
|
|
|
|
|
$mockView = m::mock('OC_FilesystemView');
|
|
|
|
|
|
|
|
$util = new Encryption\Util( $mockView, $this->userId );
|
|
|
|
|
|
|
|
$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' ) );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test setup of encryption directories when they don't yet exist
|
|
|
|
*/
|
|
|
|
function testSetupServerSideNotSetup() {
|
|
|
|
|
|
|
|
$mockView = m::mock('OC_FilesystemView');
|
|
|
|
|
2013-01-23 23:24:26 +04:00
|
|
|
$mockView->shouldReceive( 'file_exists' )->times(5)->andReturn( false );
|
|
|
|
$mockView->shouldReceive( 'mkdir' )->times(4)->andReturn( true );
|
2012-11-16 22:30:00 +04:00
|
|
|
$mockView->shouldReceive( 'file_put_contents' )->withAnyArgs();
|
|
|
|
|
|
|
|
$util = new Encryption\Util( $mockView, $this->userId );
|
|
|
|
|
|
|
|
$this->assertEquals( true, $util->setupServerSide( $this->pass ) );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test setup of encryption directories when they already exist
|
|
|
|
*/
|
|
|
|
function testSetupServerSideIsSetup() {
|
|
|
|
|
|
|
|
$mockView = m::mock('OC_FilesystemView');
|
|
|
|
|
2013-01-23 23:24:26 +04:00
|
|
|
$mockView->shouldReceive( 'file_exists' )->times(6)->andReturn( true );
|
2012-11-16 22:30:00 +04:00
|
|
|
$mockView->shouldReceive( 'file_put_contents' )->withAnyArgs();
|
|
|
|
|
|
|
|
$util = new Encryption\Util( $mockView, $this->userId );
|
|
|
|
|
|
|
|
$this->assertEquals( true, $util->setupServerSide( $this->pass ) );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test checking whether account is ready for encryption, when it isn't ready
|
|
|
|
*/
|
|
|
|
function testReadyNotReady() {
|
|
|
|
|
|
|
|
$mockView = m::mock('OC_FilesystemView');
|
|
|
|
|
|
|
|
$mockView->shouldReceive( 'file_exists' )->times(1)->andReturn( false );
|
|
|
|
|
|
|
|
$util = new Encryption\Util( $mockView, $this->userId );
|
|
|
|
|
|
|
|
$this->assertEquals( false, $util->ready() );
|
|
|
|
|
|
|
|
# TODO: Add more tests here to check that if any of the dirs are
|
|
|
|
# then false will be returned. Use strict ordering?
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test checking whether account is ready for encryption, when it is ready
|
|
|
|
*/
|
|
|
|
function testReadyIsReady() {
|
|
|
|
|
|
|
|
$mockView = m::mock('OC_FilesystemView');
|
|
|
|
|
|
|
|
$mockView->shouldReceive( 'file_exists' )->times(3)->andReturn( true );
|
|
|
|
|
|
|
|
$util = new Encryption\Util( $mockView, $this->userId );
|
|
|
|
|
|
|
|
$this->assertEquals( true, $util->ready() );
|
|
|
|
|
|
|
|
# TODO: Add more tests here to check that if any of the dirs are
|
|
|
|
# then false will be returned. Use strict ordering?
|
|
|
|
|
|
|
|
}
|
2013-01-23 23:24:26 +04:00
|
|
|
|
2013-02-20 23:18:00 +04:00
|
|
|
function testFindEncFiles() {
|
2013-01-23 23:24:26 +04:00
|
|
|
|
|
|
|
// $this->view->chroot( "/data/{$this->userId}/files" );
|
|
|
|
|
|
|
|
$util = new Encryption\Util( $this->view, $this->userId );
|
|
|
|
|
2013-02-20 23:18:00 +04:00
|
|
|
$files = $util->findEncFiles( '/', 'encrypted' );
|
2013-01-23 23:24:26 +04:00
|
|
|
|
|
|
|
var_dump( $files );
|
|
|
|
|
|
|
|
# TODO: Add more tests here to check that if any of the dirs are
|
|
|
|
# then false will be returned. Use strict ordering?
|
|
|
|
|
|
|
|
}
|
2012-11-20 23:10:10 +04:00
|
|
|
|
|
|
|
// /**
|
|
|
|
// * @brief test decryption using legacy blowfish method
|
|
|
|
// * @depends testLegacyEncryptLong
|
|
|
|
// */
|
|
|
|
// function testLegacyKeyRecryptKeyfileDecrypt( $recrypted ) {
|
|
|
|
//
|
|
|
|
// $decrypted = Encryption\Crypt::keyDecryptKeyfile( $recrypted['data'], $recrypted['key'], $this->genPrivateKey );
|
|
|
|
//
|
|
|
|
// $this->assertEquals( $this->dataLong, $decrypted );
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
2012-07-25 15:38:40 +04:00
|
|
|
// // Cannot use this test for now due to hidden dependencies in OC_FileCache
|
|
|
|
// function testIsLegacyEncryptedContent() {
|
|
|
|
//
|
2012-10-17 19:35:19 +04:00
|
|
|
// $keyfileContent = OCA\Encryption\Crypt::symmetricEncryptFileContent( $this->legacyEncryptedData, 'hat' );
|
2012-07-25 15:38:40 +04:00
|
|
|
//
|
2012-10-17 19:35:19 +04:00
|
|
|
// $this->assertFalse( OCA\Encryption\Crypt::isLegacyEncryptedContent( $keyfileContent, '/files/admin/test.txt' ) );
|
2012-07-25 15:38:40 +04:00
|
|
|
//
|
|
|
|
// OC_FileCache::put( '/admin/files/legacy-encrypted-test.txt', $this->legacyEncryptedData );
|
|
|
|
//
|
2012-10-17 19:35:19 +04:00
|
|
|
// $this->assertTrue( OCA\Encryption\Crypt::isLegacyEncryptedContent( $this->legacyEncryptedData, '/files/admin/test.txt' ) );
|
2012-07-25 15:38:40 +04:00
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Cannot use this test for now due to need for different root in OC_Filesystem_view class
|
|
|
|
// function testGetLegacyKey() {
|
|
|
|
//
|
2012-10-17 19:35:19 +04:00
|
|
|
// $c = new \OCA\Encryption\Util( $view, false );
|
2012-07-25 15:38:40 +04:00
|
|
|
//
|
|
|
|
// $bool = $c->getLegacyKey( 'admin' );
|
|
|
|
//
|
|
|
|
// $this->assertTrue( $bool );
|
|
|
|
//
|
|
|
|
// $this->assertTrue( $c->legacyKey );
|
|
|
|
//
|
|
|
|
// $this->assertTrue( is_int( $c->legacyKey ) );
|
|
|
|
//
|
|
|
|
// $this->assertTrue( strlen( $c->legacyKey ) == 20 );
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
|
|
|
// // Cannot use this test for now due to need for different root in OC_Filesystem_view class
|
|
|
|
// function testLegacyDecrypt() {
|
|
|
|
//
|
2012-10-17 19:35:19 +04:00
|
|
|
// $c = new OCA\Encryption\Util( $this->view, false );
|
2012-07-25 15:38:40 +04:00
|
|
|
//
|
|
|
|
// $bool = $c->getLegacyKey( 'admin' );
|
|
|
|
//
|
|
|
|
// $encrypted = $c->legacyEncrypt( $this->data, $c->legacyKey );
|
|
|
|
//
|
|
|
|
// $decrypted = $c->legacyDecrypt( $encrypted, $c->legacyKey );
|
|
|
|
//
|
2013-01-24 19:47:17 +04:00
|
|
|
// $this->assertEquals( $decrypted, $this->data );
|
2012-07-25 15:38:40 +04:00
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|