improved tests

This commit is contained in:
Florin Peter 2013-05-22 00:55:16 +02:00
parent 9ca9a22c6a
commit afbfa742d7
6 changed files with 301 additions and 388 deletions

View File

@ -359,24 +359,6 @@ class Proxy extends \OC_FileProxy
} }
/**
* @param $path
* @param $mime
* @return string
*/
public function postGetMimeType($path, $mime)
{
if (Crypt::isCatfileContent($path)) {
$mime = \OCP\Files::getMimeType('crypt://' . $path, 'w');
}
return $mime;
}
/** /**
* @param $path * @param $path
* @param $data * @param $data
@ -402,43 +384,6 @@ class Proxy extends \OC_FileProxy
return $data; return $data;
} }
/**
* @param $path
* @param $data
* @return mixed
*/
public function postStat($path, $data)
{
$content = '';
$view = new \OC_FilesystemView('/');
if($view->file_exists($path)) {
// disable encryption proxy
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
// we only need 24 byte from the last chunk
$handle = $view->fopen($path, 'r');
if (!fseek($handle, -24, SEEK_END)) {
$content = fgets($handle);
}
// re-enable proxy
\OC_FileProxy::$enabled = $proxyStatus;
}
// check if file is encrypted
if (Crypt::isCatfileContent($content)) {
// get file info from cache
$cached = $view->getFileInfo($path);
// set the real file size
$data['size'] = $cached['unencrypted_size'];
}
return $data;
}
/** /**
* @param $path * @param $path
* @param $size * @param $size

View File

@ -432,29 +432,6 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
$this->view->unlink($this->userId . '/files/' . $filename); $this->view->unlink($this->userId . '/files/' . $filename);
} }
// Is this test still necessary?
// 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;
//
// }
function testSymmetricEncryptFileContentKeyfile() function testSymmetricEncryptFileContentKeyfile()
{ {
@ -585,6 +562,7 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
$this->assertEquals($this->dataLong, $decrypted); $this->assertEquals($this->dataLong, $decrypted);
$this->assertFalse(Encryption\Crypt::getBlowfish(''));
} }
/** /**
@ -852,69 +830,4 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase
// tear down // tear down
$view->unlink($filename); $view->unlink($filename);
} }
// function testEncryption(){
//
// $key=uniqid();
// $file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
// $source=file_get_contents($file); //nice large text file
// $encrypted=OC_Encryption\Crypt::encrypt($source,$key);
// $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
// $decrypted=rtrim($decrypted, "\0");
// $this->assertNotEquals($encrypted,$source);
// $this->assertEquals($decrypted,$source);
//
// $chunk=substr($source,0,8192);
// $encrypted=OC_Encryption\Crypt::encrypt($chunk,$key);
// $this->assertEquals(strlen($chunk),strlen($encrypted));
// $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
// $decrypted=rtrim($decrypted, "\0");
// $this->assertEquals($decrypted,$chunk);
//
// $encrypted=OC_Encryption\Crypt::blockEncrypt($source,$key);
// $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key);
// $this->assertNotEquals($encrypted,$source);
// $this->assertEquals($decrypted,$source);
//
// $tmpFileEncrypted=OCP\Files::tmpFile();
// OC_Encryption\Crypt::encryptfile($file,$tmpFileEncrypted,$key);
// $encrypted=file_get_contents($tmpFileEncrypted);
// $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key);
// $this->assertNotEquals($encrypted,$source);
// $this->assertEquals($decrypted,$source);
//
// $tmpFileDecrypted=OCP\Files::tmpFile();
// OC_Encryption\Crypt::decryptfile($tmpFileEncrypted,$tmpFileDecrypted,$key);
// $decrypted=file_get_contents($tmpFileDecrypted);
// $this->assertEquals($decrypted,$source);
//
// $file=OC::$SERVERROOT.'/core/img/weather-clear.png';
// $source=file_get_contents($file); //binary file
// $encrypted=OC_Encryption\Crypt::encrypt($source,$key);
// $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
// $decrypted=rtrim($decrypted, "\0");
// $this->assertEquals($decrypted,$source);
//
// $encrypted=OC_Encryption\Crypt::blockEncrypt($source,$key);
// $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key);
// $this->assertEquals($decrypted,$source);
//
// }
//
// function testBinary(){
// $key=uniqid();
//
// $file=__DIR__.'/binary';
// $source=file_get_contents($file); //binary file
// $encrypted=OC_Encryption\Crypt::encrypt($source,$key);
// $decrypted=OC_Encryption\Crypt::decrypt($encrypted,$key);
//
// $decrypted=rtrim($decrypted, "\0");
// $this->assertEquals($decrypted,$source);
//
// $encrypted=OC_Encryption\Crypt::blockEncrypt($source,$key);
// $decrypted=OC_Encryption\Crypt::blockDecrypt($encrypted,$key,strlen($source));
// $this->assertEquals($decrypted,$source);
// }
} }

View File

@ -31,6 +31,7 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase
*/ */
public $view; public $view;
public $randomKey; public $randomKey;
public $dataShort;
function setUp() function setUp()
{ {
@ -150,34 +151,18 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase
//$view = new \OC_FilesystemView( '/' . $this->userId . '/files_encryption/keyfiles' ); //$view = new \OC_FilesystemView( '/' . $this->userId . '/files_encryption/keyfiles' );
Encryption\Keymanager::setFileKey($this->view, $file, $this->userId, $key['key']); Encryption\Keymanager::setFileKey($this->view, $file, $this->userId, $key['key']);
// Disable encryption proxy to prevent recursive calls // enable encryption proxy
$proxyStatus = \OC_FileProxy::$enabled; $proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = true; \OC_FileProxy::$enabled = true;
// cleanup // cleanup
$this->view->unlink('/' . $this->userId . '/files/' . $file); $this->view->unlink('/' . $this->userId . '/files/' . $file);
// Re-enable proxy - our work is done // change encryption proxy to previous state
\OC_FileProxy::$enabled = $proxyStatus; \OC_FileProxy::$enabled = $proxyStatus;
} }
// /**
// * @depends testGetPrivateKey
// */
// function testGetPrivateKey_decrypt() {
//
// $key = Encryption\Keymanager::getPrivateKey( $this->view, $this->userId );
//
// # TODO: replace call to Crypt with a mock object?
// $decrypted = Encryption\Crypt::symmetricDecryptFileContent( $key, $this->passphrase );
//
// $this->assertEquals( 1704, strlen( $decrypted ) );
//
// $this->assertEquals( '-----BEGIN PRIVATE KEY-----', substr( $decrypted, 0, 27 ) );
//
// }
function testGetUserKeys() function testGetUserKeys()
{ {
@ -201,4 +186,60 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase
$this->assertArrayHasKey('key', $sslInfoPrivate); $this->assertArrayHasKey('key', $sslInfoPrivate);
} }
function testFixPartialFilePath()
{
$partFilename = 'testfile.txt.part';
$filename = 'testfile.txt';
$this->assertTrue(Encryption\Keymanager::isPartialFilePath($partFilename));
$this->assertEquals('testfile.txt', Encryption\Keymanager::fixPartialFilePath($partFilename));
$this->assertFalse(Encryption\Keymanager::isPartialFilePath($filename));
$this->assertEquals('testfile.txt', Encryption\Keymanager::fixPartialFilePath($filename));
}
function testRecursiveDelShareKeys()
{
// generate filename
$filename = '/tmp-' . time() . '.txt';
// create folder structure
$this->view->mkdir('/admin/files/folder1');
$this->view->mkdir('/admin/files/folder1/subfolder');
$this->view->mkdir('/admin/files/folder1/subfolder/subsubfolder');
// enable encryption proxy
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = true;
// save file with content
$cryptedFile = file_put_contents('crypt:///folder1/subfolder/subsubfolder/' . $filename, $this->dataShort);
// test that data was successfully written
$this->assertTrue(is_int($cryptedFile));
// change encryption proxy to previous state
\OC_FileProxy::$enabled = $proxyStatus;
// recursive delete keys
Encryption\Keymanager::delShareKey($this->view, array('admin'), '/folder1/');
// check if share key not exists
$this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/folder1/subfolder/subsubfolder/' . $filename . '.admin.shareKey'));
// enable encryption proxy
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = true;
// cleanup
$this->view->unlink('/admin/files/folder1');
// change encryption proxy to previous state
\OC_FileProxy::$enabled = $proxyStatus;
}
} }

View File

@ -312,7 +312,7 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase
$this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user1.shareKey')); $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.user1.shareKey'));
// cleanup // cleanup
$this->view->unlink('/admin/files' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename); $this->view->unlink('/admin/files' . $this->folder1);
// check if share key not exists // check if share key not exists
$this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.admin.shareKey')); $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.admin.shareKey'));
@ -621,7 +621,8 @@ class Test_Encryption_Share extends \PHPUnit_Framework_TestCase
$this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey')); $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
$this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey')); $this->assertFalse($this->view->file_exists('/admin/files_encryption/share-keys/' . $this->folder1 . $this->subfolder . $this->subsubfolder . '/' . $this->filename . '.' . $recoveryKeyId . '.shareKey'));
\OCA\Encryption\Helper::adminDisableRecovery('test123'); $this->assertTrue(\OCA\Encryption\Helper::adminEnableRecovery(null, 'test123'));
$this->assertTrue(\OCA\Encryption\Helper::adminDisableRecovery('test123'));
$this->assertEquals(0, \OC_Appconfig::getValue('files_encryption', 'recoveryAdminEnabled')); $this->assertEquals(0, \OC_Appconfig::getValue('files_encryption', 'recoveryAdminEnabled'));
} }

View File

@ -1,226 +1,182 @@
<?php <?php
// /** /**
// * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com> * ownCloud
// * This file is licensed under the Affero General Public License version 3 or *
// * later. * @author Florin Peter
// * See the COPYING-README file. * @copyright 2013 Florin Peter <owncloud@florin-peter.de>
// */ *
// * This library is free software; you can redistribute it and/or
// namespace OCA\Encryption; * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
// * License as published by the Free Software Foundation; either
// class Test_Stream extends \PHPUnit_Framework_TestCase { * version 3 of the License, or any later version.
// *
// function setUp() { * This library is distributed in the hope that it will be useful,
// * but WITHOUT ANY WARRANTY; without even the implied warranty of
// \OC_Filesystem::mount( 'OC_Filestorage_Local', array(), '/' ); * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
// $this->empty = ''; *
// * You should have received a copy of the GNU Affero General Public
// $this->stream = new Stream(); * License along with this library. If not, see <http://www.gnu.org/licenses/>.
// *
// $this->dataLong = file_get_contents( realpath( dirname(__FILE__).'/../lib/crypt.php' ) ); */
// $this->dataShort = 'hats';
// require_once realpath(dirname(__FILE__) . '/../../../lib/base.php');
// $this->emptyTmpFilePath = \OCP\Files::tmpFile(); require_once realpath(dirname(__FILE__) . '/../lib/crypt.php');
// require_once realpath(dirname(__FILE__) . '/../lib/keymanager.php');
// $this->dataTmpFilePath = \OCP\Files::tmpFile(); require_once realpath(dirname(__FILE__) . '/../lib/proxy.php');
// require_once realpath(dirname(__FILE__) . '/../lib/stream.php');
// file_put_contents( $this->dataTmpFilePath, "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec a diam lectus. Sed sit amet ipsum mauris. Maecenas congue ligula ac quam viverra nec consectetur ante hendrerit. Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean ut gravida lorem. Ut turpis felis, pulvinar a semper sed, adipiscing id dolor. Pellentesque auctor nisi id magna consequat sagittis. Curabitur dapibus enim sit amet elit pharetra tincidunt feugiat nisl imperdiet. Ut convallis libero in urna ultrices accumsan. Donec sed odio eros. Donec viverra mi quis quam pulvinar at malesuada arcu rhoncus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In rutrum accumsan ultricies. Mauris vitae nisi at sem facilisis semper ac in est." ); require_once realpath(dirname(__FILE__) . '/../lib/util.php');
// require_once realpath(dirname(__FILE__) . '/../appinfo/app.php');
// }
// use OCA\Encryption;
// function testStreamOpen() {
// /**
// $stream1 = new Stream(); * Class Test_Encryption_Stream
// * @brief this class provide basic stream tests
// $handle1 = $stream1->stream_open( $this->emptyTmpFilePath, 'wb', array(), $this->empty ); */
// class Test_Encryption_Stream extends \PHPUnit_Framework_TestCase
// // Test that resource was returned successfully {
// $this->assertTrue( $handle1 );
// public $userId;
// // Test that file has correct size public $pass;
// $this->assertEquals( 0, $stream1->size ); /**
// * @var \OC_FilesystemView
// // Test that path is correct */
// $this->assertEquals( $this->emptyTmpFilePath, $stream1->rawPath ); public $view;
// public $dataShort;
// $stream2 = new Stream(); public $stateFilesTrashbin;
//
// $handle2 = $stream2->stream_open( 'crypt://' . $this->emptyTmpFilePath, 'wb', array(), $this->empty ); function setUp()
// {
// // Test that protocol identifier is removed from path // reset backend
// $this->assertEquals( $this->emptyTmpFilePath, $stream2->rawPath ); \OC_User::useBackend('database');
//
// // "Stat failed error" prevents this test from executing // set user id
// // $stream3 = new Stream(); \OC_User::setUserId('admin');
// // $this->userId = 'admin';
// // $handle3 = $stream3->stream_open( $this->dataTmpFilePath, 'r', array(), $this->empty ); $this->pass = 'admin';
// //
// // $this->assertEquals( 0, $stream3->size ); // init filesystem view
// $this->view = new \OC_FilesystemView('/');
// }
// // init short data
// function testStreamWrite() { $this->dataShort = 'hats';
//
// $stream1 = new Stream(); // init filesystem related hooks
// \OCA\Encryption\Helper::registerFilesystemHooks();
// $handle1 = $stream1->stream_open( $this->emptyTmpFilePath, 'r+b', array(), $this->empty );
// // register encryption file proxy
// # what about the keymanager? there is no key for the newly created temporary file! \OC_FileProxy::register(new OCA\Encryption\Proxy());
//
// $stream1->stream_write( $this->dataShort ); // remember files_trashbin state
// $this->stateFilesTrashbin = OC_App::isEnabled('files_trashbin');
// }
// // we don't want to tests with app files_trashbin enabled
// // function getStream( $id, $mode, $size ) { \OC_App::disable('files_trashbin');
// //
// // if ( $id === '' ) { // init filesystem for user
// // \OC_Util::tearDownFS();
// // $id = uniqid(); \OC_User::setUserId('');
// // } \OC\Files\Filesystem::tearDown();
// // \OC_Util::setupFS($this->userId);
// // \OC_User::setUserId($this->userId);
// // if ( !isset( $this->tmpFiles[$id] ) ) {
// // // login user
// // // If tempfile with given name does not already exist, create it $params['uid'] = $this->userId;
// // $params['password'] = $this->pass;
// // $file = OCP\Files::tmpFile(); OCA\Encryption\Hooks::login($params);
// // }
// // $this->tmpFiles[$id] = $file;
// // function tearDown()
// // } else { {
// // // reset app files_trashbin
// // $file = $this->tmpFiles[$id]; if ($this->stateFilesTrashbin) {
// // OC_App::enable('files_trashbin');
// // } } else {
// // OC_App::disable('files_trashbin');
// // $stream = fopen( $file, $mode ); }
// //
// // Stream::$sourceStreams[$id] = array( 'path' => 'dummy' . $id, 'stream' => $stream, 'size' => $size ); // clear all proxies
// // \OC_FileProxy::clearProxies();
// // return fopen( 'crypt://streams/'.$id, $mode ); }
// //
// // } function testStreamOptions() {
// // $filename = '/tmp-' . time();
// // function testStream( ){ $view = new \OC\Files\View('/' . $this->userId . '/files');
// //
// // $stream = $this->getStream( 'test1', 'w', strlen( 'foobar' ) ); // Save short data as encrypted file using stream wrapper
// // $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
// // fwrite( $stream, 'foobar' );
// // // Test that data was successfully written
// // fclose( $stream ); $this->assertTrue(is_int($cryptedFile));
// //
// // $handle = $view->fopen($filename, 'r');
// // $stream = $this->getStream( 'test1', 'r', strlen( 'foobar' ) );
// // // check if stream is at position zero
// // $data = fread( $stream, 6 ); $this->assertEquals(0,ftell($handle));
// //
// // fclose( $stream ); // set stream options
// // $this->assertTrue(flock($handle, LOCK_SH));
// // $this->assertEquals( 'foobar', $data ); $this->assertTrue(flock($handle, LOCK_UN));
// //
// // // tear down
// // $file = OC::$SERVERROOT.'/3rdparty/MDB2.php'; $view->unlink($filename);
// // }
// // $source = fopen( $file, 'r' );
// // function testStreamSetBlocking() {
// // $target = $this->getStream( 'test2', 'w', 0 ); $filename = '/tmp-' . time();
// // $view = new \OC\Files\View('/' . $this->userId . '/files');
// // OCP\Files::streamCopy( $source, $target );
// // // Save short data as encrypted file using stream wrapper
// // fclose( $target ); $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
// //
// // fclose( $source ); // Test that data was successfully written
// // $this->assertTrue(is_int($cryptedFile));
// //
// // $stream = $this->getStream( 'test2', 'r', filesize( $file ) ); $handle = $view->fopen($filename, 'r');
// //
// // $data = stream_get_contents( $stream ); // set stream options
// // $this->assertTrue(stream_set_blocking($handle,1));
// // $original = file_get_contents( $file );
// // // tear down
// // $this->assertEquals( strlen( $original ), strlen( $data ) ); $view->unlink($filename);
// // }
// // $this->assertEquals( $original, $data );
// // function testStreamSetTimeout() {
// // } $filename = '/tmp-' . time();
// $view = new \OC\Files\View('/' . $this->userId . '/files');
// }
// // Save short data as encrypted file using stream wrapper
// // class Test_CryptStream extends PHPUnit_Framework_TestCase { $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
// // private $tmpFiles=array();
// // // Test that data was successfully written
// // function testStream(){ $this->assertTrue(is_int($cryptedFile));
// // $stream=$this->getStream('test1','w',strlen('foobar'));
// // fwrite($stream,'foobar'); $handle = $view->fopen($filename, 'r');
// // fclose($stream);
// // // set stream options
// // $stream=$this->getStream('test1','r',strlen('foobar')); $this->assertFalse(stream_set_timeout($handle,1));
// // $data=fread($stream,6);
// // fclose($stream); // tear down
// // $this->assertEquals('foobar',$data); $view->unlink($filename);
// // }
// // $file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
// // $source=fopen($file,'r'); function testStreamSetWriteBuffer() {
// // $target=$this->getStream('test2','w',0); $filename = '/tmp-' . time();
// // OCP\Files::streamCopy($source,$target); $view = new \OC\Files\View('/' . $this->userId . '/files');
// // fclose($target);
// // fclose($source); // Save short data as encrypted file using stream wrapper
// // $cryptedFile = $view->file_put_contents($filename, $this->dataShort);
// // $stream=$this->getStream('test2','r',filesize($file));
// // $data=stream_get_contents($stream); // Test that data was successfully written
// // $original=file_get_contents($file); $this->assertTrue(is_int($cryptedFile));
// // $this->assertEquals(strlen($original),strlen($data));
// // $this->assertEquals($original,$data); $handle = $view->fopen($filename, 'r');
// // }
// // // set stream options
// // /** $this->assertEquals(0, stream_set_write_buffer($handle,1024));
// // * get a cryptstream to a temporary file
// // * @param string $id // tear down
// // * @param string $mode $view->unlink($filename);
// // * @param int size }
// // * @return resource }
// // */
// // function getStream($id,$mode,$size){
// // if($id===''){
// // $id=uniqid();
// // }
// // if(!isset($this->tmpFiles[$id])){
// // $file=OCP\Files::tmpFile();
// // $this->tmpFiles[$id]=$file;
// // }else{
// // $file=$this->tmpFiles[$id];
// // }
// // $stream=fopen($file,$mode);
// // OC_CryptStream::$sourceStreams[$id]=array('path'=>'dummy'.$id,'stream'=>$stream,'size'=>$size);
// // return fopen('crypt://streams/'.$id,$mode);
// // }
// //
// // function testBinary(){
// // $file=__DIR__.'/binary';
// // $source=file_get_contents($file);
// //
// // $stream=$this->getStream('test','w',strlen($source));
// // fwrite($stream,$source);
// // fclose($stream);
// //
// // $stream=$this->getStream('test','r',strlen($source));
// // $data=stream_get_contents($stream);
// // fclose($stream);
// // $this->assertEquals(strlen($data),strlen($source));
// // $this->assertEquals($source,$data);
// //
// // $file=__DIR__.'/zeros';
// // $source=file_get_contents($file);
// //
// // $stream=$this->getStream('test2','w',strlen($source));
// // fwrite($stream,$source);
// // fclose($stream);
// //
// // $stream=$this->getStream('test2','r',strlen($source));
// // $data=stream_get_contents($stream);
// // fclose($stream);
// // $this->assertEquals(strlen($data),strlen($source));
// // $this->assertEquals($source,$data);
// // }
// // }

View File

@ -217,4 +217,61 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase
$this->assertEquals($file, $filename); $this->assertEquals($file, $filename);
} }
function testIsSharedPath() {
$sharedPath = '/user1/files/Shared/test';
$path = '/user1/files/test';
$this->assertTrue($this->util->isSharedPath($sharedPath));
$this->assertFalse($this->util->isSharedPath($path));
}
function testEncryptLagacyFiles()
{
$userView = new \OC_FilesystemView( '/' . $this->userId);
$view = new \OC_FilesystemView( '/' . $this->userId . '/files' );
// Disable encryption proxy to prevent recursive calls
$proxyStatus = \OC_FileProxy::$enabled;
\OC_FileProxy::$enabled = false;
$encryptionKeyContent = file_get_contents($this->legacyEncryptedDataKey);
$userView->file_put_contents('/encryption.key', $encryptionKeyContent);
$legacyEncryptedData = file_get_contents($this->legacyEncryptedData);
$view->mkdir('/test/');
$view->mkdir('/test/subtest/');
$view->file_put_contents('/test/subtest/legacy-encrypted-text.txt', $legacyEncryptedData);
$fileInfo = $view->getFileInfo('/test/subtest/legacy-encrypted-text.txt');
$fileInfo['encrypted'] = true;
$view->putFileInfo('/test/subtest/legacy-encrypted-text.txt', $fileInfo);
\OC_FileProxy::$enabled = $proxyStatus;
$params['uid'] = $this->userId;
$params['password'] = $this->pass;
$util = new Encryption\Util($this->view, $this->userId);
$util->setMigrationStatus(0);
$this->assertTrue(OCA\Encryption\Hooks::login($params));
$this->assertEquals($this->lagacyKey, $_SESSION['legacyKey']);
$files = $util->findEncFiles('/' . $this->userId . '/files/');
$this->assertTrue(is_array($files));
$found = false;
foreach($files['encrypted'] as $encryptedFile) {
if($encryptedFile['name'] === 'legacy-encrypted-text.txt') {
$found = true;
break;
}
}
$this->assertTrue($found);
}
} }