2012-04-18 01:10:14 +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_CryptStream extends UnitTestCase {
|
|
|
|
private $tmpFiles=array();
|
|
|
|
|
|
|
|
function testStream(){
|
|
|
|
$stream=$this->getStream('test1','w');
|
|
|
|
fwrite($stream,'foobar');
|
|
|
|
fclose($stream);
|
|
|
|
|
|
|
|
$stream=$this->getStream('test1','r');
|
|
|
|
$data=fread($stream,6);
|
|
|
|
fclose($stream);
|
|
|
|
$this->assertEqual('foobar',$data);
|
|
|
|
|
|
|
|
$file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
|
|
|
|
$source=fopen($file,'r');
|
|
|
|
$target=$this->getStream('test2','w');
|
2012-05-02 14:54:31 +04:00
|
|
|
OCP\Files::streamCopy($source,$target);
|
2012-04-18 01:10:14 +04:00
|
|
|
fclose($target);
|
|
|
|
fclose($source);
|
|
|
|
|
|
|
|
$stream=$this->getStream('test2','r');
|
|
|
|
$data=stream_get_contents($stream);
|
|
|
|
$original=file_get_contents($file);
|
|
|
|
$this->assertEqual(strlen($original),strlen($data));
|
|
|
|
$this->assertEqual($original,$data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get a cryptstream to a temporary file
|
|
|
|
* @param string $id
|
|
|
|
* @param string $mode
|
|
|
|
* @return resource
|
|
|
|
*/
|
|
|
|
function getStream($id,$mode){
|
|
|
|
if($id===''){
|
|
|
|
$id=uniqid();
|
|
|
|
}
|
|
|
|
if(!isset($this->tmpFiles[$id])){
|
2012-05-02 14:54:31 +04:00
|
|
|
$file=OCP\Files::tmpFile();
|
2012-04-18 01:10:14 +04:00
|
|
|
$this->tmpFiles[$id]=$file;
|
|
|
|
}else{
|
|
|
|
$file=$this->tmpFiles[$id];
|
|
|
|
}
|
|
|
|
$stream=fopen($file,$mode);
|
2012-06-16 01:48:39 +04:00
|
|
|
OC_CryptStream::$sourceStreams[$id]=array('path'=>'dummy'.$id,'stream'=>$stream);
|
2012-04-18 01:10:14 +04:00
|
|
|
return fopen('crypt://streams/'.$id,$mode);
|
|
|
|
}
|
2012-06-16 01:48:39 +04:00
|
|
|
|
|
|
|
function testBinary(){
|
|
|
|
$file=__DIR__.'/binary';
|
|
|
|
$source=file_get_contents($file);
|
|
|
|
|
|
|
|
$stream=$this->getStream('test','w');
|
|
|
|
fwrite($stream,$source);
|
|
|
|
fclose($stream);
|
|
|
|
|
|
|
|
$stream=$this->getStream('test','r');
|
|
|
|
$data=stream_get_contents($stream);
|
|
|
|
fclose($stream);
|
|
|
|
$this->assertEqual(strlen($data),strlen($source));
|
|
|
|
$this->assertEqual($source,$data);
|
|
|
|
}
|
2012-04-18 01:10:14 +04:00
|
|
|
}
|