mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-13 19:46:39 +00:00
signer/core: fix TestSignTx to decode res2 (#32749)
Decode the modified transaction and verify the value differs from original. --------- Co-authored-by: lightclient <lightclient@protonmail.com>
This commit is contained in:
parent
2e2fece0bb
commit
16b735fddd
1 changed files with 20 additions and 22 deletions
|
|
@ -18,7 +18,6 @@ package core_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -97,12 +96,12 @@ func (ui *headlessUi) ApproveNewAccount(request *core.NewAccountRequest) (core.N
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ui *headlessUi) ShowError(message string) {
|
func (ui *headlessUi) ShowError(message string) {
|
||||||
//stdout is used by communication
|
// stdout is used by communication
|
||||||
fmt.Fprintln(os.Stderr, message)
|
fmt.Fprintln(os.Stderr, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ui *headlessUi) ShowInfo(message string) {
|
func (ui *headlessUi) ShowInfo(message string) {
|
||||||
//stdout is used by communication
|
// stdout is used by communication
|
||||||
fmt.Fprintln(os.Stderr, message)
|
fmt.Fprintln(os.Stderr, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -128,7 +127,7 @@ func setup(t *testing.T) (*core.SignerAPI, *headlessUi) {
|
||||||
func createAccount(ui *headlessUi, api *core.SignerAPI, t *testing.T) {
|
func createAccount(ui *headlessUi, api *core.SignerAPI, t *testing.T) {
|
||||||
ui.approveCh <- "Y"
|
ui.approveCh <- "Y"
|
||||||
ui.inputCh <- "a_long_password"
|
ui.inputCh <- "a_long_password"
|
||||||
_, err := api.New(context.Background())
|
_, err := api.New(t.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
@ -143,7 +142,7 @@ func failCreateAccountWithPassword(ui *headlessUi, api *core.SignerAPI, password
|
||||||
ui.inputCh <- password
|
ui.inputCh <- password
|
||||||
ui.inputCh <- password
|
ui.inputCh <- password
|
||||||
|
|
||||||
addr, err := api.New(context.Background())
|
addr, err := api.New(t.Context())
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("Should have returned an error")
|
t.Fatal("Should have returned an error")
|
||||||
}
|
}
|
||||||
|
|
@ -154,7 +153,7 @@ func failCreateAccountWithPassword(ui *headlessUi, api *core.SignerAPI, password
|
||||||
|
|
||||||
func failCreateAccount(ui *headlessUi, api *core.SignerAPI, t *testing.T) {
|
func failCreateAccount(ui *headlessUi, api *core.SignerAPI, t *testing.T) {
|
||||||
ui.approveCh <- "N"
|
ui.approveCh <- "N"
|
||||||
addr, err := api.New(context.Background())
|
addr, err := api.New(t.Context())
|
||||||
if err != core.ErrRequestDenied {
|
if err != core.ErrRequestDenied {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
@ -165,7 +164,7 @@ func failCreateAccount(ui *headlessUi, api *core.SignerAPI, t *testing.T) {
|
||||||
|
|
||||||
func list(ui *headlessUi, api *core.SignerAPI, t *testing.T) ([]common.Address, error) {
|
func list(ui *headlessUi, api *core.SignerAPI, t *testing.T) ([]common.Address, error) {
|
||||||
ui.approveCh <- "A"
|
ui.approveCh <- "A"
|
||||||
return api.List(context.Background())
|
return api.List(t.Context())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNewAcc(t *testing.T) {
|
func TestNewAcc(t *testing.T) {
|
||||||
|
|
@ -199,7 +198,7 @@ func TestNewAcc(t *testing.T) {
|
||||||
// Testing listing:
|
// Testing listing:
|
||||||
// Listing one Account
|
// Listing one Account
|
||||||
control.approveCh <- "1"
|
control.approveCh <- "1"
|
||||||
list, err := api.List(context.Background())
|
list, err := api.List(t.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
@ -208,7 +207,7 @@ func TestNewAcc(t *testing.T) {
|
||||||
}
|
}
|
||||||
// Listing denied
|
// Listing denied
|
||||||
control.approveCh <- "Nope"
|
control.approveCh <- "Nope"
|
||||||
list, err = api.List(context.Background())
|
list, err = api.List(t.Context())
|
||||||
if len(list) != 0 {
|
if len(list) != 0 {
|
||||||
t.Fatalf("List should be empty")
|
t.Fatalf("List should be empty")
|
||||||
}
|
}
|
||||||
|
|
@ -246,7 +245,7 @@ func TestSignTx(t *testing.T) {
|
||||||
api, control := setup(t)
|
api, control := setup(t)
|
||||||
createAccount(control, api, t)
|
createAccount(control, api, t)
|
||||||
control.approveCh <- "A"
|
control.approveCh <- "A"
|
||||||
list, err = api.List(context.Background())
|
list, err = api.List(t.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
@ -260,7 +259,7 @@ func TestSignTx(t *testing.T) {
|
||||||
|
|
||||||
control.approveCh <- "Y"
|
control.approveCh <- "Y"
|
||||||
control.inputCh <- "wrongpassword"
|
control.inputCh <- "wrongpassword"
|
||||||
res, err = api.SignTransaction(context.Background(), tx, &methodSig)
|
res, err = api.SignTransaction(t.Context(), tx, &methodSig)
|
||||||
if res != nil {
|
if res != nil {
|
||||||
t.Errorf("Expected nil-response, got %v", res)
|
t.Errorf("Expected nil-response, got %v", res)
|
||||||
}
|
}
|
||||||
|
|
@ -268,7 +267,7 @@ func TestSignTx(t *testing.T) {
|
||||||
t.Errorf("Expected ErrLocked! %v", err)
|
t.Errorf("Expected ErrLocked! %v", err)
|
||||||
}
|
}
|
||||||
control.approveCh <- "No way"
|
control.approveCh <- "No way"
|
||||||
res, err = api.SignTransaction(context.Background(), tx, &methodSig)
|
res, err = api.SignTransaction(t.Context(), tx, &methodSig)
|
||||||
if res != nil {
|
if res != nil {
|
||||||
t.Errorf("Expected nil-response, got %v", res)
|
t.Errorf("Expected nil-response, got %v", res)
|
||||||
}
|
}
|
||||||
|
|
@ -278,22 +277,21 @@ func TestSignTx(t *testing.T) {
|
||||||
// Sign with correct password
|
// Sign with correct password
|
||||||
control.approveCh <- "Y"
|
control.approveCh <- "Y"
|
||||||
control.inputCh <- "a_long_password"
|
control.inputCh <- "a_long_password"
|
||||||
res, err = api.SignTransaction(context.Background(), tx, &methodSig)
|
res, err = api.SignTransaction(t.Context(), tx, &methodSig)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
parsedTx := &types.Transaction{}
|
parsedTx := &types.Transaction{}
|
||||||
rlp.DecodeBytes(res.Raw, parsedTx)
|
rlp.DecodeBytes(res.Raw, parsedTx)
|
||||||
|
|
||||||
//The tx should NOT be modified by the UI
|
// The tx should NOT be modified by the UI
|
||||||
if parsedTx.Value().Cmp(tx.Value.ToInt()) != 0 {
|
if parsedTx.Value().Cmp(tx.Value.ToInt()) != 0 {
|
||||||
t.Errorf("Expected value to be unchanged, expected %v got %v", tx.Value, parsedTx.Value())
|
t.Errorf("Expected value to be unchanged, expected %v got %v", tx.Value, parsedTx.Value())
|
||||||
}
|
}
|
||||||
control.approveCh <- "Y"
|
control.approveCh <- "Y"
|
||||||
control.inputCh <- "a_long_password"
|
control.inputCh <- "a_long_password"
|
||||||
|
|
||||||
res2, err = api.SignTransaction(context.Background(), tx, &methodSig)
|
res2, err = api.SignTransaction(t.Context(), tx, &methodSig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
@ -301,20 +299,20 @@ func TestSignTx(t *testing.T) {
|
||||||
t.Error("Expected tx to be unmodified by UI")
|
t.Error("Expected tx to be unmodified by UI")
|
||||||
}
|
}
|
||||||
|
|
||||||
//The tx is modified by the UI
|
// The tx is modified by the UI
|
||||||
control.approveCh <- "M"
|
control.approveCh <- "M"
|
||||||
control.inputCh <- "a_long_password"
|
control.inputCh <- "a_long_password"
|
||||||
|
|
||||||
res2, err = api.SignTransaction(context.Background(), tx, &methodSig)
|
res2, err = api.SignTransaction(t.Context(), tx, &methodSig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
parsedTx2 := &types.Transaction{}
|
parsedTx2 := &types.Transaction{}
|
||||||
rlp.DecodeBytes(res.Raw, parsedTx2)
|
rlp.DecodeBytes(res2.Raw, parsedTx2)
|
||||||
|
|
||||||
//The tx should be modified by the UI
|
// The tx should be modified by the UI
|
||||||
if parsedTx2.Value().Cmp(tx.Value.ToInt()) != 0 {
|
if parsedTx2.Value().Cmp(tx.Value.ToInt()) == 0 {
|
||||||
t.Errorf("Expected value to be unchanged, got %v", parsedTx.Value())
|
t.Errorf("Expected value to be changed, got %v", parsedTx2.Value())
|
||||||
}
|
}
|
||||||
if bytes.Equal(res.Raw, res2.Raw) {
|
if bytes.Equal(res.Raw, res2.Raw) {
|
||||||
t.Error("Expected tx to be modified by UI")
|
t.Error("Expected tx to be modified by UI")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue