EncryptionWrapperTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace Test\Encryption;
  22. use OC\Encryption\EncryptionWrapper;
  23. use OC\Encryption\Manager;
  24. use OC\Memcache\ArrayCache;
  25. use OCP\Files\Storage;
  26. use OCP\ILogger;
  27. use Test\TestCase;
  28. class EncryptionWrapperTest extends TestCase {
  29. /** @var EncryptionWrapper */
  30. private $instance;
  31. /** @var \PHPUnit_Framework_MockObject_MockObject | \OCP\ILogger */
  32. private $logger;
  33. /** @var \PHPUnit_Framework_MockObject_MockObject | \OC\Encryption\Manager */
  34. private $manager;
  35. /** @var \PHPUnit_Framework_MockObject_MockObject | \OC\Memcache\ArrayCache */
  36. private $arrayCache;
  37. public function setUp() {
  38. parent::setUp();
  39. $this->arrayCache = $this->createMock(ArrayCache::class);
  40. $this->manager = $this->createMock(Manager::class);
  41. $this->logger = $this->createMock(ILogger::class);
  42. $this->logger = $this->createMock(ILogger::class);
  43. $this->instance = new EncryptionWrapper($this->arrayCache, $this->manager, $this->logger);
  44. }
  45. /**
  46. * @dataProvider provideWrapStorage
  47. */
  48. public function testWrapStorage($expectedWrapped, $wrappedStorages) {
  49. $storage = $this->getMockBuilder(Storage::class)
  50. ->disableOriginalConstructor()
  51. ->getMock();
  52. foreach ($wrappedStorages as $wrapper) {
  53. $storage->expects($this->any())
  54. ->method('instanceOfStorage')
  55. ->willReturnMap([
  56. [$wrapper, true],
  57. ]);
  58. }
  59. $mount = $this->getMockBuilder('OCP\Files\Mount\IMountPoint')
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $returnedStorage = $this->instance->wrapStorage('mountPoint', $storage, $mount);
  63. $this->assertEquals(
  64. $expectedWrapped,
  65. $returnedStorage->instanceOfStorage('OC\Files\Storage\Wrapper\Encryption'),
  66. 'Asserted that the storage is (not) wrapped with encryption'
  67. );
  68. }
  69. public function provideWrapStorage() {
  70. return [
  71. // Wrap when not wrapped or not wrapped with storage
  72. [true, []],
  73. [true, ['OCA\Files_Trashbin\Storage']],
  74. // Do not wrap shared storages
  75. [false, ['OCA\Files_Sharing\SharedStorage']],
  76. [false, ['OCA\Files_Sharing\External\Storage']],
  77. [false, ['OC\Files\Storage\OwnCloud']],
  78. [false, ['OCA\Files_Sharing\SharedStorage', 'OCA\Files_Sharing\External\Storage']],
  79. [false, ['OCA\Files_Sharing\SharedStorage', 'OC\Files\Storage\OwnCloud']],
  80. [false, ['OCA\Files_Sharing\External\Storage', 'OC\Files\Storage\OwnCloud']],
  81. [false, ['OCA\Files_Sharing\SharedStorage', 'OCA\Files_Sharing\External\Storage', 'OC\Files\Storage\OwnCloud']],
  82. ];
  83. }
  84. }