]> source.dussan.org Git - nextcloud-server.git/commitdiff
Fix FixEncryptedVersionTest test
authorVincent Petry <vincent@nextcloud.com>
Thu, 24 Jun 2021 08:51:07 +0000 (10:51 +0200)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Wed, 30 Jun 2021 12:23:31 +0000 (12:23 +0000)
Fixed setup to use EncryptionTrait like other existing tests.
Fix expectations to not rely on side effects from previous test cases.

Signed-off-by: Vincent Petry <vincent@nextcloud.com>
apps/encryption/lib/Command/FixEncryptedVersion.php
apps/encryption/tests/Command/FixEncryptedVersionTest.php

index 88a72f43324fe2b304010ea49dd07342b55b889b..e2181f9a229179a5564c460c2dc83f916ab6be02 100644 (file)
@@ -121,12 +121,12 @@ class FixEncryptedVersion extends Command {
        private function walkPathOfUser($user, $path, OutputInterface $output): int {
                $this->setupUserFs($user);
                if (!$this->view->file_exists($path)) {
-                       $output->writeln("<error>Path $path does not exist. Please provide a valid path.</error>");
+                       $output->writeln("<error>Path \"$path\" does not exist. Please provide a valid path.</error>");
                        return 1;
                }
 
                if ($this->view->is_file($path)) {
-                       $output->writeln("Verifying the content of file $path");
+                       $output->writeln("Verifying the content of file \"$path\"");
                        $this->verifyFileContent($path, $output);
                        return 0;
                }
@@ -139,7 +139,7 @@ class FixEncryptedVersion extends Command {
                                if ($this->view->is_dir($path)) {
                                        $directories[] = $path;
                                } else {
-                                       $output->writeln("Verifying the content of file $path");
+                                       $output->writeln("Verifying the content of file \"$path\"");
                                        $this->verifyFileContent($path, $output);
                                }
                        }
@@ -164,7 +164,7 @@ class FixEncryptedVersion extends Command {
                        $handle = $this->view->fopen($path, 'rb');
 
                        if (\fread($handle, 9001) !== false) {
-                               $output->writeln("<info>The file $path is: OK</info>");
+                               $output->writeln("<info>The file \"$path\" is: OK</info>");
                        }
 
                        \fclose($handle);
@@ -175,7 +175,7 @@ class FixEncryptedVersion extends Command {
                        //If allowOnce is set to false, this becomes recursive.
                        if ($ignoreCorrectEncVersionCall === true) {
                                //Lets rectify the file by correcting encrypted version
-                               $output->writeln("<info>Attempting to fix the path: $path</info>");
+                               $output->writeln("<info>Attempting to fix the path: \"$path\"</info>");
                                return $this->correctEncryptedVersion($path, $output);
                        }
                        return false;
index 425562885566427f1f1283b9c7f935495f700593..a530275784ae88a847c28321f98bd9460cf5855a 100644 (file)
 
 namespace OCA\Encryption\Tests\Command;
 
-use OC\Files\Filesystem;
 use OC\Files\View;
 use OCA\Encryption\Command\FixEncryptedVersion;
-use OCA\Encryption\Crypto\Crypt;
-use OCA\Encryption\KeyManager;
-use OCA\Encryption\Session;
-use OCA\Encryption\Util;
-use OCP\Files\IRootFolder;
-use OCP\IUserManager;
 use Symfony\Component\Console\Tester\CommandTester;
-use OCA\Encryption\Users\Setup;
 use Test\TestCase;
+use Test\Traits\EncryptionTrait;
+use Test\Traits\MountProviderTrait;
+use Test\Traits\UserTrait;
 
 /**
  * Class FixEncryptedVersionTest
@@ -41,16 +36,11 @@ use Test\TestCase;
  * @package OCA\Encryption\Tests\Command
  */
 class FixEncryptedVersionTest extends TestCase {
-       public const TEST_ENCRYPTION_VERSION_AFFECTED_USER = 'test_enc_version_affected_user1';
+       use MountProviderTrait;
+       use EncryptionTrait;
+       use UserTrait;
 
-       /** @var IRootFolder */
-       private $rootFolder;
-
-       /** @var IUserManager */
-       private $userManager;
-
-       /** @var View */
-       private $view;
+       private $userId;
 
        /** @var FixEncryptedVersion */
        private $fixEncryptedVersion;
@@ -58,88 +48,31 @@ class FixEncryptedVersionTest extends TestCase {
        /** @var CommandTester */
        private $commandTester;
 
-       public static function setUpBeforeClass(): void {
-               parent::setUpBeforeClass();
-               //Enable encryption
-               \OC::$server->getConfig()->setAppValue('core', 'encryption_enabled', 'yes');
-               //Enable Masterkey
-               \OC::$server->getConfig()->setAppValue('encryption', 'useMasterKey', '1');
-               $crypt = new Crypt(\OC::$server->getLogger(), \OC::$server->getUserSession(), \OC::$server->getConfig(), \OC::$server->getL10N('encryption'));
-               $encryptionSession = new Session(\OC::$server->getSession());
-               $view = new View("/");
-               $encryptionUtil = new Util($view, $crypt, \OC::$server->getLogger(), \OC::$server->getUserSession(), \OC::$server->getConfig(), \OC::$server->getUserManager());
-               $keyManager = new KeyManager(
-                       \OC::$server->getEncryptionKeyStorage(),
-                       $crypt,
-                       \OC::$server->getConfig(),
-                       \OC::$server->getUserSession(),
-                       $encryptionSession,
-                       \OC::$server->getLogger(),
-                       $encryptionUtil
-               );
-               $userSetup = new Setup(\OC::$server->getLogger(), \OC::$server->getUserSession(), $crypt, $keyManager);
-               $userSetup->setupSystem();
-               \OC::$server->getUserManager()->createUser(self::TEST_ENCRYPTION_VERSION_AFFECTED_USER, 'foo');
-       }
-
-       public static function tearDownAfterClass(): void {
-               parent::tearDownAfterClass();
-               \OC\Files\Filesystem::clearMounts();
-               $user = \OC::$server->getUserManager()->get(self::TEST_ENCRYPTION_VERSION_AFFECTED_USER);
-               if ($user !== null) {
-                       $user->delete();
-               }
-               \OC::$server->getConfig()->deleteAppValue('core', 'encryption_enabled');
-               \OC::$server->getConfig()->deleteAppValue('core', 'default_encryption_module');
-               \OC::$server->getConfig()->deleteAppValues('encryption');
-               Filesystem::getLoader()->removeStorageWrapper("oc_encryption");
-       }
-
        public function setUp(): void {
                parent::setUp();
-               $this->rootFolder = \OC::$server->getRootFolder();
-               $this->userManager = \OC::$server->getUserManager();
-               $this->view = new View("/");
-               $this->fixEncryptedVersion = new FixEncryptedVersion($this->rootFolder, $this->userManager, $this->view);
-               $this->commandTester = new CommandTester($this->fixEncryptedVersion);
-       }
-
-       /**
-        * In this test the encrypted version is set to zero whereas it should have been
-        * set to a positive non zero number.
-        */
-       public function testEncryptedVersionIsNotZero() {
-               \OC::$server->getUserSession()->login(self::TEST_ENCRYPTION_VERSION_AFFECTED_USER, 'foo');
-               $view = new View("/" . self::TEST_ENCRYPTION_VERSION_AFFECTED_USER . "/files");
-
-               $view->touch('hello.txt');
-               $view->touch('world.txt');
-               $view->file_put_contents('hello.txt', 'a test string for hello');
-               $view->file_put_contents('world.txt', 'a test string for world');
-
-               $fileInfo = $view->getFileInfo('hello.txt');
 
-               $storage = $fileInfo->getStorage();
-               $cache = $storage->getCache();
-               $fileCache = $cache->get($fileInfo->getId());
+               \OC::$server->getConfig()->setAppValue('encryption', 'useMasterKey', '1');
 
-               //Now change the encrypted version to zero
-               $cacheInfo = ['encryptedVersion' => 0, 'encrypted' => 0];
-               $cache->put($fileCache->getPath(), $cacheInfo);
+               $this->userId = $this->getUniqueId('user_');
 
-               $this->commandTester->execute([
-                       'user' => self::TEST_ENCRYPTION_VERSION_AFFECTED_USER
-               ]);
+               $this->createUser($this->userId, 'foo12345678');
+               $tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
+               $this->registerMount($this->userId, '\OC\Files\Storage\Local', '/' . $this->userId, ['datadir' => $tmpFolder]);
+               $this->setupForUser($this->userId, 'foo12345678');
+               $this->loginWithEncryption($this->userId);
 
-               $output = $this->commandTester->getDisplay();
+               $this->fixEncryptedVersion = new FixEncryptedVersion(
+                       \OC::$server->getConfig(),
+                       \OC::$server->getLogger(),
+                       \OC::$server->getRootFolder(),
+                       \OC::$server->getUserManager(),
+                       new View('/')
+               );
+               $this->commandTester = new CommandTester($this->fixEncryptedVersion);
 
-               $this->assertStringContainsString("Verifying the content of file /test_enc_version_affected_user1/files/hello.txt
-Attempting to fix the path: /test_enc_version_affected_user1/files/hello.txt
-Increment the encrypted version to 1
-The file /test_enc_version_affected_user1/files/hello.txt is: OK
-Fixed the file: /test_enc_version_affected_user1/files/hello.txt with version 1", $output);
-               $this->assertStringContainsString("Verifying the content of file /test_enc_version_affected_user1/files/world.txt
-The file /test_enc_version_affected_user1/files/world.txt is: OK", $output);
+               $this->assertTrue(\OC::$server->getEncryptionManager()->isEnabled());
+               $this->assertTrue(\OC::$server->getEncryptionManager()->isReady());
+               $this->assertTrue(\OC::$server->getEncryptionManager()->isReadyForUser($this->userId));
        }
 
        /**
@@ -147,8 +80,7 @@ The file /test_enc_version_affected_user1/files/world.txt is: OK", $output);
         * but greater than zero
         */
        public function testEncryptedVersionLessThanOriginalValue() {
-               \OC::$server->getUserSession()->login(self::TEST_ENCRYPTION_VERSION_AFFECTED_USER, 'foo');
-               $view = new View("/" . self::TEST_ENCRYPTION_VERSION_AFFECTED_USER . "/files");
+               $view = new View("/" . $this->userId . "/files");
 
                $view->touch('hello.txt');
                $view->touch('world.txt');
@@ -184,30 +116,28 @@ The file /test_enc_version_affected_user1/files/world.txt is: OK", $output);
                $cache2->put($filecache2->getPath(), $cacheInfo);
 
                $this->commandTester->execute([
-                       'user' => self::TEST_ENCRYPTION_VERSION_AFFECTED_USER
+                       'user' => $this->userId
                ]);
 
                $output = $this->commandTester->getDisplay();
 
-               $this->assertStringContainsString("Verifying the content of file /test_enc_version_affected_user1/files/foo.txt
-The file /test_enc_version_affected_user1/files/foo.txt is: OK", $output);
-               $this->assertStringContainsString("Verifying the content of file /test_enc_version_affected_user1/files/hello.txt
-Attempting to fix the path: /test_enc_version_affected_user1/files/hello.txt
+               $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/foo.txt\"
+The file \"/$this->userId/files/foo.txt\" is: OK", $output);
+               $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/hello.txt\"
+Attempting to fix the path: \"/$this->userId/files/hello.txt\"
 Decrement the encrypted version to 1
 Increment the encrypted version to 3
 Increment the encrypted version to 4
 Increment the encrypted version to 5
-Increment the encrypted version to 6
-The file /test_enc_version_affected_user1/files/hello.txt is: OK
-Fixed the file: /test_enc_version_affected_user1/files/hello.txt with version 6", $output);
-               $this->assertStringContainsString("Verifying the content of file /test_enc_version_affected_user1/files/world.txt
-Attempting to fix the path: /test_enc_version_affected_user1/files/world.txt
+The file \"/$this->userId/files/hello.txt\" is: OK
+Fixed the file: \"/$this->userId/files/hello.txt\" with version 5", $output);
+               $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/world.txt\"
+Attempting to fix the path: \"/$this->userId/files/world.txt\"
 Increment the encrypted version to 2
 Increment the encrypted version to 3
 Increment the encrypted version to 4
-Increment the encrypted version to 5
-The file /test_enc_version_affected_user1/files/world.txt is: OK
-Fixed the file: /test_enc_version_affected_user1/files/world.txt with version 5", $output);
+The file \"/$this->userId/files/world.txt\" is: OK
+Fixed the file: \"/$this->userId/files/world.txt\" with version 4", $output);
        }
 
        /**
@@ -215,8 +145,7 @@ Fixed the file: /test_enc_version_affected_user1/files/world.txt with version 5"
         * but greater than zero
         */
        public function testEncryptedVersionGreaterThanOriginalValue() {
-               \OC::$server->getUserSession()->login(self::TEST_ENCRYPTION_VERSION_AFFECTED_USER, 'foo');
-               $view = new View("/" . self::TEST_ENCRYPTION_VERSION_AFFECTED_USER . "/files");
+               $view = new View("/" . $this->userId . "/files");
 
                $view->touch('hello.txt');
                $view->touch('world.txt');
@@ -237,7 +166,7 @@ Fixed the file: /test_enc_version_affected_user1/files/world.txt with version 5"
                $fileCache1 = $cache1->get($fileInfo1->getId());
 
                //Now change the encrypted version to fifteen
-               $cacheInfo = ['encryptedVersion' => 15, 'encrypted' => 15];
+               $cacheInfo = ['encryptedVersion' => 5, 'encrypted' => 5];
                $cache1->put($fileCache1->getPath(), $cacheInfo);
 
                $fileInfo2 = $view->getFileInfo('world.txt');
@@ -246,42 +175,33 @@ Fixed the file: /test_enc_version_affected_user1/files/world.txt with version 5"
                $filecache2 = $cache2->get($fileInfo2->getId());
 
                //Now change the encrypted version to 1
-               $cacheInfo = ['encryptedVersion' => 15, 'encrypted' => 15];
+               $cacheInfo = ['encryptedVersion' => 6, 'encrypted' => 6];
                $cache2->put($filecache2->getPath(), $cacheInfo);
 
                $this->commandTester->execute([
-                       'user' => self::TEST_ENCRYPTION_VERSION_AFFECTED_USER
+                       'user' => $this->userId
                ]);
 
                $output = $this->commandTester->getDisplay();
 
-               $this->assertStringContainsString("Verifying the content of file /test_enc_version_affected_user1/files/foo.txt
-The file /test_enc_version_affected_user1/files/foo.txt is: OK", $output);
-               $this->assertStringContainsString("Verifying the content of file /test_enc_version_affected_user1/files/hello.txt
-Attempting to fix the path: /test_enc_version_affected_user1/files/hello.txt
-Decrement the encrypted version to 14
-Decrement the encrypted version to 13
-Decrement the encrypted version to 12
-Decrement the encrypted version to 11
-Decrement the encrypted version to 10
-Decrement the encrypted version to 9
-The file /test_enc_version_affected_user1/files/hello.txt is: OK
-Fixed the file: /test_enc_version_affected_user1/files/hello.txt with version 9", $output);
-               $this->assertStringContainsString("Verifying the content of file /test_enc_version_affected_user1/files/world.txt
-Attempting to fix the path: /test_enc_version_affected_user1/files/world.txt
-Decrement the encrypted version to 14
-Decrement the encrypted version to 13
-Decrement the encrypted version to 12
-Decrement the encrypted version to 11
-Decrement the encrypted version to 10
-Decrement the encrypted version to 9
-The file /test_enc_version_affected_user1/files/world.txt is: OK
-Fixed the file: /test_enc_version_affected_user1/files/world.txt with version 9", $output);
+               $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/foo.txt\"
+The file \"/$this->userId/files/foo.txt\" is: OK", $output);
+               $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/hello.txt\"
+Attempting to fix the path: \"/$this->userId/files/hello.txt\"
+Decrement the encrypted version to 4
+Decrement the encrypted version to 3
+The file \"/$this->userId/files/hello.txt\" is: OK
+Fixed the file: \"/$this->userId/files/hello.txt\" with version 3", $output);
+               $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/world.txt\"
+Attempting to fix the path: \"/$this->userId/files/world.txt\"
+Decrement the encrypted version to 5
+Decrement the encrypted version to 4
+The file \"/$this->userId/files/world.txt\" is: OK
+Fixed the file: \"/$this->userId/files/world.txt\" with version 4", $output);
        }
 
        public function testVersionIsRestoredToOriginalIfNoFixIsFound() {
-               \OC::$server->getUserSession()->login(self::TEST_ENCRYPTION_VERSION_AFFECTED_USER, 'foo');
-               $view = new View("/" . self::TEST_ENCRYPTION_VERSION_AFFECTED_USER . "/files");
+               $view = new View("/" . $this->userId . "/files");
 
                $view->touch('bar.txt');
                for ($i = 0; $i < 40; $i++) {
@@ -298,7 +218,7 @@ Fixed the file: /test_enc_version_affected_user1/files/world.txt with version 9"
                $cache->put($fileCache->getPath(), $cacheInfo);
 
                $this->commandTester->execute([
-                       'user' => self::TEST_ENCRYPTION_VERSION_AFFECTED_USER
+                       'user' => $this->userId
                ]);
 
                $cacheInfo = $cache->get($fileInfo->getId());
@@ -311,36 +231,43 @@ Fixed the file: /test_enc_version_affected_user1/files/world.txt with version 9"
         * Test commands with a file path
         */
        public function testExecuteWithFilePathOption() {
-               \OC::$server->getUserSession()->login(self::TEST_ENCRYPTION_VERSION_AFFECTED_USER, 'foo');
+               $view = new View("/" . $this->userId . "/files");
+
+               $view->touch('hello.txt');
+               $view->touch('world.txt');
 
                $this->commandTester->execute([
-                       'user' => self::TEST_ENCRYPTION_VERSION_AFFECTED_USER,
+                       'user' => $this->userId,
                        '--path' => "/hello.txt"
                ]);
 
                $output = $this->commandTester->getDisplay();
 
-               $this->assertStringContainsString("Verifying the content of file /test_enc_version_affected_user1/files/hello.txt
-The file /test_enc_version_affected_user1/files/hello.txt is: OK", $output);
+               $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/hello.txt\"
+The file \"/$this->userId/files/hello.txt\" is: OK", $output);
+               $this->assertStringNotContainsString('world.txt', $output);
        }
 
        /**
         * Test commands with a directory path
         */
        public function testExecuteWithDirectoryPathOption() {
+               $view = new View("/" . $this->userId . "/files");
+
+               $view->mkdir('sub');
+               $view->touch('sub/hello.txt');
+               $view->touch('world.txt');
+
                $this->commandTester->execute([
-                       'user' => self::TEST_ENCRYPTION_VERSION_AFFECTED_USER,
-                       '--path' => "/"
+                       'user' => $this->userId,
+                       '--path' => "/sub"
                ]);
 
                $output = $this->commandTester->getDisplay();
 
-               $this->assertStringContainsString("Verifying the content of file /test_enc_version_affected_user1/files/hello.txt
-The file /test_enc_version_affected_user1/files/hello.txt is: OK", $output);
-               $this->assertStringContainsString("Verifying the content of file /test_enc_version_affected_user1/files/world.txt
-The file /test_enc_version_affected_user1/files/world.txt is: OK", $output);
-               $this->assertStringContainsString("Verifying the content of file /test_enc_version_affected_user1/files/foo.txt
-The file /test_enc_version_affected_user1/files/foo.txt is: OK", $output);
+               $this->assertStringContainsString("Verifying the content of file \"/$this->userId/files/sub/hello.txt\"
+The file \"/$this->userId/files/sub/hello.txt\" is: OK", $output);
+               $this->assertStringNotContainsString('world.txt', $output);
        }
 
        /**
@@ -354,7 +281,7 @@ The file /test_enc_version_affected_user1/files/foo.txt is: OK", $output);
 
                $output = $this->commandTester->getDisplay();
 
-               $this->assertStringContainsString("No user id provided.", $output);
+               $this->assertStringContainsString('does not exist', $output);
        }
 
        /**
@@ -362,12 +289,12 @@ The file /test_enc_version_affected_user1/files/foo.txt is: OK", $output);
         */
        public function testExecuteWithNonExistentPath() {
                $this->commandTester->execute([
-                       'user' => self::TEST_ENCRYPTION_VERSION_AFFECTED_USER,
-                       '--path' => "/non-exist"
+                       'user' => $this->userId,
+                       '--path' => '/non-exist'
                ]);
 
                $output = $this->commandTester->getDisplay();
 
-               $this->assertStringContainsString("Please provide a valid path.", $output);
+               $this->assertStringContainsString('Please provide a valid path.', $output);
        }
 }