github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/ibc-go/modules/apps/transfer/module_test.go (about)

     1  package transfer_test
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  
     7  	capabilitytypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/capability/types"
     8  	"github.com/fibonacci-chain/fbc/libs/ibc-go/modules/apps/transfer/types"
     9  	channeltypes "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/04-channel/types"
    10  	host "github.com/fibonacci-chain/fbc/libs/ibc-go/modules/core/24-host"
    11  	ibctesting "github.com/fibonacci-chain/fbc/libs/ibc-go/testing"
    12  	"github.com/stretchr/testify/suite"
    13  )
    14  
    15  func (suite *TransferTestSuite) TestOnChanOpenInit() {
    16  	var (
    17  		channel *channeltypes.Channel
    18  		path    *ibctesting.Path
    19  		chanCap *capabilitytypes.Capability
    20  	)
    21  
    22  	testCases := []struct {
    23  		name     string
    24  		malleate func()
    25  		expPass  bool
    26  	}{
    27  
    28  		{
    29  			"success", func() {}, true,
    30  		},
    31  		{
    32  			"max channels reached", func() {
    33  				path.EndpointA.ChannelID = channeltypes.FormatChannelIdentifier(math.MaxUint32 + 1)
    34  			}, false,
    35  		},
    36  		{
    37  			"invalid order - ORDERED", func() {
    38  				channel.Ordering = channeltypes.ORDERED
    39  			}, false,
    40  		},
    41  		{
    42  			"invalid port ID", func() {
    43  				path.EndpointA.ChannelConfig.PortID = ibctesting.MockPort
    44  			}, false,
    45  		},
    46  		{
    47  			"invalid version", func() {
    48  				channel.Version = "version"
    49  			}, false,
    50  		},
    51  		{
    52  			"capability already claimed", func() {
    53  				err := suite.chainA.GetSimApp().ScopedTransferKeeper.ClaimCapability(suite.chainA.GetContext(), chanCap, host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID))
    54  				suite.Require().NoError(err)
    55  			}, false,
    56  		},
    57  	}
    58  
    59  	for _, tc := range testCases {
    60  		tc := tc
    61  
    62  		suite.Run(tc.name, func() {
    63  			suite.SetupTest() // reset
    64  			path = NewTransferPath(suite.chainA, suite.chainB)
    65  			suite.coordinator.SetupConnections(path)
    66  			path.EndpointA.ChannelID = ibctesting.FirstChannelID
    67  
    68  			counterparty := channeltypes.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
    69  			channel = &channeltypes.Channel{
    70  				State:          channeltypes.INIT,
    71  				Ordering:       channeltypes.UNORDERED,
    72  				Counterparty:   counterparty,
    73  				ConnectionHops: []string{path.EndpointA.ConnectionID},
    74  				Version:        types.Version,
    75  			}
    76  
    77  			module, _, err := suite.chainA.App().GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), ibctesting.TransferPort)
    78  			suite.Require().NoError(err)
    79  
    80  			chanCap, err = suite.chainA.App().GetScopedIBCKeeper().NewCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(ibctesting.TransferPort, path.EndpointA.ChannelID))
    81  			suite.Require().NoError(err)
    82  
    83  			cbs, ok := suite.chainA.App().GetIBCKeeper().Router.GetRoute(module)
    84  			suite.Require().True(ok)
    85  
    86  			tc.malleate() // explicitly change fields in channel and testChannel
    87  
    88  			_, err = cbs.OnChanOpenInit(suite.chainA.GetContext(), channel.Ordering, channel.GetConnectionHops(),
    89  				path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, chanCap, channel.Counterparty, channel.GetVersion(),
    90  			)
    91  
    92  			if tc.expPass {
    93  				suite.Require().NoError(err)
    94  			} else {
    95  				suite.Require().Error(err)
    96  			}
    97  
    98  		})
    99  	}
   100  }
   101  
   102  func (suite *TransferTestSuite) TestOnChanOpenTry() {
   103  	var (
   104  		channel             *channeltypes.Channel
   105  		chanCap             *capabilitytypes.Capability
   106  		path                *ibctesting.Path
   107  		counterpartyVersion string
   108  	)
   109  
   110  	testCases := []struct {
   111  		name     string
   112  		malleate func()
   113  		expPass  bool
   114  	}{
   115  		//
   116  		{
   117  			"success", func() {}, true,
   118  		},
   119  		{
   120  			"max channels reached", func() {
   121  				path.EndpointA.ChannelID = channeltypes.FormatChannelIdentifier(math.MaxUint32 + 1)
   122  			}, false,
   123  		},
   124  		{
   125  			"capability already claimed in INIT should pass", func() {
   126  				err := suite.chainA.GetSimApp().ScopedTransferKeeper.ClaimCapability(suite.chainA.GetContext(), chanCap, host.ChannelCapabilityPath(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID))
   127  				suite.Require().NoError(err)
   128  			}, true,
   129  		},
   130  		{
   131  			"invalid order - ORDERED", func() {
   132  				channel.Ordering = channeltypes.ORDERED
   133  			}, false,
   134  		},
   135  		{
   136  			"invalid port ID", func() {
   137  				path.EndpointA.ChannelConfig.PortID = ibctesting.MockPort
   138  			}, false,
   139  		},
   140  		{
   141  			"invalid counterparty version", func() {
   142  				counterpartyVersion = "version"
   143  			}, false,
   144  		},
   145  	}
   146  
   147  	for _, tc := range testCases {
   148  		tc := tc
   149  
   150  		suite.Run(tc.name, func() {
   151  			suite.SetupTest() // reset
   152  
   153  			path = NewTransferPath(suite.chainA, suite.chainB)
   154  			suite.coordinator.SetupConnections(path)
   155  			path.EndpointA.ChannelID = ibctesting.FirstChannelID
   156  
   157  			counterparty := channeltypes.NewCounterparty(path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID)
   158  			channel = &channeltypes.Channel{
   159  				State:          channeltypes.TRYOPEN,
   160  				Ordering:       channeltypes.UNORDERED,
   161  				Counterparty:   counterparty,
   162  				ConnectionHops: []string{path.EndpointA.ConnectionID},
   163  				Version:        types.Version,
   164  			}
   165  			counterpartyVersion = types.Version
   166  
   167  			module, _, err := suite.chainA.App().GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), ibctesting.TransferPort)
   168  			suite.Require().NoError(err)
   169  
   170  			chanCap, err = suite.chainA.App().GetScopedIBCKeeper().NewCapability(suite.chainA.GetContext(), host.ChannelCapabilityPath(ibctesting.TransferPort, path.EndpointA.ChannelID))
   171  			suite.Require().NoError(err)
   172  
   173  			cbs, ok := suite.chainA.App().GetIBCKeeper().Router.GetRoute(module)
   174  			suite.Require().True(ok)
   175  
   176  			tc.malleate() // explicitly change fields in channel and testChannel
   177  
   178  			version, err := cbs.OnChanOpenTry(suite.chainA.GetContext(), channel.Ordering, channel.GetConnectionHops(),
   179  				path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, chanCap, channel.Counterparty, channel.GetVersion(), counterpartyVersion,
   180  			)
   181  
   182  			if tc.expPass {
   183  				suite.Require().NoError(err)
   184  				suite.Require().Equal(types.Version, version)
   185  			} else {
   186  				suite.Require().Error(err)
   187  			}
   188  
   189  		})
   190  	}
   191  }
   192  
   193  func (suite *TransferTestSuite) TestOnChanOpenAck() {
   194  	var counterpartyVersion string
   195  
   196  	testCases := []struct {
   197  		name     string
   198  		malleate func()
   199  		expPass  bool
   200  	}{
   201  
   202  		{
   203  			"success", func() {}, true,
   204  		},
   205  		{
   206  			"invalid counterparty version", func() {
   207  				counterpartyVersion = "version"
   208  			}, false,
   209  		},
   210  	}
   211  
   212  	for _, tc := range testCases {
   213  		tc := tc
   214  
   215  		suite.Run(tc.name, func() {
   216  			suite.SetupTest() // reset
   217  
   218  			path := NewTransferPath(suite.chainA, suite.chainB)
   219  			suite.coordinator.SetupConnections(path)
   220  			path.EndpointA.ChannelID = ibctesting.FirstChannelID
   221  			counterpartyVersion = types.Version
   222  
   223  			module, _, err := suite.chainA.App().GetIBCKeeper().PortKeeper.LookupModuleByPort(suite.chainA.GetContext(), ibctesting.TransferPort)
   224  			suite.Require().NoError(err)
   225  
   226  			cbs, ok := suite.chainA.App().GetIBCKeeper().Router.GetRoute(module)
   227  			suite.Require().True(ok)
   228  
   229  			tc.malleate() // explicitly change fields in channel and testChannel
   230  
   231  			err = cbs.OnChanOpenAck(suite.chainA.GetContext(), path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, path.EndpointA.Counterparty.ChannelID, counterpartyVersion)
   232  
   233  			if tc.expPass {
   234  				suite.Require().NoError(err)
   235  			} else {
   236  				suite.Require().Error(err)
   237  			}
   238  
   239  		})
   240  	}
   241  }
   242  
   243  func TestTransferTestSuite2(t *testing.T) {
   244  	suite.Run(t, new(TransferTestSuite))
   245  }