EncryptedStorageTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Robin Appelman <robin@icewind.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\encryption\tests;
  23. use OC\Files\Storage\Temporary;
  24. use OC\Files\Storage\Wrapper\Encryption;
  25. use OC\Files\View;
  26. use OCP\Files\Mount\IMountManager;
  27. use OCP\Files\Storage\IDisableEncryptionStorage;
  28. use Test\TestCase;
  29. use Test\Traits\EncryptionTrait;
  30. use Test\Traits\MountProviderTrait;
  31. use Test\Traits\UserTrait;
  32. class TemporaryNoEncrypted extends Temporary implements IDisableEncryptionStorage {
  33. }
  34. /**
  35. * @group DB
  36. */
  37. class EncryptedStorageTest extends TestCase {
  38. use MountProviderTrait;
  39. use EncryptionTrait;
  40. use UserTrait;
  41. public function testMoveFromEncrypted() {
  42. $this->createUser("test1", "test2");
  43. $this->setupForUser("test1", 'test2');
  44. $unwrapped = new Temporary();
  45. $this->registerMount("test1", new TemporaryNoEncrypted(), "/test1/files/unenc");
  46. $this->registerMount("test1", $unwrapped, "/test1/files/enc");
  47. $this->loginWithEncryption("test1");
  48. $view = new View("/test1/files");
  49. /** @var IMountManager $mountManager */
  50. $mountManager = \OC::$server->get(IMountManager::class);
  51. $encryptedMount = $mountManager->find("/test1/files/enc");
  52. $unencryptedMount = $mountManager->find("/test1/files/unenc");
  53. $encryptedStorage = $encryptedMount->getStorage();
  54. $unencryptedStorage = $unencryptedMount->getStorage();
  55. $encryptedCache = $encryptedStorage->getCache();
  56. $unencryptedCache = $unencryptedStorage->getCache();
  57. $this->assertTrue($encryptedStorage->instanceOfStorage(Encryption::class));
  58. $this->assertFalse($unencryptedStorage->instanceOfStorage(Encryption::class));
  59. $encryptedStorage->file_put_contents("foo.txt", "bar");
  60. $this->assertEquals("bar", $encryptedStorage->file_get_contents("foo.txt"));
  61. $this->assertStringStartsWith("HBEGIN:oc_encryption_module:", $unwrapped->file_get_contents("foo.txt"));
  62. $this->assertTrue($encryptedCache->get("foo.txt")->isEncrypted());
  63. $view->rename("enc/foo.txt", "unenc/foo.txt");
  64. $this->assertEquals("bar", $unencryptedStorage->file_get_contents("foo.txt"));
  65. $this->assertFalse($unencryptedCache->get("foo.txt")->isEncrypted());
  66. }
  67. }