github.com/aswedchain/aswed@v1.0.1/accounts/abi/type_test.go (about)

     1  // Copyright 2016 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 abi
    18  
    19  import (
    20  	"math/big"
    21  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/davecgh/go-spew/spew"
    25  	"github.com/aswedchain/aswed/common"
    26  )
    27  
    28  // typeWithoutStringer is a alias for the Type type which simply doesn't implement
    29  // the stringer interface to allow printing type details in the tests below.
    30  type typeWithoutStringer Type
    31  
    32  // Tests that all allowed types get recognized by the type parser.
    33  func TestTypeRegexp(t *testing.T) {
    34  	tests := []struct {
    35  		blob       string
    36  		components []ArgumentMarshaling
    37  		kind       Type
    38  	}{
    39  		{"bool", nil, Type{T: BoolTy, stringKind: "bool"}},
    40  		{"bool[]", nil, Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}},
    41  		{"bool[2]", nil, Type{Size: 2, T: ArrayTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}},
    42  		{"bool[2][]", nil, Type{T: SliceTy, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}},
    43  		{"bool[][]", nil, Type{T: SliceTy, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}},
    44  		{"bool[][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}},
    45  		{"bool[2][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}},
    46  		{"bool[2][][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: SliceTy, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}, stringKind: "bool[2][][2]"}},
    47  		{"bool[2][2][2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}, stringKind: "bool[2][2][2]"}},
    48  		{"bool[][][]", nil, Type{T: SliceTy, Elem: &Type{T: SliceTy, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}, stringKind: "bool[][][]"}},
    49  		{"bool[][2][]", nil, Type{T: SliceTy, Elem: &Type{T: ArrayTy, Size: 2, Elem: &Type{T: SliceTy, Elem: &Type{T: BoolTy, stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}, stringKind: "bool[][2][]"}},
    50  		{"int8", nil, Type{Size: 8, T: IntTy, stringKind: "int8"}},
    51  		{"int16", nil, Type{Size: 16, T: IntTy, stringKind: "int16"}},
    52  		{"int32", nil, Type{Size: 32, T: IntTy, stringKind: "int32"}},
    53  		{"int64", nil, Type{Size: 64, T: IntTy, stringKind: "int64"}},
    54  		{"int256", nil, Type{Size: 256, T: IntTy, stringKind: "int256"}},
    55  		{"int8[]", nil, Type{T: SliceTy, Elem: &Type{Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[]"}},
    56  		{"int8[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[2]"}},
    57  		{"int16[]", nil, Type{T: SliceTy, Elem: &Type{Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[]"}},
    58  		{"int16[2]", nil, Type{Size: 2, T: ArrayTy, Elem: &Type{Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[2]"}},
    59  		{"int32[]", nil, Type{T: SliceTy, Elem: &Type{Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[]"}},
    60  		{"int32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[2]"}},
    61  		{"int64[]", nil, Type{T: SliceTy, Elem: &Type{Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[]"}},
    62  		{"int64[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[2]"}},
    63  		{"int256[]", nil, Type{T: SliceTy, Elem: &Type{Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[]"}},
    64  		{"int256[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[2]"}},
    65  		{"uint8", nil, Type{Size: 8, T: UintTy, stringKind: "uint8"}},
    66  		{"uint16", nil, Type{Size: 16, T: UintTy, stringKind: "uint16"}},
    67  		{"uint32", nil, Type{Size: 32, T: UintTy, stringKind: "uint32"}},
    68  		{"uint64", nil, Type{Size: 64, T: UintTy, stringKind: "uint64"}},
    69  		{"uint256", nil, Type{Size: 256, T: UintTy, stringKind: "uint256"}},
    70  		{"uint8[]", nil, Type{T: SliceTy, Elem: &Type{Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[]"}},
    71  		{"uint8[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[2]"}},
    72  		{"uint16[]", nil, Type{T: SliceTy, Elem: &Type{Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[]"}},
    73  		{"uint16[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[2]"}},
    74  		{"uint32[]", nil, Type{T: SliceTy, Elem: &Type{Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[]"}},
    75  		{"uint32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[2]"}},
    76  		{"uint64[]", nil, Type{T: SliceTy, Elem: &Type{Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[]"}},
    77  		{"uint64[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[2]"}},
    78  		{"uint256[]", nil, Type{T: SliceTy, Elem: &Type{Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}},
    79  		{"uint256[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}},
    80  		{"bytes32", nil, Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}},
    81  		{"bytes[]", nil, Type{T: SliceTy, Elem: &Type{T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[]"}},
    82  		{"bytes[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[2]"}},
    83  		{"bytes32[]", nil, Type{T: SliceTy, Elem: &Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[]"}},
    84  		{"bytes32[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[2]"}},
    85  		{"string", nil, Type{T: StringTy, stringKind: "string"}},
    86  		{"string[]", nil, Type{T: SliceTy, Elem: &Type{T: StringTy, stringKind: "string"}, stringKind: "string[]"}},
    87  		{"string[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{T: StringTy, stringKind: "string"}, stringKind: "string[2]"}},
    88  		{"address", nil, Type{Size: 20, T: AddressTy, stringKind: "address"}},
    89  		{"address[]", nil, Type{T: SliceTy, Elem: &Type{Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[]"}},
    90  		{"address[2]", nil, Type{T: ArrayTy, Size: 2, Elem: &Type{Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[2]"}},
    91  		// TODO when fixed types are implemented properly
    92  		// {"fixed", nil, Type{}},
    93  		// {"fixed128x128", nil, Type{}},
    94  		// {"fixed[]", nil, Type{}},
    95  		// {"fixed[2]", nil, Type{}},
    96  		// {"fixed128x128[]", nil, Type{}},
    97  		// {"fixed128x128[2]", nil, Type{}},
    98  		{"tuple", []ArgumentMarshaling{{Name: "a", Type: "int64"}}, Type{T: TupleTy, TupleType: reflect.TypeOf(struct {
    99  			A int64 `json:"a"`
   100  		}{}), stringKind: "(int64)",
   101  			TupleElems: []*Type{{T: IntTy, Size: 64, stringKind: "int64"}}, TupleRawNames: []string{"a"}}},
   102  		{"tuple with long name", []ArgumentMarshaling{{Name: "aTypicalParamName", Type: "int64"}}, Type{T: TupleTy, TupleType: reflect.TypeOf(struct {
   103  			ATypicalParamName int64 `json:"aTypicalParamName"`
   104  		}{}), stringKind: "(int64)",
   105  			TupleElems: []*Type{{T: IntTy, Size: 64, stringKind: "int64"}}, TupleRawNames: []string{"aTypicalParamName"}}},
   106  	}
   107  
   108  	for _, tt := range tests {
   109  		typ, err := NewType(tt.blob, "", tt.components)
   110  		if err != nil {
   111  			t.Errorf("type %q: failed to parse type string: %v", tt.blob, err)
   112  		}
   113  		if !reflect.DeepEqual(typ, tt.kind) {
   114  			t.Errorf("type %q: parsed type mismatch:\nGOT %s\nWANT %s ", tt.blob, spew.Sdump(typeWithoutStringer(typ)), spew.Sdump(typeWithoutStringer(tt.kind)))
   115  		}
   116  	}
   117  }
   118  
   119  func TestTypeCheck(t *testing.T) {
   120  	for i, test := range []struct {
   121  		typ        string
   122  		components []ArgumentMarshaling
   123  		input      interface{}
   124  		err        string
   125  	}{
   126  		{"uint", nil, big.NewInt(1), "unsupported arg type: uint"},
   127  		{"int", nil, big.NewInt(1), "unsupported arg type: int"},
   128  		{"uint256", nil, big.NewInt(1), ""},
   129  		{"uint256[][3][]", nil, [][3][]*big.Int{{{}}}, ""},
   130  		{"uint256[][][3]", nil, [3][][]*big.Int{{{}}}, ""},
   131  		{"uint256[3][][]", nil, [][][3]*big.Int{{{}}}, ""},
   132  		{"uint256[3][3][3]", nil, [3][3][3]*big.Int{{{}}}, ""},
   133  		{"uint8[][]", nil, [][]uint8{}, ""},
   134  		{"int256", nil, big.NewInt(1), ""},
   135  		{"uint8", nil, uint8(1), ""},
   136  		{"uint16", nil, uint16(1), ""},
   137  		{"uint32", nil, uint32(1), ""},
   138  		{"uint64", nil, uint64(1), ""},
   139  		{"int8", nil, int8(1), ""},
   140  		{"int16", nil, int16(1), ""},
   141  		{"int32", nil, int32(1), ""},
   142  		{"int64", nil, int64(1), ""},
   143  		{"uint24", nil, big.NewInt(1), ""},
   144  		{"uint40", nil, big.NewInt(1), ""},
   145  		{"uint48", nil, big.NewInt(1), ""},
   146  		{"uint56", nil, big.NewInt(1), ""},
   147  		{"uint72", nil, big.NewInt(1), ""},
   148  		{"uint80", nil, big.NewInt(1), ""},
   149  		{"uint88", nil, big.NewInt(1), ""},
   150  		{"uint96", nil, big.NewInt(1), ""},
   151  		{"uint104", nil, big.NewInt(1), ""},
   152  		{"uint112", nil, big.NewInt(1), ""},
   153  		{"uint120", nil, big.NewInt(1), ""},
   154  		{"uint128", nil, big.NewInt(1), ""},
   155  		{"uint136", nil, big.NewInt(1), ""},
   156  		{"uint144", nil, big.NewInt(1), ""},
   157  		{"uint152", nil, big.NewInt(1), ""},
   158  		{"uint160", nil, big.NewInt(1), ""},
   159  		{"uint168", nil, big.NewInt(1), ""},
   160  		{"uint176", nil, big.NewInt(1), ""},
   161  		{"uint184", nil, big.NewInt(1), ""},
   162  		{"uint192", nil, big.NewInt(1), ""},
   163  		{"uint200", nil, big.NewInt(1), ""},
   164  		{"uint208", nil, big.NewInt(1), ""},
   165  		{"uint216", nil, big.NewInt(1), ""},
   166  		{"uint224", nil, big.NewInt(1), ""},
   167  		{"uint232", nil, big.NewInt(1), ""},
   168  		{"uint240", nil, big.NewInt(1), ""},
   169  		{"uint248", nil, big.NewInt(1), ""},
   170  		{"int24", nil, big.NewInt(1), ""},
   171  		{"int40", nil, big.NewInt(1), ""},
   172  		{"int48", nil, big.NewInt(1), ""},
   173  		{"int56", nil, big.NewInt(1), ""},
   174  		{"int72", nil, big.NewInt(1), ""},
   175  		{"int80", nil, big.NewInt(1), ""},
   176  		{"int88", nil, big.NewInt(1), ""},
   177  		{"int96", nil, big.NewInt(1), ""},
   178  		{"int104", nil, big.NewInt(1), ""},
   179  		{"int112", nil, big.NewInt(1), ""},
   180  		{"int120", nil, big.NewInt(1), ""},
   181  		{"int128", nil, big.NewInt(1), ""},
   182  		{"int136", nil, big.NewInt(1), ""},
   183  		{"int144", nil, big.NewInt(1), ""},
   184  		{"int152", nil, big.NewInt(1), ""},
   185  		{"int160", nil, big.NewInt(1), ""},
   186  		{"int168", nil, big.NewInt(1), ""},
   187  		{"int176", nil, big.NewInt(1), ""},
   188  		{"int184", nil, big.NewInt(1), ""},
   189  		{"int192", nil, big.NewInt(1), ""},
   190  		{"int200", nil, big.NewInt(1), ""},
   191  		{"int208", nil, big.NewInt(1), ""},
   192  		{"int216", nil, big.NewInt(1), ""},
   193  		{"int224", nil, big.NewInt(1), ""},
   194  		{"int232", nil, big.NewInt(1), ""},
   195  		{"int240", nil, big.NewInt(1), ""},
   196  		{"int248", nil, big.NewInt(1), ""},
   197  		{"uint30", nil, uint8(1), "abi: cannot use uint8 as type ptr as argument"},
   198  		{"uint8", nil, uint16(1), "abi: cannot use uint16 as type uint8 as argument"},
   199  		{"uint8", nil, uint32(1), "abi: cannot use uint32 as type uint8 as argument"},
   200  		{"uint8", nil, uint64(1), "abi: cannot use uint64 as type uint8 as argument"},
   201  		{"uint8", nil, int8(1), "abi: cannot use int8 as type uint8 as argument"},
   202  		{"uint8", nil, int16(1), "abi: cannot use int16 as type uint8 as argument"},
   203  		{"uint8", nil, int32(1), "abi: cannot use int32 as type uint8 as argument"},
   204  		{"uint8", nil, int64(1), "abi: cannot use int64 as type uint8 as argument"},
   205  		{"uint16", nil, uint16(1), ""},
   206  		{"uint16", nil, uint8(1), "abi: cannot use uint8 as type uint16 as argument"},
   207  		{"uint16[]", nil, []uint16{1, 2, 3}, ""},
   208  		{"uint16[]", nil, [3]uint16{1, 2, 3}, ""},
   209  		{"uint16[]", nil, []uint32{1, 2, 3}, "abi: cannot use []uint32 as type [0]uint16 as argument"},
   210  		{"uint16[3]", nil, [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"},
   211  		{"uint16[3]", nil, [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"},
   212  		{"uint16[3]", nil, []uint16{1, 2, 3}, ""},
   213  		{"uint16[3]", nil, []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"},
   214  		{"address[]", nil, []common.Address{{1}}, ""},
   215  		{"address[1]", nil, []common.Address{{1}}, ""},
   216  		{"address[1]", nil, [1]common.Address{{1}}, ""},
   217  		{"address[2]", nil, [1]common.Address{{1}}, "abi: cannot use [1]array as type [2]array as argument"},
   218  		{"bytes32", nil, [32]byte{}, ""},
   219  		{"bytes31", nil, [31]byte{}, ""},
   220  		{"bytes30", nil, [30]byte{}, ""},
   221  		{"bytes29", nil, [29]byte{}, ""},
   222  		{"bytes28", nil, [28]byte{}, ""},
   223  		{"bytes27", nil, [27]byte{}, ""},
   224  		{"bytes26", nil, [26]byte{}, ""},
   225  		{"bytes25", nil, [25]byte{}, ""},
   226  		{"bytes24", nil, [24]byte{}, ""},
   227  		{"bytes23", nil, [23]byte{}, ""},
   228  		{"bytes22", nil, [22]byte{}, ""},
   229  		{"bytes21", nil, [21]byte{}, ""},
   230  		{"bytes20", nil, [20]byte{}, ""},
   231  		{"bytes19", nil, [19]byte{}, ""},
   232  		{"bytes18", nil, [18]byte{}, ""},
   233  		{"bytes17", nil, [17]byte{}, ""},
   234  		{"bytes16", nil, [16]byte{}, ""},
   235  		{"bytes15", nil, [15]byte{}, ""},
   236  		{"bytes14", nil, [14]byte{}, ""},
   237  		{"bytes13", nil, [13]byte{}, ""},
   238  		{"bytes12", nil, [12]byte{}, ""},
   239  		{"bytes11", nil, [11]byte{}, ""},
   240  		{"bytes10", nil, [10]byte{}, ""},
   241  		{"bytes9", nil, [9]byte{}, ""},
   242  		{"bytes8", nil, [8]byte{}, ""},
   243  		{"bytes7", nil, [7]byte{}, ""},
   244  		{"bytes6", nil, [6]byte{}, ""},
   245  		{"bytes5", nil, [5]byte{}, ""},
   246  		{"bytes4", nil, [4]byte{}, ""},
   247  		{"bytes3", nil, [3]byte{}, ""},
   248  		{"bytes2", nil, [2]byte{}, ""},
   249  		{"bytes1", nil, [1]byte{}, ""},
   250  		{"bytes32", nil, [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"},
   251  		{"bytes32", nil, common.Hash{1}, ""},
   252  		{"bytes31", nil, common.Hash{1}, "abi: cannot use common.Hash as type [31]uint8 as argument"},
   253  		{"bytes31", nil, [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"},
   254  		{"bytes", nil, []byte{0, 1}, ""},
   255  		{"bytes", nil, [2]byte{0, 1}, "abi: cannot use array as type slice as argument"},
   256  		{"bytes", nil, common.Hash{1}, "abi: cannot use array as type slice as argument"},
   257  		{"string", nil, "hello world", ""},
   258  		{"string", nil, string(""), ""},
   259  		{"string", nil, []byte{}, "abi: cannot use slice as type string as argument"},
   260  		{"bytes32[]", nil, [][32]byte{{}}, ""},
   261  		{"function", nil, [24]byte{}, ""},
   262  		{"bytes20", nil, common.Address{}, ""},
   263  		{"address", nil, [20]byte{}, ""},
   264  		{"address", nil, common.Address{}, ""},
   265  		{"bytes32[]]", nil, "", "invalid arg type in abi"},
   266  		{"invalidType", nil, "", "unsupported arg type: invalidType"},
   267  		{"invalidSlice[]", nil, "", "unsupported arg type: invalidSlice"},
   268  		// simple tuple
   269  		{"tuple", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, struct {
   270  			A *big.Int
   271  			B *big.Int
   272  		}{}, ""},
   273  		// tuple slice
   274  		{"tuple[]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct {
   275  			A *big.Int
   276  			B *big.Int
   277  		}{}, ""},
   278  		// tuple array
   279  		{"tuple[2]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct {
   280  			A *big.Int
   281  			B *big.Int
   282  		}{{big.NewInt(0), big.NewInt(0)}, {big.NewInt(0), big.NewInt(0)}}, ""},
   283  	} {
   284  		typ, err := NewType(test.typ, "", test.components)
   285  		if err != nil && len(test.err) == 0 {
   286  			t.Fatal("unexpected parse error:", err)
   287  		} else if err != nil && len(test.err) != 0 {
   288  			if err.Error() != test.err {
   289  				t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err)
   290  			}
   291  			continue
   292  		}
   293  
   294  		err = typeCheck(typ, reflect.ValueOf(test.input))
   295  		if err != nil && len(test.err) == 0 {
   296  			t.Errorf("%d failed. Expected no err but got: %v", i, err)
   297  			continue
   298  		}
   299  		if err == nil && len(test.err) != 0 {
   300  			t.Errorf("%d failed. Expected err: %v but got none", i, test.err)
   301  			continue
   302  		}
   303  
   304  		if err != nil && len(test.err) != 0 && err.Error() != test.err {
   305  			t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err)
   306  		}
   307  	}
   308  }
   309  
   310  func TestInternalType(t *testing.T) {
   311  	components := []ArgumentMarshaling{{Name: "a", Type: "int64"}}
   312  	internalType := "struct a.b[]"
   313  	kind := Type{
   314  		T: TupleTy,
   315  		TupleType: reflect.TypeOf(struct {
   316  			A int64 `json:"a"`
   317  		}{}),
   318  		stringKind:    "(int64)",
   319  		TupleRawName:  "ab[]",
   320  		TupleElems:    []*Type{{T: IntTy, Size: 64, stringKind: "int64"}},
   321  		TupleRawNames: []string{"a"},
   322  	}
   323  
   324  	blob := "tuple"
   325  	typ, err := NewType(blob, internalType, components)
   326  	if err != nil {
   327  		t.Errorf("type %q: failed to parse type string: %v", blob, err)
   328  	}
   329  	if !reflect.DeepEqual(typ, kind) {
   330  		t.Errorf("type %q: parsed type mismatch:\nGOT %s\nWANT %s ", blob, spew.Sdump(typeWithoutStringer(typ)), spew.Sdump(typeWithoutStringer(kind)))
   331  	}
   332  }
   333  
   334  func TestGetTypeSize(t *testing.T) {
   335  	var testCases = []struct {
   336  		typ        string
   337  		components []ArgumentMarshaling
   338  		typSize    int
   339  	}{
   340  		// simple array
   341  		{"uint256[2]", nil, 32 * 2},
   342  		{"address[3]", nil, 32 * 3},
   343  		{"bytes32[4]", nil, 32 * 4},
   344  		// array array
   345  		{"uint256[2][3][4]", nil, 32 * (2 * 3 * 4)},
   346  		// array tuple
   347  		{"tuple[2]", []ArgumentMarshaling{{Name: "x", Type: "bytes32"}, {Name: "y", Type: "bytes32"}}, (32 * 2) * 2},
   348  		// simple tuple
   349  		{"tuple", []ArgumentMarshaling{{Name: "x", Type: "uint256"}, {Name: "y", Type: "uint256"}}, 32 * 2},
   350  		// tuple array
   351  		{"tuple", []ArgumentMarshaling{{Name: "x", Type: "bytes32[2]"}}, 32 * 2},
   352  		// tuple tuple
   353  		{"tuple", []ArgumentMarshaling{{Name: "x", Type: "tuple", Components: []ArgumentMarshaling{{Name: "x", Type: "bytes32"}}}}, 32},
   354  		{"tuple", []ArgumentMarshaling{{Name: "x", Type: "tuple", Components: []ArgumentMarshaling{{Name: "x", Type: "bytes32[2]"}, {Name: "y", Type: "uint256"}}}}, 32 * (2 + 1)},
   355  	}
   356  
   357  	for i, data := range testCases {
   358  		typ, err := NewType(data.typ, "", data.components)
   359  		if err != nil {
   360  			t.Errorf("type %q: failed to parse type string: %v", data.typ, err)
   361  		}
   362  
   363  		result := getTypeSize(typ)
   364  		if result != data.typSize {
   365  			t.Errorf("case %d type %q: get type size error: actual: %d expected: %d", i, data.typ, result, data.typSize)
   366  		}
   367  	}
   368  }