Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests, signer: remove staticcheck warnings #20364

Merged
merged 6 commits into from
Nov 23, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion signer/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ type (
}
)

var ErrRequestDenied = errors.New("Request denied")
var ErrRequestDenied = errors.New("request denied")

// NewSignerAPI creates a new API that can be used for Account management.
// ksLocation specifies the directory where to store the password protected private
Expand Down Expand Up @@ -552,6 +552,9 @@ func (api *SignerAPI) SignTransaction(ctx context.Context, args SendTxArgs, meth
}

rlpdata, err := rlp.EncodeToBytes(signedTx)
if err != nil {
return nil, err
}
response := ethapi.SignTransactionResult{Raw: rlpdata, Tx: signedTx}

// Finally, send the signed tx to the UI
Expand Down
4 changes: 2 additions & 2 deletions signer/core/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (ui *headlessUi) ApproveTx(request *core.SignTxRequest) (core.SignTxRespons
}

func (ui *headlessUi) ApproveSignData(request *core.SignDataRequest) (core.SignDataResponse, error) {
approved := "Y" == <-ui.approveCh
approved := (<-ui.approveCh == "Y")
return core.SignDataResponse{approved}, nil
}

Expand All @@ -91,7 +91,7 @@ func (ui *headlessUi) ApproveListing(request *core.ListRequest) (core.ListRespon
}

func (ui *headlessUi) ApproveNewAccount(request *core.NewAccountRequest) (core.NewAccountResponse, error) {
if "Y" == <-ui.approveCh {
if <-ui.approveCh == "Y" {
return core.NewAccountResponse{true}, nil
}
return core.NewAccountResponse{false}, nil
Expand Down
28 changes: 0 additions & 28 deletions signer/core/cliui.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,34 +58,6 @@ func (ui *CommandlineUI) readString() string {
}
}

// readPassword reads a single line from stdin, trimming it from the trailing new
// line and returns it. The input will not be echoed.
func (ui *CommandlineUI) readPassword() string {
fmt.Printf("Enter password to approve:\n")
fmt.Printf("> ")

text, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Crit("Failed to read password", "err", err)
}
fmt.Println()
fmt.Println("-----------------------")
return string(text)
}

// readPassword reads a single line from stdin, trimming it from the trailing new
// line and returns it. The input will not be echoed.
func (ui *CommandlineUI) readPasswordText(inputstring string) string {
fmt.Printf("Enter %s:\n", inputstring)
fmt.Printf("> ")
text, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Crit("Failed to read password", "err", err)
}
fmt.Println("-----------------------")
return string(text)
}

func (ui *CommandlineUI) OnInputRequired(info UserInputRequest) (UserInputResponse, error) {

fmt.Printf("## %s\n\n%s\n", info.Title, info.Prompt)
Expand Down
2 changes: 0 additions & 2 deletions signer/core/stdioui.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package core

import (
"context"
"sync"

"github.com/ethereum/go-ethereum/internal/ethapi"
"github.com/ethereum/go-ethereum/log"
Expand All @@ -27,7 +26,6 @@ import (

type StdIOUI struct {
client rpc.Client
mu sync.Mutex
}

func NewStdIOUI() *StdIOUI {
Expand Down
2 changes: 1 addition & 1 deletion signer/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (v *ValidationMessages) getWarnings() error {
}
}
if len(messages) > 0 {
return fmt.Errorf("Validation failed: %s", strings.Join(messages, ","))
return fmt.Errorf("validation failed: %s", strings.Join(messages, ","))
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion signer/core/uiapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (s *UIServerAPI) Export(ctx context.Context, addr common.Address) (json.Raw
return nil, err
}
if wallet.URL().Scheme != keystore.KeyStoreScheme {
return nil, fmt.Errorf("Account is not a keystore-account")
return nil, fmt.Errorf("account is not a keystore-account")
}
return ioutil.ReadFile(wallet.URL().Path)
}
Expand Down
21 changes: 15 additions & 6 deletions signer/rules/deps/bindata.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Code generated by go-bindata.
// Package deps Code generated by go-bindata. (@generated) DO NOT EDIT.
// sources:
// bignumber.js
// DO NOT EDIT!

package deps

import (
Expand All @@ -20,15 +18,15 @@ import (
func bindataRead(data []byte, name string) ([]byte, error) {
gz, err := gzip.NewReader(bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("Read %q: %v", name, err)
return nil, fmt.Errorf("read %q: %v", name, err)
}

var buf bytes.Buffer
_, err = io.Copy(&buf, gz)
clErr := gz.Close()

if err != nil {
return nil, fmt.Errorf("Read %q: %v", name, err)
return nil, fmt.Errorf("read %q: %v", name, err)
}
if clErr != nil {
return nil, err
Expand All @@ -49,21 +47,32 @@ type bindataFileInfo struct {
modTime time.Time
}

// Name return file name
func (fi bindataFileInfo) Name() string {
return fi.name
}

// Size return file size
func (fi bindataFileInfo) Size() int64 {
return fi.size
}

// Mode return file mode
func (fi bindataFileInfo) Mode() os.FileMode {
return fi.mode
}

// ModTime return file modify time
func (fi bindataFileInfo) ModTime() time.Time {
return fi.modTime
}

// IsDir return file whether a directory
func (fi bindataFileInfo) IsDir() bool {
return false
return fi.mode&os.ModeDir != 0
}

// Sys return file is sys mode
func (fi bindataFileInfo) Sys() interface{} {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion signer/rules/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (r *rulesetUI) checkApproval(jsfunc string, jsarg []byte, err error) (bool,
log.Info("Op rejected")
return false, nil
}
return false, fmt.Errorf("Unknown response")
return false, fmt.Errorf("unknown response")
}

func (r *rulesetUI) ApproveTx(request *core.SignTxRequest) (core.SignTxResponse, error) {
Expand Down
5 changes: 2 additions & 3 deletions signer/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ type Storage interface {
// EphemeralStorage is an in-memory storage that does
// not persist values to disk. Mainly used for testing
type EphemeralStorage struct {
data map[string]string
namespace string
data map[string]string
}

// Put stores a value by key. 0-length keys results in noop.
Expand Down Expand Up @@ -83,5 +82,5 @@ type NoStorage struct{}
func (s *NoStorage) Put(key, value string) {}
func (s *NoStorage) Del(key string) {}
func (s *NoStorage) Get(key string) (string, error) {
return "", errors.New("I forgot")
return "", errors.New("missing key, I probably forgot")
}
38 changes: 19 additions & 19 deletions tests/block_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ func (t *BlockTest) insertBlocks(blockchain *core.BlockChain) ([]btBlock, error)
if b.BlockHeader == nil {
continue // OK - block is supposed to be invalid, continue with next block
} else {
return nil, fmt.Errorf("Block RLP decoding failed when expected to succeed: %v", err)
return nil, fmt.Errorf("block RLP decoding failed when expected to succeed: %v", err)
}
}
// RLP decoding worked, try to insert into chain:
Expand All @@ -189,16 +189,16 @@ func (t *BlockTest) insertBlocks(blockchain *core.BlockChain) ([]btBlock, error)
if b.BlockHeader == nil {
continue // OK - block is supposed to be invalid, continue with next block
} else {
return nil, fmt.Errorf("Block #%v insertion into chain failed: %v", blocks[i].Number(), err)
return nil, fmt.Errorf("block #%v insertion into chain failed: %v", blocks[i].Number(), err)
}
}
if b.BlockHeader == nil {
return nil, fmt.Errorf("Block insertion should have failed")
return nil, fmt.Errorf("block insertion should have failed")
}

// validate RLP decoding by checking all values against test file JSON
if err = validateHeader(b.BlockHeader, cb.Header()); err != nil {
return nil, fmt.Errorf("Deserialised block header validation failed: %v", err)
return nil, fmt.Errorf("deserialised block header validation failed: %v", err)
}
validBlocks = append(validBlocks, b)
}
Expand All @@ -207,49 +207,49 @@ func (t *BlockTest) insertBlocks(blockchain *core.BlockChain) ([]btBlock, error)

func validateHeader(h *btHeader, h2 *types.Header) error {
if h.Bloom != h2.Bloom {
return fmt.Errorf("Bloom: want: %x have: %x", h.Bloom, h2.Bloom)
return fmt.Errorf("bloom: want: %x have: %x", h.Bloom, h2.Bloom)
}
if h.Coinbase != h2.Coinbase {
return fmt.Errorf("Coinbase: want: %x have: %x", h.Coinbase, h2.Coinbase)
return fmt.Errorf("coinbase: want: %x have: %x", h.Coinbase, h2.Coinbase)
}
if h.MixHash != h2.MixDigest {
return fmt.Errorf("MixHash: want: %x have: %x", h.MixHash, h2.MixDigest)
}
if h.Nonce != h2.Nonce {
return fmt.Errorf("Nonce: want: %x have: %x", h.Nonce, h2.Nonce)
return fmt.Errorf("nonce: want: %x have: %x", h.Nonce, h2.Nonce)
}
if h.Number.Cmp(h2.Number) != 0 {
return fmt.Errorf("Number: want: %v have: %v", h.Number, h2.Number)
return fmt.Errorf("number: want: %v have: %v", h.Number, h2.Number)
}
if h.ParentHash != h2.ParentHash {
return fmt.Errorf("Parent hash: want: %x have: %x", h.ParentHash, h2.ParentHash)
return fmt.Errorf("parent hash: want: %x have: %x", h.ParentHash, h2.ParentHash)
}
if h.ReceiptTrie != h2.ReceiptHash {
return fmt.Errorf("Receipt hash: want: %x have: %x", h.ReceiptTrie, h2.ReceiptHash)
return fmt.Errorf("receipt hash: want: %x have: %x", h.ReceiptTrie, h2.ReceiptHash)
}
if h.TransactionsTrie != h2.TxHash {
return fmt.Errorf("Tx hash: want: %x have: %x", h.TransactionsTrie, h2.TxHash)
return fmt.Errorf("tx hash: want: %x have: %x", h.TransactionsTrie, h2.TxHash)
}
if h.StateRoot != h2.Root {
return fmt.Errorf("State hash: want: %x have: %x", h.StateRoot, h2.Root)
return fmt.Errorf("state hash: want: %x have: %x", h.StateRoot, h2.Root)
}
if h.UncleHash != h2.UncleHash {
return fmt.Errorf("Uncle hash: want: %x have: %x", h.UncleHash, h2.UncleHash)
return fmt.Errorf("uncle hash: want: %x have: %x", h.UncleHash, h2.UncleHash)
}
if !bytes.Equal(h.ExtraData, h2.Extra) {
return fmt.Errorf("Extra data: want: %x have: %x", h.ExtraData, h2.Extra)
return fmt.Errorf("extra data: want: %x have: %x", h.ExtraData, h2.Extra)
}
if h.Difficulty.Cmp(h2.Difficulty) != 0 {
return fmt.Errorf("Difficulty: want: %v have: %v", h.Difficulty, h2.Difficulty)
return fmt.Errorf("difficulty: want: %v have: %v", h.Difficulty, h2.Difficulty)
}
if h.GasLimit != h2.GasLimit {
return fmt.Errorf("GasLimit: want: %d have: %d", h.GasLimit, h2.GasLimit)
return fmt.Errorf("gasLimit: want: %d have: %d", h.GasLimit, h2.GasLimit)
}
if h.GasUsed != h2.GasUsed {
return fmt.Errorf("GasUsed: want: %d have: %d", h.GasUsed, h2.GasUsed)
return fmt.Errorf("gasUsed: want: %d have: %d", h.GasUsed, h2.GasUsed)
}
if h.Timestamp != h2.Time {
return fmt.Errorf("Timestamp: want: %v have: %v", h.Timestamp, h2.Time)
return fmt.Errorf("timestamp: want: %v have: %v", h.Timestamp, h2.Time)
}
return nil
}
Expand Down Expand Up @@ -287,7 +287,7 @@ func (t *BlockTest) validateImportedHeaders(cm *core.BlockChain, validBlocks []b
// be part of the longest chain until last block is imported.
for b := cm.CurrentBlock(); b != nil && b.NumberU64() != 0; b = cm.GetBlockByHash(b.Header().ParentHash) {
if err := validateHeader(bmap[b.Hash()].BlockHeader, b.Header()); err != nil {
return fmt.Errorf("Imported block header validation failed: %v", err)
return fmt.Errorf("imported block header validation failed: %v", err)
}
}
return nil
Expand Down
11 changes: 11 additions & 0 deletions tests/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,17 @@ func (tm *testMatcher) whitelist(pattern string) {
tm.whitelistpat = regexp.MustCompile(pattern)
}

func TestWhitelist(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the test somewhere else in the file. While this is a test file, the code around here defines an API for use by other tests. Best to not dump a unit test right in the middle of that.

t.Parallel()
tm := new(testMatcher)
tm.whitelist("invalid*")
tm.walk(t, rlpTestDir, func(t *testing.T, name string, test *RLPTest) {
if name[:len("invalidRLPTest.json")] != "invalidRLPTest.json" {
t.Fatalf("invalid test found: %s != invalidRLPTest.json", name)
}
})
}

// config defines chain config for tests matching the pattern.
func (tm *testMatcher) config(pattern string, cfg params.ChainConfig) {
tm.configpat = append(tm.configpat, testConfig{regexp.MustCompile(pattern), cfg})
Expand Down
8 changes: 4 additions & 4 deletions tests/transaction_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,25 +86,25 @@ func (tt *TransactionTest) Run(config *params.ChainConfig) error {

if testcase.fork.Sender == (common.UnprefixedAddress{}) {
if err == nil {
return fmt.Errorf("Expected error, got none (address %v)[%v]", sender.String(), testcase.name)
return fmt.Errorf("expected error, got none (address %v)[%v]", sender.String(), testcase.name)
}
continue
}
// Should resolve the right address
if err != nil {
return fmt.Errorf("Got error, expected none: %v", err)
return fmt.Errorf("got error, expected none: %v", err)
}
if sender == nil {
return fmt.Errorf("sender was nil, should be %x", common.Address(testcase.fork.Sender))
}
if *sender != common.Address(testcase.fork.Sender) {
return fmt.Errorf("Sender mismatch: got %x, want %x", sender, testcase.fork.Sender)
return fmt.Errorf("sender mismatch: got %x, want %x", sender, testcase.fork.Sender)
}
if txhash == nil {
return fmt.Errorf("txhash was nil, should be %x", common.Hash(testcase.fork.Hash))
}
if *txhash != common.Hash(testcase.fork.Hash) {
return fmt.Errorf("Hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash)
return fmt.Errorf("hash mismatch: got %x, want %x", *txhash, testcase.fork.Hash)
}
}
return nil
Expand Down