github.com/luckypickle/go-ethereum-vet@v1.14.2/signer/core/validation_test.go (about)

     1  // Copyright 2018 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package core
    18  
    19  import (
    20  	"fmt"
    21  	"math/big"
    22  	"testing"
    23  
    24  	"github.com/luckypickle/go-ethereum-vet/common"
    25  	"github.com/luckypickle/go-ethereum-vet/common/hexutil"
    26  )
    27  
    28  func hexAddr(a string) common.Address { return common.BytesToAddress(common.FromHex(a)) }
    29  func mixAddr(a string) (*common.MixedcaseAddress, error) {
    30  	return common.NewMixedcaseAddressFromString(a)
    31  }
    32  func toHexBig(h string) hexutil.Big {
    33  	b := big.NewInt(0).SetBytes(common.FromHex(h))
    34  	return hexutil.Big(*b)
    35  }
    36  func toHexUint(h string) hexutil.Uint64 {
    37  	b := big.NewInt(0).SetBytes(common.FromHex(h))
    38  	return hexutil.Uint64(b.Uint64())
    39  }
    40  func dummyTxArgs(t txtestcase) *SendTxArgs {
    41  	to, _ := mixAddr(t.to)
    42  	from, _ := mixAddr(t.from)
    43  	n := toHexUint(t.n)
    44  	gas := toHexUint(t.g)
    45  	gasPrice := toHexBig(t.gp)
    46  	value := toHexBig(t.value)
    47  	var (
    48  		data, input *hexutil.Bytes
    49  	)
    50  	if t.d != "" {
    51  		a := hexutil.Bytes(common.FromHex(t.d))
    52  		data = &a
    53  	}
    54  	if t.i != "" {
    55  		a := hexutil.Bytes(common.FromHex(t.i))
    56  		input = &a
    57  
    58  	}
    59  	return &SendTxArgs{
    60  		From:     *from,
    61  		To:       to,
    62  		Value:    value,
    63  		Nonce:    n,
    64  		GasPrice: gasPrice,
    65  		Gas:      gas,
    66  		Data:     data,
    67  		Input:    input,
    68  	}
    69  }
    70  
    71  type txtestcase struct {
    72  	from, to, n, g, gp, value, d, i string
    73  	expectErr                       bool
    74  	numMessages                     int
    75  }
    76  
    77  func TestValidator(t *testing.T) {
    78  	var (
    79  		// use empty db, there are other tests for the abi-specific stuff
    80  		db, _ = NewEmptyAbiDB()
    81  		v     = NewValidator(db)
    82  	)
    83  	testcases := []txtestcase{
    84  		// Invalid to checksum
    85  		{from: "000000000000000000000000000000000000dead", to: "000000000000000000000000000000000000dead",
    86  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 1},
    87  		// valid 0x000000000000000000000000000000000000dEaD
    88  		{from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
    89  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 0},
    90  		// conflicting input and data
    91  		{from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
    92  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x01", i: "0x02", expectErr: true},
    93  		// Data can't be parsed
    94  		{from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
    95  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x0102", numMessages: 1},
    96  		// Data (on Input) can't be parsed
    97  		{from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
    98  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", i: "0x0102", numMessages: 1},
    99  		// Send to 0
   100  		{from: "000000000000000000000000000000000000dead", to: "0x0000000000000000000000000000000000000000",
   101  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 1},
   102  		// Create empty contract (no value)
   103  		{from: "000000000000000000000000000000000000dead", to: "",
   104  			n: "0x01", g: "0x20", gp: "0x40", value: "0x00", numMessages: 1},
   105  		// Create empty contract (with value)
   106  		{from: "000000000000000000000000000000000000dead", to: "",
   107  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", expectErr: true},
   108  		// Small payload for create
   109  		{from: "000000000000000000000000000000000000dead", to: "",
   110  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x01", numMessages: 1},
   111  	}
   112  	for i, test := range testcases {
   113  		msgs, err := v.ValidateTransaction(dummyTxArgs(test), nil)
   114  		if err == nil && test.expectErr {
   115  			t.Errorf("Test %d, expected error", i)
   116  			for _, msg := range msgs.Messages {
   117  				fmt.Printf("* %s: %s\n", msg.Typ, msg.Message)
   118  			}
   119  		}
   120  		if err != nil && !test.expectErr {
   121  			t.Errorf("Test %d, unexpected error: %v", i, err)
   122  		}
   123  		if err == nil {
   124  			got := len(msgs.Messages)
   125  			if got != test.numMessages {
   126  				for _, msg := range msgs.Messages {
   127  					fmt.Printf("* %s: %s\n", msg.Typ, msg.Message)
   128  				}
   129  				t.Errorf("Test %d, expected %d messages, got %d", i, test.numMessages, got)
   130  			} else {
   131  				//Debug printout, remove later
   132  				for _, msg := range msgs.Messages {
   133  					fmt.Printf("* [%d] %s: %s\n", i, msg.Typ, msg.Message)
   134  				}
   135  				fmt.Println()
   136  			}
   137  		}
   138  	}
   139  }