Skip to content

Commit 16b735f

Browse files
signer/core: fix TestSignTx to decode res2 (#32749)
Decode the modified transaction and verify the value differs from original. --------- Co-authored-by: lightclient <[email protected]>
1 parent 2e2fece commit 16b735f

File tree

1 file changed

+20
-22
lines changed

1 file changed

+20
-22
lines changed

signer/core/api_test.go

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package core_test
1818

1919
import (
2020
"bytes"
21-
"context"
2221
"fmt"
2322
"math/big"
2423
"os"
@@ -97,12 +96,12 @@ func (ui *headlessUi) ApproveNewAccount(request *core.NewAccountRequest) (core.N
9796
}
9897

9998
func (ui *headlessUi) ShowError(message string) {
100-
//stdout is used by communication
99+
// stdout is used by communication
101100
fmt.Fprintln(os.Stderr, message)
102101
}
103102

104103
func (ui *headlessUi) ShowInfo(message string) {
105-
//stdout is used by communication
104+
// stdout is used by communication
106105
fmt.Fprintln(os.Stderr, message)
107106
}
108107

@@ -128,7 +127,7 @@ func setup(t *testing.T) (*core.SignerAPI, *headlessUi) {
128127
func createAccount(ui *headlessUi, api *core.SignerAPI, t *testing.T) {
129128
ui.approveCh <- "Y"
130129
ui.inputCh <- "a_long_password"
131-
_, err := api.New(context.Background())
130+
_, err := api.New(t.Context())
132131
if err != nil {
133132
t.Fatal(err)
134133
}
@@ -143,7 +142,7 @@ func failCreateAccountWithPassword(ui *headlessUi, api *core.SignerAPI, password
143142
ui.inputCh <- password
144143
ui.inputCh <- password
145144

146-
addr, err := api.New(context.Background())
145+
addr, err := api.New(t.Context())
147146
if err == nil {
148147
t.Fatal("Should have returned an error")
149148
}
@@ -154,7 +153,7 @@ func failCreateAccountWithPassword(ui *headlessUi, api *core.SignerAPI, password
154153

155154
func failCreateAccount(ui *headlessUi, api *core.SignerAPI, t *testing.T) {
156155
ui.approveCh <- "N"
157-
addr, err := api.New(context.Background())
156+
addr, err := api.New(t.Context())
158157
if err != core.ErrRequestDenied {
159158
t.Fatal(err)
160159
}
@@ -165,7 +164,7 @@ func failCreateAccount(ui *headlessUi, api *core.SignerAPI, t *testing.T) {
165164

166165
func list(ui *headlessUi, api *core.SignerAPI, t *testing.T) ([]common.Address, error) {
167166
ui.approveCh <- "A"
168-
return api.List(context.Background())
167+
return api.List(t.Context())
169168
}
170169

171170
func TestNewAcc(t *testing.T) {
@@ -199,7 +198,7 @@ func TestNewAcc(t *testing.T) {
199198
// Testing listing:
200199
// Listing one Account
201200
control.approveCh <- "1"
202-
list, err := api.List(context.Background())
201+
list, err := api.List(t.Context())
203202
if err != nil {
204203
t.Fatal(err)
205204
}
@@ -208,7 +207,7 @@ func TestNewAcc(t *testing.T) {
208207
}
209208
// Listing denied
210209
control.approveCh <- "Nope"
211-
list, err = api.List(context.Background())
210+
list, err = api.List(t.Context())
212211
if len(list) != 0 {
213212
t.Fatalf("List should be empty")
214213
}
@@ -246,7 +245,7 @@ func TestSignTx(t *testing.T) {
246245
api, control := setup(t)
247246
createAccount(control, api, t)
248247
control.approveCh <- "A"
249-
list, err = api.List(context.Background())
248+
list, err = api.List(t.Context())
250249
if err != nil {
251250
t.Fatal(err)
252251
}
@@ -260,15 +259,15 @@ func TestSignTx(t *testing.T) {
260259

261260
control.approveCh <- "Y"
262261
control.inputCh <- "wrongpassword"
263-
res, err = api.SignTransaction(context.Background(), tx, &methodSig)
262+
res, err = api.SignTransaction(t.Context(), tx, &methodSig)
264263
if res != nil {
265264
t.Errorf("Expected nil-response, got %v", res)
266265
}
267266
if err != keystore.ErrDecrypt {
268267
t.Errorf("Expected ErrLocked! %v", err)
269268
}
270269
control.approveCh <- "No way"
271-
res, err = api.SignTransaction(context.Background(), tx, &methodSig)
270+
res, err = api.SignTransaction(t.Context(), tx, &methodSig)
272271
if res != nil {
273272
t.Errorf("Expected nil-response, got %v", res)
274273
}
@@ -278,43 +277,42 @@ func TestSignTx(t *testing.T) {
278277
// Sign with correct password
279278
control.approveCh <- "Y"
280279
control.inputCh <- "a_long_password"
281-
res, err = api.SignTransaction(context.Background(), tx, &methodSig)
282-
280+
res, err = api.SignTransaction(t.Context(), tx, &methodSig)
283281
if err != nil {
284282
t.Fatal(err)
285283
}
286284
parsedTx := &types.Transaction{}
287285
rlp.DecodeBytes(res.Raw, parsedTx)
288286

289-
//The tx should NOT be modified by the UI
287+
// The tx should NOT be modified by the UI
290288
if parsedTx.Value().Cmp(tx.Value.ToInt()) != 0 {
291289
t.Errorf("Expected value to be unchanged, expected %v got %v", tx.Value, parsedTx.Value())
292290
}
293291
control.approveCh <- "Y"
294292
control.inputCh <- "a_long_password"
295293

296-
res2, err = api.SignTransaction(context.Background(), tx, &methodSig)
294+
res2, err = api.SignTransaction(t.Context(), tx, &methodSig)
297295
if err != nil {
298296
t.Fatal(err)
299297
}
300298
if !bytes.Equal(res.Raw, res2.Raw) {
301299
t.Error("Expected tx to be unmodified by UI")
302300
}
303301

304-
//The tx is modified by the UI
302+
// The tx is modified by the UI
305303
control.approveCh <- "M"
306304
control.inputCh <- "a_long_password"
307305

308-
res2, err = api.SignTransaction(context.Background(), tx, &methodSig)
306+
res2, err = api.SignTransaction(t.Context(), tx, &methodSig)
309307
if err != nil {
310308
t.Fatal(err)
311309
}
312310
parsedTx2 := &types.Transaction{}
313-
rlp.DecodeBytes(res.Raw, parsedTx2)
311+
rlp.DecodeBytes(res2.Raw, parsedTx2)
314312

315-
//The tx should be modified by the UI
316-
if parsedTx2.Value().Cmp(tx.Value.ToInt()) != 0 {
317-
t.Errorf("Expected value to be unchanged, got %v", parsedTx.Value())
313+
// The tx should be modified by the UI
314+
if parsedTx2.Value().Cmp(tx.Value.ToInt()) == 0 {
315+
t.Errorf("Expected value to be changed, got %v", parsedTx2.Value())
318316
}
319317
if bytes.Equal(res.Raw, res2.Raw) {
320318
t.Error("Expected tx to be modified by UI")

0 commit comments

Comments
 (0)