unpackv2_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package abi
  17. import (
  18. "bytes"
  19. "encoding/hex"
  20. "fmt"
  21. "math/big"
  22. "reflect"
  23. "strconv"
  24. "strings"
  25. "testing"
  26. "github.com/ethereum/go-ethereum/common"
  27. )
  28. func TestUnpackV2(t *testing.T) {
  29. for i, test := range unpackTests {
  30. t.Run(strconv.Itoa(i), func(t *testing.T) {
  31. def := fmt.Sprintf(`[{ "name" : "method", "outputs": %s}]`, test.def)
  32. abi, err := JSON(strings.NewReader(def))
  33. if err != nil {
  34. t.Fatalf("invalid ABI definition %s: %v", def, err)
  35. }
  36. encb, err := hex.DecodeString(test.enc)
  37. if err != nil {
  38. t.Fatalf("invalid hex: %s" + test.enc)
  39. }
  40. out, err := abi.Methods["method"].Outputs.UnpackValues(encb)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. if len(test.err) != 0 {
  45. // The new stuff doesn't have these types of errors
  46. return
  47. }
  48. if !reflect.DeepEqual(test.want, out[0]) {
  49. t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, test.want, out[0])
  50. }
  51. })
  52. }
  53. }
  54. func TestMultiReturnWithArrayV2(t *testing.T) {
  55. const definition = `[{"name" : "multi", "outputs": [{"type": "uint64[3]"}, {"type": "uint64"}]}]`
  56. abi, err := JSON(strings.NewReader(definition))
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. buff := new(bytes.Buffer)
  61. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000900000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000007"))
  62. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000006"))
  63. out, err := abi.Methods["multi"].Outputs.UnpackValues(buff.Bytes())
  64. ret1Exp := [3]uint64{9, 8, 7}
  65. ret2Exp := uint64(6)
  66. if !reflect.DeepEqual(out[0], ret1Exp) {
  67. t.Error("array result", out[0], "!= Expected", ret1Exp)
  68. }
  69. if out[1] != ret2Exp {
  70. t.Error("int result", out[1], "!= Expected", ret2Exp)
  71. }
  72. }
  73. func TestUnmarshalV2(t *testing.T) {
  74. const definition = `[
  75. { "name" : "int", "constant" : false, "outputs": [ { "type": "uint256" } ] },
  76. { "name" : "bool", "constant" : false, "outputs": [ { "type": "bool" } ] },
  77. { "name" : "bytes", "constant" : false, "outputs": [ { "type": "bytes" } ] },
  78. { "name" : "fixed", "constant" : false, "outputs": [ { "type": "bytes32" } ] },
  79. { "name" : "multi", "constant" : false, "outputs": [ { "type": "bytes" }, { "type": "bytes" } ] },
  80. { "name" : "intArraySingle", "constant" : false, "outputs": [ { "type": "uint256[3]" } ] },
  81. { "name" : "addressSliceSingle", "constant" : false, "outputs": [ { "type": "address[]" } ] },
  82. { "name" : "addressSliceDouble", "constant" : false, "outputs": [ { "name": "a", "type": "address[]" }, { "name": "b", "type": "address[]" } ] },
  83. { "name" : "mixedBytes", "constant" : true, "outputs": [ { "name": "a", "type": "bytes" }, { "name": "b", "type": "bytes32" } ] }]`
  84. abi, err := JSON(strings.NewReader(definition))
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. buff := new(bytes.Buffer)
  89. // marshall mixed bytes (mixedBytes)
  90. p0Exp := common.Hex2Bytes("01020000000000000000")
  91. p1Exp := common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff")
  92. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  93. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000ddeeff"))
  94. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000000a"))
  95. buff.Write(common.Hex2Bytes("0102000000000000000000000000000000000000000000000000000000000000"))
  96. mixedBytes, err := abi.Methods["mixedBytes"].Outputs.UnpackValues(buff.Bytes())
  97. if err != nil {
  98. t.Error(err)
  99. } else {
  100. p0 := mixedBytes[0].([]byte)
  101. p1 := mixedBytes[1].([32]byte)
  102. if !bytes.Equal(p0, p0Exp) {
  103. t.Errorf("unexpected value unpacked: want %x, got %x", p0Exp, p0)
  104. }
  105. if !bytes.Equal(p1[:], p1Exp) {
  106. t.Errorf("unexpected value unpacked: want %x, got %x", p1Exp, p1)
  107. }
  108. }
  109. // marshal int
  110. integer, err := abi.Methods["int"].Outputs.UnpackValues(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  111. if err != nil {
  112. t.Error(err)
  113. }
  114. if len(integer) == 0 {
  115. t.Error("Expected one integer")
  116. }
  117. intval := integer[0].(*big.Int)
  118. if intval == nil || intval.Cmp(big.NewInt(1)) != 0 {
  119. t.Error("expected Int to be 1 got", intval)
  120. }
  121. // marshal bool
  122. boolreturns, err := abi.Methods["bool"].Outputs.UnpackValues(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  123. if err != nil {
  124. t.Error(err)
  125. }
  126. boolval := boolreturns[0].(bool)
  127. if !boolval {
  128. t.Error("expected Bool to be true")
  129. }
  130. // marshal dynamic bytes max length 32
  131. buff.Reset()
  132. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  133. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  134. bytesOut := common.RightPadBytes([]byte("hello"), 32)
  135. buff.Write(bytesOut)
  136. bytesreturns, err := abi.Methods["bytes"].Outputs.UnpackValues(buff.Bytes())
  137. if err != nil {
  138. t.Error(err)
  139. }
  140. bytesval := bytesreturns[0].([]byte)
  141. if !bytes.Equal(bytesval, bytesOut) {
  142. t.Errorf("expected %x got %x", bytesOut, bytesval)
  143. }
  144. // marshall dynamic bytes max length 64
  145. buff.Reset()
  146. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  147. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040"))
  148. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  149. buff.Write(bytesOut)
  150. bytesreturns, err = abi.Methods["bytes"].Outputs.UnpackValues(buff.Bytes())
  151. if err != nil {
  152. t.Error(err)
  153. }
  154. bytesval = bytesreturns[0].([]byte)
  155. if !bytes.Equal(bytesval, bytesOut) {
  156. t.Errorf("expected %x got %x", bytesOut, bytesval)
  157. }
  158. // marshall dynamic bytes max length 64
  159. buff.Reset()
  160. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  161. buff.Write(common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000003f"))
  162. bytesOut = common.RightPadBytes([]byte("hello"), 64)
  163. buff.Write(bytesOut)
  164. bytesreturns, err = abi.Methods["bytes"].Outputs.UnpackValues(buff.Bytes())
  165. if err != nil {
  166. t.Error(err)
  167. }
  168. bytesval = bytesreturns[0].([]byte)
  169. if !bytes.Equal(bytesval, bytesOut[:len(bytesOut)-1]) {
  170. t.Errorf("expected %x got %x", bytesOut[:len(bytesOut)-1], bytesval)
  171. }
  172. // marshal dynamic bytes output empty (nil)
  173. bytesreturns, err = abi.Methods["bytes"].Outputs.UnpackValues(nil)
  174. if err == nil {
  175. t.Error("expected error")
  176. }
  177. // marshal dynamic bytes output empty
  178. buff.Reset()
  179. bytesreturns, err = abi.Methods["bytes"].Outputs.UnpackValues(buff.Bytes())
  180. if err == nil {
  181. t.Error("expected error")
  182. }
  183. // marshal dynamic bytes length 5
  184. buff.Reset()
  185. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  186. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000005"))
  187. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  188. bytesreturns, err = abi.Methods["bytes"].Outputs.UnpackValues(buff.Bytes())
  189. if err != nil {
  190. t.Error(err)
  191. }
  192. bytesval = bytesreturns[0].([]byte)
  193. if !bytes.Equal(bytesval, []byte("hello")) {
  194. t.Errorf("expected %x got %x", bytesOut, bytesval)
  195. }
  196. // marshal dynamic bytes length 5
  197. buff.Reset()
  198. buff.Write(common.RightPadBytes([]byte("hello"), 32))
  199. hashreturns, err := abi.Methods["fixed"].Outputs.UnpackValues(buff.Bytes())
  200. if err != nil {
  201. t.Error(err)
  202. }
  203. hashval := hashreturns[0].([32]byte)
  204. helloHash := common.BytesToHash(common.RightPadBytes([]byte("hello"), 32))
  205. if common.Hash(hashval) != helloHash {
  206. t.Errorf("Expected %x to equal %x", hashval, helloHash)
  207. }
  208. // marshal error
  209. buff.Reset()
  210. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020"))
  211. bytesreturns, err = abi.Methods["bytes"].Outputs.UnpackValues(buff.Bytes())
  212. if err == nil {
  213. // Error abi: cannot marshal in to go slice: offset 32 would go over slice boundary (len=64)
  214. t.Error("expected error")
  215. }
  216. bytesreturns, err = abi.Methods["multi"].Outputs.UnpackValues(make([]byte, 64))
  217. buff.Reset()
  218. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001"))
  219. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002"))
  220. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000003"))
  221. // marshal int array
  222. intArrayReturns, err := abi.Methods["intArraySingle"].Outputs.UnpackValues(buff.Bytes())
  223. if err != nil {
  224. t.Error(err)
  225. }
  226. intArray := intArrayReturns[0].([3]*big.Int)
  227. var testAgainstIntArray = [3]*big.Int{big.NewInt(1), big.NewInt(2), big.NewInt(3)}
  228. for i, intval := range intArray {
  229. if intval.Cmp(testAgainstIntArray[i]) != 0 {
  230. t.Errorf("expected %v, got %v", testAgainstIntArray[i], intval)
  231. }
  232. }
  233. // marshal address slice
  234. buff.Reset()
  235. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000020")) // offset
  236. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  237. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  238. outAddrReturns, err := abi.Methods["addressSliceSingle"].Outputs.UnpackValues(buff.Bytes())
  239. if err != nil {
  240. t.Fatal("didn't expect error:", err)
  241. }
  242. outAddr := outAddrReturns[0].([]common.Address)
  243. if len(outAddr) != 1 {
  244. t.Fatal("expected 1 item, got", len(outAddr))
  245. }
  246. if outAddr[0] != (common.Address{1}) {
  247. t.Errorf("expected %x, got %x", common.Address{1}, outAddr[0])
  248. }
  249. // marshal multiple address slice
  250. buff.Reset()
  251. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000040")) // offset
  252. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000080")) // offset
  253. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")) // size
  254. buff.Write(common.Hex2Bytes("0000000000000000000000000100000000000000000000000000000000000000"))
  255. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000002")) // size
  256. buff.Write(common.Hex2Bytes("0000000000000000000000000200000000000000000000000000000000000000"))
  257. buff.Write(common.Hex2Bytes("0000000000000000000000000300000000000000000000000000000000000000"))
  258. outAddrStructReturns, err := abi.Methods["addressSliceDouble"].Outputs.UnpackValues(buff.Bytes())
  259. if err != nil {
  260. t.Fatal("didn't expect error:", err)
  261. }
  262. A := outAddrStructReturns[0].([]common.Address)
  263. B := outAddrStructReturns[1].([]common.Address)
  264. if len(A) != 1 {
  265. t.Fatal("expected 1 item, got", len(A))
  266. }
  267. if A[0] != (common.Address{1}) {
  268. t.Errorf("expected %x, got %x", common.Address{1}, A[0])
  269. }
  270. if len(B) != 2 {
  271. t.Fatal("expected 1 item, got", len(B))
  272. }
  273. if B[0] != (common.Address{2}) {
  274. t.Errorf("expected %x, got %x", common.Address{2}, B[0])
  275. }
  276. if B[1] != (common.Address{3}) {
  277. t.Errorf("expected %x, got %x", common.Address{3}, B[1])
  278. }
  279. // marshal invalid address slice
  280. buff.Reset()
  281. buff.Write(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000100"))
  282. err = abi.Unpack(&outAddr, "addressSliceSingle", buff.Bytes())
  283. _, err = abi.Methods["addressSliceSingle"].Outputs.UnpackValues(buff.Bytes())
  284. if err == nil {
  285. t.Fatal("expected error:", err)
  286. }
  287. }