forked from bitpay/bitpay-php-keyutils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrivateKeyTest.php
More file actions
208 lines (162 loc) · 6.96 KB
/
PrivateKeyTest.php
File metadata and controls
208 lines (162 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
declare(strict_types=1);
namespace BitPayKeyUtils\UnitTest\KeyHelper;
use BitPayKeyUtils\KeyHelper\PrivateKey;
use BitPayKeyUtils\KeyHelper\PublicKey;
use PHPUnit\Framework\TestCase;
class PrivateKeyTest extends TestCase
{
private const EXAMPLE_HEX_STRING = '4f0a88d37c05095b';
private const EXAMPLE_ECDSA_KEY = '-----BEGIN EC PRIVATE KEY-----MHQCAQEEIFL3sLnioGcDvHWM/BPlNw96BOx1KKco2qsq4UwhQUosoAcGBSuBBAAK
oUQDQgAEXs1Fmq4QdPAbn3NycdEU+HOjc3kW9efbso2kI/vdDTWcSCMk310s53G3tRClDBPPuuJAsKghbPfaTaUpmXFCNA==-----END EC PRIVATE KEY-----';
public function testSetHex(): void
{
$testedObject = $this->createClassObject();
$exampleValue = 'test';
$testedObject->setHex($exampleValue);
$realValue = $this->accessProtected($testedObject, 'hex');
self::assertSame($exampleValue, $realValue);
}
public function testGenerateReturnsObjectWithRandomHexIfHexIsNotSet(): void
{
$testedObject = $this->createClassObject();
$result = $testedObject->generate();
$realValue = $this->accessProtected($result, 'hex');
self::assertIsString($realValue);
}
public function testGenerateReturnsObjectWithHexIfHexIsSet(): void
{
$testedObject = $this->createClassObject();
$expectedValue = 'test';
$testedObject->setHex($expectedValue);
$result = $testedObject->generate();
$realValue = $this->accessProtected($result, 'hex');
self::assertSame($expectedValue, $realValue);
}
public function testIsValidIfKeyNotSet(): void
{
$testedObject = $this->createClassObject();
self::assertFalse($testedObject->isValid());
}
public function testHasValidHexIfHexNotSet(): void
{
$testedObject = $this->createClassObject();
self::assertFalse($testedObject->hasValidHex());
}
public function testIsValid(): void
{
$testedObject = $this->createClassObject();
$testedObject->setHex(self::EXAMPLE_HEX_STRING);
self::assertTrue($testedObject->isValid());
}
public function testSign(): void
{
$testedObject = $this->createClassObject();
$testedObject->setHex(self::EXAMPLE_HEX_STRING);
try {
$result = $testedObject->sign('test');
} catch (\Exception $exception) {
self::fail();
}
self::assertIsString($result);
}
public function testSignWhenNoHex(): void
{
$testedObject = $this->createClassObject();
$this->expectException(\Exception::class);
$this->expectExceptionMessage('The private key must be in hex format.');
$testedObject->sign('test');
}
public function testSignWhenDataEmpty(): void
{
$testedObject = $this->createClassObject();
$testedObject->setHex(self::EXAMPLE_HEX_STRING);
$this->expectException(\Exception::class);
$this->expectExceptionMessage('You did not provide any data to sign');
$testedObject->sign([]);
}
public function testPemDecode(): void
{
$testedObject = $this->createClassObject();
$result = $testedObject->pemDecode(self::EXAMPLE_ECDSA_KEY);
self::assertIsArray($result);
self::assertSame('52f7b0b9e2a06703bc758cfc13e5370f7a04ec7528a728daab2ae14c21414a2c', $result['private_key']);
self::assertSame(
'045ecd459aae1074f01b9f737271d114f873a3737916f5e7dbb28da423fbdd0d359c482324df5d2ce771b7b510a50c13cfbae240b0a8216cf7da4da52999714234',
$result['public_key']);
}
public function testPemDecodeTooShortPemException(): void
{
$testedObject = $this->createClassObject();
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Invalid or corrupt secp256k1 key provided. Cannot decode the supplied PEM data.');
$testedObject->pemDecode('test');
}
public function testPemDecodePemShouldThrowExceptionWhenCorruptKeyProvided(): void
{
$testedObject = $this->createClassObject();
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Invalid or corrupt secp256k1 key provided. Cannot decode the supplied PEM data.');
$testedObject->pemDecode(
'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
);
}
public function testPemEncode(): void
{
$testedObject = $this->createClassObject();
$beg_ec_text = '-----BEGIN EC PRIVATE KEY-----';
$end_ec_text = '-----END EC PRIVATE KEY-----';
$exampleKeyPair = ['52f7b0b9e2a06703bc758cfc13e5370f7a04ec7528a728daab2ae14c21414a2c',
'045ecd459aae1074f01b9f737271d114f873a3737916f5e7dbb28da423fbdd0d359c482324df5d2ce771b7b510a50c13cfbae240b0a8216cf7da4da529997142',
];
$result = $testedObject->pemEncode($exampleKeyPair);
self::assertIsString($result);
self::assertStringContainsString($beg_ec_text, $result);
self::assertStringContainsString($end_ec_text, $result);
}
public function testPemEncodeCorruptSecp(): void
{
$testedObject = $this->createClassObject();
$exampleCorruptKeyPair = ['52f7b0b9e2a06703bc758cfc13e5370f7a04ec7528a728daab2ae14c21414a2c',
'7dbb28da423fbdd0d359c482324df5d2ce771b7b510a50c13cfbae240b0a8216cf7da4da52999714234',
];
$this->expectExceptionMessage('Invalid or corrupt secp256k1 keypair provided. Cannot decode the supplied PEM data.');
$this->expectException(\Exception::class);
$testedObject->pemEncode($exampleCorruptKeyPair);
}
public function test__toString(): void
{
$testedObject = $this->createClassObject();
self::assertEmpty((string)$testedObject);
}
public function test__toStringWhenHexIsSet(): void
{
$testedObject = $this->createClassObject();
$testedObject->setHex(self::EXAMPLE_HEX_STRING);
self::assertSame(self::EXAMPLE_HEX_STRING, (string)$testedObject);
}
public function testGetPublicKeyIfKeyIsNull(): void
{
$testedObject = $this->createClassObject();
$result = $testedObject->getPublicKey();
self::assertInstanceOf(PublicKey::class, $result);
}
public function testGetPublicKeyIfKeyIsNotNull(): void
{
$testedObject = $this->createClassObject();
$firstResult = $testedObject->getPublicKey();
$secondResult = $testedObject->getPublicKey();
self::assertInstanceOf(PublicKey::class, $firstResult);
self::assertSame($firstResult, $secondResult);
}
private function createClassObject(): PrivateKey
{
return new PrivateKey();
}
private function accessProtected($obj, $prop)
{
$reflection = new \ReflectionClass($obj);
$property = $reflection->getProperty($prop);
return $property->getValue($obj);
}
}