1
0

EncryptedStorageTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\encryption\tests;
  8. use OC\Files\Storage\Temporary;
  9. use OC\Files\Storage\Wrapper\Encryption;
  10. use OC\Files\View;
  11. use OCP\Files\Mount\IMountManager;
  12. use OCP\Files\Storage\IDisableEncryptionStorage;
  13. use Test\TestCase;
  14. use Test\Traits\EncryptionTrait;
  15. use Test\Traits\MountProviderTrait;
  16. use Test\Traits\UserTrait;
  17. class TemporaryNoEncrypted extends Temporary implements IDisableEncryptionStorage {
  18. }
  19. /**
  20. * @group DB
  21. */
  22. class EncryptedStorageTest extends TestCase {
  23. use MountProviderTrait;
  24. use EncryptionTrait;
  25. use UserTrait;
  26. public function testMoveFromEncrypted() {
  27. $this->createUser("test1", "test2");
  28. $this->setupForUser("test1", 'test2');
  29. $unwrapped = new Temporary();
  30. $this->registerMount("test1", new TemporaryNoEncrypted(), "/test1/files/unenc");
  31. $this->registerMount("test1", $unwrapped, "/test1/files/enc");
  32. $this->loginWithEncryption("test1");
  33. $view = new View("/test1/files");
  34. /** @var IMountManager $mountManager */
  35. $mountManager = \OC::$server->get(IMountManager::class);
  36. $encryptedMount = $mountManager->find("/test1/files/enc");
  37. $unencryptedMount = $mountManager->find("/test1/files/unenc");
  38. $encryptedStorage = $encryptedMount->getStorage();
  39. $unencryptedStorage = $unencryptedMount->getStorage();
  40. $encryptedCache = $encryptedStorage->getCache();
  41. $unencryptedCache = $unencryptedStorage->getCache();
  42. $this->assertTrue($encryptedStorage->instanceOfStorage(Encryption::class));
  43. $this->assertFalse($unencryptedStorage->instanceOfStorage(Encryption::class));
  44. $encryptedStorage->file_put_contents("foo.txt", "bar");
  45. $this->assertEquals("bar", $encryptedStorage->file_get_contents("foo.txt"));
  46. $this->assertStringStartsWith("HBEGIN:oc_encryption_module:", $unwrapped->file_get_contents("foo.txt"));
  47. $this->assertTrue($encryptedCache->get("foo.txt")->isEncrypted());
  48. $view->rename("enc/foo.txt", "unenc/foo.txt");
  49. $this->assertEquals("bar", $unencryptedStorage->file_get_contents("foo.txt"));
  50. $this->assertFalse($unencryptedCache->get("foo.txt")->isEncrypted());
  51. }
  52. }