From b3052e49326dc1f70abd0e67d9d830b65b2aec07 Mon Sep 17 00:00:00 2001 From: Vincent Petry Date: Thu, 24 Jun 2021 10:51:07 +0200 Subject: [PATCH] Fix FixEncryptedVersionTest test 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 --- .../lib/Command/FixEncryptedVersion.php | 10 +- .../tests/Command/FixEncryptedVersionTest.php | 233 ++++++------------ 2 files changed, 85 insertions(+), 158 deletions(-) diff --git a/apps/encryption/lib/Command/FixEncryptedVersion.php b/apps/encryption/lib/Command/FixEncryptedVersion.php index 88a72f43324..e2181f9a229 100644 --- a/apps/encryption/lib/Command/FixEncryptedVersion.php +++ b/apps/encryption/lib/Command/FixEncryptedVersion.php @@ -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("Path $path does not exist. Please provide a valid path."); + $output->writeln("Path \"$path\" does not exist. Please provide a valid path."); 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("The file $path is: OK"); + $output->writeln("The file \"$path\" is: OK"); } \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("Attempting to fix the path: $path"); + $output->writeln("Attempting to fix the path: \"$path\""); return $this->correctEncryptedVersion($path, $output); } return false; diff --git a/apps/encryption/tests/Command/FixEncryptedVersionTest.php b/apps/encryption/tests/Command/FixEncryptedVersionTest.php index 42556288556..a530275784a 100644 --- a/apps/encryption/tests/Command/FixEncryptedVersionTest.php +++ b/apps/encryption/tests/Command/FixEncryptedVersionTest.php @@ -21,18 +21,13 @@ 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); } } -- 2.39.5