Skip to content

Commit

Permalink
test: adding further test cases for onRecvPacket
Browse files Browse the repository at this point in the history
  • Loading branch information
seantking committed Sep 16, 2021
1 parent c438ffe commit c978736
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
13 changes: 1 addition & 12 deletions modules/apps/27-interchain-accounts/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,7 @@ func (k Keeper) createOutgoingPacket(
return []byte{}, types.ErrInvalidOutgoingData
}

var msgs []sdk.Msg

switch data := data.(type) {
case []sdk.Msg:
msgs = data
case sdk.Msg:
msgs = []sdk.Msg{data}
default:
return []byte{}, types.ErrInvalidOutgoingData
}

txBytes, err := k.SerializeCosmosTx(k.cdc, msgs)
txBytes, err := k.SerializeCosmosTx(k.cdc, data)
if err != nil {
return []byte{}, sdkerrors.Wrap(err, "invalid packet data or codec")
}
Expand Down
43 changes: 35 additions & 8 deletions modules/apps/27-interchain-accounts/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,14 @@ func (suite *KeeperTestSuite) TestTrySendTx() {
suite.Run(tc.name, func() {
suite.SetupTest() // reset
path = NewICAPath(suite.chainA, suite.chainB)
owner := "owner"
owner := suite.chainA.SenderAccount.GetAddress().String()
suite.coordinator.SetupConnections(path)

err := suite.SetupICAPath(path, owner)
suite.Require().NoError(err)

portID = path.EndpointA.ChannelConfig.PortID

tc.malleate()

_, err = suite.chainA.GetSimApp().ICAKeeper.TrySendTx(suite.chainA.GetContext(), portID, msg)
Expand All @@ -106,6 +107,7 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() {
msg sdk.Msg
txBytes []byte
packetData []byte
sourcePort string
)

testCases := []struct {
Expand All @@ -119,6 +121,10 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() {
amount, _ := sdk.ParseCoinsNormalized("100stake")
interchainAccountAddr, _ := suite.chainB.GetSimApp().ICAKeeper.GetInterchainAccountAddress(suite.chainB.GetContext(), path.EndpointA.ChannelConfig.PortID)
msg = &banktypes.MsgSend{FromAddress: interchainAccountAddr, ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
// build packet data
txBytes, err := suite.chainA.GetSimApp().ICAKeeper.SerializeCosmosTx(suite.chainA.Codec, msg)
suite.Require().NoError(err)

data := types.IBCAccountPacketData{Type: types.EXECUTE_TX,
Data: txBytes}
packetData = data.GetBytes()
Expand Down Expand Up @@ -146,18 +152,39 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() {
}, false,
},
{
"Wrong data type", func() {
"Invalid packet type", func() {
txBytes = []byte{}
// Type here is an ENUM
// Valid type is types.EXECUTE_TX
data := types.IBCAccountPacketData{Type: 100,
Data: txBytes}
packetData = data.GetBytes()
}, false,
},
{
"Cannot unmarshal interchain account packet data", func() {
"Cannot unmarshal interchain account packet data into types.IBCAccountPacketData", func() {
packetData = []byte{}
}, false,
},
{
"Unauthorised: Interchain account not found for given source portID", func() {
sourcePort = "invalid-port-id"
}, false,
},
{
"Unauthorised: Signer of message is not the interchain account associated with sourcePortID", func() {
// build MsgSend
amount, _ := sdk.ParseCoinsNormalized("100stake")
// Incorrect FromAddress
msg = &banktypes.MsgSend{FromAddress: suite.chainB.SenderAccount.GetAddress().String(), ToAddress: suite.chainB.SenderAccount.GetAddress().String(), Amount: amount}
// build packet data
txBytes, err := suite.chainA.GetSimApp().ICAKeeper.SerializeCosmosTx(suite.chainA.Codec, msg)
suite.Require().NoError(err)
data := types.IBCAccountPacketData{Type: types.EXECUTE_TX,
Data: txBytes}
packetData = data.GetBytes()
}, false,
},
}

for _, tc := range testCases {
Expand All @@ -166,7 +193,6 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset

// setup interchain account
owner := suite.chainA.SenderAccount.GetAddress().String()
path = NewICAPath(suite.chainA, suite.chainB)
suite.coordinator.SetupConnections(path)
Expand All @@ -179,13 +205,14 @@ func (suite *KeeperTestSuite) TestOnRecvPacket() {
bankMsg := &banktypes.MsgSend{FromAddress: suite.chainB.SenderAccount.GetAddress().String(), ToAddress: interchainAccountAddr, Amount: amount}
_, err = suite.chainB.SendMsgs(bankMsg)

txBytes, err = suite.chainA.GetSimApp().ICAKeeper.SerializeCosmosTx(suite.chainA.Codec, msg)
// Next we need to define the packet/data to pass into OnRecvPacket
seq := uint64(1)
// valid source port
sourcePort = path.EndpointA.ChannelConfig.PortID

// malleate packetData for test cases
tc.malleate()

packet := channeltypes.NewPacket(packetData, seq, path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(0, 100), 0)
seq := uint64(1)
packet := channeltypes.NewPacket(packetData, seq, sourcePort, path.EndpointA.ChannelID, path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, clienttypes.NewHeight(0, 100), 0)

// Pass it in here
err = suite.chainB.GetSimApp().ICAKeeper.OnRecvPacket(suite.chainB.GetContext(), packet)
Expand Down

0 comments on commit c978736

Please sign in to comment.