Skip to content

Commit

Permalink
forgot tests directory..
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kerin committed Nov 4, 2018
1 parent f2859f9 commit 60392a8
Show file tree
Hide file tree
Showing 140 changed files with 3,715 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/bug1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
Case where referenced zvals are being modified. Only the provided copy should be modified.
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

$context = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
$mainKey = "0123abcd0123abcd0123abcd0123abcd";
$secKeyOne = str_repeat("\x00", 31) . "\x01";

class Something {
private $key;
public function __construct($key) {
$this->key = $key;
}
public function getKey() {
return $this->key;
}
}

echo $mainKey . PHP_EOL;
$something = new Something($mainKey);
$copyKey = $something->getKey();

$result = secp256k1_ec_privkey_tweak_add($context, $copyKey, $secKeyOne);
echo $result . PHP_EOL;
echo $copyKey . PHP_EOL;
echo $mainKey . PHP_EOL;

?>
--EXPECT--
0123abcd0123abcd0123abcd0123abcd
1
0123abcd0123abcd0123abcd0123abce
0123abcd0123abcd0123abcd0123abcd
39 changes: 39 additions & 0 deletions tests/bug2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
--TEST--
secp256k1_ec_privkey_tweak_mul case where referenced zvals are being modified. Only the provided copy should be modified.
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

$context = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);

$mainKey = str_repeat("\x00", 31) . "\x08";
$secKey8 = str_repeat("\x00", 31) . "\x02";

class Something {
private $key;
public function __construct($key) {
$this->key = $key;
}
public function getKey() {
return $this->key;
}
}

echo unpack("H*", $mainKey)[1] . PHP_EOL;
$something = new Something($mainKey);
$copyKey = $something->getKey();

$result = secp256k1_ec_privkey_tweak_mul($context, $copyKey, $secKey8);
echo $result . PHP_EOL;
echo unpack("H*", $copyKey)[1] . PHP_EOL;
echo unpack("H*", $mainKey)[1] . PHP_EOL;

?>
--EXPECT--
0000000000000000000000000000000000000000000000000000000000000008
1
0000000000000000000000000000000000000000000000000000000000000010
0000000000000000000000000000000000000000000000000000000000000008
21 changes: 21 additions & 0 deletions tests/ecdsa_signature_parse_der_lax_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
ecdsa_signature_parse_der_lax works like secp256k1_ecdsa_signature_parse_der
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php
declare(strict_types=1);
$ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN);
$sig = hex2bin("304402207a8e3bdc7c64f31b119a849e8bb39ddbdc0a64abd4cadcc5cfc15d3ec06354ed02204719389aedb16b2dd13552eed546b24350d6e636ac454ea72afc1ffd0cf421b7");

// Parse signature using lax DER encoding - not sure if fixture violates any rules
/** @var resource $laxSig */
$laxSig = null;
$result = ecdsa_signature_parse_der_lax($ctx, $laxSig, $sig);
echo $result . PHP_EOL;

?>
--EXPECT--
1
26 changes: 26 additions & 0 deletions tests/ecdsa_signature_parse_der_lax_error1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
ecdsa_signature_parse_der_lax returns null if context is wrong resource type
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

declare(strict_types=1);

set_error_handler(function($code, $str) { echo $str . PHP_EOL; });

$ctx = tmpfile();
$sig = hex2bin("304402207a8e3bdc7c64f31b119a849e8bb39ddbdc0a64abd4cadcc5cfc15d3ec06354ed02204719389aedb16b2dd13552eed546b24350d6e636ac454ea72afc1ffd0cf421b7");

// Parse signature using lax DER encoding
/** @var resource $laxSig */
$laxSig = null;
$result = ecdsa_signature_parse_der_lax($ctx, $laxSig, $sig);
echo $result . PHP_EOL;

?>
--EXPECT--
ecdsa_signature_parse_der_lax(): supplied resource is not a valid secp256k1_context resource
0
19 changes: 19 additions & 0 deletions tests/ecdsa_signature_parse_der_lax_error2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
ecdsa_signature_parse_der_lax errors signature is garbage
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

$ctx = \secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
$sig = \hex2bin("3001024242");

// Parse signature using lax DER encoding
$result = ecdsa_signature_parse_der_lax($ctx, $laxSig, $sig);
echo $result . PHP_EOL;

?>
--EXPECT--
0
18 changes: 18 additions & 0 deletions tests/ecdsa_signature_parse_der_lax_error3.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
ecdsa_signature_parse_der_lax returns false if parameter parsing fails
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

set_error_handler(function($code, $str) { echo $str . PHP_EOL; });

$result = ecdsa_signature_parse_der_lax();
echo $result . PHP_EOL;

?>
--EXPECT--
ecdsa_signature_parse_der_lax() expects exactly 3 parameters, 0 given
0
16 changes: 16 additions & 0 deletions tests/php_minfo_function_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Code coverage for PHP_MINFO_FUNCTION(secp256k1)
--SKIPIF--
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
--FILE--
<?php
ob_start();
phpinfo(INFO_MODULES);
$v = ob_get_clean();
$r = preg_match('/secp256k1 support .* enabled/', $v);
if ($r !== 1)
var_dump($r);
echo "Done\n";
?>
--EXPECTF--
Done
17 changes: 17 additions & 0 deletions tests/secp256k1_context_clone_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
secp256k1_context_clone works
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php
$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
echo get_resource_type($ctx) . "\n";

$clone = secp256k1_context_clone($ctx);
echo get_resource_type($clone) . "\n";
?>
--EXPECT--
secp256k1_context
secp256k1_context
19 changes: 19 additions & 0 deletions tests/secp256k1_context_clone_error1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
secp256k1_context_clone returns false if provided the wrong type
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

set_error_handler(function($code, $str) { echo $str . PHP_EOL; });

$handle = tmpfile();
$result = secp256k1_context_clone($handle);
echo gettype($result) . PHP_EOL;

?>
--EXPECT--
secp256k1_context_clone(): supplied resource is not a valid secp256k1_context resource
NULL
18 changes: 18 additions & 0 deletions tests/secp256k1_context_clone_error2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
secp256k1_context_clone returns false if missing flags argument
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

set_error_handler(function($code, $str) { echo $str . PHP_EOL; });

$ctx1 = secp256k1_context_clone();
echo gettype($ctx1) . PHP_EOL;

?>
--EXPECT--
secp256k1_context_clone() expects exactly 1 parameter, 0 given
NULL
20 changes: 20 additions & 0 deletions tests/secp256k1_context_create_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
secp256k1_context_create works
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

$ctx1 = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
echo get_resource_type($ctx1) . "\n";
$ctx2 = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
echo get_resource_type($ctx2) . "\n";
$ctx3 = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY);
echo get_resource_type($ctx3) . "\n";
?>
--EXPECT--
secp256k1_context
secp256k1_context
secp256k1_context
15 changes: 15 additions & 0 deletions tests/secp256k1_context_create_error1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--TEST--
secp256k1_context_create returns false if provided invalid flags
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

$ctx1 = secp256k1_context_create(SECP256K1_CONTEXT_SIGN << 2 | (SECP256K1_CONTEXT_VERIFY+2>>1));
echo gettype($ctx1) . PHP_EOL;

?>
--EXPECT--
NULL
18 changes: 18 additions & 0 deletions tests/secp256k1_context_create_error2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
secp256k1_context_create returns false if missing flags argument
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

set_error_handler(function($code, $str) { echo $str . PHP_EOL; });

$ctx1 = secp256k1_context_create();
echo gettype($ctx1) . PHP_EOL;

?>
--EXPECT--
secp256k1_context_create() expects exactly 1 parameter, 0 given
NULL
17 changes: 17 additions & 0 deletions tests/secp256k1_context_destroy_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
secp256k1_context_destroy works
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php
$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
echo get_resource_type($ctx) . "\n";

secp256k1_context_destroy($ctx);
echo get_resource_type($ctx) . "\n";
?>
--EXPECT--
secp256k1_context
Unknown
21 changes: 21 additions & 0 deletions tests/secp256k1_context_destroy_error1.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
secp256k1_context_destroy returns false if provided the wrong resource type
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

set_error_handler(function($code, $str) { echo $str . PHP_EOL; });

$handle = tmpfile();
$result = secp256k1_context_destroy($handle);
echo gettype($result) . PHP_EOL;
echo ($result ? "true" : "false") . PHP_EOL;

?>
--EXPECT--
secp256k1_context_destroy(): supplied resource is not a valid secp256k1_context resource
boolean
false
20 changes: 20 additions & 0 deletions tests/secp256k1_context_destroy_error2.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
secp256k1_context_destroy returns false if missing flags argument
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php

set_error_handler(function($code, $str) { echo $str . PHP_EOL; });

$ctx1 = secp256k1_context_destroy();
echo gettype($ctx1) . PHP_EOL;
echo ($ctx1 ? "true" : "false") . PHP_EOL;

?>
--EXPECT--
secp256k1_context_destroy() expects exactly 1 parameter, 0 given
boolean
false
29 changes: 29 additions & 0 deletions tests/secp256k1_context_randomize_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
--TEST--
secp256k1_context_randomize works
--SKIPIF--
<?php
if (!extension_loaded("secp256k1")) print "skip extension not loaded";
?>
--FILE--
<?php
$ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
echo get_resource_type($ctx) . "\n";

$state1 = str_repeat("\x42", 32);
$result = secp256k1_context_randomize($ctx, $state1);
echo $result . PHP_EOL;

// reset operation
$result = secp256k1_context_randomize($ctx, null);
echo $result . PHP_EOL;

// reset operation (implicit)
$result = secp256k1_context_randomize($ctx);
echo $result . PHP_EOL;

?>
--EXPECT--
secp256k1_context
1
1
1
Loading

0 comments on commit 60392a8

Please sign in to comment.