phpmyadmin/test/classes/Crypto/CryptoTest.php
Maurício Meneghini Fauth 555ea56cbd
Make PHPUnit's assertions stricter
Backports #18993 to QA_5_2.

This makes merging QA_5_2 into master easier.

Signed-off-by: Maurício Meneghini Fauth <mauricio@mfauth.net>
2024-10-12 15:33:45 -03:00

79 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpMyAdmin\Tests\Crypto;
use PhpMyAdmin\Crypto\Crypto;
use PhpMyAdmin\Tests\AbstractTestCase;
use function mb_strlen;
use function str_repeat;
/**
* @covers \PhpMyAdmin\Crypto\Crypto
*/
class CryptoTest extends AbstractTestCase
{
public function testWithValidKeyFromConfig(): void
{
global $config;
$_SESSION = [];
$config->set('URLQueryEncryptionSecretKey', str_repeat('a', 32));
$crypto = new Crypto();
$encrypted = $crypto->encrypt('test');
self::assertNotSame('test', $encrypted);
self::assertSame('test', $crypto->decrypt($encrypted));
self::assertArrayNotHasKey('URLQueryEncryptionSecretKey', $_SESSION);
}
public function testWithValidKeyFromSession(): void
{
global $config;
$_SESSION = ['URLQueryEncryptionSecretKey' => str_repeat('a', 32)];
$config->set('URLQueryEncryptionSecretKey', '');
$crypto = new Crypto();
$encrypted = $crypto->encrypt('test');
self::assertNotSame('test', $encrypted);
self::assertSame('test', $crypto->decrypt($encrypted));
self::assertArrayHasKey('URLQueryEncryptionSecretKey', $_SESSION);
}
public function testWithNewSessionKey(): void
{
global $config;
$_SESSION = [];
$config->set('URLQueryEncryptionSecretKey', '');
$crypto = new Crypto();
$encrypted = $crypto->encrypt('test');
self::assertNotSame('test', $encrypted);
self::assertSame('test', $crypto->decrypt($encrypted));
self::assertArrayHasKey('URLQueryEncryptionSecretKey', $_SESSION);
self::assertSame(32, mb_strlen($_SESSION['URLQueryEncryptionSecretKey'], '8bit'));
}
public function testDecryptWithInvalidKey(): void
{
global $config;
$_SESSION = [];
$config->set('URLQueryEncryptionSecretKey', str_repeat('a', 32));
$crypto = new Crypto();
$encrypted = $crypto->encrypt('test');
self::assertNotSame('test', $encrypted);
self::assertSame('test', $crypto->decrypt($encrypted));
$config->set('URLQueryEncryptionSecretKey', str_repeat('b', 32));
$crypto = new Crypto();
self::assertNull($crypto->decrypt($encrypted));
}
}