diff --git a/wormchain/x/ibc-composability-mw/keeper/genesis_test.go b/wormchain/x/ibc-composability-mw/keeper/genesis_test.go index bb2e92d202..32115a0e33 100644 --- a/wormchain/x/ibc-composability-mw/keeper/genesis_test.go +++ b/wormchain/x/ibc-composability-mw/keeper/genesis_test.go @@ -8,7 +8,7 @@ import ( "github.com/wormhole-foundation/wormchain/x/ibc-composability-mw/types" ) -// TestGenesis ensures genesis state can be initialiazed and exported correctly +// TestGenesis ensures genesis state can be initialiazed and exported correctly. func TestGenesis(t *testing.T) { for _, tc := range []struct { dataInFlight map[string][]byte diff --git a/wormchain/x/ibc-composability-mw/keeper/keeper_test.go b/wormchain/x/ibc-composability-mw/keeper/keeper_test.go index 127d174f6f..a77ee964e5 100644 --- a/wormchain/x/ibc-composability-mw/keeper/keeper_test.go +++ b/wormchain/x/ibc-composability-mw/keeper/keeper_test.go @@ -96,22 +96,19 @@ func TestPackets(t *testing.T) { shouldErr: false, }, } { - packet, ack := keeper.OnRecvPacket(ctx, tc.packet) t.Run(tc.testName, func(t *testing.T) { - if tc.shouldErr { require.NotNil(t, ack) - return } else { require.NotNil(t, packet) require.Nil(t, ack) - } - // Should return nil because the packet is not transposed (it is an ibc hooks packet) - res := keeper.GetAndClearTransposedData(ctx, tc.packet.DestinationChannel, tc.packet.DestinationPort, tc.packet.Sequence) - require.Nil(t, res) + // Should return nil because the packet is not transposed (it is an ibc hooks packet) + res := keeper.GetAndClearTransposedData(ctx, tc.packet.DestinationChannel, tc.packet.DestinationPort, tc.packet.Sequence) + require.Nil(t, res) + } }) } diff --git a/wormchain/x/ibc-composability-mw/types/gateway_payload_test.go b/wormchain/x/ibc-composability-mw/types/gateway_payload_test.go index 5321ccfe45..698ebd31b7 100644 --- a/wormchain/x/ibc-composability-mw/types/gateway_payload_test.go +++ b/wormchain/x/ibc-composability-mw/types/gateway_payload_test.go @@ -8,22 +8,26 @@ import ( "github.com/wormhole-foundation/wormchain/x/ibc-composability-mw/types" ) +// TestGatewayPayloads tests the VerifyAndParseGatewayPayload function. func TestGatewayPayloads(t *testing.T) { - for _, tc := range []struct { + testName string memo string // use memo if present, otherwise marshal tbPayload with json tbPayload types.GatewayIbcTokenBridgePayload shouldErr bool }{ { + testName: "memo present, payload abscent - should error", memo: "abc123", shouldErr: true, }, { + testName: "memo abscent, invalid payload - should error", tbPayload: types.GatewayIbcTokenBridgePayload{}, shouldErr: true, }, { + testName: "valid transfer no payload - should pass", tbPayload: types.GatewayIbcTokenBridgePayload{ GatewayIbcTokenBridgePayloadObj: types.GatewayIbcTokenBridgePayloadObj{ Transfer: types.GatewayTransfer{ @@ -37,6 +41,7 @@ func TestGatewayPayloads(t *testing.T) { shouldErr: false, }, { + testName: "valid transfer with payload - should pass", tbPayload: types.GatewayIbcTokenBridgePayload{ GatewayIbcTokenBridgePayloadObj: types.GatewayIbcTokenBridgePayloadObj{ TransferWithPayload: types.GatewayTransferWithPayload{ @@ -50,36 +55,35 @@ func TestGatewayPayloads(t *testing.T) { shouldErr: false, }, } { + t.Run(tc.testName, func(t *testing.T) { + memo := tc.memo - memo := tc.memo - - if memo == "" { - bz, err := json.Marshal(tc.tbPayload) - require.NoError(t, err) - memo = string(bz) - } + if memo == "" { + bz, err := json.Marshal(tc.tbPayload) + require.NoError(t, err) + memo = string(bz) + } - payload, err := types.VerifyAndParseGatewayPayload(memo) + payload, err := types.VerifyAndParseGatewayPayload(memo) - if tc.shouldErr { - require.Error(t, err) - // continue to next case if err - continue - } else { - require.NoError(t, err) - } + if tc.shouldErr { + require.Error(t, err) + } else { + require.NoError(t, err) - // validate payload was parsed correctly - if payload.NoPayload { - require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Chain, payload.ChainId) - require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Recipient, payload.Recipient) - require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Fee, payload.Fee) - require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Nonce, payload.Nonce) - } else { - require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Chain, payload.ChainId) - require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Contract, payload.Recipient) - require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Payload, payload.Payload) - require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Nonce, payload.Nonce) - } + // validate payload was parsed correctly + if payload.NoPayload { + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Chain, payload.ChainId) + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Recipient, payload.Recipient) + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Fee, payload.Fee) + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.Transfer.Nonce, payload.Nonce) + } else { + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Chain, payload.ChainId) + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Contract, payload.Recipient) + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Payload, payload.Payload) + require.Equal(t, tc.tbPayload.GatewayIbcTokenBridgePayloadObj.TransferWithPayload.Nonce, payload.Nonce) + } + } + }) } } diff --git a/wormchain/x/ibc-composability-mw/types/ibc_hooks_test.go b/wormchain/x/ibc-composability-mw/types/ibc_hooks_test.go index c63a0a11b5..52a2a95470 100644 --- a/wormchain/x/ibc-composability-mw/types/ibc_hooks_test.go +++ b/wormchain/x/ibc-composability-mw/types/ibc_hooks_test.go @@ -7,16 +7,17 @@ import ( "github.com/wormhole-foundation/wormchain/x/ibc-composability-mw/types" ) +// TestFormatIbcHooksMemo tests the FormatIbcHooksMemo function. func TestFormatIbcHooksMemo(t *testing.T) { - ibcTranslatorContract := "wormhole123abc" for _, tc := range []struct { + testName string payload types.ParsedPayload shouldErr bool }{ - // Normal w/ no payload { + testName: "Normal w/o payload - should pass", payload: types.ParsedPayload{ NoPayload: true, ChainId: 1, @@ -27,8 +28,8 @@ func TestFormatIbcHooksMemo(t *testing.T) { }, shouldErr: false, }, - // Provide payload when unnecessary { + testName: "Provide payload when unnecessary - should pass", payload: types.ParsedPayload{ NoPayload: true, ChainId: 1, @@ -39,8 +40,8 @@ func TestFormatIbcHooksMemo(t *testing.T) { }, shouldErr: false, }, - // Normal w/ payload { + testName: "Normal w/ payload - should pass", payload: types.ParsedPayload{ NoPayload: false, ChainId: 1, @@ -51,8 +52,8 @@ func TestFormatIbcHooksMemo(t *testing.T) { }, shouldErr: false, }, - // Nil payload should not err { + testName: "Nil payload - should pass", payload: types.ParsedPayload{ NoPayload: true, ChainId: 1, @@ -64,23 +65,25 @@ func TestFormatIbcHooksMemo(t *testing.T) { shouldErr: false, }, } { - res, err := types.FormatIbcHooksMemo(tc.payload, ibcTranslatorContract) + t.Run(tc.testName, func(t *testing.T) { + res, err := types.FormatIbcHooksMemo(tc.payload, ibcTranslatorContract) - if tc.shouldErr { - require.Error(t, err) - continue - } else { - require.NoError(t, err) - require.NotNil(t, res) - } + if tc.shouldErr { + require.Error(t, err) + } else { + require.NoError(t, err) + require.NotNil(t, res) - if tc.payload.NoPayload { - require.NotContains(t, res, "gateway_convert_and_transfer_with_payload") - require.Contains(t, res, "recipient") - } else { - require.Contains(t, res, "gateway_convert_and_transfer_with_payload") - require.NotContains(t, res, "recipient") - require.Contains(t, res, "payload") - } + // validate payload was formatted correctly + if tc.payload.NoPayload { + require.NotContains(t, res, "gateway_convert_and_transfer_with_payload") + require.Contains(t, res, "recipient") + } else { + require.Contains(t, res, "gateway_convert_and_transfer_with_payload") + require.NotContains(t, res, "recipient") + require.Contains(t, res, "payload") + } + } + }) } } diff --git a/wormchain/x/ibc-composability-mw/types/pfm_test.go b/wormchain/x/ibc-composability-mw/types/pfm_test.go index cf30f9399c..8c9c72a5eb 100644 --- a/wormchain/x/ibc-composability-mw/types/pfm_test.go +++ b/wormchain/x/ibc-composability-mw/types/pfm_test.go @@ -9,16 +9,18 @@ import ( "github.com/wormhole-foundation/wormchain/x/ibc-composability-mw/types" ) +// TestFormatPfmMemo tests the FormatPfmMemo function. func TestFormatPfmMemo(t *testing.T) { for _, tc := range []struct { + testName string payload types.ParsedPayload queryResp types.IbcTranslatorQueryRsp timeout time.Duration retries uint8 shouldErr bool }{ - // Normal w/ no payload { + testName: "Normal w/o payload - should pass", payload: types.ParsedPayload{ NoPayload: true, ChainId: 1, @@ -34,8 +36,8 @@ func TestFormatPfmMemo(t *testing.T) { retries: 3, shouldErr: false, }, - // Provide payload when unnecessary { + testName: "Provide payload when unnecessary - should pass", payload: types.ParsedPayload{ NoPayload: true, ChainId: 1, @@ -51,8 +53,8 @@ func TestFormatPfmMemo(t *testing.T) { retries: 3, shouldErr: false, }, - // Normal w/ payload { + testName: "Normal w/ payload - should pass", payload: types.ParsedPayload{ NoPayload: false, ChainId: 1, @@ -68,8 +70,8 @@ func TestFormatPfmMemo(t *testing.T) { retries: 21, shouldErr: false, }, - // Nil payload should not err { + testName: "Nil payload - should pass", payload: types.ParsedPayload{ NoPayload: true, ChainId: 1, @@ -86,31 +88,31 @@ func TestFormatPfmMemo(t *testing.T) { shouldErr: false, }, } { + t.Run(tc.testName, func(t *testing.T) { + // turn the query response into bytes + queryRespBz, err := json.Marshal(tc.queryResp) + require.NoError(t, err) - // turn the query response into bytes - queryRespBz, err := json.Marshal(tc.queryResp) - require.NoError(t, err) - - res, err := types.FormatPfmMemo(tc.payload, queryRespBz, tc.timeout, tc.retries) + res, err := types.FormatPfmMemo(tc.payload, queryRespBz, tc.timeout, tc.retries) - if tc.shouldErr { - require.Error(t, err) - continue - } else { - require.NoError(t, err) - require.NotNil(t, res) - } + if tc.shouldErr { + require.Error(t, err) + } else { + require.NoError(t, err) + require.NotNil(t, res) - // convert response back to packet metadata - var packetMetadata types.PacketMetadata - err = json.Unmarshal([]byte(res), &packetMetadata) - require.NoError(t, err) + // convert response back to packet metadata + var packetMetadata types.PacketMetadata + err = json.Unmarshal([]byte(res), &packetMetadata) + require.NoError(t, err) - // validation checks - require.Equal(t, string(tc.payload.Recipient), packetMetadata.Forward.Receiver) - require.Equal(t, "transfer", packetMetadata.Forward.Port) - require.Equal(t, tc.queryResp.Channel, packetMetadata.Forward.Channel) - require.Equal(t, tc.timeout, packetMetadata.Forward.Timeout) - require.Equal(t, &tc.retries, packetMetadata.Forward.Retries) + // validation checks + require.Equal(t, string(tc.payload.Recipient), packetMetadata.Forward.Receiver) + require.Equal(t, "transfer", packetMetadata.Forward.Port) + require.Equal(t, tc.queryResp.Channel, packetMetadata.Forward.Channel) + require.Equal(t, tc.timeout, packetMetadata.Forward.Timeout) + require.Equal(t, &tc.retries, packetMetadata.Forward.Retries) + } + }) } }