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