From 419a674fbaed44c2864fb03c89a6f03cd3d26b4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Sun, 21 May 2023 13:02:19 +0200 Subject: [PATCH] chore(tests): Add more unitary tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- lib/SP/Core/Crypt/Crypt.php | 9 +- lib/SP/Core/Crypt/CryptInterface.php | 12 +- tests/SP/Core/Crypt/CryptPKITest.php | 3 +- tests/SP/Core/Crypt/CryptTest.php | 111 ++++++++---------- tests/SP/Core/Crypt/HashTest.php | 28 ++--- tests/SP/Core/Crypt/VaultTest.php | 85 ++++---------- .../Services/AccountCryptServiceTest.php | 3 +- tests/SP/UnitaryTestCase.php | 3 +- 8 files changed, 92 insertions(+), 162 deletions(-) diff --git a/lib/SP/Core/Crypt/Crypt.php b/lib/SP/Core/Crypt/Crypt.php index 530318f3..3aec6281 100644 --- a/lib/SP/Core/Crypt/Crypt.php +++ b/lib/SP/Core/Crypt/Crypt.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -97,15 +97,14 @@ class Crypt implements CryptInterface * * @return string|Key * @throws \SP\Core\Exceptions\CryptException - * @TODO: Update callers to use instance */ - public function unlockSecuredKey(string $key, string $password, bool $useAscii = true): Key|string + private function unlockSecuredKey(string $key, string $password, bool $useAscii = true): Key|string { try { if ($useAscii) { return KeyProtectedByPassword::loadFromAsciiSafeString($key) - ->unlockKey($password) - ->saveToAsciiSafeString(); + ->unlockKey($password) + ->saveToAsciiSafeString(); } return KeyProtectedByPassword::loadFromAsciiSafeString($key)->unlockKey($password); diff --git a/lib/SP/Core/Crypt/CryptInterface.php b/lib/SP/Core/Crypt/CryptInterface.php index ae9af0ee..d674c935 100644 --- a/lib/SP/Core/Crypt/CryptInterface.php +++ b/lib/SP/Core/Crypt/CryptInterface.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -57,16 +57,6 @@ interface CryptInterface */ public function encrypt(string $data, Key|string $securedKey, ?string $password = null): string; - /** - * @param string $key - * @param string $password - * @param bool $useAscii - * - * @return string|Key - * @throws \SP\Core\Exceptions\CryptException - */ - public function unlockSecuredKey(string $key, string $password, bool $useAscii = true): Key|string; - /** * Desencriptar datos con una clave segura * diff --git a/tests/SP/Core/Crypt/CryptPKITest.php b/tests/SP/Core/Crypt/CryptPKITest.php index 8eba592d..464a61d4 100644 --- a/tests/SP/Core/Crypt/CryptPKITest.php +++ b/tests/SP/Core/Crypt/CryptPKITest.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -108,6 +108,7 @@ class CryptPKITest extends UnitaryTestCase * This method is called before a test is executed. * * @throws SPException + * @throws \PHPUnit\Framework\MockObject\Exception */ protected function setUp(): void { diff --git a/tests/SP/Core/Crypt/CryptTest.php b/tests/SP/Core/Crypt/CryptTest.php index a34ebbda..6f10474c 100644 --- a/tests/SP/Core/Crypt/CryptTest.php +++ b/tests/SP/Core/Crypt/CryptTest.php @@ -1,10 +1,10 @@ . + * along with sysPass. If not, see . */ namespace SP\Tests\Core\Crypt; use Defuse\Crypto\Exception\CryptoException; -use PHPUnit\Framework\TestCase; use SP\Core\Crypt\Crypt; +use SP\Core\Exceptions\CryptException; +use SP\Tests\UnitaryTestCase; /** * Class CryptTest * - * Tests unitarios para comprobar el funcionamiento de la clase SP\Core\Crypt\Crypt - * - * @package SP\Tests + * @group unitary */ -class CryptTest extends TestCase +class CryptTest extends UnitaryTestCase { - const PASSWORD = 'test_password'; + /** + * Comprobar la generación de una llave de cifrado + * + * @throws \SP\Core\Exceptions\CryptException + */ + public function testMakeSecuredKey() + { + (new Crypt())->makeSecuredKey(self::$faker->password); + + $this->assertTrue(true); + } /** * Comprobar la generación de una llave de cifrado * - * @throws CryptoException + * @throws \SP\Core\Exceptions\CryptException */ - public function testMakeSecuredKey() + public function testMakeSecuredKeyNoAscii() { + (new Crypt())->makeSecuredKey(self::$faker->password, false); + $this->assertTrue(true); - - return Crypt::makeSecuredKey(self::PASSWORD); - } - - /** - * Comprobar el desbloqueo de una llave de cifrado - * - * @depends testMakeSecuredKey - * - * @param string $key LLave de cifrado - * - * @throws CryptoException - */ - public function testUnlockSecuredKey($key) - { - $this->assertTrue(true); - - Crypt::unlockSecuredKey($key, self::PASSWORD); - } - - /** - * Comprobar el desbloqueo de una llave de cifrado - * - * @depends testMakeSecuredKey - * - * @param string $key LLave de cifrado - * - * @throws CryptoException - */ - public function testUnlockSecuredKeyWithWrongPassword($key) - { - $this->expectException(CryptoException::class); - - Crypt::unlockSecuredKey($key, 'test'); } /** * Comprobar la encriptación y desencriptado de datos * - * @depends testMakeSecuredKey - * - * @param string $key LLave de cifrado - * - * @throws CryptoException + * @throws \SP\Core\Exceptions\CryptException */ - public function testEncryptAndDecrypt($key) + public function testEncryptAndDecrypt() { - $data = Crypt::encrypt('prueba', $key, self::PASSWORD); + $crypt = new Crypt(); - $this->assertSame('prueba', Crypt::decrypt($data, $key, self::PASSWORD)); + $password = self::$faker->password; + + $key = $crypt->makeSecuredKey($password); + + $data = self::$faker->text; + + $out = $crypt->encrypt($data, $key, $password); + + $this->assertSame($data, $crypt->decrypt($out, $key, $password)); } /** * Comprobar la encriptación y desencriptado de datos * - * @depends testMakeSecuredKey - * - * @param string $key LLave de cifrado - * - * @throws CryptoException + * @throws \SP\Core\Exceptions\CryptException */ - public function testEncryptAndDecryptWithDifferentPassword($key) + public function testEncryptAndDecryptWithDifferentPassword() { - $data = Crypt::encrypt('prueba', $key, self::PASSWORD); + $crypt = new Crypt(); - $this->expectException(CryptoException::class); + $password = self::$faker->password; - $this->assertSame('prueba', Crypt::decrypt($data, $key, 'test')); + $key = $crypt->makeSecuredKey($password); + + $data = $crypt->encrypt('prueba', $key, $password); + + $this->expectException(CryptException::class); + + $crypt->decrypt($data, $key, 'test'); } } diff --git a/tests/SP/Core/Crypt/HashTest.php b/tests/SP/Core/Crypt/HashTest.php index f1aebd7e..80493f7c 100644 --- a/tests/SP/Core/Crypt/HashTest.php +++ b/tests/SP/Core/Crypt/HashTest.php @@ -1,10 +1,10 @@ . + * along with sysPass. If not, see . */ namespace SP\Tests\Core\Crypt; -use Defuse\Crypto\Exception\EnvironmentIsBrokenException; use Faker\Factory; -use PHPUnit\Framework\TestCase; use SP\Core\Crypt\Hash; -use SP\Util\PasswordUtil; +use SP\Tests\UnitaryTestCase; /** * Class HashTest * - * @package SP\Tests\SP\Core\Crypt + * @group unitary */ -class HashTest extends TestCase +class HashTest extends UnitaryTestCase { - /** - * @throws EnvironmentIsBrokenException - */ public function testHashKey() { for ($i = 2; $i <= 128; $i *= 2) { - $key = PasswordUtil::generateRandomBytes($i); + $key = self::$faker->password(2, $i); $hash = Hash::hashKey($key); $this->assertNotEmpty($hash); @@ -51,9 +46,6 @@ class HashTest extends TestCase } } - /** - * @throws EnvironmentIsBrokenException - */ public function testSignMessage() { $faker = Factory::create(); @@ -61,7 +53,7 @@ class HashTest extends TestCase for ($i = 2; $i <= 128; $i *= 2) { $text = $faker->text; - $key = PasswordUtil::generateRandomBytes($i); + $key = self::$faker->password(2, $i); $hash = Hash::signMessage($text, $key); $this->assertNotEmpty($hash); diff --git a/tests/SP/Core/Crypt/VaultTest.php b/tests/SP/Core/Crypt/VaultTest.php index ac4b3e76..ce30206b 100644 --- a/tests/SP/Core/Crypt/VaultTest.php +++ b/tests/SP/Core/Crypt/VaultTest.php @@ -1,10 +1,10 @@ . + * along with sysPass. If not, see . */ namespace SP\Tests\Core\Crypt; -use Defuse\Crypto\Exception\CryptoException; -use Defuse\Crypto\Exception\EnvironmentIsBrokenException; -use PHPUnit\Framework\TestCase; +use SP\Core\Crypt\Crypt; use SP\Core\Crypt\Vault; -use SP\Util\PasswordUtil; +use SP\Tests\UnitaryTestCase; /** * Class VaultTest * - * @package SP\Tests + * @group unitary */ -class VaultTest extends TestCase +class VaultTest extends UnitaryTestCase { /** - * @var string - */ - private $key; - - /** - * Sets up the fixture, for example, open a network connection. - * This method is called before a test is executed. - * - * @throws EnvironmentIsBrokenException - */ - protected function setUp(): void - { - $this->key = PasswordUtil::generateRandomBytes(); - } - - /** - * @throws CryptoException + * @throws \SP\Core\Exceptions\CryptException */ public function testGetData() { - $vault = new Vault(); - $vault->saveData('prueba', $this->key); - $this->assertEquals('prueba', $vault->getData($this->key)); + $data = self::$faker->text; + $key = self::$faker->password; - $randomData = PasswordUtil::generateRandomBytes(); - - $vault = new Vault(); - $vault->saveData($randomData, $this->key); - $this->assertEquals($randomData, $vault->getData($this->key)); + $vault = Vault::factory(new Crypt())->saveData($data, $key); + $this->assertEquals($data, $vault->getData($key)); } - /** - * @throws CryptoException + * @throws \SP\Core\Exceptions\CryptException */ public function testGetTimeSet() { - $vault = new Vault(); - $vault->saveData('test', $this->key); + $vault = Vault::factory(new Crypt())->saveData(self::$faker->text, self::$faker->password); $this->assertTrue($vault->getTimeSet() !== 0); } /** - * @throws CryptoException + * @throws \SP\Core\Exceptions\CryptException */ public function testReKey() { - $vault = new Vault(); - $vault->saveData('prueba', $this->key); + $data = self::$faker->text; + $key = self::$faker->password; - $this->assertEquals('prueba', $vault->getData($this->key)); + $vault = Vault::factory(new Crypt())->saveData($data, $key); - $vault->reKey(1234, $this->key); + $newKey = self::$faker->password; - $this->assertEquals('prueba', $vault->getData(1234)); - } + $vaultRekey = $vault->reKey($newKey, $key); - /** - * @throws CryptoException - */ - public function testGetTimeUpdated() - { - $vault = new Vault(); - $vault->saveData('test', $this->key); - - $this->assertTrue($vault->getTimeUpdated() === 0); - - $vault->reKey(1234, $this->key); - - $this->assertTrue(is_int($vault->getTimeUpdated())); - $this->assertTrue($vault->getTimeUpdated() > 0); + $this->assertEquals($data, $vaultRekey->getData($newKey)); + $this->assertGreaterThan($vault->getTimeSet(), $vaultRekey->getTimeSet()); } } diff --git a/tests/SP/Domain/Account/Services/AccountCryptServiceTest.php b/tests/SP/Domain/Account/Services/AccountCryptServiceTest.php index 80ef0e5f..605ac8d1 100644 --- a/tests/SP/Domain/Account/Services/AccountCryptServiceTest.php +++ b/tests/SP/Domain/Account/Services/AccountCryptServiceTest.php @@ -4,7 +4,7 @@ * * @author nuxsmin * @link https://syspass.org - * @copyright 2012-2022, Rubén Domínguez nuxsmin@$syspass.org + * @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org * * This file is part of sysPass. * @@ -53,6 +53,7 @@ class AccountCryptServiceTest extends UnitaryTestCase /** * @throws \SP\Domain\Common\Services\ServiceException * @throws \SP\Infrastructure\File\FileException + * @throws \PHPUnit\Framework\MockObject\Exception */ public function testUpdateMasterPassword(): void { diff --git a/tests/SP/UnitaryTestCase.php b/tests/SP/UnitaryTestCase.php index 616befcb..94c4ee89 100644 --- a/tests/SP/UnitaryTestCase.php +++ b/tests/SP/UnitaryTestCase.php @@ -62,6 +62,7 @@ abstract class UnitaryTestCase extends TestCase /** * @throws \SP\Core\Context\ContextException + * @throws \PHPUnit\Framework\MockObject\Exception */ protected function setUp(): void { @@ -73,7 +74,7 @@ abstract class UnitaryTestCase extends TestCase /** * @return \SP\Core\Application - * @throws \SP\Core\Context\ContextException + * @throws \SP\Core\Context\ContextException|\PHPUnit\Framework\MockObject\Exception */ private function mockApplication(): Application {