github.com/theQRL/go-zond@v0.1.1/signer/fourbyte/validation_test.go (about)

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