Skip to content

Commit

Permalink
Explicitly test for algorithm lucidity
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Sep 7, 2021
1 parent d440efc commit 0d35588
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 2 deletions.
84 changes: 84 additions & 0 deletions tests/LucidityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace ParagonIE\Paseto\Tests;

use Exception;
use ParagonIE\Paseto\Exception\PasetoException;
use ParagonIE\Paseto\KeyInterface;
use ParagonIE\Paseto\Keys\SymmetricKey;
use ParagonIE\Paseto\Protocol\{
Version1,
Version2,
Version3,
Version4
};
use PHPUnit\Framework\TestCase;
use TypeError;

class LucidityTest extends TestCase
{
/**
* @return array[]
* @throws Exception
*/
public function luciditySymmetric()
{
$v4_lk = Version4::generateSymmetricKey();
$v4_sk = Version4::generateAsymmetricSecretKey();
$v4_pk = $v4_sk->getPublicKey();

$v1_lk = new SymmetricKey($v4_lk->raw(), new Version1);
$v2_lk = new SymmetricKey($v4_lk->raw(), new Version2);
$v3_lk = new SymmetricKey($v4_lk->raw(), new Version3);
return [
[
new Version4,
$v4_lk,
$v4_pk
], [
new Version4,
$v4_lk,
$v4_sk
], [
new Version4,
$v4_lk,
$v1_lk
], [
new Version4,
$v4_lk,
$v2_lk
], [
new Version4,
$v4_lk,
$v3_lk
]
];
}

/**
* @param Version1|Version2|Version3|Version4 $protocol
* @param KeyInterface $validKey
* @param KeyInterface $invalidKey
*
* @dataProvider luciditySymmetric
* @throws Exception
* @throws PasetoException
*/
public function testLocalLucidity(
$protocol,
KeyInterface $validKey,
KeyInterface $invalidKey
) {
$dummy = '{"test":true}';
$encode = $protocol::encrypt($dummy, $validKey);
$decode = $protocol::decrypt($encode, $validKey);
$this->assertSame($decode, $dummy);

$this->expectException(PasetoException::class);
try {
$protocol::decrypt($encode, $invalidKey);
} catch (TypeError $ex) {
throw new PasetoException('TypeError', 0, $ex);
}
}
}
6 changes: 4 additions & 2 deletions tests/MultiKeyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
Version3,
Version4
};
use ParagonIE\Paseto\{Builder,
use ParagonIE\Paseto\{
Builder,
JsonToken,
Parser,
ProtocolInterface,
Purpose,
ReceivingKey,
ReceivingKeyRing,
SendingKey,
SendingKeyRing};
SendingKeyRing
};
use PHPUnit\Framework\TestCase;
use TypeError;

Expand Down

0 comments on commit 0d35588

Please sign in to comment.