Skip to content

Commit

Permalink
Fix Bit-Wasp#805: Incorrect tests of resource type
Browse files Browse the repository at this point in the history
  • Loading branch information
afk11 committed Dec 11, 2019
1 parent 65ff838 commit 1aab3ee
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Crypto/EcAdapter/Impl/Secp256k1/Adapter/EcAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class EcAdapter implements EcAdapterInterface
*/
public function __construct(Math $math, GeneratorPoint $generator, $secp256k1_context_t)
{
if (!is_resource($secp256k1_context_t) || !get_resource_type($secp256k1_context_t) === SECP256K1_TYPE_CONTEXT) {
if (!is_resource($secp256k1_context_t) || get_resource_type($secp256k1_context_t) !== SECP256K1_TYPE_CONTEXT) {
throw new \InvalidArgumentException('Secp256k1: Must pass a secp256k1_context_t resource');
}

Expand Down
3 changes: 1 addition & 2 deletions src/Crypto/EcAdapter/Impl/Secp256k1/Key/PublicKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class PublicKey extends Key implements PublicKeyInterface
*/
public function __construct(EcAdapter $ecAdapter, $secp256k1_pubkey_t, bool $compressed = false)
{
if (!is_resource($secp256k1_pubkey_t) ||
!get_resource_type($secp256k1_pubkey_t) === SECP256K1_TYPE_PUBKEY) {
if (!is_resource($secp256k1_pubkey_t) || get_resource_type($secp256k1_pubkey_t) !== SECP256K1_TYPE_PUBKEY) {
throw new \InvalidArgumentException('Secp256k1\Key\PublicKey expects ' . SECP256K1_TYPE_PUBKEY . ' resource');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class CompactSignature extends Signature implements CompactSignatureInterface
*/
public function __construct(EcAdapter $ecAdapter, $secp256k1_ecdsa_signature_t, int $recid, bool $compressed)
{
if (!is_resource($secp256k1_ecdsa_signature_t)
|| SECP256K1_TYPE_RECOVERABLE_SIG !== get_resource_type($secp256k1_ecdsa_signature_t)
) {
if (!is_resource($secp256k1_ecdsa_signature_t) || SECP256K1_TYPE_RECOVERABLE_SIG !== get_resource_type($secp256k1_ecdsa_signature_t)) {
throw new \RuntimeException('CompactSignature: must pass recoverable signature resource');
}

Expand Down
4 changes: 1 addition & 3 deletions src/Crypto/EcAdapter/Impl/Secp256k1/Signature/Signature.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ class Signature extends Serializable implements SignatureInterface
*/
public function __construct(EcAdapter $adapter, \GMP $r, \GMP $s, $secp256k1_ecdsa_signature_t)
{
if (!is_resource($secp256k1_ecdsa_signature_t) ||
!get_resource_type($secp256k1_ecdsa_signature_t) === SECP256K1_TYPE_SIG
) {
if (!is_resource($secp256k1_ecdsa_signature_t) || get_resource_type($secp256k1_ecdsa_signature_t) !== SECP256K1_TYPE_SIG) {
throw new \InvalidArgumentException('Secp256k1\Signature\Signature expects ' . SECP256K1_TYPE_SIG . ' resource');
}

Expand Down
37 changes: 37 additions & 0 deletions tests/Crypto/EcAdapter/Impl/Secp256k1/Adapter/EcAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace BitWasp\Bitcoin\Tests\Crypto\EcAdapter\Impl\Secp256k1\Adapter;

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter;
use BitWasp\Bitcoin\Math\Math;
use BitWasp\Bitcoin\Tests\AbstractTestCase;

class EcAdapterTest extends AbstractTestCase
{
private function callConstructor($value)
{
$math = new Math();
$G = Bitcoin::getGenerator();
return new EcAdapter($math, $G, $value);
}
public function testContextNotResource()
{
if (!function_exists('secp256k1_context_create')) {
$this->markTestSkipped("secp256k1 not installed");
}
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Secp256k1: Must pass a secp256k1_context_t resource");
$this->callConstructor("");
}
public function testContextWrongResource()
{
if (!function_exists('secp256k1_context_create')) {
$this->markTestSkipped("secp256k1 not installed");
}
$ctx = gmp_init(1);
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage("Secp256k1: Must pass a secp256k1_context_t resource");
$this->callConstructor($ctx);
}
}
36 changes: 36 additions & 0 deletions tests/Crypto/EcAdapter/Impl/Secp256k1/Key/PublicKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

namespace BitWasp\Bitcoin\Tests\Crypto\EcAdapter\Impl\Secp256k1\Key;

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Crypto\EcAdapter\EcAdapterFactory;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Key\PublicKey;
use BitWasp\Bitcoin\Math\Math;
use BitWasp\Bitcoin\Tests\AbstractTestCase;

class PublicKeyTest extends AbstractTestCase
{
private function callConstructor($value)
{
return new PublicKey(EcAdapterFactory::getSecp256k1(new Math(), Bitcoin::getGenerator()), $value, true);
}
public function testNotResource()
{
if (!function_exists('secp256k1_context_create')) {
$this->markTestSkipped("secp256k1 not installed");
}
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Secp256k1\Key\PublicKey expects ' . SECP256K1_TYPE_PUBKEY . ' resource');
$this->callConstructor("");
}
public function testWrongResourceType()
{
if (!function_exists('secp256k1_context_create')) {
$this->markTestSkipped("secp256k1 not installed");
}
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Secp256k1\Key\PublicKey expects ' . SECP256K1_TYPE_PUBKEY . ' resource');
$this->callConstructor(gmp_init(1));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

namespace BitWasp\Bitcoin\Tests\Crypto\EcAdapter\Impl\Secp256k1\Signature;

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Crypto\EcAdapter\EcAdapterFactory;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature\CompactSignature;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature\Signature;
use BitWasp\Bitcoin\Math\Math;
use BitWasp\Bitcoin\Tests\AbstractTestCase;

class CompactSignatureTest extends AbstractTestCase
{
private function callConstructor($value)
{
return new CompactSignature(EcAdapterFactory::getSecp256k1(new Math(), Bitcoin::getGenerator()), $value, 1, true);
}
public function testNotResource()
{
if (!function_exists('secp256k1_context_create')) {
$this->markTestSkipped("secp256k1 not installed");
}
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('CompactSignature: must pass recoverable signature resource');
$this->callConstructor("");
}
public function testWrongResourceType()
{
if (!function_exists('secp256k1_context_create')) {
$this->markTestSkipped("secp256k1 not installed");
}
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('CompactSignature: must pass recoverable signature resource');
$this->callConstructor(gmp_init(1));
}
}
35 changes: 35 additions & 0 deletions tests/Crypto/EcAdapter/Impl/Secp256k1/Signature/SignatureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace BitWasp\Bitcoin\Tests\Crypto\EcAdapter\Impl\Secp256k1\Signature;

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Crypto\EcAdapter\EcAdapterFactory;
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature\Signature;
use BitWasp\Bitcoin\Math\Math;
use BitWasp\Bitcoin\Tests\AbstractTestCase;

class SignatureTest extends AbstractTestCase
{
private function callConstructor($value)
{
return new Signature(EcAdapterFactory::getSecp256k1(new Math(), Bitcoin::getGenerator()), gmp_init(1), gmp_init(1), $value);
}
public function testNotResource()
{
if (!function_exists('secp256k1_context_create')) {
$this->markTestSkipped("secp256k1 not installed");
}
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('CompactSignature: must pass recoverable signature resource');
$this->callConstructor("");
}
public function testWrongResourceType()
{
if (!function_exists('secp256k1_context_create')) {
$this->markTestSkipped("secp256k1 not installed");
}
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('CompactSignature: must pass recoverable signature resource');
$this->callConstructor(gmp_init(1));
}
}

0 comments on commit 1aab3ee

Please sign in to comment.