github.com/iotexproject/iotex-core@v1.14.1-rc1/tools/executiontester/blockchain/slice_assembler_test.go (about)

     1  package blockchain
     2  
     3  import (
     4  	"encoding/hex"
     5  	"math/big"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestIsDynamicTypeNumber(t *testing.T) {
    12  	require := require.New(t)
    13  	sa := NewSliceAssembler()
    14  	require.False(sa.IsDynamicType(25))
    15  	// test byte slice
    16  	require.False(sa.IsDynamicType([]byte{1, 2, 3, 4, 5}))
    17  	require.False(sa.IsDynamicType([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2}))
    18  	require.True(sa.IsDynamicType([]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3}))
    19  	// test other type slice
    20  	require.True(sa.IsDynamicType([]uint32{1, 2, 3, 4, 5}))
    21  	require.True(sa.IsDynamicType([]int32{1, 2, 3, 4, 5}))
    22  	require.True(sa.IsDynamicType([]uint64{1, 2, 3, 4, 5}))
    23  	require.True(sa.IsDynamicType([]int64{1, 2, 3, 4, 5}))
    24  	require.True(sa.IsDynamicType([]uint16{1, 2, 3, 4, 5}))
    25  	require.True(sa.IsDynamicType([]int16{1, 2, 3, 4, 5}))
    26  	// test string
    27  	require.True(sa.IsDynamicType("hello world"))
    28  }
    29  
    30  func TestExample(t *testing.T) {
    31  	require := require.New(t)
    32  	// example taken from https://solidity.readthedocs.io/en/v0.5.3/abi-spec.html
    33  	// call sam with the arguments "dave", true and [1,2,3]
    34  	expected, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000464617665000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003")
    35  	sa := NewSliceAssembler()
    36  	data, err := sa.PackArguments("dave", true, []uint{1, 2, 3})
    37  	require.NoError(err)
    38  	require.Equal(expected, data)
    39  }
    40  
    41  func TestSliceAssembler_PackArguments_BigInt(t *testing.T) {
    42  	require := require.New(t)
    43  	expected, _ := hex.DecodeString("000000000000000000000000000000000000000000000000000000000000001900000000000000000000000000000000000000000000000000000000000000190000000000000000000000000000000000000000000000000000000000000019")
    44  	sa := NewSliceAssembler()
    45  	target := big.NewInt(25)
    46  	data, err := sa.PackArguments(target, target, target)
    47  	require.NoError(err)
    48  	require.Equal(expected, data)
    49  }
    50  
    51  func TestSliceAssembler_PackArguments_BigInts(t *testing.T) {
    52  	require := require.New(t)
    53  	expected, _ := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000001900000000000000000000000000000000000000000000000000000000000000190000000000000000000000000000000000000000000000000000000000000019")
    54  	sa := NewSliceAssembler()
    55  	target := big.NewInt(25)
    56  	data, err := sa.PackArguments([]*big.Int{target, target, target})
    57  	require.NoError(err)
    58  	require.Equal(expected, data)
    59  }
    60  
    61  func TestSliceAssembler_PackArguments_Ints(t *testing.T) {
    62  	require := require.New(t)
    63  	expected, _ := hex.DecodeString("00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000001900000000000000000000000000000000000000000000000000000000000000190000000000000000000000000000000000000000000000000000000000000019")
    64  	sa := NewSliceAssembler()
    65  	target := int32(25)
    66  	data, err := sa.PackArguments([]int32{target, target, target})
    67  	require.NoError(err)
    68  	require.Equal(expected, data)
    69  }
    70  
    71  func TestSliceAssembler_PackArguments_Params(t *testing.T) {
    72  	require := require.New(t)
    73  	expected, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000001900000000000000000000000000000000000000000000000000000000000000190000000000000000000000000000000000000000000000000000000000000019")
    74  	sa := NewSliceAssembler()
    75  	target := 25
    76  	data, err := sa.PackArguments(5, 6, []int{target, target, target})
    77  	require.NoError(err)
    78  	require.Equal(expected, data)
    79  }
    80  
    81  func TestSliceAssembler_PackArguments_Strings(t *testing.T) {
    82  	require := require.New(t)
    83  	expected, _ := hex.DecodeString("000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20776f726c642100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20776f726c642100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20776f726c642100000000000000000000000000000000000000")
    84  	sa := NewSliceAssembler()
    85  	target := "Hello, world!"
    86  	data, err := sa.PackArguments(target, target, target)
    87  	require.NoError(err)
    88  	require.Equal(expected, data)
    89  }
    90  
    91  func TestSliceAssembler_PackArguments_String(t *testing.T) {
    92  	require := require.New(t)
    93  	expected, _ := hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20776f726c642100000000000000000000000000000000000000")
    94  	sa := NewSliceAssembler()
    95  	target := "Hello, world!"
    96  	data, err := sa.PackArguments(target)
    97  	require.NoError(err)
    98  	require.Equal(expected, data)
    99  }
   100  
   101  func TestSliceAssembler_PackArguments_Bytes(t *testing.T) {
   102  	require := require.New(t)
   103  	expected, _ := hex.DecodeString("48656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002748656c6c6f2c20776f726c642148656c6c6f2c20776f726c642148656c6c6f2c20776f726c642100000000000000000000000000000000000000000000000000")
   104  	sa := NewSliceAssembler()
   105  	target := "Hello, world!Hello, world!Hello, world!"
   106  	minTarget := "Hello"
   107  	data, err := sa.PackArguments([]byte(minTarget), []byte(target))
   108  	require.NoError(err)
   109  	require.Equal(expected, data)
   110  }