github.com/klaytn/klaytn@v1.12.1/accounts/abi/pack_test.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2017 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from accounts/abi/pack_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package abi 22 23 import ( 24 "bytes" 25 "encoding/hex" 26 "fmt" 27 "math" 28 "math/big" 29 "reflect" 30 "strconv" 31 "strings" 32 "testing" 33 34 "github.com/klaytn/klaytn/common" 35 ) 36 37 // TestPack tests the general pack/unpack tests in packing_test.go 38 func TestPack(t *testing.T) { 39 for i, test := range packUnpackTests { 40 t.Run(strconv.Itoa(i), func(t *testing.T) { 41 encb, err := hex.DecodeString(test.packed) 42 if err != nil { 43 t.Fatalf("invalid hex %s: %v", test.packed, err) 44 } 45 inDef := fmt.Sprintf(`[{ "name" : "method", "type": "function", "inputs": %s}]`, test.def) 46 inAbi, err := JSON(strings.NewReader(inDef)) 47 if err != nil { 48 t.Fatalf("invalid ABI definition %s, %v", inDef, err) 49 } 50 var packed []byte 51 packed, err = inAbi.Pack("method", test.unpacked) 52 53 if err != nil { 54 t.Fatalf("test %d (%v) failed: %v", i, test.def, err) 55 } 56 if !reflect.DeepEqual(packed[4:], encb) { 57 t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, encb, packed[4:]) 58 } 59 }) 60 } 61 } 62 63 func TestMethodPack(t *testing.T) { 64 abi, err := JSON(strings.NewReader(jsondata)) 65 if err != nil { 66 t.Fatal(err) 67 } 68 69 sig := abi.Methods["slice"].ID 70 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 71 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 72 73 packed, err := abi.Pack("slice", []uint32{1, 2}) 74 if err != nil { 75 t.Error(err) 76 } 77 78 if !bytes.Equal(packed, sig) { 79 t.Errorf("expected %x got %x", sig, packed) 80 } 81 82 addrA, addrB := common.Address{1}, common.Address{2} 83 sig = abi.Methods["sliceAddress"].ID 84 sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...) 85 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 86 sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) 87 sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) 88 89 packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB}) 90 if err != nil { 91 t.Fatal(err) 92 } 93 if !bytes.Equal(packed, sig) { 94 t.Errorf("expected %x got %x", sig, packed) 95 } 96 97 addrC, addrD := common.Address{3}, common.Address{4} 98 sig = abi.Methods["sliceMultiAddress"].ID 99 sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...) 100 sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...) 101 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 102 sig = append(sig, common.LeftPadBytes(addrA[:], 32)...) 103 sig = append(sig, common.LeftPadBytes(addrB[:], 32)...) 104 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 105 sig = append(sig, common.LeftPadBytes(addrC[:], 32)...) 106 sig = append(sig, common.LeftPadBytes(addrD[:], 32)...) 107 108 packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD}) 109 if err != nil { 110 t.Fatal(err) 111 } 112 if !bytes.Equal(packed, sig) { 113 t.Errorf("expected %x got %x", sig, packed) 114 } 115 116 sig = abi.Methods["slice256"].ID 117 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 118 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 119 120 packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)}) 121 if err != nil { 122 t.Error(err) 123 } 124 125 if !bytes.Equal(packed, sig) { 126 t.Errorf("expected %x got %x", sig, packed) 127 } 128 129 a := [2][2]*big.Int{{big.NewInt(1), big.NewInt(1)}, {big.NewInt(2), big.NewInt(0)}} 130 sig = abi.Methods["nestedArray"].ID 131 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 132 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 133 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 134 sig = append(sig, common.LeftPadBytes([]byte{0}, 32)...) 135 sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...) 136 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 137 sig = append(sig, common.LeftPadBytes(addrC[:], 32)...) 138 sig = append(sig, common.LeftPadBytes(addrD[:], 32)...) 139 packed, err = abi.Pack("nestedArray", a, []common.Address{addrC, addrD}) 140 if err != nil { 141 t.Fatal(err) 142 } 143 if !bytes.Equal(packed, sig) { 144 t.Errorf("expected %x got %x", sig, packed) 145 } 146 147 sig = abi.Methods["nestedArray2"].ID 148 sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...) 149 sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...) 150 sig = append(sig, common.LeftPadBytes([]byte{0x80}, 32)...) 151 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 152 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 153 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 154 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 155 packed, err = abi.Pack("nestedArray2", [2][]uint8{{1}, {1}}) 156 if err != nil { 157 t.Fatal(err) 158 } 159 if !bytes.Equal(packed, sig) { 160 t.Errorf("expected %x got %x", sig, packed) 161 } 162 163 sig = abi.Methods["nestedSlice"].ID 164 sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...) 165 sig = append(sig, common.LeftPadBytes([]byte{0x02}, 32)...) 166 sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...) 167 sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...) 168 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 169 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 170 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 171 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 172 sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...) 173 sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...) 174 packed, err = abi.Pack("nestedSlice", [][]uint8{{1, 2}, {1, 2}}) 175 if err != nil { 176 t.Fatal(err) 177 } 178 if !bytes.Equal(packed, sig) { 179 t.Errorf("expected %x got %x", sig, packed) 180 } 181 } 182 183 func TestPackNumber(t *testing.T) { 184 tests := []struct { 185 value reflect.Value 186 packed []byte 187 }{ 188 // Protocol limits 189 {reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")}, 190 {reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")}, 191 {reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")}, 192 193 // Type corner cases 194 {reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")}, 195 {reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")}, 196 {reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")}, 197 {reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")}, 198 199 {reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")}, 200 {reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")}, 201 {reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")}, 202 {reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")}, 203 204 {reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")}, 205 {reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")}, 206 {reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")}, 207 {reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")}, 208 } 209 for i, tt := range tests { 210 packed := packNum(tt.value) 211 if !bytes.Equal(packed, tt.packed) { 212 t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed) 213 } 214 } 215 }