EncryptionWrapperTest.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\Encryption;
  8. use OC\Encryption\EncryptionWrapper;
  9. use OC\Encryption\Manager;
  10. use OC\Memcache\ArrayCache;
  11. use OCP\Files\Storage;
  12. use Psr\Log\LoggerInterface;
  13. use Test\TestCase;
  14. class EncryptionWrapperTest extends TestCase {
  15. /** @var EncryptionWrapper */
  16. private $instance;
  17. /** @var \PHPUnit\Framework\MockObject\MockObject | LoggerInterface */
  18. private $logger;
  19. /** @var \PHPUnit\Framework\MockObject\MockObject | \OC\Encryption\Manager */
  20. private $manager;
  21. /** @var \PHPUnit\Framework\MockObject\MockObject | \OC\Memcache\ArrayCache */
  22. private $arrayCache;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $this->arrayCache = $this->createMock(ArrayCache::class);
  26. $this->manager = $this->createMock(Manager::class);
  27. $this->logger = $this->createMock(LoggerInterface::class);
  28. $this->instance = new EncryptionWrapper($this->arrayCache, $this->manager, $this->logger);
  29. }
  30. /**
  31. * @dataProvider provideWrapStorage
  32. */
  33. public function testWrapStorage($expectedWrapped, $wrappedStorages) {
  34. $storage = $this->getMockBuilder(Storage::class)
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. foreach ($wrappedStorages as $wrapper) {
  38. $storage->expects($this->any())
  39. ->method('instanceOfStorage')
  40. ->willReturnMap([
  41. [$wrapper, true],
  42. ]);
  43. }
  44. $mount = $this->getMockBuilder('OCP\Files\Mount\IMountPoint')
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $returnedStorage = $this->instance->wrapStorage('mountPoint', $storage, $mount);
  48. $this->assertEquals(
  49. $expectedWrapped,
  50. $returnedStorage->instanceOfStorage('OC\Files\Storage\Wrapper\Encryption'),
  51. 'Asserted that the storage is (not) wrapped with encryption'
  52. );
  53. }
  54. public function provideWrapStorage() {
  55. return [
  56. // Wrap when not wrapped or not wrapped with storage
  57. [true, []],
  58. [true, ['OCA\Files_Trashbin\Storage']],
  59. // Do not wrap shared storages
  60. [false, [Storage\IDisableEncryptionStorage::class]],
  61. ];
  62. }
  63. }