@@ -14,18 +14,25 @@ import (
14
14
"testing"
15
15
"time"
16
16
17
+ "github.com/golang/mock/gomock"
18
+ "github.com/iotexproject/iotex-address/address"
17
19
"github.com/pkg/errors"
18
20
"github.com/stretchr/testify/require"
19
21
20
- "github.com/iotexproject/iotex-address/address"
21
22
"github.com/iotexproject/iotex-core/action"
22
23
"github.com/iotexproject/iotex-core/config"
23
24
"github.com/iotexproject/iotex-core/state"
24
25
"github.com/iotexproject/iotex-core/test/identityset"
25
26
)
26
27
27
- func TestActionProto (t * testing.T ) {
28
+ func TestActionProtoAndGenericValidator (t * testing.T ) {
28
29
require := require .New (t )
30
+ ctrl := gomock .NewController (t )
31
+ defer ctrl .Finish ()
32
+
33
+ reg := NewRegistry ()
34
+ mp := NewMockProtocol (ctrl )
35
+ require .NoError (reg .Register ("1" , mp ))
29
36
caller , err := address .FromString ("io1mflp9m6hcgm2qcghchsdqj3z3eccrnekx9p0ms" )
30
37
require .NoError (err )
31
38
producer , err := address .FromString ("io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6" )
@@ -49,18 +56,21 @@ func TestActionProto(t *testing.T) {
49
56
Hash : config .Default .Genesis .Hash (),
50
57
Timestamp : time .Unix (config .Default .Genesis .Timestamp , 0 ),
51
58
},
59
+ Registry : reg ,
52
60
})
53
61
54
62
valid := NewGenericValidator (nil , func (sr StateReader , addr string ) (* state.Account , error ) {
55
- if strings .EqualFold ("io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6" , addr ) {
63
+ pk := identityset .PrivateKey (27 ).PublicKey ()
64
+ eAddr , _ := address .FromBytes (pk .Hash ())
65
+ if strings .EqualFold (eAddr .String (), addr ) {
56
66
return nil , errors .New ("MockChainManager nonce error" )
57
67
}
58
68
return & state.Account {Nonce : 2 }, nil
59
69
})
60
70
data , err := hex .DecodeString ("" )
61
71
require .NoError (err )
62
- // Case I: Normal
63
- {
72
+ t . Run ( "normal" , func ( t * testing. T ) {
73
+ mp . EXPECT (). Validate ( gomock . Any (), gomock . Any ()). Return ( nil ). Times ( 1 )
64
74
v , err := action .NewExecution ("" , 0 , big .NewInt (10 ), uint64 (10 ), big .NewInt (10 ), data )
65
75
require .NoError (err )
66
76
bd := & action.EnvelopeBuilder {}
@@ -72,9 +82,8 @@ func TestActionProto(t *testing.T) {
72
82
nselp := action.SealedEnvelope {}
73
83
require .NoError (nselp .LoadProto (selp .Proto ()))
74
84
require .NoError (valid .Validate (ctx , nselp ))
75
- }
76
- // Case II: GasLimit lower
77
- {
85
+ })
86
+ t .Run ("Gas limit low" , func (t * testing.T ) {
78
87
v , err := action .NewExecution ("" , 0 , big .NewInt (10 ), uint64 (10 ), big .NewInt (10 ), data )
79
88
require .NoError (err )
80
89
bd := & action.EnvelopeBuilder {}
@@ -88,31 +97,23 @@ func TestActionProto(t *testing.T) {
88
97
err = valid .Validate (ctx , nselp )
89
98
require .Error (err )
90
99
require .True (strings .Contains (err .Error (), "insufficient gas" ))
91
- }
92
- // Case III: Call cm Nonce err
93
- {
94
- caller , err := address .FromString ("io1emxf8zzqckhgjde6dqd97ts0y3q496gm3fdrl6" )
95
- require .NoError (err )
96
- ctx := WithActionCtx (ctx ,
97
- ActionCtx {
98
- Caller : caller ,
99
- })
100
+ })
101
+ t .Run ("state error" , func (t * testing.T ) {
100
102
v , err := action .NewExecution ("" , 0 , big .NewInt (10 ), uint64 (10 ), big .NewInt (10 ), data )
101
103
require .NoError (err )
102
104
bd := & action.EnvelopeBuilder {}
103
105
elp := bd .SetGasPrice (big .NewInt (10 )).
104
106
SetGasLimit (uint64 (100000 )).
105
107
SetAction (v ).Build ()
106
- selp , err := action .Sign (elp , identityset .PrivateKey (28 ))
108
+ selp , err := action .Sign (elp , identityset .PrivateKey (27 ))
107
109
require .NoError (err )
108
110
nselp := action.SealedEnvelope {}
109
111
require .NoError (nselp .LoadProto (selp .Proto ()))
110
112
err = valid .Validate (ctx , nselp )
111
113
require .Error (err )
112
114
require .True (strings .Contains (err .Error (), "invalid state of account" ))
113
- }
114
- // Case IV: Call Nonce err
115
- {
115
+ })
116
+ t .Run ("nonce too low" , func (t * testing.T ) {
116
117
v , err := action .NewExecution ("" , 1 , big .NewInt (10 ), uint64 (10 ), big .NewInt (10 ), data )
117
118
require .NoError (err )
118
119
bd := & action.EnvelopeBuilder {}
@@ -127,5 +128,40 @@ func TestActionProto(t *testing.T) {
127
128
err = valid .Validate (ctx , nselp )
128
129
require .Error (err )
129
130
require .True (strings .Contains (err .Error (), "nonce is too low" ))
130
- }
131
+ })
132
+ t .Run ("wrong recipient" , func (t * testing.T ) {
133
+ v , err := action .NewTransfer (1 , big .NewInt (1 ), "io1qyqsyqcyq5narhapakcsrhksfajfcpl24us3xp38zwvsep" , []byte {}, uint64 (100000 ), big .NewInt (10 ))
134
+ require .NoError (err )
135
+ bd := & action.EnvelopeBuilder {}
136
+ elp := bd .SetAction (v ).SetGasLimit (100000 ).
137
+ SetGasPrice (big .NewInt (10 )).
138
+ SetNonce (1 ).Build ()
139
+ selp , err := action .Sign (elp , identityset .PrivateKey (27 ))
140
+ require .NoError (err )
141
+ require .Error (valid .Validate (ctx , selp ))
142
+ })
143
+ t .Run ("wrong signature" , func (t * testing.T ) {
144
+ unsignedTsf , err := action .NewTransfer (uint64 (1 ), big .NewInt (1 ), caller .String (), []byte {}, uint64 (100000 ), big .NewInt (0 ))
145
+ require .NoError (err )
146
+
147
+ bd := & action.EnvelopeBuilder {}
148
+ elp := bd .SetNonce (1 ).
149
+ SetAction (unsignedTsf ).
150
+ SetGasLimit (100000 ).Build ()
151
+ selp := action .FakeSeal (elp , identityset .PrivateKey (27 ).PublicKey ())
152
+ require .True (strings .Contains (valid .Validate (ctx , selp ).Error (), "failed to verify action signature" ))
153
+ })
154
+ t .Run ("protocol validation failed" , func (t * testing.T ) {
155
+ me := errors .New ("mock error" )
156
+ mp .EXPECT ().Validate (gomock .Any (), gomock .Any ()).Return (me ).Times (1 )
157
+ v , err := action .NewExecution ("" , 0 , big .NewInt (10 ), uint64 (10 ), big .NewInt (10 ), data )
158
+ require .NoError (err )
159
+ bd := & action.EnvelopeBuilder {}
160
+ elp := bd .SetGasPrice (big .NewInt (10 )).
161
+ SetGasLimit (uint64 (100000 )).
162
+ SetAction (v ).Build ()
163
+ selp , err := action .Sign (elp , identityset .PrivateKey (28 ))
164
+ require .NoError (err )
165
+ require .Equal (me , errors .Cause (valid .Validate (ctx , selp )))
166
+ })
131
167
}
0 commit comments