From 0d3558824bf77af36ad0cd6e4bb69dbd90ace3c9 Mon Sep 17 00:00:00 2001 From: Paragon Initiative Enterprises Date: Tue, 7 Sep 2021 16:21:00 -0400 Subject: [PATCH] Explicitly test for algorithm lucidity --- tests/LucidityTest.php | 84 ++++++++++++++++++++++++++++++++++++++++++ tests/MultiKeyTest.php | 6 ++- 2 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 tests/LucidityTest.php diff --git a/tests/LucidityTest.php b/tests/LucidityTest.php new file mode 100644 index 0000000..8e9cd16 --- /dev/null +++ b/tests/LucidityTest.php @@ -0,0 +1,84 @@ +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); + } + } +} diff --git a/tests/MultiKeyTest.php b/tests/MultiKeyTest.php index 5c6e823..fa68293 100644 --- a/tests/MultiKeyTest.php +++ b/tests/MultiKeyTest.php @@ -14,7 +14,8 @@ Version3, Version4 }; -use ParagonIE\Paseto\{Builder, +use ParagonIE\Paseto\{ + Builder, JsonToken, Parser, ProtocolInterface, @@ -22,7 +23,8 @@ ReceivingKey, ReceivingKeyRing, SendingKey, - SendingKeyRing}; + SendingKeyRing +}; use PHPUnit\Framework\TestCase; use TypeError;