chore(tests): Add more unitary tests

Signed-off-by: Rubén D <nuxsmin@syspass.org>
This commit is contained in:
Rubén D
2023-05-21 13:02:19 +02:00
parent dfd4fd0671
commit 419a674fba
8 changed files with 92 additions and 162 deletions

View File

@@ -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);

View File

@@ -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
*

View File

@@ -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
{

View File

@@ -1,10 +1,10 @@
<?php
/**
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -19,101 +19,84 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
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');
}
}

View File

@@ -1,10 +1,10 @@
<?php
/**
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -19,31 +19,26 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
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);

View File

@@ -1,10 +1,10 @@
<?php
/**
/*
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2023, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
@@ -19,95 +19,58 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
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());
}
}

View File

@@ -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
{

View File

@@ -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
{