From 1cf54258406722581aa5712540c75351dd176730 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Thu, 24 Feb 2022 15:58:16 +0000 Subject: [PATCH] refactoring ics29 types tests --- modules/apps/29-fee/types/fee_test.go | 84 ++++--- modules/apps/29-fee/types/msgs_test.go | 298 +++++++++---------------- 2 files changed, 158 insertions(+), 224 deletions(-) diff --git a/modules/apps/29-fee/types/fee_test.go b/modules/apps/29-fee/types/fee_test.go index 349fe8dcf07..2b02e5b857b 100644 --- a/modules/apps/29-fee/types/fee_test.go +++ b/modules/apps/29-fee/types/fee_test.go @@ -1,30 +1,42 @@ -package types +package types_test import ( "testing" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto/secp256k1" + + "github.com/cosmos/ibc-go/v3/modules/apps/29-fee/types" +) + +var ( + // defaultRecvFee is the default packet receive fee used for testing purposes + defaultRecvFee = sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}) + + // defaultAckFee is the default packet acknowledgement fee used for testing purposes + defaultAckFee = sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(200)}) + + // defaultTimeoutFee is the default packet timeout fee used for testing purposes + defaultTimeoutFee = sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(300)}) + + // invalidFee is an invalid coin set used to trigger error cases for testing purposes + invalidFee = sdk.Coins{sdk.Coin{Denom: "invalid-denom", Amount: sdk.NewInt(-2)}} + + // defaultAccAddress is the default account used for testing purposes + defaultAccAddress = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()).String() ) func TestFeeTotal(t *testing.T) { - fee := Fee{ - AckFee: sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}), - RecvFee: sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}), - TimeoutFee: sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}), - } + fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) total := fee.Total() - require.Equal(t, sdk.NewInt(300), total.AmountOf(sdk.DefaultBondDenom)) + require.Equal(t, sdk.NewInt(600), total.AmountOf(sdk.DefaultBondDenom)) } -// TestFeeValidation tests Validate -func TestFeeValidation(t *testing.T) { +func TestPacketFeeValidation(t *testing.T) { var ( - fee Fee - ackFee sdk.Coins - receiveFee sdk.Coins - timeoutFee sdk.Coins + packetFee types.PacketFee ) testCases := []struct { @@ -37,66 +49,70 @@ func TestFeeValidation(t *testing.T) { func() {}, true, }, + { + "should fail when refund address is invalid", + func() { + packetFee.RefundAddress = "invalid-address" + }, + false, + }, { "should fail when all fees are invalid", func() { - ackFee = invalidCoins - receiveFee = invalidCoins - timeoutFee = invalidCoins + packetFee.Fee.AckFee = invalidFee + packetFee.Fee.RecvFee = invalidFee + packetFee.Fee.TimeoutFee = invalidFee }, false, }, { "should fail with single invalid fee", func() { - ackFee = invalidCoins + packetFee.Fee.AckFee = invalidFee }, false, }, { "should fail with two invalid fees", func() { - timeoutFee = invalidCoins - ackFee = invalidCoins + packetFee.Fee.TimeoutFee = invalidFee + packetFee.Fee.AckFee = invalidFee }, false, }, { "should pass with two empty fees", func() { - timeoutFee = sdk.Coins{} - ackFee = sdk.Coins{} + packetFee.Fee.TimeoutFee = sdk.Coins{} + packetFee.Fee.AckFee = sdk.Coins{} }, true, }, { "should pass with one empty fee", func() { - timeoutFee = sdk.Coins{} + packetFee.Fee.TimeoutFee = sdk.Coins{} }, true, }, { "should fail if all fees are empty", func() { - ackFee = sdk.Coins{} - receiveFee = sdk.Coins{} - timeoutFee = sdk.Coins{} + packetFee.Fee.AckFee = sdk.Coins{} + packetFee.Fee.RecvFee = sdk.Coins{} + packetFee.Fee.TimeoutFee = sdk.Coins{} }, false, }, } for _, tc := range testCases { - // build message - ackFee = validCoins - receiveFee = validCoins - timeoutFee = validCoins - - // malleate - tc.malleate() - fee = Fee{receiveFee, ackFee, timeoutFee} - err := fee.Validate() + fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) + packetFee = types.NewPacketFee(fee, defaultAccAddress, nil) + + tc.malleate() // malleate mutates test data + + err := packetFee.Validate() if tc.expPass { require.NoError(t, err) diff --git a/modules/apps/29-fee/types/msgs_test.go b/modules/apps/29-fee/types/msgs_test.go index 6ded61d407f..8a7ec915a31 100644 --- a/modules/apps/29-fee/types/msgs_test.go +++ b/modules/apps/29-fee/types/msgs_test.go @@ -1,42 +1,69 @@ -package types +package types_test import ( "testing" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/ibc-go/v3/modules/apps/29-fee/types" channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types" + ibctesting "github.com/cosmos/ibc-go/v3/testing" "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/crypto/secp256k1" ) -var ( - validChannelID = "channel-1" - invalidChannelID = "ch-1" - validPortID = "validPortId" - invalidID = "this identifier is too long to be used as a valid identifier" - validCoins = sdk.Coins{sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}} - invalidCoins = sdk.Coins{sdk.Coin{Denom: "invalid-denom", Amount: sdk.NewInt(-2)}} - validAddr = sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()).String() - invalidAddr = "invalid_address" -) - -// TestMsgTransferValidation tests ValidateBasic for MsgTransfer func TestMsgRegisterCountepartyAddressValidation(t *testing.T) { + var ( + msg *types.MsgRegisterCounterpartyAddress + ) + testCases := []struct { - name string - msg *MsgRegisterCounterpartyAddress - expPass bool + name string + malleate func() + expPass bool }{ - {"validate with correct sdk.AccAddress", NewMsgRegisterCounterpartyAddress(validAddr, validAddr, validChannelID), true}, - {"validate with incorrect destination relayer address", NewMsgRegisterCounterpartyAddress(invalidAddr, validAddr, validChannelID), false}, - {"invalid counterparty address", NewMsgRegisterCounterpartyAddress(validAddr, "", validChannelID), false}, - {"invalid counterparty address: whitespaced empty string", NewMsgRegisterCounterpartyAddress(validAddr, " ", validChannelID), false}, - {"invalid channelID", NewMsgRegisterCounterpartyAddress(validAddr, validAddr, invalidChannelID), false}, + { + "success", + func() {}, + true, + }, + { + "validate with incorrect destination relayer address", + func() { + msg.Address = "invalid-address" + }, + false, + }, + { + "invalid counterparty address", + func() { + msg.CounterpartyAddress = "" + }, + false, + }, + { + "invalid counterparty address: whitespaced empty string", + func() { + msg.CounterpartyAddress = " " + }, + false, + }, + { + "invalid channelID", + func() { + msg.ChannelId = "" + }, + false, + }, } for i, tc := range testCases { - err := tc.msg.ValidateBasic() + msg = types.NewMsgRegisterCounterpartyAddress(defaultAccAddress, defaultAccAddress, ibctesting.FirstChannelID) + + tc.malleate() + + err := msg.ValidateBasic() + if tc.expPass { require.NoError(t, err, "valid test case %d failed: %s", i, tc.name) } else { @@ -45,29 +72,15 @@ func TestMsgRegisterCountepartyAddressValidation(t *testing.T) { } } -// TestRegisterCounterpartyAddressGetSigners tests GetSigners func TestRegisterCountepartyAddressGetSigners(t *testing.T) { - addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) - // build message - msg := NewMsgRegisterCounterpartyAddress(addr.String(), "counterparty", validChannelID) - - // GetSigners - res := msg.GetSigners() - - require.Equal(t, []sdk.AccAddress{addr}, res) + accAddress := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) + msg := types.NewMsgRegisterCounterpartyAddress(accAddress.String(), defaultAccAddress, ibctesting.FirstChannelID) + require.Equal(t, []sdk.AccAddress{sdk.AccAddress(accAddress)}, msg.GetSigners()) } -// TestMsgPayPacketFeeValidation tests ValidateBasic func TestMsgPayPacketFeeValidation(t *testing.T) { var ( - signer string - channelID string - portID string - fee Fee - relayers []string - ackFee sdk.Coins - receiveFee sdk.Coins - timeoutFee sdk.Coins + msg *types.MsgPayPacketFee ) testCases := []struct { @@ -83,47 +96,38 @@ func TestMsgPayPacketFeeValidation(t *testing.T) { { "invalid channelID", func() { - channelID = invalidID + msg.SourceChannelId = "" }, false, }, { "invalid portID", func() { - portID = invalidID + msg.SourcePortId = "" }, false, }, { "relayers is not nil", func() { - relayers = []string{validAddr} + msg.Relayers = []string{defaultAccAddress} }, false, }, { "invalid signer address", func() { - signer = "invalid-addr" + msg.Signer = "invalid-address" }, false, }, } for _, tc := range testCases { - // build message - signer = validAddr - channelID = validChannelID - portID = validPortID - ackFee = validCoins - receiveFee = validCoins - timeoutFee = validCoins - relayers = nil - - // malleate - tc.malleate() - fee = Fee{receiveFee, ackFee, timeoutFee} - msg := NewMsgPayPacketFee(fee, portID, channelID, signer, relayers) + fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) + msg = types.NewMsgPayPacketFee(fee, ibctesting.MockFeePort, ibctesting.FirstChannelID, defaultAccAddress, nil) + + tc.malleate() // malleate mutates test data err := msg.ValidateBasic() @@ -135,78 +139,36 @@ func TestMsgPayPacketFeeValidation(t *testing.T) { } } -// TestPayPacketFeeGetSigners tests GetSigners func TestPayPacketFeeGetSigners(t *testing.T) { - addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) - // build message - signer := addr.String() - channelID := validChannelID - portID := validPortID - fee := Fee{validCoins, validCoins, validCoins} - msg := NewMsgPayPacketFee(fee, portID, channelID, signer, nil) - - // GetSigners - res := msg.GetSigners() - - require.Equal(t, []sdk.AccAddress{addr}, res) + refundAddr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) + fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) + msg := types.NewMsgPayPacketFee(fee, ibctesting.MockFeePort, ibctesting.FirstChannelID, refundAddr.String(), nil) + + require.Equal(t, []sdk.AccAddress{refundAddr}, msg.GetSigners()) } -// TestMsgPayPacketFeeRoute tests Route for MsgPayPacketFee func TestMsgPayPacketFeeRoute(t *testing.T) { - addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) - - // build message - signer := addr.String() - channelID := validChannelID - portID := validPortID - fee := Fee{validCoins, validCoins, validCoins} - msg := NewMsgPayPacketFee(fee, portID, channelID, signer, nil) - - require.Equal(t, RouterKey, msg.Route()) + var msg types.MsgPayPacketFee + require.Equal(t, types.RouterKey, msg.Route()) } -// TestMsgPayPacketFeeType tests Type for MsgPayPacketFee func TestMsgPayPacketFeeType(t *testing.T) { - addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) - - // build message - signer := addr.String() - channelID := validChannelID - portID := validPortID - fee := Fee{validCoins, validCoins, validCoins} - msg := NewMsgPayPacketFee(fee, portID, channelID, signer, nil) - + var msg types.MsgPayPacketFee require.Equal(t, "payPacketFee", msg.Type()) } -// TestMsgPayPacketFeeGetSignBytes tests that GetSignBytes does not panic func TestMsgPayPacketFeeGetSignBytes(t *testing.T) { - addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) - - // build message - signer := addr.String() - channelID := validChannelID - portID := validPortID - fee := Fee{validCoins, validCoins, validCoins} - msg := NewMsgPayPacketFee(fee, portID, channelID, signer, nil) + fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) + msg := types.NewMsgPayPacketFee(fee, ibctesting.MockFeePort, ibctesting.FirstChannelID, defaultAccAddress, nil) require.NotPanics(t, func() { _ = msg.GetSignBytes() }) } -// TestMsgPayPacketFeeAsyncValidation tests ValidateBasic func TestMsgPayPacketFeeAsyncValidation(t *testing.T) { var ( - signer string - channelID string - portID string - fee Fee - relayers []string - seq uint64 - ackFee sdk.Coins - receiveFee sdk.Coins - timeoutFee sdk.Coins + msg *types.MsgPayPacketFeeAsync ) testCases := []struct { @@ -222,107 +184,96 @@ func TestMsgPayPacketFeeAsyncValidation(t *testing.T) { { "invalid channelID", func() { - channelID = invalidID + msg.PacketId.ChannelId = "" }, false, }, { "invalid portID", func() { - portID = invalidID + msg.PacketId.PortId = "" }, false, }, { - "relayers is not nil", + "invalid sequence", func() { - relayers = []string{validAddr} + msg.PacketId.Sequence = 0 }, false, }, { - "invalid signer address", + "relayers is not nil", func() { - signer = "invalid-addr" + msg.PacketFee.Relayers = []string{defaultAccAddress} }, false, }, { - "invalid sequence", + "invalid signer address", func() { - seq = 0 + msg.PacketFee.RefundAddress = "invalid-addr" }, false, }, { "should fail when all fees are invalid", func() { - ackFee = invalidCoins - receiveFee = invalidCoins - timeoutFee = invalidCoins + msg.PacketFee.Fee.AckFee = invalidFee + msg.PacketFee.Fee.RecvFee = invalidFee + msg.PacketFee.Fee.TimeoutFee = invalidFee }, false, }, { "should fail with single invalid fee", func() { - ackFee = invalidCoins + msg.PacketFee.Fee.AckFee = invalidFee }, false, }, { "should fail with two invalid fees", func() { - timeoutFee = invalidCoins - ackFee = invalidCoins + msg.PacketFee.Fee.AckFee = invalidFee + msg.PacketFee.Fee.TimeoutFee = invalidFee }, false, }, { "should pass with two empty fees", func() { - timeoutFee = sdk.Coins{} - ackFee = sdk.Coins{} + msg.PacketFee.Fee.AckFee = sdk.Coins{} + msg.PacketFee.Fee.TimeoutFee = sdk.Coins{} }, true, }, { "should pass with one empty fee", func() { - timeoutFee = sdk.Coins{} + msg.PacketFee.Fee.TimeoutFee = sdk.Coins{} }, true, }, { "should fail if all fees are empty", func() { - ackFee = sdk.Coins{} - receiveFee = sdk.Coins{} - timeoutFee = sdk.Coins{} + msg.PacketFee.Fee.AckFee = sdk.Coins{} + msg.PacketFee.Fee.RecvFee = sdk.Coins{} + msg.PacketFee.Fee.TimeoutFee = sdk.Coins{} }, false, }, } for _, tc := range testCases { - // build message - signer = validAddr - channelID = validChannelID - portID = validPortID - ackFee = validCoins - receiveFee = validCoins - timeoutFee = validCoins - relayers = nil - seq = 1 - - // malleate - tc.malleate() - fee = Fee{receiveFee, ackFee, timeoutFee} + packetID := channeltypes.NewPacketId(ibctesting.FirstChannelID, ibctesting.MockFeePort, 1) + fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) + packetFee := types.NewPacketFee(fee, defaultAccAddress, nil) - packetID := channeltypes.NewPacketId(channelID, portID, seq) - packetFee := NewPacketFee(fee, signer, relayers) + msg = types.NewMsgPayPacketFeeAsync(packetID, packetFee) - msg := NewMsgPayPacketFeeAsync(packetID, packetFee) + tc.malleate() // malleate mutates test data err := msg.ValidateBasic() @@ -334,66 +285,33 @@ func TestMsgPayPacketFeeAsyncValidation(t *testing.T) { } } -// TestRegisterCounterpartyAddressGetSigners tests GetSigners func TestPayPacketFeeAsyncGetSigners(t *testing.T) { refundAddr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) - fee := NewFee(validCoins, validCoins, validCoins) + packetID := channeltypes.NewPacketId(ibctesting.FirstChannelID, ibctesting.MockFeePort, 1) + fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) + packetFee := types.NewPacketFee(fee, refundAddr.String(), nil) - packetID := channeltypes.NewPacketId(validChannelID, validPortID, 1) - packetFee := NewPacketFee(fee, refundAddr.String(), nil) - - msg := NewMsgPayPacketFeeAsync(packetID, packetFee) + msg := types.NewMsgPayPacketFeeAsync(packetID, packetFee) require.Equal(t, []sdk.AccAddress{refundAddr}, msg.GetSigners()) } -// TestMsgPayPacketFeeAsyncRoute tests Route for MsgPayPacketFeeAsync func TestMsgPayPacketFeeAsyncRoute(t *testing.T) { - addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) - - // build message - channelID := validChannelID - portID := validPortID - fee := Fee{validCoins, validCoins, validCoins} - seq := uint64(1) - packetID := channeltypes.NewPacketId(channelID, portID, seq) - packetFee := NewPacketFee(fee, addr.String(), nil) - - msg := NewMsgPayPacketFeeAsync(packetID, packetFee) - - require.Equal(t, RouterKey, msg.Route()) + var msg types.MsgPayPacketFeeAsync + require.Equal(t, types.RouterKey, msg.Route()) } -// TestMsgPayPacketFeeAsyncType tests Type for MsgPayPacketFeeAsync func TestMsgPayPacketFeeAsyncType(t *testing.T) { - addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) - - // build message - channelID := validChannelID - portID := validPortID - fee := Fee{validCoins, validCoins, validCoins} - seq := uint64(1) - packetID := channeltypes.NewPacketId(channelID, portID, seq) - packetFee := NewPacketFee(fee, addr.String(), nil) - - msg := NewMsgPayPacketFeeAsync(packetID, packetFee) - + var msg types.MsgPayPacketFeeAsync require.Equal(t, "payPacketFeeAsync", msg.Type()) } -// TestMsgPayPacketFeeAsyncGetSignBytes tests that GetSignBytes does not panic func TestMsgPayPacketFeeAsyncGetSignBytes(t *testing.T) { - addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address()) - - // build message - channelID := validChannelID - portID := validPortID - fee := Fee{validCoins, validCoins, validCoins} - seq := uint64(1) - packetID := channeltypes.NewPacketId(channelID, portID, seq) - packetFee := NewPacketFee(fee, addr.String(), nil) + packetID := channeltypes.NewPacketId(ibctesting.FirstChannelID, ibctesting.MockFeePort, 1) + fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee) + packetFee := types.NewPacketFee(fee, defaultAccAddress, nil) - msg := NewMsgPayPacketFeeAsync(packetID, packetFee) + msg := types.NewMsgPayPacketFeeAsync(packetID, packetFee) require.NotPanics(t, func() { _ = msg.GetSignBytes()