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