Skip to content

Commit ac08142

Browse files
committed
Fix Bit-Wasp#805: Incorrect tests of resource type
1 parent 65ff838 commit ac08142

File tree

8 files changed

+148
-9
lines changed

8 files changed

+148
-9
lines changed

src/Crypto/EcAdapter/Impl/Secp256k1/Adapter/EcAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class EcAdapter implements EcAdapterInterface
4444
*/
4545
public function __construct(Math $math, GeneratorPoint $generator, $secp256k1_context_t)
4646
{
47-
if (!is_resource($secp256k1_context_t) || !get_resource_type($secp256k1_context_t) === SECP256K1_TYPE_CONTEXT) {
47+
if (!is_resource($secp256k1_context_t) || get_resource_type($secp256k1_context_t) !== SECP256K1_TYPE_CONTEXT) {
4848
throw new \InvalidArgumentException('Secp256k1: Must pass a secp256k1_context_t resource');
4949
}
5050

src/Crypto/EcAdapter/Impl/Secp256k1/Key/PublicKey.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ class PublicKey extends Key implements PublicKeyInterface
3838
*/
3939
public function __construct(EcAdapter $ecAdapter, $secp256k1_pubkey_t, bool $compressed = false)
4040
{
41-
if (!is_resource($secp256k1_pubkey_t) ||
42-
!get_resource_type($secp256k1_pubkey_t) === SECP256K1_TYPE_PUBKEY) {
41+
if (!is_resource($secp256k1_pubkey_t) || get_resource_type($secp256k1_pubkey_t) !== SECP256K1_TYPE_PUBKEY) {
4342
throw new \InvalidArgumentException('Secp256k1\Key\PublicKey expects ' . SECP256K1_TYPE_PUBKEY . ' resource');
4443
}
4544

src/Crypto/EcAdapter/Impl/Secp256k1/Signature/CompactSignature.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ class CompactSignature extends Signature implements CompactSignatureInterface
3939
*/
4040
public function __construct(EcAdapter $ecAdapter, $secp256k1_ecdsa_signature_t, int $recid, bool $compressed)
4141
{
42-
if (!is_resource($secp256k1_ecdsa_signature_t)
43-
|| SECP256K1_TYPE_RECOVERABLE_SIG !== get_resource_type($secp256k1_ecdsa_signature_t)
44-
) {
42+
if (!is_resource($secp256k1_ecdsa_signature_t) || SECP256K1_TYPE_RECOVERABLE_SIG !== get_resource_type($secp256k1_ecdsa_signature_t)) {
4543
throw new \RuntimeException('CompactSignature: must pass recoverable signature resource');
4644
}
4745

src/Crypto/EcAdapter/Impl/Secp256k1/Signature/Signature.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ class Signature extends Serializable implements SignatureInterface
4040
*/
4141
public function __construct(EcAdapter $adapter, \GMP $r, \GMP $s, $secp256k1_ecdsa_signature_t)
4242
{
43-
if (!is_resource($secp256k1_ecdsa_signature_t) ||
44-
!get_resource_type($secp256k1_ecdsa_signature_t) === SECP256K1_TYPE_SIG
45-
) {
43+
if (!is_resource($secp256k1_ecdsa_signature_t) || get_resource_type($secp256k1_ecdsa_signature_t) !== SECP256K1_TYPE_SIG) {
4644
throw new \InvalidArgumentException('Secp256k1\Signature\Signature expects ' . SECP256K1_TYPE_SIG . ' resource');
4745
}
4846

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace BitWasp\Bitcoin\Tests\Crypto\EcAdapter\Impl\Secp256k1\Adapter;
4+
5+
use BitWasp\Bitcoin\Bitcoin;
6+
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter;
7+
use BitWasp\Bitcoin\Math\Math;
8+
use BitWasp\Bitcoin\Tests\AbstractTestCase;
9+
10+
class EcAdapterTest extends AbstractTestCase
11+
{
12+
private function callConstructor($value)
13+
{
14+
$math = new Math();
15+
$G = Bitcoin::getGenerator();
16+
return new EcAdapter($math, $G, $value);
17+
}
18+
public function testContextNotResource()
19+
{
20+
if (!function_exists('secp256k1_context_create')) {
21+
$this->markTestSkipped("secp256k1 not installed");
22+
}
23+
$this->expectException(\InvalidArgumentException::class);
24+
$this->expectExceptionMessage("Secp256k1: Must pass a secp256k1_context_t resource");
25+
$this->callConstructor("");
26+
}
27+
public function testContextWrongResource()
28+
{
29+
if (!function_exists('secp256k1_context_create')) {
30+
$this->markTestSkipped("secp256k1 not installed");
31+
}
32+
$ctx = gmp_init(1);
33+
$this->expectException(\InvalidArgumentException::class);
34+
$this->expectExceptionMessage("Secp256k1: Must pass a secp256k1_context_t resource");
35+
$this->callConstructor($ctx);
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace BitWasp\Bitcoin\Tests\Crypto\EcAdapter\Impl\Secp256k1\Key;
4+
5+
use BitWasp\Bitcoin\Bitcoin;
6+
use BitWasp\Bitcoin\Crypto\EcAdapter\EcAdapterFactory;
7+
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Adapter\EcAdapter;
8+
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Key\PublicKey;
9+
use BitWasp\Bitcoin\Math\Math;
10+
use BitWasp\Bitcoin\Tests\AbstractTestCase;
11+
12+
class PublicKeyTest extends AbstractTestCase
13+
{
14+
private function callConstructor($value)
15+
{
16+
return new PublicKey(EcAdapterFactory::getSecp256k1(new Math(), Bitcoin::getGenerator()), $value, true);
17+
}
18+
public function testNotResource()
19+
{
20+
if (!function_exists('secp256k1_context_create')) {
21+
$this->markTestSkipped("secp256k1 not installed");
22+
}
23+
$this->expectException(\InvalidArgumentException::class);
24+
$this->expectExceptionMessage('Secp256k1\Key\PublicKey expects ' . SECP256K1_TYPE_PUBKEY . ' resource');
25+
$this->callConstructor("");
26+
}
27+
public function testWrongResourceType()
28+
{
29+
if (!function_exists('secp256k1_context_create')) {
30+
$this->markTestSkipped("secp256k1 not installed");
31+
}
32+
$this->expectException(\InvalidArgumentException::class);
33+
$this->expectExceptionMessage('Secp256k1\Key\PublicKey expects ' . SECP256K1_TYPE_PUBKEY . ' resource');
34+
$this->callConstructor(gmp_init(1));
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace BitWasp\Bitcoin\Tests\Crypto\EcAdapter\Impl\Secp256k1\Signature;
4+
5+
use BitWasp\Bitcoin\Bitcoin;
6+
use BitWasp\Bitcoin\Crypto\EcAdapter\EcAdapterFactory;
7+
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature\CompactSignature;
8+
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature\Signature;
9+
use BitWasp\Bitcoin\Math\Math;
10+
use BitWasp\Bitcoin\Tests\AbstractTestCase;
11+
12+
class CompactSignatureTest extends AbstractTestCase
13+
{
14+
private function callConstructor($value)
15+
{
16+
return new CompactSignature(EcAdapterFactory::getSecp256k1(new Math(), Bitcoin::getGenerator()), $value, 1, true);
17+
}
18+
public function testNotResource()
19+
{
20+
if (!function_exists('secp256k1_context_create')) {
21+
$this->markTestSkipped("secp256k1 not installed");
22+
}
23+
$this->expectException(\InvalidArgumentException::class);
24+
$this->expectExceptionMessage('CompactSignature: must pass recoverable signature resource');
25+
$this->callConstructor("");
26+
}
27+
public function testWrongResourceType()
28+
{
29+
if (!function_exists('secp256k1_context_create')) {
30+
$this->markTestSkipped("secp256k1 not installed");
31+
}
32+
$this->expectException(\InvalidArgumentException::class);
33+
$this->expectExceptionMessage('CompactSignature: must pass recoverable signature resource');
34+
$this->callConstructor(gmp_init(1));
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace BitWasp\Bitcoin\Tests\Crypto\EcAdapter\Impl\Secp256k1\Signature;
4+
5+
use BitWasp\Bitcoin\Bitcoin;
6+
use BitWasp\Bitcoin\Crypto\EcAdapter\EcAdapterFactory;
7+
use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\Secp256k1\Signature\Signature;
8+
use BitWasp\Bitcoin\Math\Math;
9+
use BitWasp\Bitcoin\Tests\AbstractTestCase;
10+
11+
class SignatureTest extends AbstractTestCase
12+
{
13+
private function callConstructor($value)
14+
{
15+
return new Signature(EcAdapterFactory::getSecp256k1(new Math(), Bitcoin::getGenerator()), gmp_init(1), gmp_init(1), $value);
16+
}
17+
public function testNotResource()
18+
{
19+
if (!function_exists('secp256k1_context_create')) {
20+
$this->markTestSkipped("secp256k1 not installed");
21+
}
22+
$this->expectException(\InvalidArgumentException::class);
23+
$this->expectExceptionMessage('CompactSignature: must pass recoverable signature resource');
24+
$this->callConstructor("");
25+
}
26+
public function testWrongResourceType()
27+
{
28+
if (!function_exists('secp256k1_context_create')) {
29+
$this->markTestSkipped("secp256k1 not installed");
30+
}
31+
$this->expectException(\InvalidArgumentException::class);
32+
$this->expectExceptionMessage('CompactSignature: must pass recoverable signature resource');
33+
$this->callConstructor(gmp_init(1));
34+
}
35+
}

0 commit comments

Comments
 (0)