Skip to content

Commit c160c17

Browse files
author
Roman Bylbas
committed
Added tests for Ed25519Key and Secp256K1Key
Signed-off-by: Roman Bylbas <romka.bulbas@gmail.com>
1 parent 3dac193 commit c160c17

8 files changed

+153
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.idea
22
/vendor
3+
.phpunit.*

tests/Assets/Ed25519PublicKey.pem

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MCowBQYDK2VwAyEA2MlSwH0IxuvstH1WCGFtXXomJaEFPIzKosRgWxUzjMc=
3+
-----END PUBLIC KEY-----

tests/Assets/Ed25519SecretKey.pem

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MC4CAQAwBQYDK2VwBCIEINRjlbiAvFJvucKQAmvDatlOviXQg9bU+yh1dw8+Mexs
3+
-----END PRIVATE KEY-----

tests/Assets/Secp256K1PublicKey.pem

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAExtEDVh+DpBKB0njgfkWQVSm/JddPoYRj
3+
GjCa/nvD3AZbWpRrnx2PmN7Z2Ax7AFZlKkZH/DXHzTw1hEzTMIcnwA==
4+
-----END PUBLIC KEY-----

tests/Assets/Secp256K1SecretKey.pem

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-----BEGIN EC PRIVATE KEY-----
2+
MHQCAQEEIMrpkkX79/3XH7FsLPt6hLDTlu2keHoV4dkG0H8dpWlToAcGBSuBBAAK
3+
oUQDQgAExtEDVh+DpBKB0njgfkWQVSm/JddPoYRjGjCa/nvD3AZbWpRrnx2PmN7Z
4+
2Ax7AFZlKkZH/DXHzTw1hEzTMIcnwA==
5+
-----END EC PRIVATE KEY-----

tests/Rpc/RpcClientTest.php tests/Functional/Rpc/RpcClientTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Tests\Rpc;
3+
namespace Tests\Functional\Rpc;
44

55
use PHPUnit\Framework\TestCase;
66

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Tests\Unit\Util\Crypto;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
use Casper\Util\Crypto\Ed25519Key;
8+
9+
class Ed25519KeyTest extends TestCase
10+
{
11+
/**
12+
* @throws \Exception
13+
*/
14+
public function testCreateNewEd25519FromFile(): void
15+
{
16+
$publicKeyValue = 'd8c952c07d08c6ebecb47d5608616d5d7a2625a1053c8ccaa2c4605b15338cc7';
17+
$ed25519KeyPair = Ed25519Key::createFromPrivateKeyFile('tests/Assets/Ed25519SecretKey.pem');
18+
19+
$this->assertEquals($publicKeyValue, $ed25519KeyPair->getPublicKey());
20+
}
21+
22+
/**
23+
* @throws \Exception
24+
*/
25+
public function testCantCreateNewEd25519FromWrongFile(): void
26+
{
27+
$this->expectException(\Exception::class);
28+
Ed25519Key::createFromPrivateKeyFile('tests/Assets/Secp256K1SecretKey.pem');
29+
}
30+
31+
/**
32+
* @throws \Exception
33+
*/
34+
public function testExportPublicKeyInPem(): void
35+
{
36+
$publicKeyInPem = file_get_contents('tests/Assets/Ed25519PublicKey.pem');
37+
$ed25519KeyPair = Ed25519Key::createFromPrivateKeyFile('tests/Assets/Ed25519SecretKey.pem');
38+
39+
$this->assertEquals($publicKeyInPem, $ed25519KeyPair->exportPublicKeyInPem());
40+
}
41+
42+
/**
43+
* @throws \Exception
44+
*/
45+
public function testExportPrivateKeyInPem(): void
46+
{
47+
$privateKeyInPem = file_get_contents('tests/Assets/Ed25519SecretKey.pem');
48+
$ed25519KeyPair = Ed25519Key::createFromPrivateKeyFile('tests/Assets/Ed25519SecretKey.pem');
49+
50+
$this->assertEquals($privateKeyInPem, $ed25519KeyPair->exportPrivateKeyInPem());
51+
}
52+
53+
public function testSigningAndVerification(): void
54+
{
55+
$message = 'Hello world';
56+
$ed25519KeyPair = Ed25519Key::createFromPrivateKeyFile('tests/Assets/Ed25519SecretKey.pem');
57+
58+
$signature = $ed25519KeyPair->sign($message);
59+
$isVerified = $ed25519KeyPair->verify($signature, $message);
60+
$this->assertTrue($isVerified);
61+
62+
$corruptedSignature = str_replace('3', '2', $signature);
63+
$isVerified = $ed25519KeyPair->verify($corruptedSignature, $message);
64+
$this->assertFalse($isVerified);
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Tests\Unit\Util\Crypto;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
use Casper\Util\Crypto\Secp256K1Key;
8+
9+
class Secp256K1KeyTest extends TestCase
10+
{
11+
/**
12+
* @throws \Exception
13+
*/
14+
public function testCreateNewSecp256K1FromFile(): void
15+
{
16+
$compressedPublicKeyValue = '02c6d103561f83a41281d278e07e45905529bf25d74fa184631a309afe7bc3dc06';
17+
$secp256k1KeyPair = Secp256K1Key::createFromPrivateKeyFile('tests/Assets/Secp256K1SecretKey.pem');
18+
19+
$this->assertEquals($compressedPublicKeyValue, $secp256k1KeyPair->getPublicKey());
20+
}
21+
22+
/**
23+
* @throws \Exception
24+
*/
25+
public function testCantCreateNewSecp256K1FromWrongFile(): void
26+
{
27+
$this->expectException(\RuntimeException::class);
28+
$this->expectExceptionMessage('Invalid data.');
29+
Secp256K1Key::createFromPrivateKeyFile('tests/Assets/Ed25519SecretKey.pem');
30+
}
31+
32+
/**
33+
* @throws \Exception
34+
*/
35+
public function testExportPublicKeyInPem(): void
36+
{
37+
$publicKeyInPem = file_get_contents('tests/Assets/Secp256K1PublicKey.pem');
38+
$secp256k1KeyPair = Secp256K1Key::createFromPrivateKeyFile('tests/Assets/Secp256K1SecretKey.pem');
39+
40+
$this->assertEquals($publicKeyInPem, $secp256k1KeyPair->exportPublicKeyInPem());
41+
}
42+
43+
/**
44+
* @throws \Exception
45+
*/
46+
public function testExportPrivateKeyInPem(): void
47+
{
48+
$privateKeyInPem = file_get_contents('tests/Assets/Secp256K1SecretKey.pem');
49+
$secp256k1KeyPair = Secp256K1Key::createFromPrivateKeyFile('tests/Assets/Secp256K1SecretKey.pem');
50+
51+
$this->assertEquals($privateKeyInPem, $secp256k1KeyPair->exportPrivateKeyInPem());
52+
}
53+
54+
/**
55+
* @throws \Exception
56+
*/
57+
public function testSigningAndVerification(): void
58+
{
59+
$message = 'Hello world';
60+
$secp256k1KeyPair = Secp256K1Key::createFromPrivateKeyFile('tests/Assets/Secp256K1SecretKey.pem');
61+
62+
$signature = $secp256k1KeyPair->sign($message);
63+
$isVerified = $secp256k1KeyPair->verify($signature, $message);
64+
$this->assertTrue($isVerified);
65+
66+
$corruptedSignature = str_replace('3', '2', $signature);
67+
$isVerified = $secp256k1KeyPair->verify($corruptedSignature, $message);
68+
$this->assertFalse($isVerified);
69+
}
70+
}

0 commit comments

Comments
 (0)