stream.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_CryptStream extends UnitTestCase {
  9. private $tmpFiles=array();
  10. function testStream(){
  11. $stream=$this->getStream('test1','w');
  12. fwrite($stream,'foobar');
  13. fclose($stream);
  14. $stream=$this->getStream('test1','r');
  15. $data=fread($stream,6);
  16. fclose($stream);
  17. $this->assertEqual('foobar',$data);
  18. $file=OC::$SERVERROOT.'/3rdparty/MDB2.php';
  19. $source=fopen($file,'r');
  20. $target=$this->getStream('test2','w');
  21. OCP\Files::streamCopy($source,$target);
  22. fclose($target);
  23. fclose($source);
  24. $stream=$this->getStream('test2','r');
  25. $data=stream_get_contents($stream);
  26. $original=file_get_contents($file);
  27. $this->assertEqual(strlen($original),strlen($data));
  28. $this->assertEqual($original,$data);
  29. }
  30. /**
  31. * get a cryptstream to a temporary file
  32. * @param string $id
  33. * @param string $mode
  34. * @return resource
  35. */
  36. function getStream($id,$mode){
  37. if($id===''){
  38. $id=uniqid();
  39. }
  40. if(!isset($this->tmpFiles[$id])){
  41. $file=OCP\Files::tmpFile();
  42. $this->tmpFiles[$id]=$file;
  43. }else{
  44. $file=$this->tmpFiles[$id];
  45. }
  46. $stream=fopen($file,$mode);
  47. OC_CryptStream::$sourceStreams[$id]=array('path'=>'dummy','stream'=>$stream);
  48. return fopen('crypt://streams/'.$id,$mode);
  49. }
  50. }