Skip to content

Commit

Permalink
[ioctl] Build Execution unittest (#3631)
Browse files Browse the repository at this point in the history
* build Execution unittest

* fix nested comment hotspot

* define a constant to fix code smell

* fix nested comment hotspot

* fix security hotspot

* Delete redundant file

* replace mock anytimes with exact times

* control test data with var

* delete useless code

Co-authored-by: huofei <68298506@qq.com>
  • Loading branch information
LuckyPigeon and huof6829 authored Oct 13, 2022
1 parent 62e8eb7 commit 75cb0a1
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ioctl/newcmd/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func SendAction(client ioctl.Client,
signer = addr.String()
nonce, err = checkNonce(client, nonce, signer)
if err != nil {
return errors.Wrap(err, "failed to get nonce ")
return errors.Wrap(err, "failed to get nonce")
}
elp.SetNonce(nonce)
}
Expand Down Expand Up @@ -385,7 +385,7 @@ func SendAction(client ioctl.Client,
return SendRaw(client, cmd, selp)
}

// Execute sends signed execution transaction to blockchain
// Execute sends signed execution's transaction to blockchain
func Execute(client ioctl.Client,
cmd *cobra.Command,
contract string,
Expand Down
107 changes: 105 additions & 2 deletions ioctl/newcmd/action/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package action

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/accounts/keystore"
Expand Down Expand Up @@ -155,8 +156,8 @@ func TestSendAction(t *testing.T) {
}}

client.EXPECT().SelectTranslation(gomock.Any()).Return("action", config.English).Times(64)
client.EXPECT().SetEndpointWithFlag(gomock.Any()).AnyTimes()
client.EXPECT().SetInsecureWithFlag(gomock.Any()).AnyTimes()
client.EXPECT().SetEndpointWithFlag(gomock.Any()).Times(8)
client.EXPECT().SetInsecureWithFlag(gomock.Any()).Times(8)
client.EXPECT().IsCryptoSm2().Return(false).Times(15)
client.EXPECT().NewKeyStore().Return(ks).Times(15)

Expand Down Expand Up @@ -247,6 +248,108 @@ func TestSendAction(t *testing.T) {
})
}

func TestExecute(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
client := mock_ioctlclient.NewMockClient(ctrl)
apiServiceClient := mock_iotexapi.NewMockAPIServiceClient(ctrl)
passwd := "123456"
bytecode := "608060405234801561001057600080fd5b506040516040806108018339810180604052810190808051906020019092919080519060200190929190505050816004819055508060058190555050506107a58061005c6000396000f300608060405260043610610078576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631249c58b1461007d57806327e235e31461009457806353277879146100eb5780636941b84414610142578063810ad50514610199578063a9059cbb14610223575b600080fd5b34801561008957600080fd5b50610092610270565b005b3480156100a057600080fd5b506100d5600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610475565b6040518082815260200191505060405180910390f35b3480156100f757600080fd5b5061012c600480360381019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919050505061048d565b6040518082815260200191505060405180910390f35b34801561014e57600080fd5b50610183600480360381019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506104a5565b6040518082815260200191505060405180910390f35b3480156101a557600080fd5b506101da600480360381019080803573ffffffffffffffffffffffffffffffffffffffff1690602001909291905050506104bd565b604051808373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018281526020019250505060405180910390f35b34801561022f57600080fd5b5061026e600480360381019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610501565b005b436004546000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054011115151561032a576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260108152602001807f746f6f20736f6f6e20746f206d696e740000000000000000000000000000000081525060200191505060405180910390fd5b436000803373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550600554600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008282540192505081905550600260003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600081548092919060010191905055503373ffffffffffffffffffffffffffffffffffffffff16600073ffffffffffffffffffffffffffffffffffffffff167fec61728879a33aa50b55e1f4789dcfc1c680f30a24d7b8694a9f874e242a97b46005546040518082815260200191505060405180910390a3565b60016020528060005260406000206000915090505481565b60026020528060005260406000206000915090505481565b60006020528060005260406000206000915090505481565b60036020528060005260406000206000915090508060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16908060010154905082565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101515156105b8576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260148152602001807f696e73756666696369656e742062616c616e636500000000000000000000000081525060200191505060405180910390fd5b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555060408051908101604052803373ffffffffffffffffffffffffffffffffffffffff16815260200182815250600360008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008201518160000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550602082015181600101559050508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fec61728879a33aa50b55e1f4789dcfc1c680f30a24d7b8694a9f874e242a97b4836040518082815260200191505060405180910390a350505600a165627a7a7230582047e5e1380e66d6b109548617ae59ff7baf70ee2d4a6734559b8fc5cabca0870b0029000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000186a0"

ks := keystore.NewKeyStore(t.TempDir(), 2, 1)
acc, err := ks.NewAccount(passwd)
require.NoError(err)
accAddr, err := address.FromBytes(acc.Address.Bytes())
require.NoError(err)

chainMetaResponse := &iotexapi.GetChainMetaResponse{ChainMeta: &iotextypes.ChainMeta{}}
elp := createEnvelope(0)
cost, err := elp.Cost()
require.NoError(err)
accountResponse := &iotexapi.GetAccountResponse{AccountMeta: &iotextypes.AccountMeta{
Address: accAddr.String(),
Nonce: 1,
PendingNonce: 1,
Balance: cost.String(),
}}

client.EXPECT().APIServiceClient().Return(apiServiceClient, nil).Times(13)
client.EXPECT().Address(gomock.Any()).Return(accAddr.String(), nil).Times(2)
client.EXPECT().Alias(gomock.Any()).Return("producer", nil).Times(4)
client.EXPECT().Config().Return(config.Config{
Explorer: "iotexscan",
Endpoint: "testnet1",
}).Times(4)

apiServiceClient.EXPECT().GetChainMeta(gomock.Any(), gomock.Any()).Return(chainMetaResponse, nil).Times(2)
apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(accountResponse, nil).Times(5)
apiServiceClient.EXPECT().SendAction(gomock.Any(), gomock.Any()).Return(&iotexapi.SendActionResponse{}, nil).Times(2)

client.EXPECT().SelectTranslation(gomock.Any()).Return("action", config.English).Times(7)
client.EXPECT().SetEndpointWithFlag(gomock.Any()).Times(7)
client.EXPECT().SetInsecureWithFlag(gomock.Any()).Times(7)
client.EXPECT().IsCryptoSm2().Return(false).Times(10)
client.EXPECT().NewKeyStore().Return(ks).Times(4)

t.Run("failed to get signer address", func(t *testing.T) {
expectedErr := errors.New("failed to get signer address")
client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return("", expectedErr)
cmd := NewActionCmd(client)
err = Execute(client, cmd, "test", big.NewInt(100), []byte(bytecode), "100", accAddr.String(), passwd, 0, 100, true)
require.Error(err, expectedErr)
})

client.EXPECT().AddressWithDefaultIfNotExist(gomock.Any()).Return(accAddr.String(), nil).Times(4)

t.Run("sends signed execution's transaction to blockchain", func(t *testing.T) {
cmd := NewActionCmd(client)
err = Execute(client, cmd, "test", big.NewInt(100), []byte(bytecode), "100", accAddr.String(), passwd, 0, 100, true)
require.NoError(err)
})

t.Run("gas limit equals to 0", func(t *testing.T) {
t.Run("sends signed execution's transaction to blockchain", func(t *testing.T) {
cmd := NewActionCmd(client)
apiServiceClient.EXPECT().EstimateActionGasConsumption(gomock.Any(), gomock.Any()).Return(&iotexapi.EstimateActionGasConsumptionResponse{
Gas: uint64(100),
}, nil)
err = Execute(client, cmd, "test", big.NewInt(100), []byte(bytecode), "100", accAddr.String(), passwd, 0, 0, true)
require.NoError(err)
})

t.Run("failed to fix Execution gas limit", func(t *testing.T) {
expectedErr := errors.New("failed to fix Execution gas limit")
apiServiceClient.EXPECT().EstimateActionGasConsumption(gomock.Any(), gomock.Any()).Return(nil, expectedErr)
cmd := NewActionCmd(client)
err = Execute(client, cmd, "test", big.NewInt(100), []byte(bytecode), "100", accAddr.String(), passwd, 0, 0, true)
require.Error(err, expectedErr)
})
})

t.Run("failed to get nonce", func(t *testing.T) {
expectedErr := errors.New("failed to get nonce")
apiServiceClient.EXPECT().GetAccount(gomock.Any(), gomock.Any()).Return(nil, expectedErr)
cmd := NewActionCmd(client)
err = Execute(client, cmd, "test", big.NewInt(100), []byte(bytecode), "100", accAddr.String(), passwd, 0, 100, true)
require.Error(err, expectedErr)
})

t.Run("failed to get gas price", func(t *testing.T) {
expectedErr := errors.New("failed to get gas price")
apiServiceClient.EXPECT().SuggestGasPrice(gomock.Any(), gomock.Any()).Return(nil, expectedErr)
cmd := NewActionCmd(client)
err = Execute(client, cmd, "test", big.NewInt(100), []byte(bytecode), "", accAddr.String(), passwd, 0, 100, true)
require.Error(err, expectedErr)
})

t.Run("failed to deploy contract with empty bytecode", func(t *testing.T) {
expectedErr := errors.New("failed to deploy contract with empty bytecode")
cmd := NewActionCmd(client)
err = Execute(client, cmd, "", big.NewInt(100), []byte(""), "100", accAddr.String(), passwd, 0, 100, true)
require.Error(err, expectedErr)
})
}

func TestRead(t *testing.T) {
require := require.New(t)
ctrl := gomock.NewController(t)
Expand Down

0 comments on commit 75cb0a1

Please sign in to comment.