15
15
16
16
package org .tron .core .config .args ;
17
17
18
+ import static org .junit .Assert .assertEquals ;
19
+ import static org .junit .Assert .assertThrows ;
20
+ import static org .junit .Assert .assertTrue ;
18
21
import static org .junit .Assert .fail ;
19
22
20
23
import com .google .common .collect .Lists ;
21
24
import java .io .IOException ;
25
+ import java .security .SecureRandom ;
22
26
import org .junit .Assert ;
23
27
import org .junit .Before ;
24
28
import org .junit .Rule ;
25
29
import org .junit .Test ;
26
30
import org .junit .rules .TemporaryFolder ;
31
+ import org .tron .common .utils .ByteArray ;
27
32
import org .tron .common .utils .LocalWitnesses ;
28
33
import org .tron .common .utils .PublicMethod ;
29
34
import org .tron .common .utils .StringUtil ;
30
35
import org .tron .core .Constant ;
36
+ import org .tron .core .exception .TronError ;
37
+ import org .tron .core .exception .TronError .ErrCode ;
31
38
32
39
public class LocalWitnessTest {
33
40
@@ -42,7 +49,7 @@ public void setLocalWitness() {
42
49
localWitness
43
50
.setPrivateKeys (
44
51
Lists .newArrayList (
45
- PRIVATE_KEY ));
52
+ PRIVATE_KEY ));
46
53
}
47
54
48
55
@ Test
@@ -52,13 +59,13 @@ public void whenSetNullPrivateKey() {
52
59
Assert .assertNotNull (localWitness .getPublicKey ());
53
60
}
54
61
55
- @ Test (expected = IllegalArgumentException .class )
62
+ @ Test (expected = TronError .class )
56
63
public void whenSetEmptyPrivateKey () {
57
64
localWitness .setPrivateKeys (Lists .newArrayList ("" ));
58
65
fail ("private key must be 64-bits hex string" );
59
66
}
60
67
61
- @ Test (expected = IllegalArgumentException .class )
68
+ @ Test (expected = TronError .class )
62
69
public void whenSetBadFormatPrivateKey () {
63
70
localWitness .setPrivateKeys (Lists .newArrayList ("a111" ));
64
71
fail ("private key must be 64-bits hex string" );
@@ -104,62 +111,27 @@ public void testValidPrivateKeyWithPrefix() {
104
111
@ Test
105
112
public void testInvalidPrivateKey () {
106
113
LocalWitnesses localWitnesses = new LocalWitnesses ();
114
+ String expectedMessage = "private key must be 64 hex string" ;
115
+ assertTronError (localWitnesses , null , expectedMessage );
116
+ assertTronError (localWitnesses , "" , expectedMessage );
117
+ assertTronError (localWitnesses , " " , expectedMessage );
118
+ assertTronError (localWitnesses , "11111" , expectedMessage );
119
+ String expectedMessage2 = "private key must be hex string" ;
120
+ SecureRandom secureRandom = new SecureRandom ();
121
+ byte [] keyBytes = new byte [31 ];
122
+ secureRandom .nextBytes (keyBytes );
123
+ final String privateKey = ByteArray .toHexString (keyBytes ) + " " ;
124
+ assertTronError (localWitnesses , privateKey , expectedMessage2 );
125
+ final String privateKey2 = "xy" + ByteArray .toHexString (keyBytes );
126
+ assertTronError (localWitnesses , privateKey2 , expectedMessage2 );
127
+ }
107
128
108
- try {
109
- localWitnesses .addPrivateKeys (null );
110
- fail ("should throw IllegalArgumentException" );
111
- } catch (IllegalArgumentException e ) {
112
- Assert .assertTrue (e .getMessage ().contains ("private key must be 64 hex string" ));
113
- } catch (Exception e ) {
114
- fail ("should IllegalArgumentException,actual exception: " + e .getClass ().getSimpleName ());
115
- }
116
-
117
- try {
118
- localWitnesses .addPrivateKeys ("" );
119
- fail ("should throw IllegalArgumentException" );
120
- } catch (IllegalArgumentException e ) {
121
- Assert .assertTrue (e .getMessage ().contains ("private key must be 64 hex string" ));
122
- } catch (Exception e ) {
123
- fail ("should IllegalArgumentException,actual exception: " + e .getClass ().getSimpleName ());
124
- }
125
-
126
- try {
127
- localWitnesses .addPrivateKeys (" " );
128
- fail ("should throw IllegalArgumentException" );
129
- } catch (IllegalArgumentException e ) {
130
- Assert .assertTrue (e .getMessage ().contains ("private key must be 64 hex string" ));
131
- } catch (Exception e ) {
132
- fail ("should IllegalArgumentException,actual exception: " + e .getClass ().getSimpleName ());
133
- }
134
-
135
- try {
136
- localWitnesses .addPrivateKeys ("11111" );
137
- fail ("should throw IllegalArgumentException" );
138
- } catch (IllegalArgumentException e ) {
139
- Assert .assertTrue (e .getMessage ().contains ("private key must be 64 hex string" ));
140
- } catch (Exception e ) {
141
- fail ("should IllegalArgumentException,actual exception: " + e .getClass ().getSimpleName ());
142
- }
143
-
144
- try {
145
- String privateKey = "11111111111111111111111111111111111111111111111111111111111111 " ;
146
- localWitnesses .addPrivateKeys (privateKey );
147
- fail ("should throw IllegalArgumentException" );
148
- } catch (IllegalArgumentException e ) {
149
- Assert .assertTrue (e .getMessage ().contains ("private key must be hex string" ));
150
- } catch (Exception e ) {
151
- fail ("should IllegalArgumentException,actual exception: " + e .getClass ().getSimpleName ());
152
- }
153
-
154
- try {
155
- String privateKey = "xy11111111111111111111111111111111111111111111111111111111111111" ;
156
- localWitnesses .addPrivateKeys (privateKey );
157
- fail ("should throw IllegalArgumentException" );
158
- } catch (IllegalArgumentException e ) {
159
- Assert .assertTrue (e .getMessage ().contains ("private key must be hex string" ));
160
- } catch (Exception e ) {
161
- fail ("should IllegalArgumentException,actual exception: " + e .getClass ().getSimpleName ());
162
- }
129
+ private void assertTronError (LocalWitnesses localWitnesses , String privateKey ,
130
+ String expectedMessage ) {
131
+ TronError thrown = assertThrows (TronError .class ,
132
+ () -> localWitnesses .addPrivateKeys (privateKey ));
133
+ assertEquals (ErrCode .WITNESS_INIT , thrown .getErrCode ());
134
+ assertTrue (thrown .getMessage ().contains (expectedMessage ));
163
135
}
164
136
165
137
@ Test
0 commit comments