EncryptionWrapperTest.php 2.2 KB

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