github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/accounts/abi/type_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package abi 19 20 import ( 21 "math/big" 22 "reflect" 23 "testing" 24 25 "github.com/AigarNetwork/aigar/common" 26 "github.com/davecgh/go-spew/spew" 27 ) 28 29 // typeWithoutStringer is a alias for the Type type which simply doesn't implement 30 // the stringer interface to allow printing type details in the tests below. 31 type typeWithoutStringer Type 32 33 // Tests that all allowed types get recognized by the type parser. 34 func TestTypeRegexp(t *testing.T) { 35 tests := []struct { 36 blob string 37 components []ArgumentMarshaling 38 kind Type 39 }{ 40 {"bool", nil, Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}}, 41 {"bool[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool(nil)), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}}, 42 {"bool[2]", nil, Type{Size: 2, Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}}, 43 {"bool[2][]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}}, 44 {"bool[][]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}}, 45 {"bool[][2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}}, 46 {"bool[2][2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}}, 47 {"bool[2][][2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][][2]bool{}), Elem: &Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][]"}, stringKind: "bool[2][][2]"}}, 48 {"bool[2][2][2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][2]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[2]"}, stringKind: "bool[2][2]"}, stringKind: "bool[2][2][2]"}}, 49 {"bool[][][]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][]"}, stringKind: "bool[][][]"}}, 50 {"bool[][2][]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][2][]bool{}), Elem: &Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]bool{}), Elem: &Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]bool{}), Elem: &Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}, stringKind: "bool[]"}, stringKind: "bool[][2]"}, stringKind: "bool[][2][]"}}, 51 {"int8", nil, Type{Kind: reflect.Int8, Type: int8T, Size: 8, T: IntTy, stringKind: "int8"}}, 52 {"int16", nil, Type{Kind: reflect.Int16, Type: int16T, Size: 16, T: IntTy, stringKind: "int16"}}, 53 {"int32", nil, Type{Kind: reflect.Int32, Type: int32T, Size: 32, T: IntTy, stringKind: "int32"}}, 54 {"int64", nil, Type{Kind: reflect.Int64, Type: int64T, Size: 64, T: IntTy, stringKind: "int64"}}, 55 {"int256", nil, Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: IntTy, stringKind: "int256"}}, 56 {"int8[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int8{}), Elem: &Type{Kind: reflect.Int8, Type: int8T, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[]"}}, 57 {"int8[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int8{}), Elem: &Type{Kind: reflect.Int8, Type: int8T, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[2]"}}, 58 {"int16[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int16{}), Elem: &Type{Kind: reflect.Int16, Type: int16T, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[]"}}, 59 {"int16[2]", nil, Type{Size: 2, Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]int16{}), Elem: &Type{Kind: reflect.Int16, Type: int16T, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[2]"}}, 60 {"int32[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int32{}), Elem: &Type{Kind: reflect.Int32, Type: int32T, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[]"}}, 61 {"int32[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int32{}), Elem: &Type{Kind: reflect.Int32, Type: int32T, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[2]"}}, 62 {"int64[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int64{}), Elem: &Type{Kind: reflect.Int64, Type: int64T, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[]"}}, 63 {"int64[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int64{}), Elem: &Type{Kind: reflect.Int64, Type: int64T, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[2]"}}, 64 {"int256[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[]"}}, 65 {"int256[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[2]"}}, 66 {"uint8", nil, Type{Kind: reflect.Uint8, Type: uint8T, Size: 8, T: UintTy, stringKind: "uint8"}}, 67 {"uint16", nil, Type{Kind: reflect.Uint16, Type: uint16T, Size: 16, T: UintTy, stringKind: "uint16"}}, 68 {"uint32", nil, Type{Kind: reflect.Uint32, Type: uint32T, Size: 32, T: UintTy, stringKind: "uint32"}}, 69 {"uint64", nil, Type{Kind: reflect.Uint64, Type: uint64T, Size: 64, T: UintTy, stringKind: "uint64"}}, 70 {"uint256", nil, Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: UintTy, stringKind: "uint256"}}, 71 {"uint8[]", nil, Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]uint8{}), Elem: &Type{Kind: reflect.Uint8, Type: uint8T, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[]"}}, 72 {"uint8[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint8{}), Elem: &Type{Kind: reflect.Uint8, Type: uint8T, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[2]"}}, 73 {"uint16[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint16{}), Elem: &Type{Kind: reflect.Uint16, Type: uint16T, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[]"}}, 74 {"uint16[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint16{}), Elem: &Type{Kind: reflect.Uint16, Type: uint16T, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[2]"}}, 75 {"uint32[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint32{}), Elem: &Type{Kind: reflect.Uint32, Type: uint32T, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[]"}}, 76 {"uint32[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint32{}), Elem: &Type{Kind: reflect.Uint32, Type: uint32T, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[2]"}}, 77 {"uint64[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint64{}), Elem: &Type{Kind: reflect.Uint64, Type: uint64T, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[]"}}, 78 {"uint64[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint64{}), Elem: &Type{Kind: reflect.Uint64, Type: uint64T, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[2]"}}, 79 {"uint256[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}}, 80 {"uint256[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]*big.Int{}), Size: 2, Elem: &Type{Kind: reflect.Ptr, Type: bigT, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}}, 81 {"bytes32", nil, Type{Kind: reflect.Array, T: FixedBytesTy, Size: 32, Type: reflect.TypeOf([32]byte{}), stringKind: "bytes32"}}, 82 {"bytes[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][]byte{}), Elem: &Type{Kind: reflect.Slice, Type: reflect.TypeOf([]byte{}), T: BytesTy, stringKind: "bytes"}, stringKind: "bytes[]"}}, 83 {"bytes[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][]byte{}), Elem: &Type{T: BytesTy, Type: reflect.TypeOf([]byte{}), Kind: reflect.Slice, stringKind: "bytes"}, stringKind: "bytes[2]"}}, 84 {"bytes32[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([][32]byte{}), Elem: &Type{Kind: reflect.Array, Type: reflect.TypeOf([32]byte{}), T: FixedBytesTy, Size: 32, stringKind: "bytes32"}, stringKind: "bytes32[]"}}, 85 {"bytes32[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2][32]byte{}), Elem: &Type{Kind: reflect.Array, T: FixedBytesTy, Size: 32, Type: reflect.TypeOf([32]byte{}), stringKind: "bytes32"}, stringKind: "bytes32[2]"}}, 86 {"string", nil, Type{Kind: reflect.String, T: StringTy, Type: reflect.TypeOf(""), stringKind: "string"}}, 87 {"string[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]string{}), Elem: &Type{Kind: reflect.String, Type: reflect.TypeOf(""), T: StringTy, stringKind: "string"}, stringKind: "string[]"}}, 88 {"string[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]string{}), Elem: &Type{Kind: reflect.String, T: StringTy, Type: reflect.TypeOf(""), stringKind: "string"}, stringKind: "string[2]"}}, 89 {"address", nil, Type{Kind: reflect.Array, Type: addressT, Size: 20, T: AddressTy, stringKind: "address"}}, 90 {"address[]", nil, Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: addressT, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[]"}}, 91 {"address[2]", nil, Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: addressT, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[2]"}}, 92 // TODO when fixed types are implemented properly 93 // {"fixed", nil, Type{}}, 94 // {"fixed128x128", nil, Type{}}, 95 // {"fixed[]", nil, Type{}}, 96 // {"fixed[2]", nil, Type{}}, 97 // {"fixed128x128[]", nil, Type{}}, 98 // {"fixed128x128[2]", nil, Type{}}, 99 {"tuple", []ArgumentMarshaling{{Name: "a", Type: "int64"}}, Type{Kind: reflect.Struct, T: TupleTy, Type: reflect.TypeOf(struct { 100 A int64 `json:"a"` 101 }{}), stringKind: "(int64)", 102 TupleElems: []*Type{{Kind: reflect.Int64, T: IntTy, Type: reflect.TypeOf(int64(0)), Size: 64, stringKind: "int64"}}, TupleRawNames: []string{"a"}}}, 103 {"tuple with long name", []ArgumentMarshaling{{Name: "aTypicalParamName", Type: "int64"}}, Type{Kind: reflect.Struct, T: TupleTy, Type: reflect.TypeOf(struct { 104 ATypicalParamName int64 `json:"aTypicalParamName"` 105 }{}), stringKind: "(int64)", 106 TupleElems: []*Type{{Kind: reflect.Int64, T: IntTy, Type: reflect.TypeOf(int64(0)), Size: 64, stringKind: "int64"}}, TupleRawNames: []string{"aTypicalParamName"}}}, 107 } 108 109 for _, tt := range tests { 110 typ, err := NewType(tt.blob, "", tt.components) 111 if err != nil { 112 t.Errorf("type %q: failed to parse type string: %v", tt.blob, err) 113 } 114 if !reflect.DeepEqual(typ, tt.kind) { 115 t.Errorf("type %q: parsed type mismatch:\nGOT %s\nWANT %s ", tt.blob, spew.Sdump(typeWithoutStringer(typ)), spew.Sdump(typeWithoutStringer(tt.kind))) 116 } 117 } 118 } 119 120 func TestTypeCheck(t *testing.T) { 121 for i, test := range []struct { 122 typ string 123 components []ArgumentMarshaling 124 input interface{} 125 err string 126 }{ 127 {"uint", nil, big.NewInt(1), "unsupported arg type: uint"}, 128 {"int", nil, big.NewInt(1), "unsupported arg type: int"}, 129 {"uint256", nil, big.NewInt(1), ""}, 130 {"uint256[][3][]", nil, [][3][]*big.Int{{{}}}, ""}, 131 {"uint256[][][3]", nil, [3][][]*big.Int{{{}}}, ""}, 132 {"uint256[3][][]", nil, [][][3]*big.Int{{{}}}, ""}, 133 {"uint256[3][3][3]", nil, [3][3][3]*big.Int{{{}}}, ""}, 134 {"uint8[][]", nil, [][]uint8{}, ""}, 135 {"int256", nil, big.NewInt(1), ""}, 136 {"uint8", nil, uint8(1), ""}, 137 {"uint16", nil, uint16(1), ""}, 138 {"uint32", nil, uint32(1), ""}, 139 {"uint64", nil, uint64(1), ""}, 140 {"int8", nil, int8(1), ""}, 141 {"int16", nil, int16(1), ""}, 142 {"int32", nil, int32(1), ""}, 143 {"int64", nil, int64(1), ""}, 144 {"uint24", nil, big.NewInt(1), ""}, 145 {"uint40", nil, big.NewInt(1), ""}, 146 {"uint48", nil, big.NewInt(1), ""}, 147 {"uint56", nil, big.NewInt(1), ""}, 148 {"uint72", nil, big.NewInt(1), ""}, 149 {"uint80", nil, big.NewInt(1), ""}, 150 {"uint88", nil, big.NewInt(1), ""}, 151 {"uint96", nil, big.NewInt(1), ""}, 152 {"uint104", nil, big.NewInt(1), ""}, 153 {"uint112", nil, big.NewInt(1), ""}, 154 {"uint120", nil, big.NewInt(1), ""}, 155 {"uint128", nil, big.NewInt(1), ""}, 156 {"uint136", nil, big.NewInt(1), ""}, 157 {"uint144", nil, big.NewInt(1), ""}, 158 {"uint152", nil, big.NewInt(1), ""}, 159 {"uint160", nil, big.NewInt(1), ""}, 160 {"uint168", nil, big.NewInt(1), ""}, 161 {"uint176", nil, big.NewInt(1), ""}, 162 {"uint184", nil, big.NewInt(1), ""}, 163 {"uint192", nil, big.NewInt(1), ""}, 164 {"uint200", nil, big.NewInt(1), ""}, 165 {"uint208", nil, big.NewInt(1), ""}, 166 {"uint216", nil, big.NewInt(1), ""}, 167 {"uint224", nil, big.NewInt(1), ""}, 168 {"uint232", nil, big.NewInt(1), ""}, 169 {"uint240", nil, big.NewInt(1), ""}, 170 {"uint248", nil, big.NewInt(1), ""}, 171 {"int24", nil, big.NewInt(1), ""}, 172 {"int40", nil, big.NewInt(1), ""}, 173 {"int48", nil, big.NewInt(1), ""}, 174 {"int56", nil, big.NewInt(1), ""}, 175 {"int72", nil, big.NewInt(1), ""}, 176 {"int80", nil, big.NewInt(1), ""}, 177 {"int88", nil, big.NewInt(1), ""}, 178 {"int96", nil, big.NewInt(1), ""}, 179 {"int104", nil, big.NewInt(1), ""}, 180 {"int112", nil, big.NewInt(1), ""}, 181 {"int120", nil, big.NewInt(1), ""}, 182 {"int128", nil, big.NewInt(1), ""}, 183 {"int136", nil, big.NewInt(1), ""}, 184 {"int144", nil, big.NewInt(1), ""}, 185 {"int152", nil, big.NewInt(1), ""}, 186 {"int160", nil, big.NewInt(1), ""}, 187 {"int168", nil, big.NewInt(1), ""}, 188 {"int176", nil, big.NewInt(1), ""}, 189 {"int184", nil, big.NewInt(1), ""}, 190 {"int192", nil, big.NewInt(1), ""}, 191 {"int200", nil, big.NewInt(1), ""}, 192 {"int208", nil, big.NewInt(1), ""}, 193 {"int216", nil, big.NewInt(1), ""}, 194 {"int224", nil, big.NewInt(1), ""}, 195 {"int232", nil, big.NewInt(1), ""}, 196 {"int240", nil, big.NewInt(1), ""}, 197 {"int248", nil, big.NewInt(1), ""}, 198 {"uint30", nil, uint8(1), "abi: cannot use uint8 as type ptr as argument"}, 199 {"uint8", nil, uint16(1), "abi: cannot use uint16 as type uint8 as argument"}, 200 {"uint8", nil, uint32(1), "abi: cannot use uint32 as type uint8 as argument"}, 201 {"uint8", nil, uint64(1), "abi: cannot use uint64 as type uint8 as argument"}, 202 {"uint8", nil, int8(1), "abi: cannot use int8 as type uint8 as argument"}, 203 {"uint8", nil, int16(1), "abi: cannot use int16 as type uint8 as argument"}, 204 {"uint8", nil, int32(1), "abi: cannot use int32 as type uint8 as argument"}, 205 {"uint8", nil, int64(1), "abi: cannot use int64 as type uint8 as argument"}, 206 {"uint16", nil, uint16(1), ""}, 207 {"uint16", nil, uint8(1), "abi: cannot use uint8 as type uint16 as argument"}, 208 {"uint16[]", nil, []uint16{1, 2, 3}, ""}, 209 {"uint16[]", nil, [3]uint16{1, 2, 3}, ""}, 210 {"uint16[]", nil, []uint32{1, 2, 3}, "abi: cannot use []uint32 as type [0]uint16 as argument"}, 211 {"uint16[3]", nil, [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"}, 212 {"uint16[3]", nil, [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, 213 {"uint16[3]", nil, []uint16{1, 2, 3}, ""}, 214 {"uint16[3]", nil, []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, 215 {"address[]", nil, []common.Address{{1}}, ""}, 216 {"address[1]", nil, []common.Address{{1}}, ""}, 217 {"address[1]", nil, [1]common.Address{{1}}, ""}, 218 {"address[2]", nil, [1]common.Address{{1}}, "abi: cannot use [1]array as type [2]array as argument"}, 219 {"bytes32", nil, [32]byte{}, ""}, 220 {"bytes31", nil, [31]byte{}, ""}, 221 {"bytes30", nil, [30]byte{}, ""}, 222 {"bytes29", nil, [29]byte{}, ""}, 223 {"bytes28", nil, [28]byte{}, ""}, 224 {"bytes27", nil, [27]byte{}, ""}, 225 {"bytes26", nil, [26]byte{}, ""}, 226 {"bytes25", nil, [25]byte{}, ""}, 227 {"bytes24", nil, [24]byte{}, ""}, 228 {"bytes23", nil, [23]byte{}, ""}, 229 {"bytes22", nil, [22]byte{}, ""}, 230 {"bytes21", nil, [21]byte{}, ""}, 231 {"bytes20", nil, [20]byte{}, ""}, 232 {"bytes19", nil, [19]byte{}, ""}, 233 {"bytes18", nil, [18]byte{}, ""}, 234 {"bytes17", nil, [17]byte{}, ""}, 235 {"bytes16", nil, [16]byte{}, ""}, 236 {"bytes15", nil, [15]byte{}, ""}, 237 {"bytes14", nil, [14]byte{}, ""}, 238 {"bytes13", nil, [13]byte{}, ""}, 239 {"bytes12", nil, [12]byte{}, ""}, 240 {"bytes11", nil, [11]byte{}, ""}, 241 {"bytes10", nil, [10]byte{}, ""}, 242 {"bytes9", nil, [9]byte{}, ""}, 243 {"bytes8", nil, [8]byte{}, ""}, 244 {"bytes7", nil, [7]byte{}, ""}, 245 {"bytes6", nil, [6]byte{}, ""}, 246 {"bytes5", nil, [5]byte{}, ""}, 247 {"bytes4", nil, [4]byte{}, ""}, 248 {"bytes3", nil, [3]byte{}, ""}, 249 {"bytes2", nil, [2]byte{}, ""}, 250 {"bytes1", nil, [1]byte{}, ""}, 251 {"bytes32", nil, [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"}, 252 {"bytes32", nil, common.Hash{1}, ""}, 253 {"bytes31", nil, common.Hash{1}, "abi: cannot use common.Hash as type [31]uint8 as argument"}, 254 {"bytes31", nil, [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"}, 255 {"bytes", nil, []byte{0, 1}, ""}, 256 {"bytes", nil, [2]byte{0, 1}, "abi: cannot use array as type slice as argument"}, 257 {"bytes", nil, common.Hash{1}, "abi: cannot use array as type slice as argument"}, 258 {"string", nil, "hello world", ""}, 259 {"string", nil, string(""), ""}, 260 {"string", nil, []byte{}, "abi: cannot use slice as type string as argument"}, 261 {"bytes32[]", nil, [][32]byte{{}}, ""}, 262 {"function", nil, [24]byte{}, ""}, 263 {"bytes20", nil, common.Address{}, ""}, 264 {"address", nil, [20]byte{}, ""}, 265 {"address", nil, common.Address{}, ""}, 266 {"bytes32[]]", nil, "", "invalid arg type in abi"}, 267 {"invalidType", nil, "", "unsupported arg type: invalidType"}, 268 {"invalidSlice[]", nil, "", "unsupported arg type: invalidSlice"}, 269 // simple tuple 270 {"tuple", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, struct { 271 A *big.Int 272 B *big.Int 273 }{}, ""}, 274 // tuple slice 275 {"tuple[]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct { 276 A *big.Int 277 B *big.Int 278 }{}, ""}, 279 // tuple array 280 {"tuple[2]", []ArgumentMarshaling{{Name: "a", Type: "uint256"}, {Name: "b", Type: "uint256"}}, []struct { 281 A *big.Int 282 B *big.Int 283 }{{big.NewInt(0), big.NewInt(0)}, {big.NewInt(0), big.NewInt(0)}}, ""}, 284 } { 285 typ, err := NewType(test.typ, "", test.components) 286 if err != nil && len(test.err) == 0 { 287 t.Fatal("unexpected parse error:", err) 288 } else if err != nil && len(test.err) != 0 { 289 if err.Error() != test.err { 290 t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) 291 } 292 continue 293 } 294 295 err = typeCheck(typ, reflect.ValueOf(test.input)) 296 if err != nil && len(test.err) == 0 { 297 t.Errorf("%d failed. Expected no err but got: %v", i, err) 298 continue 299 } 300 if err == nil && len(test.err) != 0 { 301 t.Errorf("%d failed. Expected err: %v but got none", i, test.err) 302 continue 303 } 304 305 if err != nil && len(test.err) != 0 && err.Error() != test.err { 306 t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) 307 } 308 } 309 }