aboutsummaryrefslogtreecommitdiffstats
path: root/accounts/abi/abi_test.go
diff options
context:
space:
mode:
authorJeffrey Wilcke <geffobscura@gmail.com>2016-02-09 20:57:00 +0800
committerJeffrey Wilcke <geffobscura@gmail.com>2016-02-11 17:16:38 +0800
commitecc876cec0b4c70bfa5ff9939ac86715bf9579de (patch)
tree54538afc6ccd8f0ff7eb1a35e0caf54920f11046 /accounts/abi/abi_test.go
parent856b9e9c500dae60ed84e75d9577b2deb504558e (diff)
downloadgo-tangerine-ecc876cec0b4c70bfa5ff9939ac86715bf9579de.tar
go-tangerine-ecc876cec0b4c70bfa5ff9939ac86715bf9579de.tar.gz
go-tangerine-ecc876cec0b4c70bfa5ff9939ac86715bf9579de.tar.bz2
go-tangerine-ecc876cec0b4c70bfa5ff9939ac86715bf9579de.tar.lz
go-tangerine-ecc876cec0b4c70bfa5ff9939ac86715bf9579de.tar.xz
go-tangerine-ecc876cec0b4c70bfa5ff9939ac86715bf9579de.tar.zst
go-tangerine-ecc876cec0b4c70bfa5ff9939ac86715bf9579de.zip
accounts/abi: fixed return tuple and string, bytes return type parsing
Removed old unmarshalling of return types: `abi.Call(...).([]byte)`. This is now replaced by a new syntax: ``` var a []byte err := abi.Call(&a, ...) ``` It also addresses a few issues with Bytes and Strings and can also handle both fixed and arbitrary sized byte slices, including strings.
Diffstat (limited to 'accounts/abi/abi_test.go')
-rw-r--r--accounts/abi/abi_test.go233
1 files changed, 233 insertions, 0 deletions
diff --git a/accounts/abi/abi_test.go b/accounts/abi/abi_test.go
index 000c118f8..bb0143d21 100644
--- a/accounts/abi/abi_test.go
+++ b/accounts/abi/abi_test.go
@@ -394,6 +394,7 @@ func TestBytes(t *testing.T) {
}
}
+/*
func TestReturn(t *testing.T) {
const definition = `[
{ "type" : "function", "name" : "balance", "const" : true, "inputs" : [], "outputs" : [ { "name": "", "type": "hash" } ] },
@@ -422,6 +423,7 @@ func TestReturn(t *testing.T) {
t.Errorf("expected type common.Address, got %T", r)
}
}
+*/
func TestDefaultFunctionParsing(t *testing.T) {
const definition = `[{ "name" : "balance" }]`
@@ -458,3 +460,234 @@ func TestBareEvents(t *testing.T) {
t.Error("expected 'name' event to be present")
}
}
+
+func TestMultiReturnWithStruct(t *testing.T) {
+ const definition = `[
+ { "name" : "multi", "const" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
+
+ abi, err := JSON(strings.NewReader(definition))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // 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"))
+ stringOut := "hello"
+ buff.Write(common.RightPadBytes([]byte(stringOut), 32))
+
+ var inter struct {
+ Int *big.Int
+ String string
+ }
+ err = abi.unmarshal(&inter, "multi", buff.Bytes())
+ if err != nil {
+ t.Error(err)
+ }
+
+ if inter.Int == nil || inter.Int.Cmp(big.NewInt(1)) != 0 {
+ t.Error("expected Int to be 1 got", inter.Int)
+ }
+
+ if inter.String != stringOut {
+ t.Error("expected String to be", stringOut, "got", inter.String)
+ }
+
+ var reversed struct {
+ String string
+ Int *big.Int
+ }
+
+ err = abi.unmarshal(&reversed, "multi", buff.Bytes())
+ if err != nil {
+ t.Error(err)
+ }
+
+ if reversed.Int == nil || reversed.Int.Cmp(big.NewInt(1)) != 0 {
+ t.Error("expected Int to be 1 got", reversed.Int)
+ }
+
+ if reversed.String != stringOut {
+ t.Error("expected String to be", stringOut, "got", reversed.String)
+ }
+}
+
+func TestMultiReturnWithSlice(t *testing.T) {
+ const definition = `[
+ { "name" : "multi", "const" : false, "outputs": [ { "name": "Int", "type": "uint256" }, { "name": "String", "type": "string" } ] }]`
+
+ abi, err := JSON(strings.NewReader(definition))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // 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"))
+ stringOut := "hello"
+ buff.Write(common.RightPadBytes([]byte(stringOut), 32))
+
+ var inter []interface{}
+ err = abi.unmarshal(&inter, "multi", buff.Bytes())
+ if err != nil {
+ t.Error(err)
+ }
+
+ if len(inter) != 2 {
+ t.Fatal("expected 2 results got", len(inter))
+ }
+
+ if num, ok := inter[0].(*big.Int); !ok || num.Cmp(big.NewInt(1)) != 0 {
+ t.Error("expected index 0 to be 1 got", num)
+ }
+
+ if str, ok := inter[1].(string); !ok || str != stringOut {
+ t.Error("expected index 1 to be", stringOut, "got", str)
+ }
+}
+
+func TestUnmarshal(t *testing.T) {
+ const definition = `[
+ { "name" : "int", "const" : false, "outputs": [ { "type": "uint256" } ] },
+ { "name" : "bool", "const" : false, "outputs": [ { "type": "bool" } ] },
+ { "name" : "bytes", "const" : false, "outputs": [ { "type": "bytes" } ] },
+ { "name" : "multi", "const" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
+ { "name" : "mixedBytes", "const" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
+
+ abi, err := JSON(strings.NewReader(definition))
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // marshal int
+ var Int *big.Int
+ err = abi.unmarshal(&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.unmarshal(&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 := new(bytes.Buffer)
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
+ bytesOut := common.RightPadBytes([]byte("hello"), 32)
+ buff.Write(bytesOut)
+
+ var Bytes []byte
+ err = abi.unmarshal(&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.unmarshal(&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 63
+ buff.Reset()
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
+ buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
+ bytesOut = common.RightPadBytes([]byte("hello"), 63)
+ buff.Write(bytesOut)
+
+ err = abi.unmarshal(&Bytes, "bytes", buff.Bytes())
+ if err != nil {
+ t.Error(err)
+ }
+
+ if !bytes.Equal(Bytes, bytesOut) {
+ t.Errorf("expected %x got %x", bytesOut, Bytes)
+ }
+
+ // marshal dynamic bytes output empty
+ err = abi.unmarshal(&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.unmarshal(&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 error
+ buff.Reset()
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
+ err = abi.unmarshal(&Bytes, "bytes", buff.Bytes())
+ if err == nil {
+ t.Error("expected error")
+ }
+
+ err = abi.unmarshal(&Bytes, "multi", make([]byte, 64))
+ if err == nil {
+ t.Error("expected error")
+ }
+
+ // marshal mixed bytes
+ buff.Reset()
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
+ fixed := common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")
+ buff.Write(fixed)
+ buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
+ bytesOut = common.RightPadBytes([]byte("hello"), 32)
+ buff.Write(bytesOut)
+
+ var out []interface{}
+ err = abi.unmarshal(&out, "mixedBytes", buff.Bytes())
+ if err != nil {
+ t.Fatal("didn't expect error:", err)
+ }
+
+ if !bytes.Equal(bytesOut, out[0].([]byte)) {
+ t.Errorf("expected %x, got %x", bytesOut, out[0])
+ }
+
+ if !bytes.Equal(fixed, out[1].([]byte)) {
+ t.Errorf("expected %x, got %x", fixed, out[1])
+ }
+}