github.com/amazechain/amc@v0.1.3/internal/avm/abi/pack_test.go (about)

     1  // Copyright 2023 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package abi
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"math"
    24  	"reflect"
    25  	"strconv"
    26  	"strings"
    27  	"testing"
    28  
    29  	"github.com/amazechain/amc/internal/avm/common"
    30  )
    31  
    32  // TestPack tests the general pack/unpack tests in packing_test.go
    33  func TestPack(t *testing.T) {
    34  	for i, test := range packUnpackTests {
    35  		t.Run(strconv.Itoa(i), func(t *testing.T) {
    36  			encb, err := hex.DecodeString(test.packed)
    37  			if err != nil {
    38  				t.Fatalf("invalid hex %s: %v", test.packed, err)
    39  			}
    40  			inDef := fmt.Sprintf(`[{ "name" : "method", "type": "function", "inputs": %s}]`, test.def)
    41  			inAbi, err := JSON(strings.NewReader(inDef))
    42  			if err != nil {
    43  				t.Fatalf("invalid ABI definition %s, %v", inDef, err)
    44  			}
    45  			var packed []byte
    46  			packed, err = inAbi.Pack("method", test.unpacked)
    47  
    48  			if err != nil {
    49  				t.Fatalf("test %d (%v) failed: %v", i, test.def, err)
    50  			}
    51  			if !reflect.DeepEqual(packed[4:], encb) {
    52  				t.Errorf("test %d (%v) failed: expected %v, got %v", i, test.def, encb, packed[4:])
    53  			}
    54  		})
    55  	}
    56  }
    57  
    58  //func TestMethodPack(t *testing.T) {
    59  //	abi, err := JSON(strings.NewReader(jsondata))
    60  //	if err != nil {
    61  //		t.Fatal(err)
    62  //	}
    63  //
    64  //	sig := abi.Methods["slice"].ID
    65  //	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
    66  //	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
    67  //
    68  //	packed, err := abi.Pack("slice", []uint32{1, 2})
    69  //	if err != nil {
    70  //		t.Error(err)
    71  //	}
    72  //
    73  //	if !bytes.Equal(packed, sig) {
    74  //		t.Errorf("expected %x got %x", sig, packed)
    75  //	}
    76  //
    77  //	var addrA, addrB = common.Address{1}, common.Address{2}
    78  //	sig = abi.Methods["sliceAddress"].ID
    79  //	sig = append(sig, common.LeftPadBytes([]byte{32}, 32)...)
    80  //	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
    81  //	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
    82  //	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
    83  //
    84  //	packed, err = abi.Pack("sliceAddress", []common.Address{addrA, addrB})
    85  //	if err != nil {
    86  //		t.Fatal(err)
    87  //	}
    88  //	if !bytes.Equal(packed, sig) {
    89  //		t.Errorf("expected %x got %x", sig, packed)
    90  //	}
    91  //
    92  //	var addrC, addrD = common.Address{3}, common.Address{4}
    93  //	sig = abi.Methods["sliceMultiAddress"].ID
    94  //	sig = append(sig, common.LeftPadBytes([]byte{64}, 32)...)
    95  //	sig = append(sig, common.LeftPadBytes([]byte{160}, 32)...)
    96  //	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
    97  //	sig = append(sig, common.LeftPadBytes(addrA[:], 32)...)
    98  //	sig = append(sig, common.LeftPadBytes(addrB[:], 32)...)
    99  //	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   100  //	sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
   101  //	sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
   102  //
   103  //	packed, err = abi.Pack("sliceMultiAddress", []common.Address{addrA, addrB}, []common.Address{addrC, addrD})
   104  //	if err != nil {
   105  //		t.Fatal(err)
   106  //	}
   107  //	if !bytes.Equal(packed, sig) {
   108  //		t.Errorf("expected %x got %x", sig, packed)
   109  //	}
   110  //
   111  //	sig = abi.Methods["slice256"].ID
   112  //	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   113  //	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   114  //
   115  //	packed, err = abi.Pack("slice256", []*big.Int{big.NewInt(1), big.NewInt(2)})
   116  //	if err != nil {
   117  //		t.Error(err)
   118  //	}
   119  //
   120  //	if !bytes.Equal(packed, sig) {
   121  //		t.Errorf("expected %x got %x", sig, packed)
   122  //	}
   123  //
   124  //	a := [2][2]*big.Int{{big.NewInt(1), big.NewInt(1)}, {big.NewInt(2), big.NewInt(0)}}
   125  //	sig = abi.Methods["nestedArray"].ID
   126  //	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   127  //	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   128  //	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   129  //	sig = append(sig, common.LeftPadBytes([]byte{0}, 32)...)
   130  //	sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
   131  //	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   132  //	sig = append(sig, common.LeftPadBytes(addrC[:], 32)...)
   133  //	sig = append(sig, common.LeftPadBytes(addrD[:], 32)...)
   134  //	packed, err = abi.Pack("nestedArray", a, []common.Address{addrC, addrD})
   135  //	if err != nil {
   136  //		t.Fatal(err)
   137  //	}
   138  //	if !bytes.Equal(packed, sig) {
   139  //		t.Errorf("expected %x got %x", sig, packed)
   140  //	}
   141  //
   142  //	sig = abi.Methods["nestedArray2"].ID
   143  //	sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
   144  //	sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
   145  //	sig = append(sig, common.LeftPadBytes([]byte{0x80}, 32)...)
   146  //	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   147  //	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   148  //	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   149  //	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   150  //	packed, err = abi.Pack("nestedArray2", [2][]uint8{{1}, {1}})
   151  //	if err != nil {
   152  //		t.Fatal(err)
   153  //	}
   154  //	if !bytes.Equal(packed, sig) {
   155  //		t.Errorf("expected %x got %x", sig, packed)
   156  //	}
   157  //
   158  //	sig = abi.Methods["nestedSlice"].ID
   159  //	sig = append(sig, common.LeftPadBytes([]byte{0x20}, 32)...)
   160  //	sig = append(sig, common.LeftPadBytes([]byte{0x02}, 32)...)
   161  //	sig = append(sig, common.LeftPadBytes([]byte{0x40}, 32)...)
   162  //	sig = append(sig, common.LeftPadBytes([]byte{0xa0}, 32)...)
   163  //	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   164  //	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   165  //	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   166  //	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   167  //	sig = append(sig, common.LeftPadBytes([]byte{1}, 32)...)
   168  //	sig = append(sig, common.LeftPadBytes([]byte{2}, 32)...)
   169  //	packed, err = abi.Pack("nestedSlice", [][]uint8{{1, 2}, {1, 2}})
   170  //	if err != nil {
   171  //		t.Fatal(err)
   172  //	}
   173  //	if !bytes.Equal(packed, sig) {
   174  //		t.Errorf("expected %x got %x", sig, packed)
   175  //	}
   176  //}
   177  
   178  func TestPackNumber(t *testing.T) {
   179  	tests := []struct {
   180  		value  reflect.Value
   181  		packed []byte
   182  	}{
   183  		// Protocol limits
   184  		{reflect.ValueOf(0), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000000")},
   185  		{reflect.ValueOf(1), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000000001")},
   186  		{reflect.ValueOf(-1), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")},
   187  
   188  		// Type corner cases
   189  		{reflect.ValueOf(uint8(math.MaxUint8)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000000000ff")},
   190  		{reflect.ValueOf(uint16(math.MaxUint16)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000ffff")},
   191  		{reflect.ValueOf(uint32(math.MaxUint32)), common.Hex2Bytes("00000000000000000000000000000000000000000000000000000000ffffffff")},
   192  		{reflect.ValueOf(uint64(math.MaxUint64)), common.Hex2Bytes("000000000000000000000000000000000000000000000000ffffffffffffffff")},
   193  
   194  		{reflect.ValueOf(int8(math.MaxInt8)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000000000007f")},
   195  		{reflect.ValueOf(int16(math.MaxInt16)), common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000007fff")},
   196  		{reflect.ValueOf(int32(math.MaxInt32)), common.Hex2Bytes("000000000000000000000000000000000000000000000000000000007fffffff")},
   197  		{reflect.ValueOf(int64(math.MaxInt64)), common.Hex2Bytes("0000000000000000000000000000000000000000000000007fffffffffffffff")},
   198  
   199  		{reflect.ValueOf(int8(math.MinInt8)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80")},
   200  		{reflect.ValueOf(int16(math.MinInt16)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8000")},
   201  		{reflect.ValueOf(int32(math.MinInt32)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffffffffffff80000000")},
   202  		{reflect.ValueOf(int64(math.MinInt64)), common.Hex2Bytes("ffffffffffffffffffffffffffffffffffffffffffffffff8000000000000000")},
   203  	}
   204  	for i, tt := range tests {
   205  		packed := packNum(tt.value)
   206  		if !bytes.Equal(packed, tt.packed) {
   207  			t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
   208  		}
   209  	}
   210  }