plugeth/accounts/abi/unpack_test.go

877 lines
36 KiB
Go
Raw Normal View History

// Copyright 2017 The go-ethereum Authors
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package abi
import (
"bytes"
"encoding/hex"
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
"fmt"
"math/big"
"reflect"
"strconv"
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
"strings"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/require"
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
)
type unpackTest struct {
def string // ABI definition JSON
enc string // evm return data
want interface{} // the expected output
err string // empty or error if expected
}
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
func (test unpackTest) checkError(err error) error {
if err != nil {
if len(test.err) == 0 {
return fmt.Errorf("expected no err but got: %v", err)
} else if err.Error() != test.err {
return fmt.Errorf("expected err: '%v' got err: %q", test.err, err)
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
}
} else if len(test.err) > 0 {
return fmt.Errorf("expected err: %v but got none", test.err)
}
return nil
}
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
var unpackTests = []unpackTest{
{
def: `[{ "type": "bool" }]`,
enc: "0000000000000000000000000000000000000000000000000000000000000001",
want: true,
},
{
def: `[{ "type": "bool" }]`,
enc: "0000000000000000000000000000000000000000000000000000000000000000",
want: false,
},
{
def: `[{ "type": "bool" }]`,
enc: "0000000000000000000000000000000000000000000000000001000000000001",
want: false,
err: "abi: improperly encoded boolean value",
},
{
def: `[{ "type": "bool" }]`,
enc: "0000000000000000000000000000000000000000000000000000000000000003",
want: false,
err: "abi: improperly encoded boolean value",
},
{
def: `[{"type": "uint32"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000001",
want: uint32(1),
},
{
def: `[{"type": "uint32"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000001",
want: uint16(0),
err: "abi: cannot unmarshal uint32 in to uint16",
},
{
def: `[{"type": "uint17"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000001",
want: uint16(0),
err: "abi: cannot unmarshal *big.Int in to uint16",
},
{
def: `[{"type": "uint17"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000001",
want: big.NewInt(1),
},
{
def: `[{"type": "int32"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000001",
want: int32(1),
},
{
def: `[{"type": "int32"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000001",
want: int16(0),
err: "abi: cannot unmarshal int32 in to int16",
},
{
def: `[{"type": "int17"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000001",
want: int16(0),
err: "abi: cannot unmarshal *big.Int in to int16",
},
{
def: `[{"type": "int17"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000001",
want: big.NewInt(1),
},
{
def: `[{"type": "int256"}]`,
enc: "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
want: big.NewInt(-1),
},
{
def: `[{"type": "address"}]`,
enc: "0000000000000000000000000100000000000000000000000000000000000000",
want: common.Address{1},
},
{
def: `[{"type": "bytes32"}]`,
enc: "0100000000000000000000000000000000000000000000000000000000000000",
want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
},
{
def: `[{"type": "bytes"}]`,
enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
want: common.Hex2Bytes("0100000000000000000000000000000000000000000000000000000000000000"),
},
{
def: `[{"type": "bytes"}]`,
enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
want: [32]byte{},
err: "abi: cannot unmarshal []uint8 in to [32]uint8",
},
{
def: `[{"type": "bytes32"}]`,
enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200100000000000000000000000000000000000000000000000000000000000000",
want: []byte(nil),
err: "abi: cannot unmarshal [32]uint8 in to []uint8",
},
{
def: `[{"type": "bytes32"}]`,
enc: "0100000000000000000000000000000000000000000000000000000000000000",
want: [32]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
},
{
def: `[{"type": "function"}]`,
enc: "0100000000000000000000000000000000000000000000000000000000000000",
want: [24]byte{1},
},
// slices
{
def: `[{"type": "uint8[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: []uint8{1, 2},
},
{
def: `[{"type": "uint8[2]"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: [2]uint8{1, 2},
},
// multi dimensional, if these pass, all types that don't require length prefix should pass
{
def: `[{"type": "uint8[][]"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000E0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: [][]uint8{{1, 2}, {1, 2}},
},
{
def: `[{"type": "uint8[2][2]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: [2][2]uint8{{1, 2}, {1, 2}},
},
{
def: `[{"type": "uint8[][2]"}]`,
enc: "000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001",
want: [2][]uint8{{1}, {1}},
},
{
def: `[{"type": "uint8[2][]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: [][2]uint8{{1, 2}},
},
{
def: `[{"type": "uint8[2][]"}]`,
enc: "000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: [][2]uint8{{1, 2}, {1, 2}},
},
{
def: `[{"type": "uint16[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: []uint16{1, 2},
},
{
def: `[{"type": "uint16[2]"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: [2]uint16{1, 2},
},
{
def: `[{"type": "uint32[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: []uint32{1, 2},
},
{
def: `[{"type": "uint32[2]"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: [2]uint32{1, 2},
},
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676) * accounts/abi/bind: support for multi-dim arrays Also: - reduce usage of regexes a bit. - fix minor Java syntax problems Fixes #15648 * accounts/abi/bind: Add some more documentation * accounts/abi/bind: Improve code readability * accounts/abi: bugfix for unpacking nested arrays The code previously assumed the arrays/slices were always 1 level deep. While the packing supports nested arrays (!!!). The current code for unpacking doesn't return the "consumed" length, so this fix had to work around that by calculating it (i.e. packing and getting resulting length) after the unpacking of the array element. It's far from ideal, but unpacking behaviour is fixed now. * accounts/abi: Fix unpacking of nested arrays Removed the temporary workaround of packing to calculate size, which was incorrect for slice-like types anyway. Full size of nested arrays is used now. * accounts/abi: deeply nested array unpack test Test unpacking of an array nested more than one level. * accounts/abi: Add deeply nested array pack test Same as the deep nested array unpack test, but the other way around. * accounts/abi/bind: deeply nested arrays bind test Test the usage of bindings that were generated for methods with multi-dimensional (and not just a single extra dimension, like foo[2][3]) array arguments and returns. edit: trigger rebuild, CI failed to fetch linter module. * accounts/abi/bind: improve array binding wrapArray uses a regex now, and arrayBindingJava is improved. * accounts/abi: Improve naming of element size func The full step size for unpacking an array is now retrieved with "getFullElemSize". * accounts/abi: support nested nested array args Previously, the code only considered the outer-size of the array, ignoring the size of the contents. This was fine for most types, but nested arrays are packed directly into it, and count towards the total size. This resulted in arguments following a nested array to replicate some of the binary contents of the array. The fix: for arrays, calculate their complete contents size: count the arg.Type.Elem.Size when Elem is an Array, and repeat when their child is an array too, etc. The count is the number of 32 byte elements, similar to how it previously counted, but nested. * accounts/abi: Test deep nested arr multi-arguments Arguments with a deeply nested array should not cause the next arguments to be read from the wrong position.
2018-03-04 22:24:17 +00:00
{
def: `[{"type": "uint32[2][3][4]"}]`,
enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000015000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000170000000000000000000000000000000000000000000000000000000000000018",
want: [4][3][2]uint32{{{1, 2}, {3, 4}, {5, 6}}, {{7, 8}, {9, 10}, {11, 12}}, {{13, 14}, {15, 16}, {17, 18}}, {{19, 20}, {21, 22}, {23, 24}}},
},
{
def: `[{"type": "uint64[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: []uint64{1, 2},
},
{
def: `[{"type": "uint64[2]"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: [2]uint64{1, 2},
},
{
def: `[{"type": "uint256[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: []*big.Int{big.NewInt(1), big.NewInt(2)},
},
{
def: `[{"type": "uint256[3]"}]`,
enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
},
{
def: `[{"type": "int8[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: []int8{1, 2},
},
{
def: `[{"type": "int8[2]"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: [2]int8{1, 2},
},
{
def: `[{"type": "int16[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: []int16{1, 2},
},
{
def: `[{"type": "int16[2]"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: [2]int16{1, 2},
},
{
def: `[{"type": "int32[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: []int32{1, 2},
},
{
def: `[{"type": "int32[2]"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: [2]int32{1, 2},
},
{
def: `[{"type": "int64[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: []int64{1, 2},
},
{
def: `[{"type": "int64[2]"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: [2]int64{1, 2},
},
{
def: `[{"type": "int256[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: []*big.Int{big.NewInt(1), big.NewInt(2)},
},
{
def: `[{"type": "int256[3]"}]`,
enc: "000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003",
want: [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)},
},
// struct outputs
{
def: `[{"name":"int1","type":"int256"},{"name":"int2","type":"int256"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: struct {
Int1 *big.Int
Int2 *big.Int
}{big.NewInt(1), big.NewInt(2)},
},
{
def: `[{"name":"int","type":"int256"},{"name":"Int","type":"int256"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: struct {
Int1 *big.Int
Int2 *big.Int
}{},
err: "abi: multiple outputs mapping to the same struct field 'Int'",
},
{
def: `[{"name":"int","type":"int256"},{"name":"_int","type":"int256"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: struct {
Int1 *big.Int
Int2 *big.Int
}{},
err: "abi: multiple outputs mapping to the same struct field 'Int'",
},
{
def: `[{"name":"Int","type":"int256"},{"name":"_int","type":"int256"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: struct {
Int1 *big.Int
Int2 *big.Int
}{},
err: "abi: multiple outputs mapping to the same struct field 'Int'",
},
{
def: `[{"name":"Int","type":"int256"},{"name":"_","type":"int256"}]`,
enc: "00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002",
want: struct {
Int1 *big.Int
Int2 *big.Int
}{},
err: "abi: purely underscored output cannot unpack to struct",
},
}
func TestUnpack(t *testing.T) {
for i, test := range unpackTests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
abi, err := JSON(strings.NewReader(def))
if err != nil {
t.Fatalf("invalid ABI definition %s: %v", def, err)
}
encb, err := hex.DecodeString(test.enc)
if err != nil {
t.Fatalf("invalid hex: %s" + test.enc)
}
outptr := reflect.New(reflect.TypeOf(test.want))
err = abi.Unpack(outptr.Interface(), "method", encb)
if err := test.checkError(err); err != nil {
t.Errorf("test %d (%v) failed: %v", i, test.def, err)
return
}
out := outptr.Elem().Interface()
if !reflect.DeepEqual(test.want, out) {
t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out)
}
})
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
}
}
func TestUnpackSetDynamicArrayOutput(t *testing.T) {
abi, err := JSON(strings.NewReader(`[{"constant":true,"inputs":[],"name":"testDynamicFixedBytes15","outputs":[{"name":"","type":"bytes15[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"testDynamicFixedBytes32","outputs":[{"name":"","type":"bytes32[]"}],"payable":false,"stateMutability":"view","type":"function"}]`))
if err != nil {
t.Fatal(err)
}
var (
marshalledReturn32 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783132333435363738393000000000000000000000000000000000000000003078303938373635343332310000000000000000000000000000000000000000")
marshalledReturn15 = common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000230783031323334350000000000000000000000000000000000000000000000003078393837363534000000000000000000000000000000000000000000000000")
out32 [][32]byte
out15 [][15]byte
)
// test 32
err = abi.Unpack(&out32, "testDynamicFixedBytes32", marshalledReturn32)
if err != nil {
t.Fatal(err)
}
if len(out32) != 2 {
t.Fatalf("expected array with 2 values, got %d", len(out32))
}
expected := common.Hex2Bytes("3078313233343536373839300000000000000000000000000000000000000000")
if !bytes.Equal(out32[0][:], expected) {
t.Errorf("expected %x, got %x\n", expected, out32[0])
}
expected = common.Hex2Bytes("3078303938373635343332310000000000000000000000000000000000000000")
if !bytes.Equal(out32[1][:], expected) {
t.Errorf("expected %x, got %x\n", expected, out32[1])
}
// test 15
err = abi.Unpack(&out15, "testDynamicFixedBytes32", marshalledReturn15)
if err != nil {
t.Fatal(err)
}
if len(out15) != 2 {
t.Fatalf("expected array with 2 values, got %d", len(out15))
}
expected = common.Hex2Bytes("307830313233343500000000000000")
if !bytes.Equal(out15[0][:], expected) {
t.Errorf("expected %x, got %x\n", expected, out15[0])
}
expected = common.Hex2Bytes("307839383736353400000000000000")
if !bytes.Equal(out15[1][:], expected) {
t.Errorf("expected %x, got %x\n", expected, out15[1])
}
}
type methodMultiOutput struct {
Int *big.Int
String string
}
func methodMultiReturn(require *require.Assertions) (ABI, []byte, methodMultiOutput) {
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
const definition = `[
{ "name" : "multi", "constant" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
var expected = methodMultiOutput{big.NewInt(1), "hello"}
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
abi, err := JSON(strings.NewReader(definition))
require.NoError(err)
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
// using buff to make the code readable
buff := new(bytes.Buffer)
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
buff.Write(common.RightPadBytes([]byte(expected.String), 32))
return abi, buff.Bytes(), expected
}
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
func TestMethodMultiReturn(t *testing.T) {
type reversed struct {
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
String string
Int *big.Int
}
abi, data, expected := methodMultiReturn(require.New(t))
bigint := new(big.Int)
var testCases = []struct {
dest interface{}
expected interface{}
error string
name string
}{{
&methodMultiOutput{},
&expected,
"",
"Can unpack into structure",
}, {
&reversed{},
&reversed{expected.String, expected.Int},
"",
"Can unpack into reversed structure",
}, {
&[]interface{}{&bigint, new(string)},
&[]interface{}{&expected.Int, &expected.String},
"",
"Can unpack into a slice",
}, {
&[2]interface{}{&bigint, new(string)},
&[2]interface{}{&expected.Int, &expected.String},
"",
"Can unpack into an array",
}, {
&[]interface{}{new(int), new(int)},
&[]interface{}{&expected.Int, &expected.String},
"abi: cannot unmarshal *big.Int in to int",
"Can not unpack into a slice with wrong types",
}, {
&[]interface{}{new(int)},
&[]interface{}{},
"abi: insufficient number of elements in the list/array for unpack, want 2, got 1",
"Can not unpack into a slice with wrong types",
}}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
require := require.New(t)
err := abi.Unpack(tc.dest, "multi", data)
if tc.error == "" {
require.Nil(err, "Should be able to unpack method outputs.")
require.Equal(tc.expected, tc.dest)
} else {
require.EqualError(err, tc.error)
}
})
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
}
}
func TestMultiReturnWithArray(t *testing.T) {
const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
abi, err := JSON(strings.NewReader(definition))
if err != nil {
t.Fatal(err)
}
buff := new(bytes.Buffer)
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000090000000000000000000000000000000000000000000000000000000000000009"))
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000008"))
ret1, ret1Exp := new([3]uint64), [3]uint64{9, 9, 9}
ret2, ret2Exp := new(uint64), uint64(8)
if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
accounts/abi: Abi binding support for nested arrays, fixes #15648, including nested array unpack fix (#15676) * accounts/abi/bind: support for multi-dim arrays Also: - reduce usage of regexes a bit. - fix minor Java syntax problems Fixes #15648 * accounts/abi/bind: Add some more documentation * accounts/abi/bind: Improve code readability * accounts/abi: bugfix for unpacking nested arrays The code previously assumed the arrays/slices were always 1 level deep. While the packing supports nested arrays (!!!). The current code for unpacking doesn't return the "consumed" length, so this fix had to work around that by calculating it (i.e. packing and getting resulting length) after the unpacking of the array element. It's far from ideal, but unpacking behaviour is fixed now. * accounts/abi: Fix unpacking of nested arrays Removed the temporary workaround of packing to calculate size, which was incorrect for slice-like types anyway. Full size of nested arrays is used now. * accounts/abi: deeply nested array unpack test Test unpacking of an array nested more than one level. * accounts/abi: Add deeply nested array pack test Same as the deep nested array unpack test, but the other way around. * accounts/abi/bind: deeply nested arrays bind test Test the usage of bindings that were generated for methods with multi-dimensional (and not just a single extra dimension, like foo[2][3]) array arguments and returns. edit: trigger rebuild, CI failed to fetch linter module. * accounts/abi/bind: improve array binding wrapArray uses a regex now, and arrayBindingJava is improved. * accounts/abi: Improve naming of element size func The full step size for unpacking an array is now retrieved with "getFullElemSize". * accounts/abi: support nested nested array args Previously, the code only considered the outer-size of the array, ignoring the size of the contents. This was fine for most types, but nested arrays are packed directly into it, and count towards the total size. This resulted in arguments following a nested array to replicate some of the binary contents of the array. The fix: for arrays, calculate their complete contents size: count the arg.Type.Elem.Size when Elem is an Array, and repeat when their child is an array too, etc. The count is the number of 32 byte elements, similar to how it previously counted, but nested. * accounts/abi: Test deep nested arr multi-arguments Arguments with a deeply nested array should not cause the next arguments to be read from the wrong position.
2018-03-04 22:24:17 +00:00
t.Fatal(err)
}
if !reflect.DeepEqual(*ret1, ret1Exp) {
t.Error("array result", *ret1, "!= Expected", ret1Exp)
}
if *ret2 != ret2Exp {
t.Error("int result", *ret2, "!= Expected", ret2Exp)
}
}
func TestMultiReturnWithDeeplyNestedArray(t *testing.T) {
// Similar to TestMultiReturnWithArray, but with a special case in mind:
// values of nested static arrays count towards the size as well, and any element following
// after such nested array argument should be read with the correct offset,
// so that it does not read content from the previous array argument.
const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3][2][4]"}, {"type": "uint64"}]}]`
abi, err := JSON(strings.NewReader(definition))
if err != nil {
t.Fatal(err)
}
buff := new(bytes.Buffer)
// construct the test array, each 3 char element is joined with 61 '0' chars,
// to from the ((3 + 61) * 0.5) = 32 byte elements in the array.
buff.Write(common.Hex2Bytes(strings.Join([]string{
"", //empty, to apply the 61-char separator to the first element as well.
"111", "112", "113", "121", "122", "123",
"211", "212", "213", "221", "222", "223",
"311", "312", "313", "321", "322", "323",
"411", "412", "413", "421", "422", "423",
}, "0000000000000000000000000000000000000000000000000000000000000")))
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000009876"))
ret1, ret1Exp := new([4][2][3]uint64), [4][2][3]uint64{
{{0x111, 0x112, 0x113}, {0x121, 0x122, 0x123}},
{{0x211, 0x212, 0x213}, {0x221, 0x222, 0x223}},
{{0x311, 0x312, 0x313}, {0x321, 0x322, 0x323}},
{{0x411, 0x412, 0x413}, {0x421, 0x422, 0x423}},
}
ret2, ret2Exp := new(uint64), uint64(0x9876)
if err := abi.Unpack(&[]interface{}{ret1, ret2}, "multi", buff.Bytes()); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(*ret1, ret1Exp) {
t.Error("array result", *ret1, "!= Expected", ret1Exp)
}
if *ret2 != ret2Exp {
t.Error("int result", *ret2, "!= Expected", ret2Exp)
}
}
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
func TestUnmarshal(t *testing.T) {
const definition = `[
{ "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
{ "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
{ "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
{ "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
{ "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
{ "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
{ "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
{ "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
{ "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
abi, err := JSON(strings.NewReader(definition))
if err != nil {
t.Fatal(err)
}
buff := new(bytes.Buffer)
// marshall mixed bytes (mixedBytes)
p0, p0Exp := []byte{}, common.Hex2Bytes("01020000000000000000")
p1, p1Exp := [32]byte{}, common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
mixedBytes := []interface{}{&p0, &p1}
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
err = abi.Unpack(&mixedBytes, "mixedBytes", buff.Bytes())
2017-11-08 10:45:52 +00:00
if err != nil {
t.Error(err)
} else {
if !bytes.Equal(p0, p0Exp) {
t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
}
if !bytes.Equal(p1[:], p1Exp) {
t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
}
}
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
// marshal int
var Int *big.Int
err = abi.Unpack(&Int, "int", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
if err != nil {
t.Error(err)
}
if Int == nil || Int.Cmp(big.NewInt(1)) != 0 {
t.Error("expected Int to be 1 got", Int)
}
// marshal bool
var Bool bool
err = abi.Unpack(&Bool, "bool", common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
if err != nil {
t.Error(err)
}
if !Bool {
t.Error("expected Bool to be true")
}
// marshal dynamic bytes max length 32
buff.Reset()
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
bytesOut := common.RightPadBytes([]byte("hello"), 32)
buff.Write(bytesOut)
var Bytes []byte
err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
if err != nil {
t.Error(err)
}
if !bytes.Equal(Bytes, bytesOut) {
t.Errorf("expected %x got %x", bytesOut, Bytes)
}
// marshall dynamic bytes max length 64
buff.Reset()
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
bytesOut = common.RightPadBytes([]byte("hello"), 64)
buff.Write(bytesOut)
err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
if err != nil {
t.Error(err)
}
if !bytes.Equal(Bytes, bytesOut) {
t.Errorf("expected %x got %x", bytesOut, Bytes)
}
// marshall dynamic bytes max length 64
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
buff.Reset()
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
bytesOut = common.RightPadBytes([]byte("hello"), 64)
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
buff.Write(bytesOut)
err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
if err != nil {
t.Error(err)
}
if !bytes.Equal(Bytes, bytesOut[:len(bytesOut)-1]) {
t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], Bytes)
accounts/abi: reorganizing package with small fixes (#14610) * accounts/abi: reorganizing package and some notes and a quick correction of name. Signed-off-by: RJ Catalano <rj@monax.io> get rid of some imports Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move file names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix boolean decode function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix for the array set and for creating a bool Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: be very very very correct Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix up error message and variable names Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: take out unnecessary argument in pack method Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: add bool unpack test and add a panic to readBool function Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix panic message Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: change from panic to basic error Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix nil to false Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fill out type regex tests and fill with the correct type for integers Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: move packNumbers into pack.go. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: separation of the testing suite into appropriately named files. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: change to hex string tests. Signed-off-by: RJ Catalano <rj@monax.io> * account/abi: fix up rest of tests to hex Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: declare bool at the package level Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: use errors package in the error file. Signed-off-by: RJ Catalano <rj@monax.io> * accounts/abi: fix ugly hack and fix error type declaration. Signed-off-by: RJ Catalano <rj@monax.io>
2017-06-27 08:05:33 +00:00
}
// marshal dynamic bytes output empty
err = abi.Unpack(&Bytes, "bytes", nil)
if err == nil {
t.Error("expected error")
}
// marshal dynamic bytes length 5
buff.Reset()
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
buff.Write(common.RightPadBytes([]byte("hello"), 32))
err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
if err != nil {
t.Error(err)
}
if !bytes.Equal(Bytes, []byte("hello")) {
t.Errorf("expected %x got %x", bytesOut, Bytes)
}
// marshal dynamic bytes length 5
buff.Reset()
buff.Write(common.RightPadBytes([]byte("hello"), 32))
var hash common.Hash
err = abi.Unpack(&hash, "fixed", buff.Bytes())
if err != nil {
t.Error(err)
}
helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
if hash != helloHash {
t.Errorf("Expected %x to equal %x", hash, helloHash)
}
// marshal error
buff.Reset()
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
err = abi.Unpack(&Bytes, "bytes", buff.Bytes())
if err == nil {
t.Error("expected error")
}
err = abi.Unpack(&Bytes, "multi", make([]byte, 64))
if err == nil {
t.Error("expected error")
}
buff.Reset()
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
// marshal int array
var intArray [3]*big.Int
err = abi.Unpack(&intArray, "intArraySingle", buff.Bytes())
if err != nil {
t.Error(err)
}
var testAgainstIntArray [3]*big.Int
testAgainstIntArray[0] = big.NewInt(1)
testAgainstIntArray[1] = big.NewInt(2)
testAgainstIntArray[2] = big.NewInt(3)
for i, Int := range intArray {
if Int.Cmp(testAgainstIntArray[i]) != 0 {
t.Errorf("expected %v, got %v", testAgainstIntArray[i], Int)
}
}
// marshal address slice
buff.Reset()
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
var outAddr []common.Address
err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
if err != nil {
t.Fatal("didn't expect error:", err)
}
if len(outAddr) != 1 {
t.Fatal("expected 1 item, got", len(outAddr))
}
if outAddr[0] != (common.Address{1}) {
t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
}
// marshal multiple address slice
buff.Reset()
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
var outAddrStruct struct {
A []common.Address
B []common.Address
}
err = abi.Unpack(&outAddrStruct, "addressSliceDouble", buff.Bytes())
if err != nil {
t.Fatal("didn't expect error:", err)
}
if len(outAddrStruct.A) != 1 {
t.Fatal("expected 1 item, got", len(outAddrStruct.A))
}
if outAddrStruct.A[0] != (common.Address{1}) {
t.Errorf("expected %x, got %x", common.Address{1}, outAddrStruct.A[0])
}
if len(outAddrStruct.B) != 2 {
t.Fatal("expected 1 item, got", len(outAddrStruct.B))
}
if outAddrStruct.B[0] != (common.Address{2}) {
t.Errorf("expected %x, got %x", common.Address{2}, outAddrStruct.B[0])
}
if outAddrStruct.B[1] != (common.Address{3}) {
t.Errorf("expected %x, got %x", common.Address{3}, outAddrStruct.B[1])
}
// marshal invalid address slice
buff.Reset()
buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
if err == nil {
t.Fatal("expected error:", err)
}
}
func TestOOMMaliciousInput(t *testing.T) {
oomTests := []unpackTest{
{
def: `[{"type": "uint8[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
"0000000000000000000000000000000000000000000000000000000000000003" + // num elems
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
},
{ // Length larger than 64 bits
def: `[{"type": "uint8[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
"00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000002" + // num elems
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
},
{ // Offset very large (over 64 bits)
def: `[{"type": "uint8[]"}]`,
enc: "00ffffffffffffffffffffffffffffffffffffffffffffff0000000000000020" + // offset
"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
},
{ // Offset very large (below 64 bits)
def: `[{"type": "uint8[]"}]`,
enc: "0000000000000000000000000000000000000000000000007ffffffffff00020" + // offset
"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
},
{ // Offset negative (as 64 bit)
def: `[{"type": "uint8[]"}]`,
enc: "000000000000000000000000000000000000000000000000f000000000000020" + // offset
"0000000000000000000000000000000000000000000000000000000000000002" + // num elems
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
},
{ // Negative length
def: `[{"type": "uint8[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
"000000000000000000000000000000000000000000000000f000000000000002" + // num elems
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
},
{ // Very large length
def: `[{"type": "uint8[]"}]`,
enc: "0000000000000000000000000000000000000000000000000000000000000020" + // offset
"0000000000000000000000000000000000000000000000007fffffffff000002" + // num elems
"0000000000000000000000000000000000000000000000000000000000000001" + // elem 1
"0000000000000000000000000000000000000000000000000000000000000002", // elem 2
},
}
for i, test := range oomTests {
def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
abi, err := JSON(strings.NewReader(def))
if err != nil {
t.Fatalf("invalid ABI definition %s: %v", def, err)
}
encb, err := hex.DecodeString(test.enc)
if err != nil {
t.Fatalf("invalid hex: %s" + test.enc)
}
_, err = abi.Methods["method"].Outputs.UnpackValues(encb)
if err == nil {
t.Fatalf("Expected error on malicious input, test %d", i)
}
}
}