github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/signer/core/validation_test.go (about)

     1  
     2  package core
     3  
     4  import (
     5  	"fmt"
     6  	"math/big"
     7  	"testing"
     8  
     9  	"github.com/quickchainproject/quickchain/common"
    10  	"github.com/quickchainproject/quickchain/common/hexutil"
    11  )
    12  
    13  func hexAddr(a string) common.Address { return common.BytesToAddress(common.FromHex(a)) }
    14  func mixAddr(a string) (*common.MixedcaseAddress, error) {
    15  	return common.NewMixedcaseAddressFromString(a)
    16  }
    17  func toHexBig(h string) hexutil.Big {
    18  	b := big.NewInt(0).SetBytes(common.FromHex(h))
    19  	return hexutil.Big(*b)
    20  }
    21  func toHexUint(h string) hexutil.Uint64 {
    22  	b := big.NewInt(0).SetBytes(common.FromHex(h))
    23  	return hexutil.Uint64(b.Uint64())
    24  }
    25  func dummyTxArgs(t txtestcase) *SendTxArgs {
    26  	to, _ := mixAddr(t.to)
    27  	from, _ := mixAddr(t.from)
    28  	n := toHexUint(t.n)
    29  	gas := toHexUint(t.g)
    30  	gasPrice := toHexBig(t.gp)
    31  	value := toHexBig(t.value)
    32  	var (
    33  		data, input *hexutil.Bytes
    34  	)
    35  	if t.d != "" {
    36  		a := hexutil.Bytes(common.FromHex(t.d))
    37  		data = &a
    38  	}
    39  	if t.i != "" {
    40  		a := hexutil.Bytes(common.FromHex(t.i))
    41  		input = &a
    42  
    43  	}
    44  	return &SendTxArgs{
    45  		From:     *from,
    46  		To:       to,
    47  		Value:    value,
    48  		Nonce:    n,
    49  		GasPrice: gasPrice,
    50  		Gas:      gas,
    51  		Data:     data,
    52  		Input:    input,
    53  	}
    54  }
    55  
    56  type txtestcase struct {
    57  	from, to, n, g, gp, value, d, i string
    58  	expectErr                       bool
    59  	numMessages                     int
    60  }
    61  
    62  func TestValidator(t *testing.T) {
    63  	var (
    64  		// use empty db, there are other tests for the abi-specific stuff
    65  		db, _ = NewEmptyAbiDB()
    66  		v     = NewValidator(db)
    67  	)
    68  	testcases := []txtestcase{
    69  		// Invalid to checksum
    70  		{from: "000000000000000000000000000000000000dead", to: "000000000000000000000000000000000000dead",
    71  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 1},
    72  		// valid 0x000000000000000000000000000000000000dEaD
    73  		{from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
    74  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 0},
    75  		// conflicting input and data
    76  		{from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
    77  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x01", i: "0x02", expectErr: true},
    78  		// Data can't be parsed
    79  		{from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
    80  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x0102", numMessages: 1},
    81  		// Data (on Input) can't be parsed
    82  		{from: "000000000000000000000000000000000000dead", to: "0x000000000000000000000000000000000000dEaD",
    83  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", i: "0x0102", numMessages: 1},
    84  		// Send to 0
    85  		{from: "000000000000000000000000000000000000dead", to: "0x0000000000000000000000000000000000000000",
    86  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", numMessages: 1},
    87  		// Create empty contract (no value)
    88  		{from: "000000000000000000000000000000000000dead", to: "",
    89  			n: "0x01", g: "0x20", gp: "0x40", value: "0x00", numMessages: 1},
    90  		// Create empty contract (with value)
    91  		{from: "000000000000000000000000000000000000dead", to: "",
    92  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", expectErr: true},
    93  		// Small payload for create
    94  		{from: "000000000000000000000000000000000000dead", to: "",
    95  			n: "0x01", g: "0x20", gp: "0x40", value: "0x01", d: "0x01", numMessages: 1},
    96  	}
    97  	for i, test := range testcases {
    98  		msgs, err := v.ValidateTransaction(dummyTxArgs(test), nil)
    99  		if err == nil && test.expectErr {
   100  			t.Errorf("Test %d, expected error", i)
   101  			for _, msg := range msgs.Messages {
   102  				fmt.Printf("* %s: %s\n", msg.Typ, msg.Message)
   103  			}
   104  		}
   105  		if err != nil && !test.expectErr {
   106  			t.Errorf("Test %d, unexpected error: %v", i, err)
   107  		}
   108  		if err == nil {
   109  			got := len(msgs.Messages)
   110  			if got != test.numMessages {
   111  				for _, msg := range msgs.Messages {
   112  					fmt.Printf("* %s: %s\n", msg.Typ, msg.Message)
   113  				}
   114  				t.Errorf("Test %d, expected %d messages, got %d", i, test.numMessages, got)
   115  			} else {
   116  				//Debug printout, remove later
   117  				for _, msg := range msgs.Messages {
   118  					fmt.Printf("* [%d] %s: %s\n", i, msg.Typ, msg.Message)
   119  				}
   120  				fmt.Println()
   121  			}
   122  		}
   123  	}
   124  }