2012-05-11 22:32:37 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Test_CryptProxy extends UnitTestCase {
|
2012-06-16 01:11:33 +04:00
|
|
|
private $oldConfig;
|
2012-06-21 20:53:10 +04:00
|
|
|
private $oldKey;
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function setUp() {
|
2012-06-30 03:05:18 +04:00
|
|
|
$user=OC_User::getUser();
|
|
|
|
|
2012-06-16 01:11:33 +04:00
|
|
|
$this->oldConfig=OCP\Config::getAppValue('files_encryption','enable_encryption','true');
|
|
|
|
OCP\Config::setAppValue('files_encryption','enable_encryption','true');
|
2012-06-21 20:53:10 +04:00
|
|
|
$this->oldKey=isset($_SESSION['enckey'])?$_SESSION['enckey']:null;
|
2012-08-29 10:42:49 +04:00
|
|
|
|
|
|
|
|
2012-05-11 22:32:37 +04:00
|
|
|
//set testing key
|
|
|
|
$_SESSION['enckey']=md5(time());
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2012-05-11 22:32:37 +04:00
|
|
|
//clear all proxies and hooks so we can do clean testing
|
|
|
|
OC_FileProxy::clearProxies();
|
|
|
|
OC_Hook::clear('OC_Filesystem');
|
|
|
|
|
|
|
|
//enable only the encryption hook
|
|
|
|
OC_FileProxy::register(new OC_FileProxy_Encryption());
|
|
|
|
|
|
|
|
//set up temporary storage
|
2012-10-10 15:18:36 +04:00
|
|
|
\OC\Files\Filesystem::clearMounts();
|
2012-10-25 20:26:08 +04:00
|
|
|
\OC\Files\Filesystem::mount('\OC\Files\Storage\Temporary' ,array(), '/');
|
2012-05-11 22:32:37 +04:00
|
|
|
|
2012-10-10 15:18:36 +04:00
|
|
|
\OC\Files\Filesystem::init('/'.$user.'/files');
|
2012-06-30 03:05:18 +04:00
|
|
|
|
2012-05-11 22:32:37 +04:00
|
|
|
//set up the users home folder in the temp storage
|
2012-10-10 15:18:36 +04:00
|
|
|
$rootView=new \OC\Files\View('');
|
2012-06-30 03:05:18 +04:00
|
|
|
$rootView->mkdir('/'.$user);
|
|
|
|
$rootView->mkdir('/'.$user.'/files');
|
2012-05-11 22:32:37 +04:00
|
|
|
}
|
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function tearDown() {
|
2012-06-16 01:11:33 +04:00
|
|
|
OCP\Config::setAppValue('files_encryption','enable_encryption',$this->oldConfig);
|
2012-09-07 17:22:01 +04:00
|
|
|
if(!is_null($this->oldKey)) {
|
2012-06-21 20:53:10 +04:00
|
|
|
$_SESSION['enckey']=$this->oldKey;
|
|
|
|
}
|
2012-06-16 01:11:33 +04:00
|
|
|
}
|
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function testSimple() {
|
2012-05-11 22:32:37 +04:00
|
|
|
$file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
|
|
|
|
$original=file_get_contents($file);
|
|
|
|
|
2012-10-10 15:18:36 +04:00
|
|
|
\OC\Files\Filesystem::file_put_contents('/file',$original);
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2012-05-11 22:32:37 +04:00
|
|
|
OC_FileProxy::$enabled=false;
|
2012-10-10 15:18:36 +04:00
|
|
|
$stored=\OC\Files\Filesystem::file_get_contents('/file');
|
2012-05-11 22:32:37 +04:00
|
|
|
OC_FileProxy::$enabled=true;
|
2012-08-29 10:42:49 +04:00
|
|
|
|
2012-10-10 15:18:36 +04:00
|
|
|
$fromFile=\OC\Files\Filesystem::file_get_contents('/file');
|
2012-05-11 22:32:37 +04:00
|
|
|
$this->assertNotEqual($original,$stored);
|
2012-10-24 00:53:54 +04:00
|
|
|
$this->assertEqual(strlen($original), strlen($fromFile));
|
2012-05-11 22:32:37 +04:00
|
|
|
$this->assertEqual($original,$fromFile);
|
2012-06-09 19:33:57 +04:00
|
|
|
|
2012-06-16 01:11:33 +04:00
|
|
|
}
|
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function testView() {
|
2012-06-16 01:11:33 +04:00
|
|
|
$file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
|
|
|
|
$original=file_get_contents($file);
|
|
|
|
|
2012-10-10 15:18:36 +04:00
|
|
|
$rootView=new \OC\Files\View('');
|
|
|
|
$view=new \OC\Files\View('/'.OC_User::getUser());
|
2012-06-09 19:33:57 +04:00
|
|
|
$userDir='/'.OC_User::getUser().'/files';
|
|
|
|
|
2012-06-16 01:11:33 +04:00
|
|
|
$rootView->file_put_contents($userDir.'/file',$original);
|
|
|
|
|
|
|
|
OC_FileProxy::$enabled=false;
|
|
|
|
$stored=$rootView->file_get_contents($userDir.'/file');
|
|
|
|
OC_FileProxy::$enabled=true;
|
|
|
|
|
|
|
|
$this->assertNotEqual($original,$stored);
|
2012-06-09 19:33:57 +04:00
|
|
|
$fromFile=$rootView->file_get_contents($userDir.'/file');
|
|
|
|
$this->assertEqual($original,$fromFile);
|
|
|
|
|
|
|
|
$fromFile=$view->file_get_contents('files/file');
|
|
|
|
$this->assertEqual($original,$fromFile);
|
2012-06-16 01:11:33 +04:00
|
|
|
}
|
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
public function testBinary() {
|
2012-06-16 01:11:33 +04:00
|
|
|
$file=__DIR__.'/binary';
|
|
|
|
$original=file_get_contents($file);
|
|
|
|
|
2012-10-10 15:18:36 +04:00
|
|
|
\OC\Files\Filesystem::file_put_contents('/file',$original);
|
2012-06-09 19:33:57 +04:00
|
|
|
|
2012-06-16 01:11:33 +04:00
|
|
|
OC_FileProxy::$enabled=false;
|
2012-10-10 15:18:36 +04:00
|
|
|
$stored=\OC\Files\Filesystem::file_get_contents('/file');
|
2012-06-16 01:11:33 +04:00
|
|
|
OC_FileProxy::$enabled=true;
|
|
|
|
|
2012-10-10 15:18:36 +04:00
|
|
|
$fromFile=\OC\Files\Filesystem::file_get_contents('/file');
|
2012-06-16 01:11:33 +04:00
|
|
|
$this->assertNotEqual($original,$stored);
|
2012-10-24 00:53:54 +04:00
|
|
|
$this->assertEqual(strlen($original), strlen($fromFile));
|
2012-06-16 01:11:33 +04:00
|
|
|
$this->assertEqual($original,$fromFile);
|
2012-06-21 19:37:53 +04:00
|
|
|
|
|
|
|
$file=__DIR__.'/zeros';
|
|
|
|
$original=file_get_contents($file);
|
|
|
|
|
2012-10-10 15:18:36 +04:00
|
|
|
\OC\Files\Filesystem::file_put_contents('/file',$original);
|
2012-06-21 19:37:53 +04:00
|
|
|
|
|
|
|
OC_FileProxy::$enabled=false;
|
2012-10-10 15:18:36 +04:00
|
|
|
$stored=\OC\Files\Filesystem::file_get_contents('/file');
|
2012-06-21 19:37:53 +04:00
|
|
|
OC_FileProxy::$enabled=true;
|
|
|
|
|
2012-10-10 15:18:36 +04:00
|
|
|
$fromFile=\OC\Files\Filesystem::file_get_contents('/file');
|
2012-06-21 19:37:53 +04:00
|
|
|
$this->assertNotEqual($original,$stored);
|
2012-10-24 00:53:54 +04:00
|
|
|
$this->assertEqual(strlen($original), strlen($fromFile));
|
2012-05-11 22:32:37 +04:00
|
|
|
}
|
|
|
|
}
|