github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/accounts/abi/type_test.go (about) 1 // Copyright 2016 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum 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 Spectrum 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 Spectrum 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/SmartMeshFoundation/Spectrum/common" 25 "github.com/davecgh/go-spew/spew" 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 kind Type 37 }{ 38 {"bool", Type{Kind: reflect.Bool, T: BoolTy, Type: reflect.TypeOf(bool(false)), stringKind: "bool"}}, 39 {"bool[]", 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[]"}}, 40 {"bool[2]", 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]"}}, 41 {"bool[2][]", 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][]"}}, 42 {"bool[][]", 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[][]"}}, 43 {"bool[][2]", 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]"}}, 44 {"bool[2][2]", 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]"}}, 45 {"bool[2][][2]", 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]"}}, 46 {"bool[2][2][2]", 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]"}}, 47 {"bool[][][]", 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[][][]"}}, 48 {"bool[][2][]", 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][]"}}, 49 {"int8", Type{Kind: reflect.Int8, Type: int8_t, Size: 8, T: IntTy, stringKind: "int8"}}, 50 {"int16", Type{Kind: reflect.Int16, Type: int16_t, Size: 16, T: IntTy, stringKind: "int16"}}, 51 {"int32", Type{Kind: reflect.Int32, Type: int32_t, Size: 32, T: IntTy, stringKind: "int32"}}, 52 {"int64", Type{Kind: reflect.Int64, Type: int64_t, Size: 64, T: IntTy, stringKind: "int64"}}, 53 {"int256", Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, stringKind: "int256"}}, 54 {"int8[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int8{}), Elem: &Type{Kind: reflect.Int8, Type: int8_t, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[]"}}, 55 {"int8[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int8{}), Elem: &Type{Kind: reflect.Int8, Type: int8_t, Size: 8, T: IntTy, stringKind: "int8"}, stringKind: "int8[2]"}}, 56 {"int16[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int16{}), Elem: &Type{Kind: reflect.Int16, Type: int16_t, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[]"}}, 57 {"int16[2]", Type{Size: 2, Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]int16{}), Elem: &Type{Kind: reflect.Int16, Type: int16_t, Size: 16, T: IntTy, stringKind: "int16"}, stringKind: "int16[2]"}}, 58 {"int32[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int32{}), Elem: &Type{Kind: reflect.Int32, Type: int32_t, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[]"}}, 59 {"int32[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int32{}), Elem: &Type{Kind: reflect.Int32, Type: int32_t, Size: 32, T: IntTy, stringKind: "int32"}, stringKind: "int32[2]"}}, 60 {"int64[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]int64{}), Elem: &Type{Kind: reflect.Int64, Type: int64_t, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[]"}}, 61 {"int64[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]int64{}), Elem: &Type{Kind: reflect.Int64, Type: int64_t, Size: 64, T: IntTy, stringKind: "int64"}, stringKind: "int64[2]"}}, 62 {"int256[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[]"}}, 63 {"int256[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: IntTy, stringKind: "int256"}, stringKind: "int256[2]"}}, 64 {"uint8", Type{Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, stringKind: "uint8"}}, 65 {"uint16", Type{Kind: reflect.Uint16, Type: uint16_t, Size: 16, T: UintTy, stringKind: "uint16"}}, 66 {"uint32", Type{Kind: reflect.Uint32, Type: uint32_t, Size: 32, T: UintTy, stringKind: "uint32"}}, 67 {"uint64", Type{Kind: reflect.Uint64, Type: uint64_t, Size: 64, T: UintTy, stringKind: "uint64"}}, 68 {"uint256", Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: UintTy, stringKind: "uint256"}}, 69 {"uint8[]", Type{Kind: reflect.Slice, T: SliceTy, Type: reflect.TypeOf([]uint8{}), Elem: &Type{Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[]"}}, 70 {"uint8[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint8{}), Elem: &Type{Kind: reflect.Uint8, Type: uint8_t, Size: 8, T: UintTy, stringKind: "uint8"}, stringKind: "uint8[2]"}}, 71 {"uint16[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint16{}), Elem: &Type{Kind: reflect.Uint16, Type: uint16_t, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[]"}}, 72 {"uint16[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint16{}), Elem: &Type{Kind: reflect.Uint16, Type: uint16_t, Size: 16, T: UintTy, stringKind: "uint16"}, stringKind: "uint16[2]"}}, 73 {"uint32[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint32{}), Elem: &Type{Kind: reflect.Uint32, Type: uint32_t, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[]"}}, 74 {"uint32[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint32{}), Elem: &Type{Kind: reflect.Uint32, Type: uint32_t, Size: 32, T: UintTy, stringKind: "uint32"}, stringKind: "uint32[2]"}}, 75 {"uint64[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]uint64{}), Elem: &Type{Kind: reflect.Uint64, Type: uint64_t, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[]"}}, 76 {"uint64[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]uint64{}), Elem: &Type{Kind: reflect.Uint64, Type: uint64_t, Size: 64, T: UintTy, stringKind: "uint64"}, stringKind: "uint64[2]"}}, 77 {"uint256[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]*big.Int{}), Elem: &Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[]"}}, 78 {"uint256[2]", Type{Kind: reflect.Array, T: ArrayTy, Type: reflect.TypeOf([2]*big.Int{}), Size: 2, Elem: &Type{Kind: reflect.Ptr, Type: big_t, Size: 256, T: UintTy, stringKind: "uint256"}, stringKind: "uint256[2]"}}, 79 {"bytes32", Type{Kind: reflect.Array, T: FixedBytesTy, Size: 32, Type: reflect.TypeOf([32]byte{}), stringKind: "bytes32"}}, 80 {"bytes[]", 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[]"}}, 81 {"bytes[2]", 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]"}}, 82 {"bytes32[]", 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[]"}}, 83 {"bytes32[2]", 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]"}}, 84 {"string", Type{Kind: reflect.String, T: StringTy, Type: reflect.TypeOf(""), stringKind: "string"}}, 85 {"string[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]string{}), Elem: &Type{Kind: reflect.String, Type: reflect.TypeOf(""), T: StringTy, stringKind: "string"}, stringKind: "string[]"}}, 86 {"string[2]", 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]"}}, 87 {"address", Type{Kind: reflect.Array, Type: address_t, Size: 20, T: AddressTy, stringKind: "address"}}, 88 {"address[]", Type{T: SliceTy, Kind: reflect.Slice, Type: reflect.TypeOf([]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: address_t, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[]"}}, 89 {"address[2]", Type{Kind: reflect.Array, T: ArrayTy, Size: 2, Type: reflect.TypeOf([2]common.Address{}), Elem: &Type{Kind: reflect.Array, Type: address_t, Size: 20, T: AddressTy, stringKind: "address"}, stringKind: "address[2]"}}, 90 // TODO when fixed types are implemented properly 91 // {"fixed", Type{}}, 92 // {"fixed128x128", Type{}}, 93 // {"fixed[]", Type{}}, 94 // {"fixed[2]", Type{}}, 95 // {"fixed128x128[]", Type{}}, 96 // {"fixed128x128[2]", Type{}}, 97 } 98 99 for _, tt := range tests { 100 typ, err := NewType(tt.blob) 101 if err != nil { 102 t.Errorf("type %q: failed to parse type string: %v", tt.blob, err) 103 } 104 if !reflect.DeepEqual(typ, tt.kind) { 105 t.Errorf("type %q: parsed type mismatch:\nGOT %s\nWANT %s ", tt.blob, spew.Sdump(typeWithoutStringer(typ)), spew.Sdump(typeWithoutStringer(tt.kind))) 106 } 107 } 108 } 109 110 func TestTypeCheck(t *testing.T) { 111 for i, test := range []struct { 112 typ string 113 input interface{} 114 err string 115 }{ 116 {"uint", big.NewInt(1), "unsupported arg type: uint"}, 117 {"int", big.NewInt(1), "unsupported arg type: int"}, 118 {"uint256", big.NewInt(1), ""}, 119 {"uint256[][3][]", [][3][]*big.Int{{{}}}, ""}, 120 {"uint256[][][3]", [3][][]*big.Int{{{}}}, ""}, 121 {"uint256[3][][]", [][][3]*big.Int{{{}}}, ""}, 122 {"uint256[3][3][3]", [3][3][3]*big.Int{{{}}}, ""}, 123 {"uint8[][]", [][]uint8{}, ""}, 124 {"int256", big.NewInt(1), ""}, 125 {"uint8", uint8(1), ""}, 126 {"uint16", uint16(1), ""}, 127 {"uint32", uint32(1), ""}, 128 {"uint64", uint64(1), ""}, 129 {"int8", int8(1), ""}, 130 {"int16", int16(1), ""}, 131 {"int32", int32(1), ""}, 132 {"int64", int64(1), ""}, 133 {"uint24", big.NewInt(1), ""}, 134 {"uint40", big.NewInt(1), ""}, 135 {"uint48", big.NewInt(1), ""}, 136 {"uint56", big.NewInt(1), ""}, 137 {"uint72", big.NewInt(1), ""}, 138 {"uint80", big.NewInt(1), ""}, 139 {"uint88", big.NewInt(1), ""}, 140 {"uint96", big.NewInt(1), ""}, 141 {"uint104", big.NewInt(1), ""}, 142 {"uint112", big.NewInt(1), ""}, 143 {"uint120", big.NewInt(1), ""}, 144 {"uint128", big.NewInt(1), ""}, 145 {"uint136", big.NewInt(1), ""}, 146 {"uint144", big.NewInt(1), ""}, 147 {"uint152", big.NewInt(1), ""}, 148 {"uint160", big.NewInt(1), ""}, 149 {"uint168", big.NewInt(1), ""}, 150 {"uint176", big.NewInt(1), ""}, 151 {"uint184", big.NewInt(1), ""}, 152 {"uint192", big.NewInt(1), ""}, 153 {"uint200", big.NewInt(1), ""}, 154 {"uint208", big.NewInt(1), ""}, 155 {"uint216", big.NewInt(1), ""}, 156 {"uint224", big.NewInt(1), ""}, 157 {"uint232", big.NewInt(1), ""}, 158 {"uint240", big.NewInt(1), ""}, 159 {"uint248", big.NewInt(1), ""}, 160 {"int24", big.NewInt(1), ""}, 161 {"int40", big.NewInt(1), ""}, 162 {"int48", big.NewInt(1), ""}, 163 {"int56", big.NewInt(1), ""}, 164 {"int72", big.NewInt(1), ""}, 165 {"int80", big.NewInt(1), ""}, 166 {"int88", big.NewInt(1), ""}, 167 {"int96", big.NewInt(1), ""}, 168 {"int104", big.NewInt(1), ""}, 169 {"int112", big.NewInt(1), ""}, 170 {"int120", big.NewInt(1), ""}, 171 {"int128", big.NewInt(1), ""}, 172 {"int136", big.NewInt(1), ""}, 173 {"int144", big.NewInt(1), ""}, 174 {"int152", big.NewInt(1), ""}, 175 {"int160", big.NewInt(1), ""}, 176 {"int168", big.NewInt(1), ""}, 177 {"int176", big.NewInt(1), ""}, 178 {"int184", big.NewInt(1), ""}, 179 {"int192", big.NewInt(1), ""}, 180 {"int200", big.NewInt(1), ""}, 181 {"int208", big.NewInt(1), ""}, 182 {"int216", big.NewInt(1), ""}, 183 {"int224", big.NewInt(1), ""}, 184 {"int232", big.NewInt(1), ""}, 185 {"int240", big.NewInt(1), ""}, 186 {"int248", big.NewInt(1), ""}, 187 {"uint30", uint8(1), "abi: cannot use uint8 as type ptr as argument"}, 188 {"uint8", uint16(1), "abi: cannot use uint16 as type uint8 as argument"}, 189 {"uint8", uint32(1), "abi: cannot use uint32 as type uint8 as argument"}, 190 {"uint8", uint64(1), "abi: cannot use uint64 as type uint8 as argument"}, 191 {"uint8", int8(1), "abi: cannot use int8 as type uint8 as argument"}, 192 {"uint8", int16(1), "abi: cannot use int16 as type uint8 as argument"}, 193 {"uint8", int32(1), "abi: cannot use int32 as type uint8 as argument"}, 194 {"uint8", int64(1), "abi: cannot use int64 as type uint8 as argument"}, 195 {"uint16", uint16(1), ""}, 196 {"uint16", uint8(1), "abi: cannot use uint8 as type uint16 as argument"}, 197 {"uint16[]", []uint16{1, 2, 3}, ""}, 198 {"uint16[]", [3]uint16{1, 2, 3}, ""}, 199 {"uint16[]", []uint32{1, 2, 3}, "abi: cannot use []uint32 as type [0]uint16 as argument"}, 200 {"uint16[3]", [3]uint32{1, 2, 3}, "abi: cannot use [3]uint32 as type [3]uint16 as argument"}, 201 {"uint16[3]", [4]uint16{1, 2, 3}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, 202 {"uint16[3]", []uint16{1, 2, 3}, ""}, 203 {"uint16[3]", []uint16{1, 2, 3, 4}, "abi: cannot use [4]uint16 as type [3]uint16 as argument"}, 204 {"address[]", []common.Address{{1}}, ""}, 205 {"address[1]", []common.Address{{1}}, ""}, 206 {"address[1]", [1]common.Address{{1}}, ""}, 207 {"address[2]", [1]common.Address{{1}}, "abi: cannot use [1]array as type [2]array as argument"}, 208 {"bytes32", [32]byte{}, ""}, 209 {"bytes31", [31]byte{}, ""}, 210 {"bytes30", [30]byte{}, ""}, 211 {"bytes29", [29]byte{}, ""}, 212 {"bytes28", [28]byte{}, ""}, 213 {"bytes27", [27]byte{}, ""}, 214 {"bytes26", [26]byte{}, ""}, 215 {"bytes25", [25]byte{}, ""}, 216 {"bytes24", [24]byte{}, ""}, 217 {"bytes23", [23]byte{}, ""}, 218 {"bytes22", [22]byte{}, ""}, 219 {"bytes21", [21]byte{}, ""}, 220 {"bytes20", [20]byte{}, ""}, 221 {"bytes19", [19]byte{}, ""}, 222 {"bytes18", [18]byte{}, ""}, 223 {"bytes17", [17]byte{}, ""}, 224 {"bytes16", [16]byte{}, ""}, 225 {"bytes15", [15]byte{}, ""}, 226 {"bytes14", [14]byte{}, ""}, 227 {"bytes13", [13]byte{}, ""}, 228 {"bytes12", [12]byte{}, ""}, 229 {"bytes11", [11]byte{}, ""}, 230 {"bytes10", [10]byte{}, ""}, 231 {"bytes9", [9]byte{}, ""}, 232 {"bytes8", [8]byte{}, ""}, 233 {"bytes7", [7]byte{}, ""}, 234 {"bytes6", [6]byte{}, ""}, 235 {"bytes5", [5]byte{}, ""}, 236 {"bytes4", [4]byte{}, ""}, 237 {"bytes3", [3]byte{}, ""}, 238 {"bytes2", [2]byte{}, ""}, 239 {"bytes1", [1]byte{}, ""}, 240 {"bytes32", [33]byte{}, "abi: cannot use [33]uint8 as type [32]uint8 as argument"}, 241 {"bytes32", common.Hash{1}, ""}, 242 {"bytes31", common.Hash{1}, "abi: cannot use common.Hash as type [31]uint8 as argument"}, 243 {"bytes31", [32]byte{}, "abi: cannot use [32]uint8 as type [31]uint8 as argument"}, 244 {"bytes", []byte{0, 1}, ""}, 245 {"bytes", [2]byte{0, 1}, "abi: cannot use array as type slice as argument"}, 246 {"bytes", common.Hash{1}, "abi: cannot use array as type slice as argument"}, 247 {"string", "hello world", ""}, 248 {"string", string(""), ""}, 249 {"string", []byte{}, "abi: cannot use slice as type string as argument"}, 250 {"bytes32[]", [][32]byte{{}}, ""}, 251 {"function", [24]byte{}, ""}, 252 {"bytes20", common.Address{}, ""}, 253 {"address", [20]byte{}, ""}, 254 {"address", common.Address{}, ""}, 255 } { 256 typ, err := NewType(test.typ) 257 if err != nil && len(test.err) == 0 { 258 t.Fatal("unexpected parse error:", err) 259 } else if err != nil && len(test.err) != 0 { 260 if err.Error() != test.err { 261 t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) 262 } 263 continue 264 } 265 266 err = typeCheck(typ, reflect.ValueOf(test.input)) 267 if err != nil && len(test.err) == 0 { 268 t.Errorf("%d failed. Expected no err but got: %v", i, err) 269 continue 270 } 271 if err == nil && len(test.err) != 0 { 272 t.Errorf("%d failed. Expected err: %v but got none", i, test.err) 273 continue 274 } 275 276 if err != nil && len(test.err) != 0 && err.Error() != test.err { 277 t.Errorf("%d failed. Expected err: '%v' got err: '%v'", i, test.err, err) 278 } 279 } 280 }