Skip to content

Commit

Permalink
Add encrypter specific tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vokomarov committed Sep 24, 2024
1 parent c231076 commit 474e1ec
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
39 changes: 39 additions & 0 deletions tests/Feature/Service/Encrypter/AES256ECBTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Service\Encrypter;

use App\Service\Encrypter\AES256ECB;
use Spiral\Encrypter\Exception\EncrypterException;
use Tests\Fixtures;
use Tests\TestCase;

class AES256ECBTest extends TestCase
{
public function testEncryptDecrypt(): void
{
$key = Fixtures::string();
$message = Fixtures::string(256);

$encrypter = new AES256ECB();
$encrypted = $encrypter->encrypt($message, $key);

$this->assertNotEmpty($encrypted);
$this->assertNotEquals($message, $encrypted);
$this->assertEquals($message, $encrypter->decrypt($encrypted, $key));
}

public function testDecryptError(): void
{
$key = Fixtures::string();
$message = Fixtures::string(256);

$encrypter = new AES256ECB();
$encrypted = $encrypter->encrypt($message, $key);

$this->expectException(EncrypterException::class);

$this->assertEquals($message, $encrypter->decrypt($encrypted, $key . Fixtures::string(1)));
}
}
39 changes: 39 additions & 0 deletions tests/Feature/Service/Encrypter/AES256GCMTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Service\Encrypter;

use App\Service\Encrypter\AES256GCM;
use Spiral\Encrypter\Exception\EncrypterException;
use Tests\Fixtures;
use Tests\TestCase;

class AES256GCMTest extends TestCase
{
public function testEncryptDecrypt(): void
{
$key = Fixtures::string();
$message = Fixtures::string(256);

$encrypter = new AES256GCM();
$encrypted = $encrypter->encrypt($message, $key);

$this->assertNotEmpty($encrypted);
$this->assertNotEquals($message, $encrypted);
$this->assertEquals($message, $encrypter->decrypt($encrypted, $key));
}

public function testDecryptError(): void
{
$key = Fixtures::string();
$message = Fixtures::string(256);

$encrypter = new AES256GCM();
$encrypted = $encrypter->encrypt($message, $key);

$this->expectException(EncrypterException::class);

$this->assertEquals($message, $encrypter->decrypt($encrypted, $key . Fixtures::string(1)));
}
}

0 comments on commit 474e1ec

Please sign in to comment.