Skip to content

Commit

Permalink
chore(test): prepare test files for refactor (evmos#1391)
Browse files Browse the repository at this point in the history
* chore(test): prepare test files for refactor

* refactor modules

* changelog

* lint
  • Loading branch information
fedekunze authored Feb 14, 2023
1 parent 2871b8e commit c545822
Show file tree
Hide file tree
Showing 125 changed files with 6,199 additions and 4,723 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

- (test) [\#1391](https://github.com/evmos/evmos/pull/1391) Refactor test files
- (claims) [#1378](https://github.com/evmos/evmos/pull/1378) Validate authorized channels when updating claims params
- (test) [#1348](https://github.com/evmos/evmos/pull/1348) Add query executions to e2e upgrade test suite
- (deps) [#1370](https://github.com/evmos/evmos/pull/1370) Bump Cosmos SDK version to [`v0.46.9-ledger`](https://github.com/evmos/cosmos-sdk/releases/tag/v0.46.9-ledger)
Expand All @@ -73,7 +74,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

- (deps) [#1370](https://github.com/evmos/evmos/pull/1370) Bump Cosmos SDK version to [`v0.46.9-ledger`](https://github.com/evmos/cosmos-sdk/releases/tag/v0.46.9-ledger)
- (deps) [#1370](https://github.com/evmos/evmos/pull/1370) Bump Tendermint version to [`v0.34.26`](https://github.com/informalsystems/tendermint/releases/tag/v0.34.26)
- (deps) [#1374](https://github.com/evmos/evmos/pull/1374) Bump Gin version to [`v1.7.7`](github.com/gin-gonic/gin/releases/tag/v1.7.7)
- (deps) [#1374](https://github.com/evmos/evmos/pull/1374) Bump Gin version to [`v1.7.7`](https://github.com/gin-gonic/gin/releases/tag/v1.7.7)
- (ante) [#1382](https://github.com/evmos/evmos/pull/1382) Add `AnteHandler` decorator for `x/authz` messages

## [v11.0.1] - 2023-02-04
Expand Down
170 changes: 0 additions & 170 deletions app/ante/cosmos/ante_test.go

This file was deleted.

158 changes: 158 additions & 0 deletions app/ante/cosmos/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"time"

"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
sdkvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
"github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
Expand Down Expand Up @@ -270,3 +272,159 @@ func TestAuthzLimiterDecorator(t *testing.T) {
})
}
}

func (suite *AnteTestSuite) TestRejectMsgsInAuthz() {
testPrivKeys, testAddresses, err := generatePrivKeyAddressPairs(10)
suite.Require().NoError(err)

distantFuture := time.Date(9000, 1, 1, 0, 0, 0, 0, time.UTC)

newMsgGrant := func(msgTypeUrl string) *authz.MsgGrant {
msg, err := authz.NewMsgGrant(
testAddresses[0],
testAddresses[1],
authz.NewGenericAuthorization(msgTypeUrl),
&distantFuture,
)
if err != nil {
panic(err)
}
return msg
}

testcases := []struct {
name string
msgs []sdk.Msg
expectedCode uint32
isEIP712 bool
}{
{
name: "a MsgGrant with MsgEthereumTx typeURL on the authorization field is blocked",
msgs: []sdk.Msg{newMsgGrant(sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}))},
expectedCode: sdkerrors.ErrUnauthorized.ABCICode(),
},
{
name: "a MsgGrant with MsgCreateVestingAccount typeURL on the authorization field is blocked",
msgs: []sdk.Msg{newMsgGrant(sdk.MsgTypeURL(&sdkvesting.MsgCreateVestingAccount{}))},
expectedCode: sdkerrors.ErrUnauthorized.ABCICode(),
},
{
name: "a MsgGrant with MsgEthereumTx typeURL on the authorization field included on EIP712 tx is blocked",
msgs: []sdk.Msg{newMsgGrant(sdk.MsgTypeURL(&evmtypes.MsgEthereumTx{}))},
expectedCode: sdkerrors.ErrUnauthorized.ABCICode(),
isEIP712: true,
},
{
name: "a MsgExec with nested messages (valid: MsgSend and invalid: MsgEthereumTx) is blocked",
msgs: []sdk.Msg{
newMsgExec(
testAddresses[1],
[]sdk.Msg{
banktypes.NewMsgSend(
testAddresses[0],
testAddresses[3],
sdk.NewCoins(sdk.NewInt64Coin(evmtypes.DefaultEVMDenom, 100e6)),
),
&evmtypes.MsgEthereumTx{},
},
),
},
expectedCode: sdkerrors.ErrUnauthorized.ABCICode(),
},
{
name: "a MsgExec with nested MsgExec messages that has invalid messages is blocked",
msgs: []sdk.Msg{
createNestedMsgExec(
testAddresses[1],
2,
[]sdk.Msg{
&evmtypes.MsgEthereumTx{},
},
),
},
expectedCode: sdkerrors.ErrUnauthorized.ABCICode(),
},
{
name: "a MsgExec with more nested MsgExec messages than allowed and with valid messages is blocked",
msgs: []sdk.Msg{
createNestedMsgExec(
testAddresses[1],
6,
[]sdk.Msg{
banktypes.NewMsgSend(
testAddresses[0],
testAddresses[3],
sdk.NewCoins(sdk.NewInt64Coin(evmtypes.DefaultEVMDenom, 100e6)),
),
},
),
},
expectedCode: sdkerrors.ErrUnauthorized.ABCICode(),
},
{
name: "two MsgExec messages NOT containing a blocked msg but between the two have more nesting than the allowed. Then, is blocked",
msgs: []sdk.Msg{
createNestedMsgExec(
testAddresses[1],
5,
[]sdk.Msg{
banktypes.NewMsgSend(
testAddresses[0],
testAddresses[3],
sdk.NewCoins(sdk.NewInt64Coin(evmtypes.DefaultEVMDenom, 100e6)),
),
},
),
createNestedMsgExec(
testAddresses[1],
5,
[]sdk.Msg{
banktypes.NewMsgSend(
testAddresses[0],
testAddresses[3],
sdk.NewCoins(sdk.NewInt64Coin(evmtypes.DefaultEVMDenom, 100e6)),
),
},
),
},
expectedCode: sdkerrors.ErrUnauthorized.ABCICode(),
},
}

for _, tc := range testcases {
suite.Run(fmt.Sprintf("Case %s", tc.name), func() {
suite.SetupTest()
var (
tx sdk.Tx
err error
)

if tc.isEIP712 {
tx, err = createEIP712CosmosTx(testAddresses[0], testPrivKeys[0], tc.msgs)
} else {
tx, err = createTx(testPrivKeys[0], tc.msgs...)
}
suite.Require().NoError(err)

txEncoder := suite.clientCtx.TxConfig.TxEncoder()
bz, err := txEncoder(tx)
suite.Require().NoError(err)

resCheckTx := suite.app.CheckTx(
abci.RequestCheckTx{
Tx: bz,
Type: abci.CheckTxType_New,
},
)
suite.Require().Equal(resCheckTx.Code, tc.expectedCode, resCheckTx.Log)

// TODO uncomment this on v12 release. ATM the anteHandler works on CheckTx mode
// resDeliverTx := suite.app.DeliverTx(
// abci.RequestDeliverTx{
// Tx: bz,
// },
// )
// suite.Require().Equal(resDeliverTx.Code, tc.expectedCode, resDeliverTx.Log)
})
}
}
Loading

0 comments on commit c545822

Please sign in to comment.