2012-04-17 22:56:53 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2012-07-24 20:53:12 +04:00
|
|
|
* Copyright (c) 2012 Sam Tuke <samtuke@owncloud.com>, and
|
|
|
|
* Robin Appelman <icewind@owncloud.com>
|
2012-04-17 22:56:53 +04:00
|
|
|
* 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-28 22:39:19 +04:00
|
|
|
require_once realpath( dirname(__FILE__).'/../../../3rdparty/Crypt_Blowfish/Blowfish.php' );
|
2012-08-15 17:52:03 +04:00
|
|
|
require_once realpath( dirname(__FILE__).'/../../../lib/base.php' );
|
2012-11-14 19:09:12 +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' );
|
2013-05-10 01:22:22 +04:00
|
|
|
require_once realpath( dirname(__FILE__).'/../lib/helper.php' );
|
2012-11-14 19:09:12 +04:00
|
|
|
require_once realpath( dirname(__FILE__).'/../appinfo/app.php' );
|
|
|
|
|
|
|
|
use OCA\Encryption;
|
2012-08-15 17:52:03 +04:00
|
|
|
|
2012-12-11 19:10:56 +04:00
|
|
|
// This has to go here because otherwise session errors arise, and the private
|
|
|
|
// encryption key needs to be saved in the session
|
|
|
|
|
2013-01-05 21:12:23 +04:00
|
|
|
/**
|
|
|
|
* @note It would be better to use Mockery here for mocking out the session
|
|
|
|
* handling process, and isolate calls to session class and data from the unit
|
|
|
|
* tests relating to them (stream etc.). However getting mockery to work and
|
|
|
|
* overload classes whilst also using the OC autoloader is difficult due to
|
|
|
|
* load order Pear errors.
|
|
|
|
*/
|
2012-12-12 21:39:43 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
class Test_Crypt extends \PHPUnit_Framework_TestCase {
|
2012-07-24 20:53:12 +04:00
|
|
|
|
2012-07-18 21:52:00 +04:00
|
|
|
function setUp() {
|
2013-04-30 01:41:49 +04:00
|
|
|
// reset backend
|
2013-05-15 04:38:08 +04:00
|
|
|
\OC_User::clearBackends();
|
2013-04-30 01:41:49 +04:00
|
|
|
\OC_User::useBackend('database');
|
|
|
|
|
2013-05-01 03:43:56 +04:00
|
|
|
// set content for encrypting / decrypting in tests
|
2012-08-23 19:43:10 +04:00
|
|
|
$this->dataLong = file_get_contents( realpath( dirname(__FILE__).'/../lib/crypt.php' ) );
|
|
|
|
$this->dataShort = 'hats';
|
|
|
|
$this->dataUrl = realpath( dirname(__FILE__).'/../lib/crypt.php' );
|
2012-07-25 15:38:40 +04:00
|
|
|
$this->legacyData = realpath( dirname(__FILE__).'/legacy-text.txt' );
|
2012-07-24 20:53:12 +04:00
|
|
|
$this->legacyEncryptedData = realpath( dirname(__FILE__).'/legacy-encrypted-text.txt' );
|
2012-11-14 19:09:12 +04:00
|
|
|
$this->randomKey = Encryption\Crypt::generateKey();
|
2012-08-23 19:43:10 +04:00
|
|
|
|
2012-11-28 22:39:19 +04:00
|
|
|
$keypair = Encryption\Crypt::createKeypair();
|
|
|
|
$this->genPublicKey = $keypair['publicKey'];
|
|
|
|
$this->genPrivateKey = $keypair['privateKey'];
|
|
|
|
|
2012-08-23 19:43:10 +04:00
|
|
|
$this->view = new \OC_FilesystemView( '/' );
|
2012-09-11 16:40:45 +04:00
|
|
|
|
2013-01-02 23:29:22 +04:00
|
|
|
\OC_User::setUserId( 'admin' );
|
2012-11-16 22:31:37 +04:00
|
|
|
$this->userId = 'admin';
|
2012-11-28 22:39:19 +04:00
|
|
|
$this->pass = 'admin';
|
2013-04-29 11:12:43 +04:00
|
|
|
|
|
|
|
$userHome = \OC_User::getHome($this->userId);
|
2013-04-30 01:41:49 +04:00
|
|
|
$this->dataDir = str_replace('/'.$this->userId, '', $userHome);
|
2013-04-29 11:12:43 +04:00
|
|
|
|
2013-05-15 04:38:08 +04:00
|
|
|
// Filesystem related hooks
|
|
|
|
\OCA\Encryption\Helper::registerFilesystemHooks();
|
|
|
|
|
2013-05-17 03:07:26 +04:00
|
|
|
// Filesystem related hooks
|
|
|
|
\OCA\Encryption\Helper::registerUserHooks();
|
|
|
|
|
2013-05-15 04:38:08 +04:00
|
|
|
\OC_FileProxy::register(new OCA\Encryption\Proxy());
|
|
|
|
|
2013-05-17 03:07:26 +04: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 04:38:08 +04:00
|
|
|
\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-05-01 03:43:56 +04:00
|
|
|
|
|
|
|
$params['uid'] = $this->userId;
|
|
|
|
$params['password'] = $this->pass;
|
|
|
|
OCA\Encryption\Hooks::login($params);
|
2013-05-15 04:38:08 +04:00
|
|
|
|
2012-07-18 21:52:00 +04:00
|
|
|
}
|
|
|
|
|
2013-01-02 23:29:22 +04:00
|
|
|
function tearDown() {
|
2013-05-16 02:36:40 +04:00
|
|
|
\OC_FileProxy::clearProxies();
|
2013-05-17 03:07:26 +04:00
|
|
|
|
|
|
|
// reset app files_trashbin
|
|
|
|
if ($this->stateFilesTrashbin) {
|
|
|
|
OC_App::enable('files_trashbin');
|
|
|
|
} else {
|
|
|
|
OC_App::disable('files_trashbin');
|
|
|
|
}
|
2013-05-15 04:38:08 +04:00
|
|
|
}
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2013-05-01 03:43:56 +04:00
|
|
|
function testGenerateKey() {
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
# TODO: use more accurate (larger) string length for test confirmation
|
|
|
|
|
2012-11-14 19:09:12 +04:00
|
|
|
$key = Encryption\Crypt::generateKey();
|
2012-07-24 20:53:12 +04:00
|
|
|
|
2012-08-14 22:06:56 +04:00
|
|
|
$this->assertTrue( strlen( $key ) > 16 );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-24 20:53:12 +04:00
|
|
|
function testGenerateIv() {
|
|
|
|
|
2012-11-14 19:09:12 +04:00
|
|
|
$iv = Encryption\Crypt::generateIv();
|
|
|
|
|
|
|
|
$this->assertEquals( 16, strlen( $iv ) );
|
|
|
|
|
|
|
|
return $iv;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testGenerateIv
|
|
|
|
*/
|
|
|
|
function testConcatIv( $iv ) {
|
|
|
|
|
2012-11-15 15:50:05 +04:00
|
|
|
$catFile = Encryption\Crypt::concatIv( $this->dataLong, $iv );
|
2012-11-14 19:09:12 +04:00
|
|
|
|
|
|
|
// Fetch encryption metadata from end of file
|
|
|
|
$meta = substr( $catFile, -22 );
|
|
|
|
|
2012-11-15 15:50:05 +04:00
|
|
|
$identifier = substr( $meta, 0, 6);
|
2012-11-14 19:09:12 +04:00
|
|
|
|
|
|
|
// Fetch IV from end of file
|
2012-11-15 15:50:05 +04:00
|
|
|
$foundIv = substr( $meta, 6 );
|
|
|
|
|
|
|
|
$this->assertEquals( '00iv00', $identifier );
|
2012-11-14 19:09:12 +04:00
|
|
|
|
|
|
|
$this->assertEquals( $iv, $foundIv );
|
|
|
|
|
|
|
|
// Remove IV and IV identifier text to expose encrypted content
|
|
|
|
$data = substr( $catFile, 0, -22 );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $data );
|
2012-11-15 15:50:05 +04:00
|
|
|
|
|
|
|
return array(
|
|
|
|
'iv' => $iv
|
|
|
|
, 'catfile' => $catFile
|
|
|
|
);
|
2012-11-14 19:09:12 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-11-15 15:50:05 +04:00
|
|
|
* @depends testConcatIv
|
2012-11-14 19:09:12 +04:00
|
|
|
*/
|
2012-11-15 15:50:05 +04:00
|
|
|
function testSplitIv( $testConcatIv ) {
|
2012-11-14 19:09:12 +04:00
|
|
|
|
2012-11-15 15:50:05 +04:00
|
|
|
// Split catfile into components
|
|
|
|
$splitCatfile = Encryption\Crypt::splitIv( $testConcatIv['catfile'] );
|
2012-11-14 19:09:12 +04:00
|
|
|
|
2012-11-15 15:50:05 +04:00
|
|
|
// Check that original IV and split IV match
|
|
|
|
$this->assertEquals( $testConcatIv['iv'], $splitCatfile['iv'] );
|
2012-11-14 19:09:12 +04:00
|
|
|
|
2012-11-15 15:50:05 +04:00
|
|
|
// Check that original data and split data match
|
|
|
|
$this->assertEquals( $this->dataLong, $splitCatfile['encrypted'] );
|
2012-07-24 20:53:12 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-11-16 22:31:37 +04:00
|
|
|
function testAddPadding() {
|
|
|
|
|
|
|
|
$padded = Encryption\Crypt::addPadding( $this->dataLong );
|
|
|
|
|
|
|
|
$padding = substr( $padded, -2 );
|
|
|
|
|
|
|
|
$this->assertEquals( 'xx' , $padding );
|
|
|
|
|
|
|
|
return $padded;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @depends testAddPadding
|
|
|
|
*/
|
|
|
|
function testRemovePadding( $padded ) {
|
|
|
|
|
|
|
|
$noPadding = Encryption\Crypt::RemovePadding( $padded );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $noPadding );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-18 21:52:00 +04:00
|
|
|
function testEncrypt() {
|
|
|
|
|
|
|
|
$random = openssl_random_pseudo_bytes( 13 );
|
|
|
|
|
|
|
|
$iv = substr( base64_encode( $random ), 0, -4 ); // i.e. E5IG033j+mRNKrht
|
|
|
|
|
2012-11-14 19:09:12 +04:00
|
|
|
$crypted = Encryption\Crypt::encrypt( $this->dataUrl, $iv, 'hat' );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-23 19:43:10 +04:00
|
|
|
$this->assertNotEquals( $this->dataUrl, $crypted );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testDecrypt() {
|
|
|
|
|
|
|
|
$random = openssl_random_pseudo_bytes( 13 );
|
|
|
|
|
|
|
|
$iv = substr( base64_encode( $random ), 0, -4 ); // i.e. E5IG033j+mRNKrht
|
|
|
|
|
2012-11-14 19:09:12 +04:00
|
|
|
$crypted = Encryption\Crypt::encrypt( $this->dataUrl, $iv, 'hat' );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-11-14 19:09:12 +04:00
|
|
|
$decrypt = Encryption\Crypt::decrypt( $crypted, $iv, 'hat' );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-23 19:43:10 +04:00
|
|
|
$this->assertEquals( $this->dataUrl, $decrypt );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function testSymmetricEncryptFileContent() {
|
|
|
|
|
|
|
|
# TODO: search in keyfile for actual content as IV will ensure this test always passes
|
|
|
|
|
2012-11-14 19:09:12 +04:00
|
|
|
$crypted = Encryption\Crypt::symmetricEncryptFileContent( $this->dataShort, 'hat' );
|
2012-08-16 22:18:18 +04:00
|
|
|
|
2012-10-16 18:02:51 +04:00
|
|
|
$this->assertNotEquals( $this->dataShort, $crypted );
|
2012-08-16 22:18:18 +04:00
|
|
|
|
|
|
|
|
2012-11-14 19:09:12 +04:00
|
|
|
$decrypt = Encryption\Crypt::symmetricDecryptFileContent( $crypted, 'hat' );
|
2012-08-16 22:18:18 +04:00
|
|
|
|
2012-10-16 18:02:51 +04:00
|
|
|
$this->assertEquals( $this->dataShort, $decrypt );
|
2012-08-16 22:18:18 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-10-17 19:35:19 +04:00
|
|
|
// These aren't used for now
|
|
|
|
// function testSymmetricBlockEncryptShortFileContent() {
|
|
|
|
//
|
2012-11-14 19:09:12 +04:00
|
|
|
// $crypted = Encryption\Crypt::symmetricBlockEncryptFileContent( $this->dataShort, $this->randomKey );
|
2012-10-17 19:35:19 +04:00
|
|
|
//
|
|
|
|
// $this->assertNotEquals( $this->dataShort, $crypted );
|
|
|
|
//
|
|
|
|
//
|
2012-11-14 19:09:12 +04:00
|
|
|
// $decrypt = Encryption\Crypt::symmetricBlockDecryptFileContent( $crypted, $this->randomKey );
|
2012-10-17 19:35:19 +04:00
|
|
|
//
|
|
|
|
// $this->assertEquals( $this->dataShort, $decrypt );
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// function testSymmetricBlockEncryptLongFileContent() {
|
|
|
|
//
|
2012-11-14 19:09:12 +04:00
|
|
|
// $crypted = Encryption\Crypt::symmetricBlockEncryptFileContent( $this->dataLong, $this->randomKey );
|
2012-10-17 19:35:19 +04:00
|
|
|
//
|
|
|
|
// $this->assertNotEquals( $this->dataLong, $crypted );
|
|
|
|
//
|
|
|
|
//
|
2012-11-14 19:09:12 +04:00
|
|
|
// $decrypt = Encryption\Crypt::symmetricBlockDecryptFileContent( $crypted, $this->randomKey );
|
2012-10-17 19:35:19 +04:00
|
|
|
//
|
|
|
|
// $this->assertEquals( $this->dataLong, $decrypt );
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
|
2013-04-30 01:41:49 +04:00
|
|
|
function testSymmetricStreamEncryptShortFileContent() {
|
2012-08-23 22:19:39 +04:00
|
|
|
|
2013-04-29 11:12:43 +04:00
|
|
|
$filename = 'tmp-'.time().'.test';
|
|
|
|
|
2012-10-10 21:40:59 +04:00
|
|
|
$cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataShort );
|
2012-08-23 22:19:39 +04:00
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
2013-04-29 11:12:43 +04:00
|
|
|
|
2013-04-30 01:41:49 +04:00
|
|
|
// Disable encryption proxy to prevent recursive calls
|
|
|
|
$proxyStatus = \OC_FileProxy::$enabled;
|
|
|
|
\OC_FileProxy::$enabled = false;
|
|
|
|
|
2013-04-29 11:12:43 +04:00
|
|
|
// Get file contents without using any wrapper to get it's actual contents on disk
|
2013-04-30 01:41:49 +04:00
|
|
|
$retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename);
|
|
|
|
|
|
|
|
// Re-enable proxy - our work is done
|
|
|
|
\OC_FileProxy::$enabled = $proxyStatus;
|
2013-04-29 11:12:43 +04:00
|
|
|
|
2012-08-23 22:19:39 +04:00
|
|
|
// Check that the file was encrypted before being written to disk
|
|
|
|
$this->assertNotEquals( $this->dataShort, $retreivedCryptedFile );
|
2013-04-29 11:12:43 +04:00
|
|
|
|
|
|
|
// Get the encrypted keyfile
|
|
|
|
$encKeyfile = Encryption\Keymanager::getFileKey( $this->view, $this->userId, $filename );
|
|
|
|
|
|
|
|
// Attempt to fetch the user's shareKey
|
|
|
|
$shareKey = Encryption\Keymanager::getShareKey( $this->view, $this->userId, $filename );
|
|
|
|
|
|
|
|
// get session
|
|
|
|
$session = new Encryption\Session( $this->view );
|
|
|
|
|
|
|
|
// get private key
|
|
|
|
$privateKey = $session->getPrivateKey( $this->userId );
|
|
|
|
|
|
|
|
// Decrypt keyfile with shareKey
|
|
|
|
$plainKeyfile = Encryption\Crypt::multiKeyDecrypt( $encKeyfile, $shareKey, $privateKey );
|
|
|
|
|
|
|
|
// Manually decrypt
|
|
|
|
$manualDecrypt = Encryption\Crypt::symmetricDecryptFileContent( $retreivedCryptedFile, $plainKeyfile );
|
|
|
|
|
2012-12-11 21:12:46 +04:00
|
|
|
// Check that decrypted data matches
|
2012-08-23 22:19:39 +04:00
|
|
|
$this->assertEquals( $this->dataShort, $manualDecrypt );
|
2013-04-30 01:41:49 +04:00
|
|
|
|
|
|
|
// Teardown
|
2013-05-15 04:38:08 +04:00
|
|
|
$this->view->unlink( $this->userId . '/files/' . $filename );
|
2013-04-30 01:41:49 +04:00
|
|
|
|
|
|
|
Encryption\Keymanager::deleteFileKey( $this->view, $this->userId, $filename );
|
2012-04-17 22:56:53 +04:00
|
|
|
}
|
2012-08-16 22:18:18 +04:00
|
|
|
|
2012-12-11 21:12:46 +04:00
|
|
|
/**
|
|
|
|
* @brief Test that data that is written by the crypto stream wrapper
|
|
|
|
* @note Encrypted data is manually prepared and decrypted here to avoid dependency on success of stream_read
|
|
|
|
* @note If this test fails with truncate content, check that enough array slices are being rejoined to form $e, as the crypt.php file may have gotten longer and broken the manual
|
|
|
|
* reassembly of its data
|
|
|
|
*/
|
|
|
|
function testSymmetricStreamEncryptLongFileContent() {
|
|
|
|
|
|
|
|
// Generate a a random filename
|
2013-04-30 01:41:49 +04:00
|
|
|
$filename = 'tmp-'.time().'.test';
|
2012-12-11 21:12:46 +04:00
|
|
|
|
|
|
|
// Save long data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataLong.$this->dataLong );
|
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
|
|
|
|
2013-04-30 01:41:49 +04:00
|
|
|
// Disable encryption proxy to prevent recursive calls
|
|
|
|
$proxyStatus = \OC_FileProxy::$enabled;
|
|
|
|
\OC_FileProxy::$enabled = false;
|
|
|
|
|
|
|
|
// Get file contents without using any wrapper to get it's actual contents on disk
|
|
|
|
$retreivedCryptedFile = $this->view->file_get_contents($this->userId . '/files/' . $filename);
|
|
|
|
|
|
|
|
// Re-enable proxy - our work is done
|
|
|
|
\OC_FileProxy::$enabled = $proxyStatus;
|
|
|
|
|
|
|
|
|
|
|
|
// Check that the file was encrypted before being written to disk
|
2012-12-11 21:12:46 +04:00
|
|
|
$this->assertNotEquals( $this->dataLong.$this->dataLong, $retreivedCryptedFile );
|
|
|
|
|
|
|
|
// Manuallly split saved file into separate IVs and encrypted chunks
|
|
|
|
$r = preg_split('/(00iv00.{16,18})/', $retreivedCryptedFile, NULL, PREG_SPLIT_DELIM_CAPTURE);
|
|
|
|
|
|
|
|
//print_r($r);
|
|
|
|
|
|
|
|
// Join IVs and their respective data chunks
|
|
|
|
$e = array( $r[0].$r[1], $r[2].$r[3], $r[4].$r[5], $r[6].$r[7], $r[8].$r[9], $r[10].$r[11], $r[12].$r[13] );//.$r[11], $r[12].$r[13], $r[14] );
|
|
|
|
|
|
|
|
//print_r($e);
|
2013-04-30 01:41:49 +04:00
|
|
|
|
|
|
|
// Get the encrypted keyfile
|
|
|
|
$encKeyfile = Encryption\Keymanager::getFileKey( $this->view, $this->userId, $filename );
|
|
|
|
|
|
|
|
// Attempt to fetch the user's shareKey
|
|
|
|
$shareKey = Encryption\Keymanager::getShareKey( $this->view, $this->userId, $filename );
|
|
|
|
|
|
|
|
// get session
|
|
|
|
$session = new Encryption\Session( $this->view );
|
|
|
|
|
|
|
|
// get private key
|
|
|
|
$privateKey = $session->getPrivateKey( $this->userId );
|
|
|
|
|
|
|
|
// Decrypt keyfile with shareKey
|
|
|
|
$plainKeyfile = Encryption\Crypt::multiKeyDecrypt( $encKeyfile, $shareKey, $privateKey );
|
|
|
|
|
2012-12-11 21:12:46 +04:00
|
|
|
// Set var for reassembling decrypted content
|
|
|
|
$decrypt = '';
|
|
|
|
|
|
|
|
// Manually decrypt chunk
|
|
|
|
foreach ($e as $e) {
|
|
|
|
|
2013-04-30 01:41:49 +04:00
|
|
|
$chunkDecrypt = Encryption\Crypt::symmetricDecryptFileContent( $e, $plainKeyfile );
|
2012-12-11 21:12:46 +04:00
|
|
|
|
|
|
|
// Assemble decrypted chunks
|
|
|
|
$decrypt .= $chunkDecrypt;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong.$this->dataLong, $decrypt );
|
|
|
|
|
|
|
|
// Teardown
|
|
|
|
|
2013-05-15 04:38:08 +04:00
|
|
|
$this->view->unlink( $this->userId . '/files/' . $filename );
|
2012-12-11 21:12:46 +04:00
|
|
|
|
2013-04-29 11:12:43 +04:00
|
|
|
Encryption\Keymanager::deleteFileKey( $this->view, $this->userId, $filename );
|
2012-12-11 21:12:46 +04:00
|
|
|
|
|
|
|
}
|
2012-10-10 21:40:59 +04:00
|
|
|
|
2012-10-16 18:02:51 +04:00
|
|
|
/**
|
|
|
|
* @brief Test that data that is read by the crypto stream wrapper
|
|
|
|
*/
|
2012-10-10 21:40:59 +04:00
|
|
|
function testSymmetricStreamDecryptShortFileContent() {
|
|
|
|
|
|
|
|
$filename = 'tmp-'.time();
|
|
|
|
|
2012-10-16 18:02:51 +04:00
|
|
|
// Save long data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataShort );
|
2012-10-10 21:40:59 +04:00
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
2013-05-15 04:38:08 +04:00
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$decrypt = file_get_contents( 'crypt://' . $filename );
|
2012-10-10 21:40:59 +04:00
|
|
|
|
2012-10-16 18:02:51 +04:00
|
|
|
$this->assertEquals( $this->dataShort, $decrypt );
|
2013-05-15 04:38:08 +04:00
|
|
|
|
|
|
|
// tear down
|
|
|
|
$this->view->unlink( $this->userId . '/files/' . $filename );
|
2012-10-16 18:02:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
function testSymmetricStreamDecryptLongFileContent() {
|
2012-10-10 21:40:59 +04:00
|
|
|
|
2012-10-16 18:02:51 +04:00
|
|
|
$filename = 'tmp-'.time();
|
2012-10-10 21:40:59 +04:00
|
|
|
|
2012-10-16 18:02:51 +04:00
|
|
|
// Save long data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataLong );
|
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
2013-05-15 04:38:08 +04:00
|
|
|
|
|
|
|
// Get file decrypted contents
|
2012-10-16 18:02:51 +04:00
|
|
|
$decrypt = file_get_contents( 'crypt://' . $filename );
|
2013-05-15 04:38:08 +04:00
|
|
|
|
2012-10-16 18:02:51 +04:00
|
|
|
$this->assertEquals( $this->dataLong, $decrypt );
|
2013-05-15 04:38:08 +04:00
|
|
|
|
|
|
|
// tear down
|
|
|
|
$this->view->unlink( $this->userId . '/files/' . $filename );
|
2012-10-10 21:40:59 +04:00
|
|
|
}
|
2012-08-16 22:18:18 +04:00
|
|
|
|
2012-10-10 21:40:59 +04:00
|
|
|
// Is this test still necessary?
|
2012-08-23 19:43:10 +04:00
|
|
|
// function testSymmetricBlockStreamDecryptFileContent() {
|
|
|
|
//
|
|
|
|
// \OC_User::setUserId( 'admin' );
|
|
|
|
//
|
|
|
|
// // Disable encryption proxy to prevent unwanted en/decryption
|
|
|
|
// \OC_FileProxy::$enabled = false;
|
|
|
|
//
|
|
|
|
// $cryptedFile = file_put_contents( 'crypt://' . '/blockEncrypt', $this->dataUrl );
|
|
|
|
//
|
|
|
|
// // Disable encryption proxy to prevent unwanted en/decryption
|
|
|
|
// \OC_FileProxy::$enabled = false;
|
|
|
|
//
|
|
|
|
// echo "\n\n\$cryptedFile = " . $this->view->file_get_contents( '/blockEncrypt' );
|
|
|
|
//
|
|
|
|
// $retreivedCryptedFile = file_get_contents( 'crypt://' . '/blockEncrypt' );
|
|
|
|
//
|
|
|
|
// $this->assertEquals( $this->dataUrl, $retreivedCryptedFile );
|
|
|
|
//
|
|
|
|
// \OC_FileProxy::$enabled = false;
|
|
|
|
//
|
|
|
|
// }
|
2012-06-16 01:48:39 +04:00
|
|
|
|
2012-07-18 21:52:00 +04:00
|
|
|
function testSymmetricEncryptFileContentKeyfile() {
|
2012-06-16 01:48:39 +04:00
|
|
|
|
2012-07-18 21:52:00 +04:00
|
|
|
# TODO: search in keyfile for actual content as IV will ensure this test always passes
|
|
|
|
|
2012-11-14 19:09:12 +04:00
|
|
|
$crypted = Encryption\Crypt::symmetricEncryptFileContentKeyfile( $this->dataUrl );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-23 19:43:10 +04:00
|
|
|
$this->assertNotEquals( $this->dataUrl, $crypted['encrypted'] );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
|
2012-11-14 19:09:12 +04:00
|
|
|
$decrypt = Encryption\Crypt::symmetricDecryptFileContent( $crypted['encrypted'], $crypted['key'] );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-23 19:43:10 +04:00
|
|
|
$this->assertEquals( $this->dataUrl, $decrypt );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-24 20:53:12 +04:00
|
|
|
function testIsEncryptedContent() {
|
|
|
|
|
2013-03-09 22:18:34 +04:00
|
|
|
$this->assertFalse( Encryption\Crypt::isCatfileContent( $this->dataUrl ) );
|
2012-07-24 20:53:12 +04:00
|
|
|
|
2013-03-09 22:18:34 +04:00
|
|
|
$this->assertFalse( Encryption\Crypt::isCatfileContent( $this->legacyEncryptedData ) );
|
2012-07-24 20:53:12 +04:00
|
|
|
|
2012-11-14 19:09:12 +04:00
|
|
|
$keyfileContent = Encryption\Crypt::symmetricEncryptFileContent( $this->dataUrl, 'hat' );
|
2012-07-24 20:53:12 +04:00
|
|
|
|
2013-03-09 22:18:34 +04:00
|
|
|
$this->assertTrue( Encryption\Crypt::isCatfileContent( $keyfileContent ) );
|
2012-07-24 20:53:12 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-07-18 21:52:00 +04:00
|
|
|
function testMultiKeyEncrypt() {
|
|
|
|
|
|
|
|
# TODO: search in keyfile for actual content as IV will ensure this test always passes
|
|
|
|
|
2012-11-14 19:09:12 +04:00
|
|
|
$pair1 = Encryption\Crypt::createKeypair();
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-08-15 17:52:03 +04:00
|
|
|
$this->assertEquals( 2, count( $pair1 ) );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
|
|
|
$this->assertTrue( strlen( $pair1['publicKey'] ) > 1 );
|
|
|
|
|
|
|
|
$this->assertTrue( strlen( $pair1['privateKey'] ) > 1 );
|
|
|
|
|
2012-06-16 01:48:39 +04:00
|
|
|
|
2013-02-11 14:21:23 +04:00
|
|
|
$crypted = Encryption\Crypt::multiKeyEncrypt( $this->dataShort, array( $pair1['publicKey'] ) );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2013-02-11 14:21:23 +04:00
|
|
|
$this->assertNotEquals( $this->dataShort, $crypted['data'] );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-06-16 01:48:39 +04:00
|
|
|
|
2013-02-11 14:21:23 +04:00
|
|
|
$decrypt = Encryption\Crypt::multiKeyDecrypt( $crypted['data'], $crypted['keys'][0], $pair1['privateKey'] );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2013-02-11 14:21:23 +04:00
|
|
|
$this->assertEquals( $this->dataShort, $decrypt );
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-06-16 01:48:39 +04:00
|
|
|
}
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
function testKeyEncrypt() {
|
|
|
|
|
|
|
|
// Generate keypair
|
2012-11-14 19:09:12 +04:00
|
|
|
$pair1 = Encryption\Crypt::createKeypair();
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
// Encrypt data
|
2012-11-14 19:09:12 +04:00
|
|
|
$crypted = Encryption\Crypt::keyEncrypt( $this->dataUrl, $pair1['publicKey'] );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
2012-08-23 19:43:10 +04:00
|
|
|
$this->assertNotEquals( $this->dataUrl, $crypted );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
// Decrypt data
|
2012-11-14 19:09:12 +04:00
|
|
|
$decrypt = Encryption\Crypt::keyDecrypt( $crypted, $pair1['privateKey'] );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
2012-08-23 19:43:10 +04:00
|
|
|
$this->assertEquals( $this->dataUrl, $decrypt );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-11 19:10:56 +04:00
|
|
|
// What is the point of this test? It doesn't use keyEncryptKeyfile()
|
2012-08-14 22:06:56 +04:00
|
|
|
function testKeyEncryptKeyfile() {
|
|
|
|
|
|
|
|
# TODO: Don't repeat encryption from previous tests, use PHPUnit test interdependency instead
|
|
|
|
|
|
|
|
// Generate keypair
|
2012-11-14 19:09:12 +04:00
|
|
|
$pair1 = Encryption\Crypt::createKeypair();
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
// Encrypt plain data, generate keyfile & encrypted file
|
2012-11-14 19:09:12 +04:00
|
|
|
$cryptedData = Encryption\Crypt::symmetricEncryptFileContentKeyfile( $this->dataUrl );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
// Encrypt keyfile
|
2012-11-14 19:09:12 +04:00
|
|
|
$cryptedKey = Encryption\Crypt::keyEncrypt( $cryptedData['key'], $pair1['publicKey'] );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
// Decrypt keyfile
|
2012-11-14 19:09:12 +04:00
|
|
|
$decryptKey = Encryption\Crypt::keyDecrypt( $cryptedKey, $pair1['privateKey'] );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
// Decrypt encrypted file
|
2012-11-14 19:09:12 +04:00
|
|
|
$decryptData = Encryption\Crypt::symmetricDecryptFileContent( $cryptedData['encrypted'], $decryptKey );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
2012-08-23 19:43:10 +04:00
|
|
|
$this->assertEquals( $this->dataUrl, $decryptData );
|
2012-08-14 22:06:56 +04:00
|
|
|
|
|
|
|
}
|
2012-12-11 19:10:56 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test functionality of keyEncryptKeyfile() and
|
|
|
|
* keyDecryptKeyfile()
|
|
|
|
*/
|
|
|
|
function testKeyDecryptKeyfile() {
|
|
|
|
|
|
|
|
$encrypted = Encryption\Crypt::keyEncryptKeyfile( $this->dataShort, $this->genPublicKey );
|
|
|
|
|
|
|
|
$this->assertNotEquals( $encrypted['data'], $this->dataShort );
|
|
|
|
|
|
|
|
$decrypted = Encryption\Crypt::keyDecryptKeyfile( $encrypted['data'], $encrypted['key'], $this->genPrivateKey );
|
|
|
|
|
|
|
|
$this->assertEquals( $decrypted, $this->dataShort );
|
|
|
|
|
|
|
|
}
|
2012-07-18 21:52:00 +04:00
|
|
|
|
2012-11-28 22:39:19 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test encryption using legacy blowfish method
|
|
|
|
*/
|
|
|
|
function testLegacyEncryptShort() {
|
|
|
|
|
|
|
|
$crypted = Encryption\Crypt::legacyEncrypt( $this->dataShort, $this->pass );
|
|
|
|
|
|
|
|
$this->assertNotEquals( $this->dataShort, $crypted );
|
|
|
|
|
|
|
|
# TODO: search inencrypted text for actual content to ensure it
|
|
|
|
# genuine transformation
|
|
|
|
|
|
|
|
return $crypted;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-11 21:12:46 +04:00
|
|
|
/**
|
|
|
|
* @brief test decryption using legacy blowfish method
|
|
|
|
* @depends testLegacyEncryptShort
|
|
|
|
*/
|
|
|
|
function testLegacyDecryptShort( $crypted ) {
|
|
|
|
|
|
|
|
$decrypted = Encryption\Crypt::legacyDecrypt( $crypted, $this->pass );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataShort, $decrypted );
|
|
|
|
|
|
|
|
}
|
2012-11-28 22:39:19 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test encryption using legacy blowfish method
|
|
|
|
*/
|
|
|
|
function testLegacyEncryptLong() {
|
|
|
|
|
|
|
|
$crypted = Encryption\Crypt::legacyEncrypt( $this->dataLong, $this->pass );
|
|
|
|
|
|
|
|
$this->assertNotEquals( $this->dataLong, $crypted );
|
|
|
|
|
|
|
|
# TODO: search inencrypted text for actual content to ensure it
|
|
|
|
# genuine transformation
|
|
|
|
|
|
|
|
return $crypted;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-11 21:12:46 +04:00
|
|
|
/**
|
|
|
|
* @brief test decryption using legacy blowfish method
|
|
|
|
* @depends testLegacyEncryptLong
|
|
|
|
*/
|
|
|
|
function testLegacyDecryptLong( $crypted ) {
|
|
|
|
|
|
|
|
$decrypted = Encryption\Crypt::legacyDecrypt( $crypted, $this->pass );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $decrypted );
|
|
|
|
|
|
|
|
}
|
2012-11-28 22:39:19 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test generation of legacy encryption key
|
|
|
|
* @depends testLegacyDecryptShort
|
|
|
|
*/
|
|
|
|
function testLegacyCreateKey() {
|
|
|
|
|
|
|
|
// Create encrypted key
|
|
|
|
$encKey = Encryption\Crypt::legacyCreateKey( $this->pass );
|
|
|
|
|
|
|
|
// Decrypt key
|
|
|
|
$key = Encryption\Crypt::legacyDecrypt( $encKey, $this->pass );
|
|
|
|
|
|
|
|
$this->assertTrue( is_numeric( $key ) );
|
|
|
|
|
|
|
|
// Check that key is correct length
|
|
|
|
$this->assertEquals( 20, strlen( $key ) );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief test decryption using legacy blowfish method
|
|
|
|
* @depends testLegacyEncryptLong
|
|
|
|
*/
|
|
|
|
function testLegacyKeyRecryptKeyfileEncrypt( $crypted ) {
|
|
|
|
|
|
|
|
$recrypted = Encryption\Crypt::LegacyKeyRecryptKeyfile( $crypted, $this->pass, $this->genPublicKey, $this->pass );
|
|
|
|
|
|
|
|
$this->assertNotEquals( $this->dataLong, $recrypted['data'] );
|
|
|
|
|
|
|
|
return $recrypted;
|
|
|
|
|
|
|
|
# TODO: search inencrypted text for actual content to ensure it
|
|
|
|
# genuine transformation
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-15 04:38:08 +04:00
|
|
|
function testRenameFile() {
|
|
|
|
|
|
|
|
$filename = 'tmp-'.time();
|
|
|
|
|
|
|
|
// Save long data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataLong );
|
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$decrypt = file_get_contents( 'crypt://' . $filename );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $decrypt );
|
|
|
|
|
|
|
|
$newFilename = 'tmp-new-'.time();
|
|
|
|
$view = new \OC\Files\View('/' . $this->userId . '/files');
|
|
|
|
$view->rename( $filename, $newFilename );
|
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$newDecrypt = file_get_contents( 'crypt://' . $newFilename );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $newDecrypt );
|
|
|
|
|
|
|
|
// tear down
|
|
|
|
$view->unlink( $newFilename );
|
|
|
|
}
|
|
|
|
|
|
|
|
function testMoveFileIntoFolder() {
|
|
|
|
|
|
|
|
$filename = 'tmp-'.time();
|
|
|
|
|
|
|
|
// Save long data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataLong );
|
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$decrypt = file_get_contents( 'crypt://' . $filename );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $decrypt );
|
|
|
|
|
|
|
|
$newFolder = '/newfolder1';
|
|
|
|
$newFilename = 'tmp-new-'.time();
|
|
|
|
$view = new \OC\Files\View('/' . $this->userId . '/files');
|
|
|
|
$view->mkdir($newFolder);
|
|
|
|
$view->rename( $filename, $newFolder . '/' . $newFilename );
|
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$newDecrypt = file_get_contents( 'crypt://' . $newFolder . '/' . $newFilename );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $newDecrypt );
|
|
|
|
|
|
|
|
// tear down
|
|
|
|
$view->unlink( $newFolder );
|
|
|
|
}
|
|
|
|
|
2013-05-16 00:42:22 +04:00
|
|
|
function testRenameFolder() {
|
|
|
|
|
|
|
|
$filename = '/tmp-'.time();
|
|
|
|
|
|
|
|
$folder = '/folder';
|
|
|
|
$newFolder = '/newfolder';
|
|
|
|
$view = new \OC\Files\View('/' . $this->userId . '/files');
|
|
|
|
$view->mkdir($folder);
|
|
|
|
|
|
|
|
// Save long data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = file_put_contents( 'crypt://' . $folder . $filename, $this->dataLong );
|
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$decrypt = file_get_contents( 'crypt://' . $folder . $filename );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $decrypt );
|
|
|
|
|
|
|
|
// rename folder
|
|
|
|
$view->rename($folder, $newFolder);
|
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$newDecrypt = file_get_contents( 'crypt://' . $newFolder . $filename );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $newDecrypt );
|
|
|
|
|
|
|
|
// tear down
|
|
|
|
$view->unlink( $newFolder );
|
|
|
|
}
|
|
|
|
|
2013-05-15 23:00:35 +04:00
|
|
|
function testChangePassphrase() {
|
|
|
|
|
|
|
|
$filename = 'tmp-'.time();
|
|
|
|
|
|
|
|
// Save long data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = file_put_contents( 'crypt://' . $filename, $this->dataLong );
|
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$decrypt = file_get_contents( 'crypt://' . $filename );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $decrypt );
|
|
|
|
|
|
|
|
// change password
|
2013-05-16 00:42:22 +04:00
|
|
|
\OC_User::setPassword($this->userId, 'test');
|
2013-05-15 23:00:35 +04:00
|
|
|
|
|
|
|
// relogin
|
|
|
|
$params['uid'] = $this->userId;
|
|
|
|
$params['password'] = 'test';
|
|
|
|
OCA\Encryption\Hooks::login($params);
|
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$newDecrypt = file_get_contents( 'crypt://' . $filename );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $newDecrypt );
|
|
|
|
|
|
|
|
// tear down
|
|
|
|
// change password back
|
2013-05-16 00:42:22 +04:00
|
|
|
\OC_User::setPassword($this->userId, $this->pass);
|
2013-05-15 23:00:35 +04:00
|
|
|
$view = new \OC\Files\View('/' . $this->userId . '/files');
|
|
|
|
$view->unlink( $filename );
|
|
|
|
}
|
2013-05-16 02:36:40 +04:00
|
|
|
|
|
|
|
function testViewFilePutAndGetContents() {
|
|
|
|
|
|
|
|
$filename = '/tmp-'.time();
|
|
|
|
$view = new \OC\Files\View('/' . $this->userId . '/files');
|
|
|
|
|
|
|
|
// Save short data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = $view->file_put_contents( $filename, $this->dataShort );
|
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$decrypt = $view->file_get_contents( $filename );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataShort, $decrypt );
|
|
|
|
|
|
|
|
// Save long data as encrypted file using stream wrapper
|
|
|
|
$cryptedFileLong = $view->file_put_contents( $filename, $this->dataLong );
|
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFileLong ) );
|
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$decryptLong = $view->file_get_contents( $filename );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataLong, $decryptLong );
|
|
|
|
|
|
|
|
// tear down
|
|
|
|
$view->unlink( $filename );
|
|
|
|
}
|
|
|
|
|
2013-05-16 02:44:40 +04:00
|
|
|
function testTouchExistingFile() {
|
2013-05-16 02:36:40 +04:00
|
|
|
$filename = '/tmp-'.time();
|
|
|
|
$view = new \OC\Files\View('/' . $this->userId . '/files');
|
|
|
|
|
|
|
|
// Save short data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = $view->file_put_contents( $filename, $this->dataShort );
|
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
|
|
|
|
|
|
|
$view->touch($filename);
|
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$decrypt = $view->file_get_contents( $filename );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataShort, $decrypt );
|
|
|
|
|
|
|
|
// tear down
|
|
|
|
$view->unlink( $filename );
|
|
|
|
}
|
|
|
|
|
2013-05-16 02:44:40 +04:00
|
|
|
function testTouchFile() {
|
|
|
|
$filename = '/tmp-'.time();
|
|
|
|
$view = new \OC\Files\View('/' . $this->userId . '/files');
|
|
|
|
|
|
|
|
$view->touch($filename);
|
|
|
|
|
|
|
|
// Save short data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = $view->file_put_contents( $filename, $this->dataShort );
|
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$decrypt = $view->file_get_contents( $filename );
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataShort, $decrypt );
|
|
|
|
|
|
|
|
// tear down
|
|
|
|
$view->unlink( $filename );
|
|
|
|
}
|
|
|
|
|
2013-05-16 02:36:40 +04:00
|
|
|
function testFopenFile() {
|
|
|
|
$filename = '/tmp-'.time();
|
|
|
|
$view = new \OC\Files\View('/' . $this->userId . '/files');
|
|
|
|
|
|
|
|
// Save short data as encrypted file using stream wrapper
|
|
|
|
$cryptedFile = $view->file_put_contents( $filename, $this->dataShort );
|
|
|
|
|
|
|
|
// Test that data was successfully written
|
|
|
|
$this->assertTrue( is_int( $cryptedFile ) );
|
|
|
|
|
|
|
|
$handle = $view->fopen($filename, 'r');
|
|
|
|
|
|
|
|
// Get file decrypted contents
|
|
|
|
$decrypt = fgets($handle);
|
|
|
|
|
|
|
|
$this->assertEquals( $this->dataShort, $decrypt );
|
|
|
|
|
|
|
|
// tear down
|
|
|
|
$view->unlink( $filename );
|
|
|
|
}
|
2012-07-18 21:52:00 +04:00
|
|
|
// function testEncryption(){
|
|
|
|
//
|
|
|
|
// $key=uniqid();
|
|
|
|
// $file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
|
|
|
|
// $source=file_get_contents($file); //nice large text file
|
2012-11-14 19:09:12 +04:00
|
|
|
// $encrypted=OC_Encryption\Crypt::encrypt($source,$key);
|
|
|
|
// $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
|
2012-07-18 21:52:00 +04:00
|
|
|
// $decrypted=rtrim($decrypted, "\0");
|
2012-08-15 17:52:03 +04:00
|
|
|
// $this->assertNotEquals($encrypted,$source);
|
2013-01-24 19:47:17 +04:00
|
|
|
// $this->assertEquals($decrypted,$source);
|
2012-07-18 21:52:00 +04:00
|
|
|
//
|
|
|
|
// $chunk=substr($source,0,8192);
|
2012-11-14 19:09:12 +04:00
|
|
|
// $encrypted=OC_Encryption\Crypt::encrypt($chunk,$key);
|
2013-01-24 19:47:17 +04:00
|
|
|
// $this->assertEquals(strlen($chunk),strlen($encrypted));
|
2012-11-14 19:09:12 +04:00
|
|
|
// $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
|
2012-07-18 21:52:00 +04:00
|
|
|
// $decrypted=rtrim($decrypted, "\0");
|
2013-01-24 19:47:17 +04:00
|
|
|
// $this->assertEquals($decrypted,$chunk);
|
2012-07-18 21:52:00 +04:00
|
|
|
//
|
2012-11-14 19:09:12 +04:00
|
|
|
// $encrypted=OC_Encryption\Crypt::blockEncrypt($source,$key);
|
|
|
|
// $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key);
|
2012-08-15 17:52:03 +04:00
|
|
|
// $this->assertNotEquals($encrypted,$source);
|
2013-01-24 19:47:17 +04:00
|
|
|
// $this->assertEquals($decrypted,$source);
|
2012-07-18 21:52:00 +04:00
|
|
|
//
|
|
|
|
// $tmpFileEncrypted=OCP\Files::tmpFile();
|
2012-11-14 19:09:12 +04:00
|
|
|
// OC_Encryption\Crypt::encryptfile($file,$tmpFileEncrypted,$key);
|
2012-07-18 21:52:00 +04:00
|
|
|
// $encrypted=file_get_contents($tmpFileEncrypted);
|
2012-11-14 19:09:12 +04:00
|
|
|
// $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key);
|
2012-08-15 17:52:03 +04:00
|
|
|
// $this->assertNotEquals($encrypted,$source);
|
2013-01-24 19:47:17 +04:00
|
|
|
// $this->assertEquals($decrypted,$source);
|
2012-07-18 21:52:00 +04:00
|
|
|
//
|
|
|
|
// $tmpFileDecrypted=OCP\Files::tmpFile();
|
2012-11-14 19:09:12 +04:00
|
|
|
// OC_Encryption\Crypt::decryptfile($tmpFileEncrypted,$tmpFileDecrypted,$key);
|
2012-07-18 21:52:00 +04:00
|
|
|
// $decrypted=file_get_contents($tmpFileDecrypted);
|
2013-01-24 19:47:17 +04:00
|
|
|
// $this->assertEquals($decrypted,$source);
|
2012-07-18 21:52:00 +04:00
|
|
|
//
|
|
|
|
// $file=OC::$SERVERROOT.'/core/img/weather-clear.png';
|
|
|
|
// $source=file_get_contents($file); //binary file
|
2012-11-14 19:09:12 +04:00
|
|
|
// $encrypted=OC_Encryption\Crypt::encrypt($source,$key);
|
|
|
|
// $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
|
2012-07-18 21:52:00 +04:00
|
|
|
// $decrypted=rtrim($decrypted, "\0");
|
2013-01-24 19:47:17 +04:00
|
|
|
// $this->assertEquals($decrypted,$source);
|
2012-07-18 21:52:00 +04:00
|
|
|
//
|
2012-11-14 19:09:12 +04:00
|
|
|
// $encrypted=OC_Encryption\Crypt::blockEncrypt($source,$key);
|
|
|
|
// $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key);
|
2013-01-24 19:47:17 +04:00
|
|
|
// $this->assertEquals($decrypted,$source);
|
2012-07-18 21:52:00 +04:00
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// function testBinary(){
|
|
|
|
// $key=uniqid();
|
|
|
|
//
|
|
|
|
// $file=__DIR__.'/binary';
|
|
|
|
// $source=file_get_contents($file); //binary file
|
2012-11-14 19:09:12 +04:00
|
|
|
// $encrypted=OC_Encryption\Crypt::encrypt($source,$key);
|
|
|
|
// $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
|
2012-07-18 21:52:00 +04:00
|
|
|
//
|
|
|
|
// $decrypted=rtrim($decrypted, "\0");
|
2013-01-24 19:47:17 +04:00
|
|
|
// $this->assertEquals($decrypted,$source);
|
2012-07-18 21:52:00 +04:00
|
|
|
//
|
2012-11-14 19:09:12 +04:00
|
|
|
// $encrypted=OC_Encryption\Crypt::blockEncrypt($source,$key);
|
|
|
|
// $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key,strlen($source));
|
2013-01-24 19:47:17 +04:00
|
|
|
// $this->assertEquals($decrypted,$source);
|
2012-07-18 21:52:00 +04:00
|
|
|
// }
|
|
|
|
|
2012-04-17 22:56:53 +04:00
|
|
|
}
|