github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/contract/contract_test.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package contract
     7  
     8  import (
     9  	"bytes"
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/golang/mock/gomock"
    14  	"github.com/stretchr/testify/require"
    15  
    16  	"github.com/iotexproject/iotex-core/ioctl/config"
    17  	"github.com/iotexproject/iotex-core/ioctl/util"
    18  	"github.com/iotexproject/iotex-core/test/mock/mock_ioctlclient"
    19  )
    20  
    21  func TestNewContractCmd(t *testing.T) {
    22  	require := require.New(t)
    23  	ctrl := gomock.NewController(t)
    24  	defer ctrl.Finish()
    25  	client := mock_ioctlclient.NewMockClient(ctrl)
    26  
    27  	client.EXPECT().SelectTranslation(gomock.Any()).Return("contract", config.English).AnyTimes()
    28  	client.EXPECT().SetEndpointWithFlag(gomock.Any())
    29  	client.EXPECT().SetInsecureWithFlag(gomock.Any())
    30  
    31  	cmd := NewContractCmd(client)
    32  	result, err := util.ExecuteCmd(cmd)
    33  	require.NoError(err)
    34  	require.Contains(result, "Available Commands")
    35  }
    36  
    37  func TestReadAbiFile(t *testing.T) {
    38  	require := require.New(t)
    39  	testAbiFile := "test.abi"
    40  	abi, err := readAbiFile(testAbiFile)
    41  	require.NoError(err)
    42  	require.Equal("", abi.Constructor.Name)
    43  	require.Len(abi.Methods, 10)
    44  	require.Equal("recipients", abi.Methods["multiSend"].Inputs[0].Name)
    45  }
    46  
    47  func TestParseInput(t *testing.T) {
    48  	require := require.New(t)
    49  
    50  	testAbiFile := "test.abi"
    51  	testAbi, err := readAbiFile(testAbiFile)
    52  	require.NoError(err)
    53  
    54  	tests := []struct {
    55  		expectCode string
    56  		method     string
    57  		inputs     string
    58  	}{
    59  		{
    60  			"0xe3b48f48000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000b9c46db7b8464bad383a595fd7aa7845fbdd642b000000000000000000000000aa77fbf8596e0de5ce362dbd5ab29599a6c38ac4000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000002fa7bc000000000000000000000000000000000000000000000000000000000000007b0000000000000000000000000000000000000000000000000000000000000009504c454153452121210000000000000000000000000000000000000000000000",
    61  			"multiSend",
    62  			`{"recipients":["io1h8zxmdacge966wp6t90a02ncghaa6eptnftfqr","io14fmlh7zedcx7tn3k9k744v54nxnv8zky86tjhj"],"amounts":["3123132","123"],"payload":"PLEASE!!!"}`,
    63  		}, {
    64  			"0xba025b7100000000000000000000000000000000000000011ade48e4922161024e211c62000000000000000000000000000000000000000000000000000000000001e0f30000000000000000000000000000000000000000000000000000000000000005fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff4fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdf0d2000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000",
    65  			"testArray",
    66  			// IntTy/UintTy larger than int64/uint64 should be passes by string, otherwise the precision losses
    67  			`{"a":["87543498528347976543703735394",123123,5,-12,-134958],"b":[1,2,0]}`,
    68  		}, {
    69  			"0x3ca8b1a70000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001007a675668798ab73482982748287842900000000000000000000000000000000",
    70  			"testBytes",
    71  			`{"t":"0x07a675668798ab734829827482878429"}`,
    72  		}, {
    73  			"0x901e5dda1221120000000000000000000000000000000000000000000000000000000000",
    74  			"testFixedBytes",
    75  			`{"t":"0x122112"}`,
    76  		}, {
    77  			"0x1bfc56c6000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000010496f54655820426c6f636b636861696e00000000000000000000000000000000",
    78  			"testBoolAndString",
    79  			`{"a":true,"s":"IoTeX Blockchain"}`,
    80  		},
    81  	}
    82  
    83  	for _, test := range tests {
    84  		expect, err := decodeBytecode(test.expectCode)
    85  		require.NoError(err)
    86  
    87  		bytecode, err := packArguments(testAbi, test.method, test.inputs)
    88  		require.NoError(err)
    89  
    90  		require.True(bytes.Equal(expect, bytecode))
    91  	}
    92  }
    93  
    94  func TestParseOutput(t *testing.T) {
    95  	require := require.New(t)
    96  
    97  	testAbiFile := "test.abi"
    98  	testAbi, err := readAbiFile(testAbiFile)
    99  	require.NoError(err)
   100  
   101  	tests := []struct {
   102  		expectResult string
   103  		method       string
   104  		outputs      string
   105  	}{
   106  		{
   107  			"0",
   108  			"minTips",
   109  			"0000000000000000000000000000000000000000000000000000000000000000",
   110  		},
   111  		{
   112  			"io1cl6rl2ev5dfa988qmgzg2x4hfazmp9vn2g66ng",
   113  			"owner",
   114  			"000000000000000000000000c7f43fab2ca353d29ce0da04851ab74f45b09593",
   115  		},
   116  		{
   117  			"Hello World",
   118  			"getMessage",
   119  			"0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b48656c6c6f20576f726c64000000000000000000000000000000000000000000",
   120  		},
   121  		{
   122  			"{i:17 abc:[io1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqd39ym7 io1cl6rl2ev5dfa988qmgzg2x4hfazmp9vn2g66ng]}",
   123  			"testTuple",
   124  			"00000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c7f43fab2ca353d29ce0da04851ab74f45b09593",
   125  		},
   126  	}
   127  
   128  	for _, test := range tests {
   129  		v, err := parseOutput(testAbi, test.method, test.outputs)
   130  		require.NoError(err)
   131  		require.Equal(test.expectResult, fmt.Sprint(v))
   132  	}
   133  }