encryption.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Test\Files\Storage\Wrapper;
  3. use OC\Files\Storage\Temporary;
  4. use OC\Files\View;
  5. class Encryption extends \Test\Files\Storage\Storage {
  6. /**
  7. * @var Temporary
  8. */
  9. private $sourceStorage;
  10. public function setUp() {
  11. parent::setUp();
  12. $mockModule = $this->buildMockModule();
  13. $encryptionManager = $this->getMockBuilder('\OC\Encryption\Manager')
  14. ->disableOriginalConstructor()
  15. ->setMethods(['getDefaultEncryptionModule', 'getEncryptionModule', 'isEnabled'])
  16. ->getMock();
  17. $encryptionManager->expects($this->any())
  18. ->method('getDefaultEncryptionModule')
  19. ->willReturn($mockModule);
  20. $encryptionManager->expects($this->any())
  21. ->method('getEncryptionModule')
  22. ->willReturn($mockModule);
  23. $encryptionManager->expects($this->any())
  24. ->method('isEnabled')
  25. ->willReturn(true);
  26. $config = $this->getMockBuilder('\OCP\IConfig')
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. $util = $this->getMock('\OC\Encryption\Util', ['getUidAndFilename'], [new View(), new \OC\User\Manager(), $config]);
  30. $util->expects($this->any())
  31. ->method('getUidAndFilename')
  32. ->willReturnCallback(function ($path) {
  33. return ['user1', $path];
  34. });
  35. $file = $this->getMockBuilder('\OC\Encryption\File')
  36. ->disableOriginalConstructor()
  37. ->getMock();
  38. $logger = $this->getMock('\OC\Log');
  39. $this->sourceStorage = new Temporary(array());
  40. $keyStore = $this->getMockBuilder('\OC\Encryption\Keys\Storage')
  41. ->disableOriginalConstructor()->getMock();
  42. $mount = $this->getMockBuilder('\OC\Files\Mount\MountPoint')
  43. ->disableOriginalConstructor()
  44. ->setMethods(['getOption'])
  45. ->getMock();
  46. $mount->expects($this->any())->method('getOption')->willReturn(true);
  47. $this->instance = new EncryptionWrapper([
  48. 'storage' => $this->sourceStorage,
  49. 'root' => 'foo',
  50. 'mountPoint' => '/',
  51. 'mount' => $mount
  52. ],
  53. $encryptionManager, $util, $logger, $file, null, $keyStore
  54. );
  55. }
  56. /**
  57. * @return \PHPUnit_Framework_MockObject_MockObject
  58. */
  59. protected function buildMockModule() {
  60. $encryptionModule = $this->getMockBuilder('\OCP\Encryption\IEncryptionModule')
  61. ->disableOriginalConstructor()
  62. ->setMethods(['getId', 'getDisplayName', 'begin', 'end', 'encrypt', 'decrypt', 'update', 'shouldEncrypt', 'calculateUnencryptedSize', 'getUnencryptedBlockSize'])
  63. ->getMock();
  64. $encryptionModule->expects($this->any())->method('getId')->willReturn('UNIT_TEST_MODULE');
  65. $encryptionModule->expects($this->any())->method('getDisplayName')->willReturn('Unit test module');
  66. $encryptionModule->expects($this->any())->method('begin')->willReturn([]);
  67. $encryptionModule->expects($this->any())->method('end')->willReturn('');
  68. $encryptionModule->expects($this->any())->method('encrypt')->willReturnArgument(0);
  69. $encryptionModule->expects($this->any())->method('decrypt')->willReturnArgument(0);
  70. $encryptionModule->expects($this->any())->method('update')->willReturn(true);
  71. $encryptionModule->expects($this->any())->method('shouldEncrypt')->willReturn(true);
  72. $encryptionModule->expects($this->any())->method('calculateUnencryptedSize')->willReturn(42);
  73. $encryptionModule->expects($this->any())->method('getUnencryptedBlockSize')->willReturn(8192);
  74. return $encryptionModule;
  75. }
  76. // public function testMkDirRooted() {
  77. // $this->instance->mkdir('bar');
  78. // $this->assertTrue($this->sourceStorage->is_dir('foo/bar'));
  79. // }
  80. //
  81. // public function testFilePutContentsRooted() {
  82. // $this->instance->file_put_contents('bar', 'asd');
  83. // $this->assertEquals('asd', $this->sourceStorage->file_get_contents('foo/bar'));
  84. // }
  85. }
  86. //
  87. // FIXME: this is too bad and needs adjustment
  88. //
  89. class EncryptionWrapper extends \OC\Files\Storage\Wrapper\Encryption {
  90. private $keyStore;
  91. public function __construct(
  92. $parameters,
  93. \OC\Encryption\Manager $encryptionManager = null,
  94. \OC\Encryption\Util $util = null,
  95. \OC\Log $logger = null,
  96. \OC\Encryption\File $fileHelper = null,
  97. $uid = null,
  98. $keyStore = null
  99. ) {
  100. $this->keyStore = $keyStore;
  101. parent::__construct($parameters, $encryptionManager, $util, $logger, $fileHelper, $uid);
  102. }
  103. protected function getKeyStorage($encryptionModuleId) {
  104. return $this->keyStore;
  105. }
  106. }