Skip to content

Commit

Permalink
test: + HaliteAdapterTest
Browse files Browse the repository at this point in the history
  • Loading branch information
mle86 committed Nov 11, 2018
1 parent 19fccf6 commit 3a0c9e9
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions test/HaliteAdapterTest.php
@@ -0,0 +1,68 @@
<?php

namespace mle86\RequestAuthentication\Tests;

use mle86\RequestAuthentication\Crypto\Halite\HaliteSigningPrivateKey;
use mle86\RequestAuthentication\Crypto\Halite\HaliteSigningPublicKey;
use mle86\RequestAuthentication\Exception\CryptoErrorException;
use mle86\RequestAuthentication\Exception\InvalidArgumentException;
use mle86\RequestAuthentication\Tests\Helper\AssertException;
use PHPUnit\Framework\TestCase;

class HaliteAdapterTest extends TestCase
{
use AssertException;

public static function invalidKeyMaterial(): array { return [
[''],
['QWJj'],
]; }

public function testKeyGeneration(): HaliteSigningPrivateKey
{
$priv1 = HaliteSigningPrivateKey::generate();
$priv2 = HaliteSigningPrivateKey::generate();
$this->assertNotEquals($priv1->getEncodedForm(), $priv2->getEncodedForm());

$pub1 = HaliteSigningPublicKey::fromPrivateKey($priv1);
$pub2 = HaliteSigningPublicKey::fromPrivateKey($priv2);
$this->assertNotEquals($pub1->getEncodedForm(), $pub2->getEncodedForm());

$pub11 = HaliteSigningPublicKey::fromPrivateKey($priv1);
$this->assertEquals ($pub1->getEncodedForm(), $pub11->getEncodedForm());

return $priv1;
}

/**
* @depends testKeyGeneration
*/
public function testKeyEncoding(HaliteSigningPrivateKey $priv): void
{
$priv2 = new HaliteSigningPrivateKey($priv->getEncodedForm());
$this->assertEquals($priv->getEncodedForm(), $priv2->getEncodedForm());

$pub = HaliteSigningPublicKey::fromPrivateKey($priv);
$pub2 = new HaliteSigningPublicKey($pub->getEncodedForm());
$this->assertEquals($pub->getEncodedForm(), $pub2->getEncodedForm());
}

/**
* @dataProvider invalidKeyMaterial
* @depends testKeyEncoding
*/
public function testInvalidKeyEncoding(string $invalidKeyMaterial): void
{
$this->assertException([CryptoErrorException::class, InvalidArgumentException::class], function() use($invalidKeyMaterial) {
new HaliteSigningPrivateKey($invalidKeyMaterial);
});
$this->assertException([CryptoErrorException::class, InvalidArgumentException::class], function() use($invalidKeyMaterial) {
new HaliteSigningPublicKey($invalidKeyMaterial);
});
}

// There's no need to test the other adapter methods any further
// because the PublicKeyMethodTest will make heavy use of them.
// Halite itself is well-tested.

}

0 comments on commit 3a0c9e9

Please sign in to comment.