Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #805: Incorrect tests of resource type #827

Open
wants to merge 2 commits into
base: 1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 10 additions & 9 deletions src/Crypto/EcAdapter/Impl/Secp256k1/Signature/CompactSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,34 @@ class CompactSignature extends Signature implements CompactSignatureInterface

/**
* @param EcAdapter $ecAdapter
* @param resource $secp256k1_ecdsa_signature_t
* @param resource $secp256k1_recoverable_signature_t
* @param int $recid
* @param bool $compressed
*/
public function __construct(EcAdapter $ecAdapter, $secp256k1_ecdsa_signature_t, int $recid, bool $compressed)
public function __construct(EcAdapter $ecAdapter, $secp256k1_recoverable_signature_t, int $recid, bool $compressed)
{
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');
if (!is_resource($secp256k1_recoverable_signature_t) || SECP256K1_TYPE_RECOVERABLE_SIG !== get_resource_type($secp256k1_recoverable_signature_t)) {
throw new \RuntimeException('CompactSignature: must pass recoverable signature resource, not ' . $secp256k1_recoverable_signature_t);
}

$ser = '';
$recidout = 0;
secp256k1_ecdsa_recoverable_signature_serialize_compact($ecAdapter->getContext(), $ser, $recidout, $secp256k1_ecdsa_signature_t);
secp256k1_ecdsa_recoverable_signature_serialize_compact($ecAdapter->getContext(), $ser, $recidout, $secp256k1_recoverable_signature_t);
list ($r, $s) = array_map(
function ($val) {
return (new Buffer($val))->getGmp();
},
str_split($ser, 32)
);

$this->resource = $secp256k1_ecdsa_signature_t;
$this->resource = $secp256k1_recoverable_signature_t;
$this->recid = $recid;
$this->compressed = $compressed;
$this->ecAdapter = $ecAdapter;
parent::__construct($ecAdapter, $r, $s, $secp256k1_ecdsa_signature_t);

$sig_t = null;
secp256k1_ecdsa_recoverable_signature_convert($this->ecAdapter->getContext(), $sig_t, $this->resource);
parent::__construct($ecAdapter, $r, $s, $sig_t);
}

/**
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(\RuntimeException::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(\RuntimeException::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('Secp256k1\Signature\Signature expects ' . SECP256K1_TYPE_SIG . ' resource');
$this->callConstructor("");
}
public function testWrongResourceType()
{
if (!function_exists('secp256k1_context_create')) {
$this->markTestSkipped("secp256k1 not installed");
}
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Secp256k1\Signature\Signature expects ' . SECP256K1_TYPE_SIG . ' resource');
$this->callConstructor(gmp_init(1));
}
}