oauth2-server/tests/Utils/CryptTraitTest.php

42 lines
960 B
PHP
Raw Normal View History

2016-03-17 21:18:28 +01:00
<?php
namespace LeagueTests\Utils;
use Defuse\Crypto\Key;
2016-03-17 21:18:28 +01:00
use LeagueTests\Stubs\CryptTraitStub;
use PHPUnit\Framework\TestCase;
2016-03-17 21:18:28 +01:00
class CryptTraitTest extends TestCase
2016-03-17 21:18:28 +01:00
{
2018-02-28 20:33:19 +00:00
protected $cryptStub;
protected function setUp(): void
2018-02-28 20:33:19 +00:00
{
$this->cryptStub = new CryptTraitStub();
}
public function testEncryptDecryptWithPassword()
2016-03-17 21:18:28 +01:00
{
2018-02-28 20:33:19 +00:00
$this->cryptStub->setEncryptionKey(base64_encode(random_bytes(36)));
2018-02-28 20:33:19 +00:00
$this->encryptDecrypt();
2016-03-17 21:18:28 +01:00
}
public function testEncryptDecryptWithKey()
2016-03-17 21:18:28 +01:00
{
2018-02-28 20:33:19 +00:00
$this->cryptStub->setEncryptionKey(Key::createNewRandomKey());
2018-02-28 20:33:19 +00:00
$this->encryptDecrypt();
}
2018-02-28 20:33:19 +00:00
private function encryptDecrypt()
{
2016-03-17 21:18:28 +01:00
$payload = 'alex loves whisky';
2018-02-28 20:33:19 +00:00
$encrypted = $this->cryptStub->doEncrypt($payload);
$plainText = $this->cryptStub->doDecrypt($encrypted);
2016-03-17 21:18:28 +01:00
$this->assertNotEquals($payload, $encrypted);
$this->assertEquals($payload, $plainText);
}
}