Skip to content

Commit 22e87de

Browse files
authored
add tests/code coverage for OnRecv, OnTimeout and OnAck for controller submodule (#585)
1 parent 2583421 commit 22e87de

File tree

1 file changed

+150
-4
lines changed

1 file changed

+150
-4
lines changed

modules/apps/27-interchain-accounts/controller/ibc_module_test.go

+150-4
Original file line numberDiff line numberDiff line change
@@ -462,12 +462,70 @@ func (suite *InterchainAccountsTestSuite) TestOnRecvPacket() {
462462

463463
tc.malleate() // malleate mutates test data
464464

465-
module, _, err := suite.chainB.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainB.GetContext(), path.EndpointB.ChannelConfig.PortID)
465+
module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID)
466466
suite.Require().NoError(err)
467467

468-
cbs, ok := suite.chainB.App.GetIBCKeeper().Router.GetRoute(module)
468+
cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module)
469469
suite.Require().True(ok)
470470

471+
packet := channeltypes.NewPacket(
472+
[]byte("empty packet data"),
473+
suite.chainB.SenderAccount.GetSequence(),
474+
path.EndpointB.ChannelConfig.PortID,
475+
path.EndpointB.ChannelID,
476+
path.EndpointA.ChannelConfig.PortID,
477+
path.EndpointA.ChannelID,
478+
clienttypes.NewHeight(0, 100),
479+
0,
480+
)
481+
482+
ack := cbs.OnRecvPacket(suite.chainA.GetContext(), packet, TestAccAddress)
483+
suite.Require().Equal(tc.expPass, ack.Success())
484+
})
485+
}
486+
}
487+
488+
func (suite *InterchainAccountsTestSuite) TestOnAcknowledgementPacket() {
489+
var (
490+
path *ibctesting.Path
491+
)
492+
493+
testCases := []struct {
494+
msg string
495+
malleate func()
496+
expPass bool
497+
}{
498+
{
499+
"success",
500+
func() {},
501+
true,
502+
},
503+
{
504+
"controller submodule disabled", func() {
505+
suite.chainA.GetSimApp().ICAControllerKeeper.SetParams(suite.chainA.GetContext(), types.NewParams(false))
506+
}, false,
507+
},
508+
{
509+
"ICA auth module callback fails", func() {
510+
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnAcknowledgementPacket = func(
511+
ctx sdk.Context, packet channeltypes.Packet, acknowledgement []byte, relayer sdk.AccAddress,
512+
) error {
513+
return fmt.Errorf("mock ica auth fails")
514+
}
515+
}, false,
516+
},
517+
}
518+
519+
for _, tc := range testCases {
520+
suite.Run(tc.msg, func() {
521+
suite.SetupTest() // reset
522+
523+
path = NewICAPath(suite.chainA, suite.chainB)
524+
suite.coordinator.SetupConnections(path)
525+
526+
err := SetupICAPath(path, TestOwnerAddress)
527+
suite.Require().NoError(err)
528+
471529
packet := channeltypes.NewPacket(
472530
[]byte("empty packet data"),
473531
suite.chainA.SenderAccount.GetSequence(),
@@ -479,8 +537,96 @@ func (suite *InterchainAccountsTestSuite) TestOnRecvPacket() {
479537
0,
480538
)
481539

482-
ack := cbs.OnRecvPacket(suite.chainB.GetContext(), packet, TestAccAddress)
483-
suite.Require().Equal(tc.expPass, ack.Success())
540+
tc.malleate() // malleate mutates test data
541+
542+
module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID)
543+
suite.Require().NoError(err)
544+
545+
cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module)
546+
suite.Require().True(ok)
547+
548+
err = cbs.OnAcknowledgementPacket(suite.chainA.GetContext(), packet, []byte("ack"), nil)
549+
550+
if tc.expPass {
551+
suite.Require().NoError(err)
552+
} else {
553+
suite.Require().Error(err)
554+
}
555+
})
556+
}
557+
}
558+
559+
func (suite *InterchainAccountsTestSuite) TestOnTimeoutPacket() {
560+
var (
561+
path *ibctesting.Path
562+
)
563+
564+
testCases := []struct {
565+
msg string
566+
malleate func()
567+
expPass bool
568+
}{
569+
{
570+
"success",
571+
func() {},
572+
true,
573+
},
574+
{
575+
"controller submodule disabled", func() {
576+
suite.chainA.GetSimApp().ICAControllerKeeper.SetParams(suite.chainA.GetContext(), types.NewParams(false))
577+
}, false,
578+
},
579+
{
580+
"ICA auth module callback fails", func() {
581+
suite.chainA.GetSimApp().ICAAuthModule.IBCApp.OnTimeoutPacket = func(
582+
ctx sdk.Context, packet channeltypes.Packet, relayer sdk.AccAddress,
583+
) error {
584+
return fmt.Errorf("mock ica auth fails")
585+
}
586+
}, false,
587+
},
588+
}
589+
590+
for _, tc := range testCases {
591+
suite.Run(tc.msg, func() {
592+
suite.SetupTest() // reset
593+
594+
path = NewICAPath(suite.chainA, suite.chainB)
595+
suite.coordinator.SetupConnections(path)
596+
597+
err := SetupICAPath(path, TestOwnerAddress)
598+
suite.Require().NoError(err)
599+
600+
packet := channeltypes.NewPacket(
601+
[]byte("empty packet data"),
602+
suite.chainA.SenderAccount.GetSequence(),
603+
path.EndpointA.ChannelConfig.PortID,
604+
path.EndpointA.ChannelID,
605+
path.EndpointB.ChannelConfig.PortID,
606+
path.EndpointB.ChannelID,
607+
clienttypes.NewHeight(0, 100),
608+
0,
609+
)
610+
611+
tc.malleate() // malleate mutates test data
612+
613+
module, _, err := suite.chainA.App.GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID)
614+
suite.Require().NoError(err)
615+
616+
cbs, ok := suite.chainA.App.GetIBCKeeper().Router.GetRoute(module)
617+
suite.Require().True(ok)
618+
619+
err = cbs.OnTimeoutPacket(suite.chainA.GetContext(), packet, nil)
620+
621+
activeChannelID, found := suite.chainA.GetSimApp().ICAControllerKeeper.GetActiveChannelID(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID)
622+
623+
if tc.expPass {
624+
suite.Require().NoError(err)
625+
suite.Require().Empty(activeChannelID)
626+
suite.Require().False(found)
627+
} else {
628+
suite.Require().Error(err)
629+
}
484630
})
485631
}
486632
}

0 commit comments

Comments
 (0)