mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
clef: improve end-to-end testing
This commit is contained in:
parent
4b0674d5eb
commit
6045173044
2 changed files with 115 additions and 60 deletions
114
cmd/clef/main.go
114
cmd/clef/main.go
|
|
@ -638,19 +638,46 @@ func testExternalUI(api *core.SignerAPI) {
|
||||||
ctx := context.WithValue(context.Background(), "remote", "clef binary")
|
ctx := context.WithValue(context.Background(), "remote", "clef binary")
|
||||||
ctx = context.WithValue(ctx, "scheme", "in-proc")
|
ctx = context.WithValue(ctx, "scheme", "in-proc")
|
||||||
ctx = context.WithValue(ctx, "local", "main")
|
ctx = context.WithValue(ctx, "local", "main")
|
||||||
|
|
||||||
errs := make([]string, 0)
|
errs := make([]string, 0)
|
||||||
|
|
||||||
api.UI.ShowInfo("Testing 'ShowInfo'")
|
a := common.HexToAddress("0xdeadbeef000000000000000000000000deadbeef")
|
||||||
api.UI.ShowError("Testing 'ShowError'")
|
|
||||||
|
|
||||||
checkErr := func(method string, err error) {
|
queryUser := func(q string) string {
|
||||||
if err != nil && err != core.ErrRequestDenied {
|
resp, err := api.UI.OnInputRequired(core.UserInputRequest{
|
||||||
errs = append(errs, fmt.Sprintf("%v: %v", method, err.Error()))
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expectApprove := func(testcase string, err error) {
|
||||||
|
if err == nil || err == accounts.ErrUnknownAccount {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var err error
|
|
||||||
|
|
||||||
|
// 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{
|
cliqueHeader := types.Header{
|
||||||
common.HexToHash("0000H45H"),
|
common.HexToHash("0000H45H"),
|
||||||
common.HexToHash("0000H45H"),
|
common.HexToHash("0000H45H"),
|
||||||
|
|
@ -672,34 +699,63 @@ func testExternalUI(api *core.SignerAPI) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Fatalf("Should not error: %v", err)
|
utils.Fatalf("Should not error: %v", err)
|
||||||
}
|
}
|
||||||
addr, err := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899")
|
addr, _ := common.NewMixedcaseAddressFromString("0x0011223344556677889900112233445566778899")
|
||||||
if err != nil {
|
_, err = api.SignData(ctx, accounts.MimetypeClique, *addr, hexutil.Encode(cliqueRlp))
|
||||||
utils.Fatalf("Should not error: %v", err)
|
expectApprove("signdata - clique header", err)
|
||||||
}
|
}
|
||||||
_, err = api.SignData(ctx, "application/clique", *addr, cliqueRlp)
|
{ // Sign data test - plain text
|
||||||
checkErr("SignData", err)
|
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
|
||||||
|
|
||||||
_, err = api.SignTransaction(ctx, core.SendTxArgs{From: common.MixedcaseAddress{}}, nil)
|
api.UI.ShowInfo("Please reject next transaction")
|
||||||
checkErr("SignTransaction", err)
|
data := hexutil.Bytes([]byte{})
|
||||||
_, err = api.SignData(ctx, "text/plain", common.MixedcaseAddress{}, common.Hex2Bytes("01020304"))
|
to := common.NewMixedcaseAddress(a)
|
||||||
checkErr("SignData", err)
|
tx := core.SendTxArgs{
|
||||||
//_, err = api.SignTypedData(ctx, common.MixedcaseAddress{}, core.TypedData{})
|
Data: &data,
|
||||||
//checkErr("SignTypedData", err)
|
Nonce: 0x1,
|
||||||
_, err = api.List(ctx)
|
Value: hexutil.Big(*big.NewInt(6)),
|
||||||
checkErr("List", err)
|
From: common.NewMixedcaseAddress(a),
|
||||||
_, err = api.New(ctx)
|
To: &to,
|
||||||
checkErr("New", err)
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
api.UI.ShowInfo("Tests completed")
|
|
||||||
|
|
||||||
if len(errs) > 0 {
|
|
||||||
log.Error("Got errors")
|
|
||||||
for _, e := range errs {
|
for _, e := range errs {
|
||||||
log.Error(e)
|
log.Error(e)
|
||||||
}
|
}
|
||||||
} else {
|
result := fmt.Sprintf("Tests completed. %d errors:\n%s\n", len(errs), strings.Join(errs, "\n"))
|
||||||
log.Info("No errors")
|
api.UI.ShowInfo(result)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// getPassPhrase retrieves the password associated with clef, either fetched
|
// getPassPhrase retrieves the password associated with clef, either fetched
|
||||||
|
|
|
||||||
|
|
@ -87,8 +87,8 @@ func (ui *CommandlineUI) readPasswordText(inputstring string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ui *CommandlineUI) OnInputRequired(info UserInputRequest) (UserInputResponse, error) {
|
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 {
|
if info.IsPassword {
|
||||||
text, err := terminal.ReadPassword(int(os.Stdin.Fd()))
|
text, err := terminal.ReadPassword(int(os.Stdin.Fd()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -224,14 +224,13 @@ func (ui *CommandlineUI) ApproveNewAccount(request *NewAccountRequest) (NewAccou
|
||||||
|
|
||||||
// ShowError displays error message to user
|
// ShowError displays error message to user
|
||||||
func (ui *CommandlineUI) ShowError(message string) {
|
func (ui *CommandlineUI) ShowError(message string) {
|
||||||
fmt.Printf("-------- Error message from Clef-----------\n")
|
fmt.Printf("## Error \n%s\n", message)
|
||||||
fmt.Println(message)
|
|
||||||
fmt.Printf("-------------------------------------------\n")
|
fmt.Printf("-------------------------------------------\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ShowInfo displays info message to user
|
// ShowInfo displays info message to user
|
||||||
func (ui *CommandlineUI) ShowInfo(message string) {
|
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) {
|
func (ui *CommandlineUI) OnApprovedTx(tx ethapi.SignTransactionResult) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue