accounts/abi: add bool unpack test and add a panic to readBool function

Signed-off-by: RJ Catalano <rj@monax.io>
This commit is contained in:
RJ Catalano 2017-06-16 12:23:26 -05:00
parent ee4ea6cb3f
commit 7a861d281d
No known key found for this signature in database
GPG key ID: D4AB109D9B5D6386
2 changed files with 17 additions and 3 deletions

View file

@ -112,6 +112,13 @@ func TestSimpleMethodUnpack(t *testing.T) {
outVar string // the output variable (e.g. uint32, *big.Int, etc) outVar string // the output variable (e.g. uint32, *big.Int, etc)
err string // empty or error if expected err string // empty or error if expected
}{ }{
{
`[ { "type": "bool" } ]`,
pad([]byte{1}, 32, true),
bool(true),
"bool",
"",
},
{ {
`[ { "type": "uint32" } ]`, `[ { "type": "uint32" } ]`,
pad([]byte{1}, 32, true), pad([]byte{1}, 32, true),
@ -215,6 +222,10 @@ func TestSimpleMethodUnpack(t *testing.T) {
var outvar interface{} var outvar interface{}
switch test.outVar { switch test.outVar {
case "bool":
var v bool
err = abi.Unpack(&v, "method", test.marshalledOutput)
outvar = v
case "uint8": case "uint8":
var v uint8 var v uint8
err = abi.Unpack(&v, "method", test.marshalledOutput) err = abi.Unpack(&v, "method", test.marshalledOutput)

View file

@ -118,7 +118,7 @@ func toGoSlice(i int, t Argument, output []byte) (interface{}, error) {
case IntTy, UintTy: case IntTy, UintTy:
inter = readInteger(t.Type.Kind, returnOutput) inter = readInteger(t.Type.Kind, returnOutput)
case BoolTy: case BoolTy:
inter, err = getBoolValue(returnOutput) inter, err = readBool(returnOutput)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -160,7 +160,10 @@ func readInteger(kind reflect.Kind, b []byte) interface{} {
} }
} }
func getBoolValue(word []byte) (bool, error) { func readBool(word []byte) (bool, error) {
if len(word) != 32 {
panic("abi: fatal error. Incorrect word length.")
}
improperEncoding := "abi: improperly encoded boolean value" improperEncoding := "abi: improperly encoded boolean value"
for i, b := range word { for i, b := range word {
if b != 0 && i != 31 { if b != 0 && i != 31 {
@ -218,7 +221,7 @@ func toGoType(i int, t Argument, output []byte) (interface{}, error) {
case IntTy, UintTy: case IntTy, UintTy:
return readInteger(t.Type.Kind, returnOutput), nil return readInteger(t.Type.Kind, returnOutput), nil
case BoolTy: case BoolTy:
return getBoolValue(returnOutput) return readBool(returnOutput)
case AddressTy: case AddressTy:
return common.BytesToAddress(returnOutput), nil return common.BytesToAddress(returnOutput), nil
case HashTy: case HashTy: