github.com/theQRL/go-zond@v0.2.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 toHexBig(h string) hexutil.Big {
    29  	b := new(big.Int).SetBytes(common.FromHex(h))
    30  	return hexutil.Big(*b)
    31  }
    32  func toHexUint(h string) hexutil.Uint64 {
    33  	b := new(big.Int).SetBytes(common.FromHex(h))
    34  	return hexutil.Uint64(b.Uint64())
    35  }
    36  func dummyTxArgs(t txtestcase) *apitypes.SendTxArgs {
    37  	to, _ := common.NewMixedcaseAddressFromString(t.to)
    38  	from, _ := common.NewMixedcaseAddressFromString(t.from)
    39  	n := toHexUint(t.n)
    40  	gas := toHexUint(t.g)
    41  	maxFeePerGas := toHexBig(t.mfpg)
    42  	maxPriorityFeePerGas := toHexBig(t.mpfpg)
    43  	value := toHexBig(t.value)
    44  	var (
    45  		data, input *hexutil.Bytes
    46  	)
    47  	if t.d != "" {
    48  		a := hexutil.Bytes(common.FromHex(t.d))
    49  		data = &a
    50  	}
    51  	if t.i != "" {
    52  		a := hexutil.Bytes(common.FromHex(t.i))
    53  		input = &a
    54  	}
    55  	return &apitypes.SendTxArgs{
    56  		From:                 *from,
    57  		To:                   to,
    58  		Value:                value,
    59  		Nonce:                n,
    60  		MaxFeePerGas:         &maxFeePerGas,
    61  		MaxPriorityFeePerGas: &maxPriorityFeePerGas,
    62  		Gas:                  gas,
    63  		Data:                 data,
    64  		Input:                input,
    65  	}
    66  }
    67  
    68  type txtestcase struct {
    69  	from, to, n, g, mfpg, mpfpg, value, d, i string
    70  	expectErr                                bool
    71  	numMessages                              int
    72  }
    73  
    74  func TestTransactionValidation(t *testing.T) {
    75  	var (
    76  		// use empty db, there are other tests for the abi-specific stuff
    77  		db = newEmpty()
    78  	)
    79  	testcases := []txtestcase{
    80  		// Invalid to checksum
    81  		{from: "Z000000000000000000000000000000000000dead", to: "Z000000000000000000000000000000000000dead",
    82  			n: "0x01", g: "0x20", mfpg: "0x40", mpfpg: "0x0", value: "0x01", numMessages: 1},
    83  		// valid Z000000000000000000000000000000000000dEaD
    84  		{from: "Z000000000000000000000000000000000000dead", to: "Z000000000000000000000000000000000000dEaD",
    85  			n: "0x01", g: "0x20", mfpg: "0x40", mpfpg: "0x0", value: "0x01", numMessages: 0},
    86  		// conflicting input and data
    87  		{from: "Z000000000000000000000000000000000000dead", to: "Z000000000000000000000000000000000000dEaD",
    88  			n: "0x01", g: "0x20", mfpg: "0x40", mpfpg: "0x0", value: "0x01", d: "0x01", i: "0x02", expectErr: true},
    89  		// Data can't be parsed
    90  		{from: "Z000000000000000000000000000000000000dead", to: "Z000000000000000000000000000000000000dEaD",
    91  			n: "0x01", g: "0x20", mfpg: "0x40", mpfpg: "0x0", value: "0x01", d: "0x0102", numMessages: 1},
    92  		// Data (on Input) can't be parsed
    93  		{from: "Z000000000000000000000000000000000000dead", to: "Z000000000000000000000000000000000000dEaD",
    94  			n: "0x01", g: "0x20", mfpg: "0x40", mpfpg: "0x0", value: "0x01", i: "0x0102", numMessages: 1},
    95  		// Send to 0
    96  		{from: "Z000000000000000000000000000000000000dead", to: "Z0000000000000000000000000000000000000000",
    97  			n: "0x01", g: "0x20", mfpg: "0x40", mpfpg: "0x0", value: "0x01", numMessages: 1},
    98  		// Create empty contract (no value)
    99  		{from: "Z000000000000000000000000000000000000dead", to: "",
   100  			n: "0x01", g: "0x20", mfpg: "0x40", mpfpg: "0x0", value: "0x00", numMessages: 1},
   101  		// Create empty contract (with value)
   102  		{from: "Z000000000000000000000000000000000000dead", to: "",
   103  			n: "0x01", g: "0x20", mfpg: "0x40", mpfpg: "0x0", value: "0x01", expectErr: true},
   104  		// Small payload for create
   105  		{from: "Z000000000000000000000000000000000000dead", to: "",
   106  			n: "0x01", g: "0x20", mfpg: "0x40", mpfpg: "0x0", value: "0x01", d: "0x01", numMessages: 1},
   107  	}
   108  	for i, test := range testcases {
   109  		msgs, err := db.ValidateTransaction(nil, dummyTxArgs(test))
   110  		if err == nil && test.expectErr {
   111  			t.Errorf("Test %d, expected error", i)
   112  			for _, msg := range msgs.Messages {
   113  				t.Logf("* %s: %s", msg.Typ, msg.Message)
   114  			}
   115  		}
   116  		if err != nil && !test.expectErr {
   117  			t.Errorf("Test %d, unexpected error: %v", i, err)
   118  		}
   119  		if err == nil {
   120  			got := len(msgs.Messages)
   121  			if got != test.numMessages {
   122  				for _, msg := range msgs.Messages {
   123  					t.Logf("* %s: %s", msg.Typ, msg.Message)
   124  				}
   125  				t.Errorf("Test %d, expected %d messages, got %d", i, test.numMessages, got)
   126  			} else {
   127  				//Debug printout, remove later
   128  				for _, msg := range msgs.Messages {
   129  					t.Logf("* [%d] %s: %s", i, msg.Typ, msg.Message)
   130  				}
   131  				t.Log()
   132  			}
   133  		}
   134  	}
   135  }