2016-03-17 21:18:28 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace LeagueTests\Utils;
|
|
|
|
|
2017-11-20 07:42:09 +01:00
|
|
|
use Defuse\Crypto\Key;
|
2016-03-17 21:18:28 +01:00
|
|
|
use LeagueTests\Stubs\CryptTraitStub;
|
2017-11-08 16:07:07 -02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-03-17 21:18:28 +01:00
|
|
|
|
2017-11-08 16:07:07 -02:00
|
|
|
class CryptTraitTest extends TestCase
|
2016-03-17 21:18:28 +01:00
|
|
|
{
|
2018-02-28 20:33:19 +00:00
|
|
|
protected $cryptStub;
|
|
|
|
|
2019-06-27 12:54:22 +01:00
|
|
|
protected function setUp(): void
|
2018-02-28 20:33:19 +00:00
|
|
|
{
|
|
|
|
$this->cryptStub = new CryptTraitStub();
|
|
|
|
}
|
|
|
|
|
2017-11-20 07:42:09 +01:00
|
|
|
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)));
|
2017-11-20 07:42:09 +01:00
|
|
|
|
2018-02-28 20:33:19 +00:00
|
|
|
$this->encryptDecrypt();
|
2016-03-17 21:18:28 +01:00
|
|
|
}
|
|
|
|
|
2017-11-20 07:42:09 +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());
|
2017-11-20 07:42:09 +01:00
|
|
|
|
2018-02-28 20:33:19 +00:00
|
|
|
$this->encryptDecrypt();
|
2017-11-20 07:42:09 +01:00
|
|
|
}
|
|
|
|
|
2018-02-28 20:33:19 +00:00
|
|
|
private function encryptDecrypt()
|
2018-02-28 20:01:01 +00:00
|
|
|
{
|
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);
|
|
|
|
}
|
|
|
|
}
|