stream.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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', strlen('foobar'));
  12. fwrite($stream, 'foobar');
  13. fclose($stream);
  14. $stream=$this->getStream('test1', 'r', strlen('foobar'));
  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', 0);
  21. OCP\Files::streamCopy($source, $target);
  22. fclose($target);
  23. fclose($source);
  24. $stream=$this->getStream('test2', 'r', filesize($file));
  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. * @param int size
  35. * @return resource
  36. */
  37. function getStream($id, $mode, $size) {
  38. if($id==='') {
  39. $id=uniqid();
  40. }
  41. if(!isset($this->tmpFiles[$id])) {
  42. $file=OCP\Files::tmpFile();
  43. $this->tmpFiles[$id]=$file;
  44. }else{
  45. $file=$this->tmpFiles[$id];
  46. }
  47. $stream=fopen($file, $mode);
  48. OC_CryptStream::$sourceStreams[$id]=array('path'=>'dummy'.$id, 'stream'=>$stream, 'size'=>$size);
  49. return fopen('crypt://streams/'.$id, $mode);
  50. }
  51. function testBinary() {
  52. $file=__DIR__.'/binary';
  53. $source=file_get_contents($file);
  54. $stream=$this->getStream('test', 'w', strlen($source));
  55. fwrite($stream, $source);
  56. fclose($stream);
  57. $stream=$this->getStream('test', 'r', strlen($source));
  58. $data=stream_get_contents($stream);
  59. fclose($stream);
  60. $this->assertEqual(strlen($data), strlen($source));
  61. $this->assertEqual($source, $data);
  62. $file=__DIR__.'/zeros';
  63. $source=file_get_contents($file);
  64. $stream=$this->getStream('test2', 'w', strlen($source));
  65. fwrite($stream, $source);
  66. fclose($stream);
  67. $stream=$this->getStream('test2', 'r', strlen($source));
  68. $data=stream_get_contents($stream);
  69. fclose($stream);
  70. $this->assertEqual(strlen($data), strlen($source));
  71. $this->assertEqual($source, $data);
  72. }
  73. }