Finish testing and debugging RsaKeypair

This commit is contained in:
Jeremy Dormitzer 2018-12-18 08:42:09 -05:00
parent d3c79dfa4d
commit dacd01a47b
2 changed files with 74 additions and 2 deletions

View File

@ -1,6 +1,7 @@
<?php
namespace ActivityPub\Crypto;
use BadMethodCallException;
use phpseclib\Crypt\RSA;
class RsaKeypair
@ -46,7 +47,7 @@ class RsaKeypair
*/
public function sign( $data )
{
if ( ! $this->privateKey ) {
if ( empty( $this->privateKey ) ) {
throw new BadMethodCallException(
'Unable to sign data without a private key'
);
@ -95,7 +96,7 @@ class RsaKeypair
*/
public function fromPublicKey( string $publicKey )
{
return new RsaKeypair( $publicKey, null );
return new RsaKeypair( $publicKey, '' );
}
}
?>

View File

@ -1,8 +1,10 @@
<?php
namespace ActivityPub\Test;
use BadMethodCallException;
use ActivityPub\Crypto\RsaKeypair;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Error\Error;
class RsaKeypairTest extends TestCase
{
@ -12,5 +14,74 @@ class RsaKeypairTest extends TestCase
$this->assertStringStartsWith( '-----BEGIN PUBLIC KEY-----', $keypair->getPublicKey() );
$this->assertStringEndsWith( '-----END PUBLIC KEY-----', $keypair->getPublicKey() );
}
public function testItSignsAndValidatesSignatures()
{
$keypair = RsaKeypair::generate();
$data = 'This is some data';
$signature = $keypair->sign( $data );
$this->assertInternalType( 'string', $signature );
$this->assertNotEmpty( $signature );
$verified = $keypair->verify( $data, $signature );
$this->assertTrue( $verified );
}
public function testItGivesErrorValidatingInvalidSignature()
{
$keypair = RsaKeypair::generate();
$data = 'This is some data';
$signature = 'not a real signature';
$this->expectException( Error::class );
$verified = $keypair->verify( $data, $signature );
}
public function testItReturnsNotVerifiedForValidButWrongSignature()
{
$keypairOne = RsaKeypair::generate();
$data = 'This is some data';
$signature = $keypairOne->sign( $data );
$keypairTwo = RsaKeypair::generate();
$verified = $keypairTwo->verify( $data, $signature );
$this->assertFalse( $verified );
}
public function testItCreatesValidPublicKeyOnly()
{
$fullKeypair = RsaKeypair::generate();
$publicKeyOnly = RsaKeypair::fromPublicKey( $fullKeypair->getPublicKey() );
$data = 'This is some data';
$signature = $fullKeypair->sign( $data );
$verified = $publicKeyOnly->verify( $data, $signature );
$this->assertTrue( $verified );
}
public function testItCannotSignWithPublicKeyOnly()
{
$fullKeypair = RsaKeypair::generate();
$publicKeyOnly = RsaKeypair::fromPublicKey( $fullKeypair->getPublicKey() );
$data = 'This is some data';
$this->expectException( BadMethodCallException::class );
$this->expectExceptionMessage( 'Unable to sign data without a private key' );
$signature = $publicKeyOnly->sign( $data );
}
public function testItSignsAndVerifiesEmptyData()
{
$keypair = RsaKeypair::generate();
$data = '';
$signature = $keypair->sign( $data );
$verified = $keypair->verify( $data, $signature );
$this->assertTrue( $verified );
}
public function testItHandlesInvalidPublicKeyOnly()
{
$fullKeypair = RsaKeypair::generate();
$publicKeyOnly = RsaKeypair::fromPublicKey( 'not a real public key' );
$data = 'This is some data';
$signature = $fullKeypair->sign( $data );
$verified = $publicKeyOnly->verify( $data, $signature );
$this->assertFalse( $verified );
}
}
?>