encryption.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Test\Files\Stream;
  3. use OC\Files\View;
  4. use OCA\Encryption_Dummy\DummyModule;
  5. class Encryption extends \Test\TestCase {
  6. /**
  7. * @param string $mode
  8. * @param integer $limit
  9. */
  10. protected function getStream($fileName, $mode, $unencryptedSize) {
  11. $size = filesize($fileName);
  12. $source = fopen($fileName, $mode);
  13. $internalPath = $fileName;
  14. $fullPath = $fileName;
  15. $header = [];
  16. $uid = '';
  17. $encryptionModule = $this->buildMockModule();
  18. $storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
  19. ->disableOriginalConstructor()->getMock();
  20. $encStorage = $this->getMockBuilder('\OC\Files\Storage\Wrapper\Encryption')
  21. ->disableOriginalConstructor()->getMock();
  22. $config = $this->getMockBuilder('\OCP\IConfig')
  23. ->disableOriginalConstructor()
  24. ->getMock();
  25. $file = $this->getMockBuilder('\OC\Encryption\File')
  26. ->disableOriginalConstructor()
  27. ->getMock();
  28. $util = $this->getMock('\OC\Encryption\Util', ['getUidAndFilename'], [new View(), new \OC\User\Manager(), $config]);
  29. $util->expects($this->any())
  30. ->method('getUidAndFilename')
  31. ->willReturn(['user1', $internalPath]);
  32. return \OC\Files\Stream\Encryption::wrap($source, $internalPath,
  33. $fullPath, $header, $uid, $encryptionModule, $storage, $encStorage,
  34. $util, $file, $mode, $size, $unencryptedSize);
  35. }
  36. public function testWriteRead() {
  37. $fileName = tempnam("/tmp", "FOO");
  38. $stream = $this->getStream($fileName, 'w+', 0);
  39. $this->assertEquals(6, fwrite($stream, 'foobar'));
  40. fclose($stream);
  41. $stream = $this->getStream($fileName, 'r', 6);
  42. $this->assertEquals('foobar', fread($stream, 100));
  43. fclose($stream);
  44. }
  45. public function testSeek() {
  46. $fileName = tempnam("/tmp", "FOO");
  47. $stream = $this->getStream($fileName, 'w+', 0);
  48. $this->assertEquals(6, fwrite($stream, 'foobar'));
  49. $this->assertEquals(0, fseek($stream, 3));
  50. $this->assertEquals(6, fwrite($stream, 'foobar'));
  51. fclose($stream);
  52. $stream = $this->getStream($fileName, 'r', 9);
  53. $this->assertEquals('foofoobar', fread($stream, 100));
  54. fclose($stream);
  55. }
  56. public function testWriteReadBigFile() {
  57. $expectedData = file_get_contents(\OC::$SERVERROOT . '/tests/data/lorem-big.txt');
  58. // write it
  59. $fileName = tempnam("/tmp", "FOO");
  60. $stream = $this->getStream($fileName, 'w+', 0);
  61. fwrite($stream, $expectedData);
  62. fclose($stream);
  63. // read it all
  64. $stream = $this->getStream($fileName, 'r', strlen($expectedData));
  65. $data = stream_get_contents($stream);
  66. fclose($stream);
  67. $this->assertEquals($expectedData, $data);
  68. }
  69. /**
  70. * @return \PHPUnit_Framework_MockObject_MockObject
  71. */
  72. protected function buildMockModule() {
  73. $encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule')
  74. ->disableOriginalConstructor()
  75. ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'calculateUnencryptedSize', 'getUnencryptedBlockSize'])
  76. ->getMock();
  77. $encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE');
  78. $encryptionModule->expects($this->any())->method('getDisplayName')->willReturn('Unit test module');
  79. $encryptionModule->expects($this->any())->method('begin')->willReturn([]);
  80. $encryptionModule->expects($this->any())->method('end')->willReturn('');
  81. $encryptionModule->expects($this->any())->method('encrypt')->willReturnArgument(0);
  82. $encryptionModule->expects($this->any())->method('decrypt')->willReturnArgument(0);
  83. $encryptionModule->expects($this->any())->method('update')->willReturn(true);
  84. $encryptionModule->expects($this->any())->method('shouldEncrypt')->willReturn(true);
  85. $encryptionModule->expects($this->any())->method('calculateUnencryptedSize')->willReturn(42);
  86. $encryptionModule->expects($this->any())->method('getUnencryptedBlockSize')->willReturn(8192);
  87. return $encryptionModule;
  88. }
  89. }