From 60451730445c1cf69cc6e69b334a5389458a904f Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Fri, 22 Feb 2019 10:54:50 +0100 Subject: [PATCH] clef: improve end-to-end testing --- cmd/clef/main.go | 166 +++++++++++++++++++++++++++++-------------- signer/core/cliui.go | 9 ++- 2 files changed, 115 insertions(+), 60 deletions(-) diff --git a/cmd/clef/main.go b/cmd/clef/main.go index dcc33d6d83..a900312459 100644 --- a/cmd/clef/main.go +++ b/cmd/clef/main.go @@ -638,68 +638,124 @@ func testExternalUI(api *core.SignerAPI) { ctx := context.WithValue(context.Background(), "remote", "clef binary") ctx = context.WithValue(ctx, "scheme", "in-proc") ctx = context.WithValue(ctx, "local", "main") - errs := make([]string, 0) - api.UI.ShowInfo("Testing 'ShowInfo'") - api.UI.ShowError("Testing 'ShowError'") + a := common.HexToAddress("0xdeadbeef000000000000000000000000deadbeef") - checkErr := func(method string, err error) { - if err != nil && err != core.ErrRequestDenied { - errs = append(errs, fmt.Sprintf("%v: %v", method, err.Error())) + queryUser := func(q string) string { + resp, err := api.UI.OnInputRequired(core.UserInputRequest{ + Title: "Testing", + Prompt: q, + }) + if err != nil { + errs = append(errs, err.Error()) + } + return resp.Text + } + expectResponse := func(testcase, question, expect string) { + if got := queryUser(question); got != expect { + errs = append(errs, fmt.Sprintf("%s: got %v, expected %v", testcase, got, expect)) } } - var err error - - cliqueHeader := types.Header{ - common.HexToHash("0000H45H"), - common.HexToHash("0000H45H"), - common.HexToAddress("0000H45H"), - common.HexToHash("0000H00H"), - common.HexToHash("0000H45H"), - common.HexToHash("0000H45H"), - types.Bloom{}, - big.NewInt(1337), - big.NewInt(1337), - 1338, - 1338, - big.NewInt(1338), - []byte("Extra data Extra data Extra data Extra data Extra data Extra data Extra data Extra data"), - common.HexToHash("0x0000H45H"), - types.BlockNonce{}, - } - cliqueRlp, err := rlp.EncodeToBytes(cliqueHeader) - if err != nil { - utils.Fatalf("Should not error: %v", err) - } - addr, err := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899") - if err != nil { - utils.Fatalf("Should not error: %v", err) - } - _, err = api.SignData(ctx, "application/clique", *addr, cliqueRlp) - checkErr("SignData", err) - - _, err = api.SignTransaction(ctx, core.SendTxArgs{From: common.MixedcaseAddress{}}, nil) - checkErr("SignTransaction", err) - _, err = api.SignData(ctx, "text/plain", common.MixedcaseAddress{}, common.Hex2Bytes("01020304")) - checkErr("SignData", err) - //_, err = api.SignTypedData(ctx, common.MixedcaseAddress{}, core.TypedData{}) - //checkErr("SignTypedData", err) - _, err = api.List(ctx) - checkErr("List", err) - _, err = api.New(ctx) - checkErr("New", err) - - api.UI.ShowInfo("Tests completed") - - if len(errs) > 0 { - log.Error("Got errors") - for _, e := range errs { - log.Error(e) + expectApprove := func(testcase string, err error) { + if err == nil || err == accounts.ErrUnknownAccount { + return } - } else { - log.Info("No errors") + errs = append(errs, fmt.Sprintf("%v: expected no error, got %v", testcase, err.Error())) } + expectDeny := func(testcase string, err error) { + if err == nil || err != core.ErrRequestDenied { + errs = append(errs, fmt.Sprintf("%v: expected ErrRequestDenied, got %v", testcase, err)) + } + } + + // Test display of info and error + { + api.UI.ShowInfo("If you see this message, enter 'yes' to next question") + expectResponse("showinfo", "Did you see the message? [yes/no]", "yes") + api.UI.ShowError("If you see this message, enter 'yes' to the next question") + expectResponse("showerror", "Did you see the message? [yes/no]", "yes") + } + { // Sign data test - clique header + api.UI.ShowInfo("Please approve the next request for signing a clique header") + cliqueHeader := types.Header{ + common.HexToHash("0000H45H"), + common.HexToHash("0000H45H"), + common.HexToAddress("0000H45H"), + common.HexToHash("0000H00H"), + common.HexToHash("0000H45H"), + common.HexToHash("0000H45H"), + types.Bloom{}, + big.NewInt(1337), + big.NewInt(1337), + 1338, + 1338, + big.NewInt(1338), + []byte("Extra data Extra data Extra data Extra data Extra data Extra data Extra data Extra data"), + common.HexToHash("0x0000H45H"), + types.BlockNonce{}, + } + cliqueRlp, err := rlp.EncodeToBytes(cliqueHeader) + if err != nil { + utils.Fatalf("Should not error: %v", err) + } + addr, _ := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899") + _, err = api.SignData(ctx, accounts.MimetypeClique, *addr, hexutil.Encode(cliqueRlp)) + expectApprove("signdata - clique header", err) + } + { // Sign data test - plain text + api.UI.ShowInfo("Please approve the next request for signing text") + addr, _ := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899") + _, err := api.SignData(ctx, accounts.MimetypeTextPlain, *addr, hexutil.Encode([]byte("hello world"))) + expectApprove("signdata - text", err) + } + { // Sign data test - plain text reject + api.UI.ShowInfo("Please deny the next request for signing text") + addr, _ := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899") + _, err := api.SignData(ctx, accounts.MimetypeTextPlain, *addr, hexutil.Encode([]byte("hello world"))) + expectDeny("signdata - text", err) + } + { // Sign transaction + + api.UI.ShowInfo("Please reject next transaction") + data := hexutil.Bytes([]byte{}) + to := common.NewMixedcaseAddress(a) + tx := core.SendTxArgs{ + Data: &data, + Nonce: 0x1, + Value: hexutil.Big(*big.NewInt(6)), + From: common.NewMixedcaseAddress(a), + To: &to, + GasPrice: hexutil.Big(*big.NewInt(5)), + Gas: 1000, + Input: nil, + } + _, err := api.SignTransaction(ctx, tx, nil) + expectDeny("signtransaction [1]", err) + expectResponse("signtransaction [2]", "Did you see any warnings for the last transaction? (yes/no)", "no") + } + { // Listing + api.UI.ShowInfo("Please reject listing-request") + _, err := api.List(ctx) + expectDeny("list", err) + } + { // Import + api.UI.ShowInfo("Please reject new account-request") + _, err := api.New(ctx) + expectDeny("newaccount", err) + } + { // Metadata + api.UI.ShowInfo("Please check if you see the Origin in next listing (approve or deny)") + api.List(context.WithValue(ctx, "Origin", "origin.com")) + expectResponse("metadata - origin", "Did you see origin (origin.com)? [yes/no] ", "yes") + } + + for _, e := range errs { + log.Error(e) + } + result := fmt.Sprintf("Tests completed. %d errors:\n%s\n", len(errs), strings.Join(errs, "\n")) + api.UI.ShowInfo(result) + } // getPassPhrase retrieves the password associated with clef, either fetched diff --git a/signer/core/cliui.go b/signer/core/cliui.go index ad65246d9f..c9a33c408a 100644 --- a/signer/core/cliui.go +++ b/signer/core/cliui.go @@ -87,8 +87,8 @@ func (ui *CommandlineUI) readPasswordText(inputstring string) string { } func (ui *CommandlineUI) OnInputRequired(info UserInputRequest) (UserInputResponse, error) { - fmt.Println(info.Title) - fmt.Println(info.Prompt) + + fmt.Printf("## %s\n\n%s\n", info.Title, info.Prompt) if info.IsPassword { text, err := terminal.ReadPassword(int(os.Stdin.Fd())) if err != nil { @@ -224,14 +224,13 @@ func (ui *CommandlineUI) ApproveNewAccount(request *NewAccountRequest) (NewAccou // ShowError displays error message to user func (ui *CommandlineUI) ShowError(message string) { - fmt.Printf("-------- Error message from Clef-----------\n") - fmt.Println(message) + fmt.Printf("## Error \n%s\n", message) fmt.Printf("-------------------------------------------\n") } // ShowInfo displays info message to user func (ui *CommandlineUI) ShowInfo(message string) { - fmt.Printf("Info: %v\n", message) + fmt.Printf("## Info \n%s\n", message) } func (ui *CommandlineUI) OnApprovedTx(tx ethapi.SignTransactionResult) {