2012-04-17 21:10:14 +00: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();
|
2012-08-29 06:42:49 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
function testStream() {
|
2012-10-23 20:53:54 +00:00
|
|
|
$stream=$this->getStream('test1','w', strlen('foobar'));
|
2012-04-17 21:10:14 +00:00
|
|
|
fwrite($stream,'foobar');
|
|
|
|
fclose($stream);
|
|
|
|
|
2012-10-23 20:53:54 +00:00
|
|
|
$stream=$this->getStream('test1','r', strlen('foobar'));
|
2012-04-17 21:10:14 +00:00
|
|
|
$data=fread($stream,6);
|
|
|
|
fclose($stream);
|
|
|
|
$this->assertEqual('foobar',$data);
|
|
|
|
|
|
|
|
$file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
|
|
|
|
$source=fopen($file,'r');
|
2012-06-21 15:37:53 +00:00
|
|
|
$target=$this->getStream('test2','w',0);
|
2012-05-02 10:54:31 +00:00
|
|
|
OCP\Files::streamCopy($source,$target);
|
2012-04-17 21:10:14 +00:00
|
|
|
fclose($target);
|
|
|
|
fclose($source);
|
|
|
|
|
2012-10-23 20:53:54 +00:00
|
|
|
$stream=$this->getStream('test2','r', filesize($file));
|
2012-04-17 21:10:14 +00:00
|
|
|
$data=stream_get_contents($stream);
|
|
|
|
$original=file_get_contents($file);
|
2012-10-23 20:53:54 +00:00
|
|
|
$this->assertEqual(strlen($original), strlen($data));
|
2012-04-17 21:10:14 +00:00
|
|
|
$this->assertEqual($original,$data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get a cryptstream to a temporary file
|
|
|
|
* @param string $id
|
|
|
|
* @param string $mode
|
2012-06-21 15:37:53 +00:00
|
|
|
* @param int size
|
2012-04-17 21:10:14 +00:00
|
|
|
* @return resource
|
|
|
|
*/
|
2012-09-07 13:22:01 +00:00
|
|
|
function getStream($id,$mode,$size) {
|
|
|
|
if($id==='') {
|
2012-04-17 21:10:14 +00:00
|
|
|
$id=uniqid();
|
|
|
|
}
|
2012-09-07 13:22:01 +00:00
|
|
|
if(!isset($this->tmpFiles[$id])) {
|
2012-05-02 10:54:31 +00:00
|
|
|
$file=OCP\Files::tmpFile();
|
2012-04-17 21:10:14 +00:00
|
|
|
$this->tmpFiles[$id]=$file;
|
|
|
|
}else{
|
|
|
|
$file=$this->tmpFiles[$id];
|
|
|
|
}
|
|
|
|
$stream=fopen($file,$mode);
|
2012-06-21 15:37:53 +00:00
|
|
|
OC_CryptStream::$sourceStreams[$id]=array('path'=>'dummy'.$id,'stream'=>$stream,'size'=>$size);
|
2012-04-17 21:10:14 +00:00
|
|
|
return fopen('crypt://streams/'.$id,$mode);
|
|
|
|
}
|
2012-06-15 21:48:39 +00:00
|
|
|
|
2012-09-07 13:22:01 +00:00
|
|
|
function testBinary() {
|
2012-06-15 21:48:39 +00:00
|
|
|
$file=__DIR__.'/binary';
|
|
|
|
$source=file_get_contents($file);
|
|
|
|
|
2012-10-23 20:53:54 +00:00
|
|
|
$stream=$this->getStream('test','w', strlen($source));
|
2012-06-15 21:48:39 +00:00
|
|
|
fwrite($stream,$source);
|
|
|
|
fclose($stream);
|
|
|
|
|
2012-10-23 20:53:54 +00:00
|
|
|
$stream=$this->getStream('test','r', strlen($source));
|
2012-06-21 15:37:53 +00:00
|
|
|
$data=stream_get_contents($stream);
|
|
|
|
fclose($stream);
|
2012-10-23 20:53:54 +00:00
|
|
|
$this->assertEqual(strlen($data), strlen($source));
|
2012-06-21 15:37:53 +00:00
|
|
|
$this->assertEqual($source,$data);
|
|
|
|
|
|
|
|
$file=__DIR__.'/zeros';
|
|
|
|
$source=file_get_contents($file);
|
|
|
|
|
2012-10-23 20:53:54 +00:00
|
|
|
$stream=$this->getStream('test2','w', strlen($source));
|
2012-06-21 15:37:53 +00:00
|
|
|
fwrite($stream,$source);
|
|
|
|
fclose($stream);
|
|
|
|
|
2012-10-23 20:53:54 +00:00
|
|
|
$stream=$this->getStream('test2','r', strlen($source));
|
2012-06-15 21:48:39 +00:00
|
|
|
$data=stream_get_contents($stream);
|
|
|
|
fclose($stream);
|
2012-10-23 20:53:54 +00:00
|
|
|
$this->assertEqual(strlen($data), strlen($source));
|
2012-06-15 21:48:39 +00:00
|
|
|
$this->assertEqual($source,$data);
|
|
|
|
}
|
2012-04-17 21:10:14 +00:00
|
|
|
}
|