Skip to content

Commit

Permalink
test: full coverage for messages in types packages (#1879)
Browse files Browse the repository at this point in the history
* Add missing get signers type and route tests

* Improve crosschain msg types coverage

* Improve fungible msg types coverage

* Improve observer msg types coverage

* Add missing tests

* Changelog

* Use sample address in tests

* Add check for cctx index length

* Use require instead of assert

---------

Co-authored-by: Lucas Bertrand <[email protected]>
  • Loading branch information
skosito and lumtis authored Mar 13, 2024
1 parent ed73de8 commit 810c7c1
Show file tree
Hide file tree
Showing 41 changed files with 2,769 additions and 487 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* [1851](https://github.com/zeta-chain/node/pull/1851) - rename usdt to erc20 in e2e tests
* [1872](https://github.com/zeta-chain/node/pull/1872) - remove usage of RPC in unit test
* [1805](https://github.com/zeta-chain/node/pull/1805) - add admin and performance test and fix upgrade test
* [1879](https://github.com/zeta-chain/node/pull/1879) - full coverage for messages in types packages

### Fixes

Expand Down
52 changes: 52 additions & 0 deletions x/authority/types/message_update_policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package types_test
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/testutil/sample"

"github.com/zeta-chain/zetacore/x/authority/types"
)

Expand Down Expand Up @@ -49,3 +51,53 @@ func TestMsgUpdatePolicies_ValidateBasic(t *testing.T) {
})
}
}

func TestMsgUpdatePolicies_GetSigners(t *testing.T) {
signer := sample.AccAddress()
tests := []struct {
name string
msg *types.MsgUpdatePolicies
panics bool
}{
{
name: "valid signer",
msg: types.NewMsgUpdatePolicies(signer, sample.Policies()),
panics: false,
},
{
name: "invalid signer",
msg: types.NewMsgUpdatePolicies("invalid", sample.Policies()),
panics: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !tt.panics {
signers := tt.msg.GetSigners()
require.Equal(t, []sdk.AccAddress{sdk.MustAccAddressFromBech32(signer)}, signers)
} else {
require.Panics(t, func() {
tt.msg.GetSigners()
})
}
})
}
}

func TestMsgUpdatePolicies_Type(t *testing.T) {
msg := types.NewMsgUpdatePolicies(sample.AccAddress(), sample.Policies())
require.Equal(t, types.TypeMsgUpdatePolicies, msg.Type())
}

func TestMsgUpdatePolicies_Route(t *testing.T) {
msg := types.NewMsgUpdatePolicies(sample.AccAddress(), sample.Policies())
require.Equal(t, types.RouterKey, msg.Route())
}

func TestMsgUpdatePolicies_GetSignBytes(t *testing.T) {
msg := types.NewMsgUpdatePolicies(sample.AccAddress(), sample.Policies())
require.NotPanics(t, func() {
msg.GetSignBytes()
})
}
3 changes: 3 additions & 0 deletions x/crosschain/types/message_abort_stuck_cctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,8 @@ func (msg *MsgAbortStuckCCTX) ValidateBasic() error {
if err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
if len(msg.CctxIndex) != 66 {
return ErrInvalidCCTXIndex
}
return nil
}
72 changes: 61 additions & 11 deletions x/crosschain/types/message_abort_stuck_cctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package types_test
import (
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/testutil/sample"
Expand All @@ -12,24 +13,23 @@ import (
func TestMsgAbortStuckCCTX_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg types.MsgAbortStuckCCTX
msg *types.MsgAbortStuckCCTX
err error
}{
{
name: "invalid address",
msg: types.MsgAbortStuckCCTX{
Creator: "invalid_address",
CctxIndex: "cctx_index",
},
err: sdkerrors.ErrInvalidAddress,
msg: types.NewMsgAbortStuckCCTX("invalid_address", "cctx_index"),
err: sdkerrors.ErrInvalidAddress,
},
{
name: "invalid cctx index",
msg: types.NewMsgAbortStuckCCTX(sample.AccAddress(), "cctx_index"),
err: types.ErrInvalidCCTXIndex,
},
{
name: "valid",
msg: types.MsgAbortStuckCCTX{
Creator: sample.AccAddress(),
CctxIndex: "cctx_index",
},
err: nil,
msg: types.NewMsgAbortStuckCCTX(sample.AccAddress(), sample.GetCctxIndexFromString("test")),
err: nil,
},
}
for _, tt := range tests {
Expand All @@ -43,3 +43,53 @@ func TestMsgAbortStuckCCTX_ValidateBasic(t *testing.T) {
})
}
}

func TestMsgAbortStuckCCTX_GetSigners(t *testing.T) {
signer := sample.AccAddress()
tests := []struct {
name string
msg *types.MsgAbortStuckCCTX
panics bool
}{
{
name: "valid signer",
msg: types.NewMsgAbortStuckCCTX(signer, "cctx_index"),
panics: false,
},
{
name: "invalid signer",
msg: types.NewMsgAbortStuckCCTX("invalid", "cctx_index"),
panics: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !tt.panics {
signers := tt.msg.GetSigners()
require.Equal(t, []sdk.AccAddress{sdk.MustAccAddressFromBech32(signer)}, signers)
} else {
require.Panics(t, func() {
tt.msg.GetSigners()
})
}
})
}
}

func TestMsgAbortStuckCCTX_Type(t *testing.T) {
msg := types.NewMsgAbortStuckCCTX(sample.AccAddress(), "cctx_index")
require.Equal(t, types.TypeMsgAbortStuckCCTX, msg.Type())
}

func TestMsgAbortStuckCCTX_Route(t *testing.T) {
msg := types.NewMsgAbortStuckCCTX(sample.AccAddress(), "cctx_index")
require.Equal(t, types.RouterKey, msg.Route())
}

func TestMsgAbortStuckCCTX_GetSignBytes(t *testing.T) {
msg := types.NewMsgAbortStuckCCTX(sample.AccAddress(), "cctx_index")
require.NotPanics(t, func() {
msg.GetSignBytes()
})
}
127 changes: 111 additions & 16 deletions x/crosschain/types/message_add_to_in_tx_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/stretchr/testify/require"
"github.com/zeta-chain/zetacore/common"
Expand All @@ -14,37 +15,56 @@ import (
func TestMsgAddToInTxTracker_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg types.MsgAddToInTxTracker
msg *types.MsgAddToInTxTracker
err error
}{
{
name: "invalid address",
msg: types.MsgAddToInTxTracker{
Creator: "invalid_address",
ChainId: common.GoerliChain().ChainId,
TxHash: "hash",
CoinType: common.CoinType_Gas,
},
msg: types.NewMsgAddToInTxTracker(
"invalid_address",
common.GoerliChain().ChainId,
common.CoinType_Gas,
"hash",
),
err: sdkerrors.ErrInvalidAddress,
},
{
name: "invalid chain id",
msg: types.MsgAddToInTxTracker{
msg: types.NewMsgAddToInTxTracker(
sample.AccAddress(),
42,
common.CoinType_Gas,
"hash",
),
err: errorsmod.Wrapf(types.ErrInvalidChainID, "chain id (%d)", 42),
},
{
name: "invalid proof",
msg: &types.MsgAddToInTxTracker{
Creator: sample.AccAddress(),
ChainId: 42,
TxHash: "hash",
ChainId: common.ZetaTestnetChain().ChainId,
CoinType: common.CoinType_Gas,
Proof: &common.Proof{},
},
err: errorsmod.Wrapf(types.ErrInvalidChainID, "chain id (%d)", 42),
err: errorsmod.Wrapf(types.ErrProofVerificationFail, "chain id %d does not support proof-based trackers", common.ZetaTestnetChain().ChainId),
},
{
name: "valid",
msg: types.MsgAddToInTxTracker{
name: "invalid coin type",
msg: &types.MsgAddToInTxTracker{
Creator: sample.AccAddress(),
ChainId: common.GoerliChain().ChainId,
TxHash: "hash",
CoinType: common.CoinType_Gas,
ChainId: common.ZetaTestnetChain().ChainId,
CoinType: 5,
},
err: errorsmod.Wrapf(types.ErrProofVerificationFail, "coin-type not supported"),
},
{
name: "valid",
msg: types.NewMsgAddToInTxTracker(
sample.AccAddress(),
common.GoerliChain().ChainId,
common.CoinType_Gas,
"hash",
),
err: nil,
},
}
Expand All @@ -59,3 +79,78 @@ func TestMsgAddToInTxTracker_ValidateBasic(t *testing.T) {
})
}
}

func TestMsgAddToInTxTracker_GetSigners(t *testing.T) {
signer := sample.AccAddress()
tests := []struct {
name string
msg *types.MsgAddToInTxTracker
panics bool
}{
{
name: "valid signer",
msg: types.NewMsgAddToInTxTracker(
signer,
common.GoerliChain().ChainId,
common.CoinType_Gas,
"hash",
),
panics: false,
},
{
name: "invalid signer",
msg: types.NewMsgAddToInTxTracker(
"invalid_address",
common.GoerliChain().ChainId,
common.CoinType_Gas,
"hash",
),
panics: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if !tt.panics {
signers := tt.msg.GetSigners()
require.Equal(t, []sdk.AccAddress{sdk.MustAccAddressFromBech32(signer)}, signers)
} else {
require.Panics(t, func() {
tt.msg.GetSigners()
})
}
})
}
}

func TestMsgAddToInTxTracker_Type(t *testing.T) {
msg := types.NewMsgAddToInTxTracker(
sample.AccAddress(),
common.GoerliChain().ChainId,
common.CoinType_Gas,
"hash",
)
require.Equal(t, types.TypeMsgAddToInTxTracker, msg.Type())
}

func TestMsgAddToInTxTracker_Route(t *testing.T) {
msg := types.NewMsgAddToInTxTracker(
sample.AccAddress(),
common.GoerliChain().ChainId,
common.CoinType_Gas,
"hash",
)
require.Equal(t, types.RouterKey, msg.Route())
}

func TestMsgAddToInTxTracker_GetSignBytes(t *testing.T) {
msg := types.NewMsgAddToInTxTracker(
sample.AccAddress(),
common.GoerliChain().ChainId,
common.CoinType_Gas,
"hash",
)
require.NotPanics(t, func() {
msg.GetSignBytes()
})
}
2 changes: 1 addition & 1 deletion x/crosschain/types/message_add_to_out_tx_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (msg *MsgAddToOutTxTracker) ValidateBasic() error {
return cosmoserrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
if msg.ChainId < 0 {
return cosmoserrors.Wrapf(ErrInvalidChainID, "chain id (%d)", msg.ChainId)
return cosmoserrors.Wrapf(sdkerrors.ErrInvalidChainID, "chain id (%d)", msg.ChainId)
}
return nil
}
Loading

0 comments on commit 810c7c1

Please sign in to comment.