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' );
|
|
|
|
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;
|
|
|
|
|
2012-08-15 13:01:43 +00:00
|
|
|
class Test_Keymanager extends \PHPUnit_Framework_TestCase {
|
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
|
2012-08-23 15:43:10 +00:00
|
|
|
// Set data for use in tests
|
|
|
|
$this->data = realpath( dirname(__FILE__).'/../lib/crypt.php' );
|
2012-08-15 13:01:43 +00:00
|
|
|
$this->user = 'admin';
|
2012-08-15 13:13:03 +00:00
|
|
|
$this->passphrase = 'admin';
|
2013-01-05 17:12:23 +00:00
|
|
|
$this->filePath = '/testing';
|
2012-08-15 13:01:43 +00:00
|
|
|
$this->view = new \OC_FilesystemView( '' );
|
|
|
|
|
|
|
|
// Disable encryption proxy to prevent recursive calls
|
|
|
|
\OC_FileProxy::$enabled = false;
|
2013-01-05 17:12:23 +00:00
|
|
|
|
|
|
|
// Notify system which iser is logged in etc.
|
|
|
|
\OC_User::setUserId( 'admin' );
|
2012-08-15 13:01:43 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function tearDown(){
|
|
|
|
|
2012-08-15 13:13:03 +00:00
|
|
|
\OC_FileProxy::$enabled = true;
|
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-05 17:12:23 +00:00
|
|
|
$key = Encryption\Keymanager::getPrivateKey( $this->view, $this->user );
|
|
|
|
|
|
|
|
|
|
|
|
// Will this length vary? Perhaps we should use a range instead
|
|
|
|
$this->assertEquals( 2296, strlen( $key ) );
|
2012-08-15 13:13:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-01-05 17:12:23 +00:00
|
|
|
function testGetPublicKey() {
|
|
|
|
|
|
|
|
$key = Encryption\Keymanager::getPublicKey( $this->view, $this->user );
|
|
|
|
|
|
|
|
$this->assertEquals( 451, strlen( $key ) );
|
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
// $key = Crypt::symmetricEncryptFileContentKeyfile( $this->data, 'hat' );
|
|
|
|
//
|
|
|
|
// $tmpPath = sys_get_temp_dir(). '/' . 'testSetFileKey';
|
|
|
|
//
|
|
|
|
// $view = new \OC_FilesystemView( '/tmp/' );
|
|
|
|
//
|
|
|
|
// //$view = new \OC_FilesystemView( '/' . $this->user . '/files_encryption/keyfiles' );
|
|
|
|
//
|
2012-11-16 18:31:37 +00:00
|
|
|
// Encryption\Keymanager::setFileKey( $tmpPath, $key['key'], $view );
|
2012-08-23 15:43:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-01-05 17:12:23 +00:00
|
|
|
function testGetPrivateKey_decrypt() {
|
2012-08-15 13:13:03 +00:00
|
|
|
|
2013-01-05 17:12:23 +00:00
|
|
|
$key = Encryption\Keymanager::getPrivateKey( $this->view, $this->user );
|
2012-08-15 13:13:03 +00:00
|
|
|
|
2012-08-15 17:49:53 +00:00
|
|
|
# TODO: replace call to Crypt with a mock object?
|
2012-11-16 18:31:37 +00:00
|
|
|
$decrypted = Encryption\Crypt::symmetricDecryptFileContent( $key, $this->passphrase );
|
|
|
|
|
2013-01-05 17:12:23 +00:00
|
|
|
$this->assertEquals( 1704, strlen( $decrypted ) );
|
2012-08-15 13:01:43 +00:00
|
|
|
|
2012-08-15 13:13:03 +00:00
|
|
|
$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() {
|
|
|
|
|
|
|
|
$keys = Encryption\Keymanager::getUserKeys( $this->view, $this->user );
|
|
|
|
|
|
|
|
$this->assertEquals( 451, strlen( $keys['publicKey'] ) );
|
|
|
|
$this->assertEquals( '-----BEGIN PUBLIC KEY-----', substr( $keys['publicKey'], 0, 26 ) );
|
|
|
|
$this->assertEquals( 2296, strlen( $keys['privateKey'] ) );
|
2012-08-23 15:43:10 +00:00
|
|
|
|
2013-01-05 17:12:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testGetPublicKeys() {
|
|
|
|
|
|
|
|
# TODO: write me
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testGetFileKey() {
|
|
|
|
|
|
|
|
// Encryption\Keymanager::getFileKey( $this->view, $this->user, $this->filePath );
|
|
|
|
|
|
|
|
}
|
2012-08-23 15:43:10 +00:00
|
|
|
|
2012-08-15 13:01:43 +00:00
|
|
|
}
|