Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add more tests in middleware for code cov #11694

Merged
merged 2 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions x/auth/middleware/basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,37 @@ func (s *MWTestSuite) TestValidateBasic() {
invalidTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID())
s.Require().NoError(err)

// DeliverTx
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx})
s.Require().NotNil(err, "Did not error on invalid tx")

// SimulateTx
_, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx})
s.Require().NotNil(err, "Did not error on invalid tx")

privs, accNums, accSeqs = []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
validTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID())
s.Require().NoError(err)

// DeliverTx
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: validTx})
s.Require().Nil(err, "ValidateBasicMiddleware returned error on valid tx. err: %v", err)

// SimulateTx
_, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: validTx})
s.Require().Nil(err, "ValidateBasicMiddleware returned error on valid tx. err: %v", err)

// test middleware skips on recheck
ctx = ctx.WithIsReCheckTx(true)

// middleware should skip processing invalidTx on recheck and thus return nil-error
// DeliverTx
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx})
s.Require().Nil(err, "ValidateBasicMiddleware ran on ReCheck")

// SimulateTx
_, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx})
s.Require().Nil(err, "ValidateBasicMiddleware ran on ReCheck")
}

func (s *MWTestSuite) TestValidateMemo() {
Expand All @@ -73,17 +88,26 @@ func (s *MWTestSuite) TestValidateMemo() {
s.Require().NoError(err)

// require that long memos get rejected
// DeliverTx
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx})
s.Require().NotNil(err, "Did not error on tx with high memo")

// SimulateTx
_, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: invalidTx})
s.Require().NotNil(err, "Did not error on tx with high memo")

txBuilder.SetMemo(strings.Repeat("01234567890", 10))
validTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID())
s.Require().NoError(err)

// require small memos pass ValidateMemo middleware
// DeliverTx
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: validTx})
s.Require().Nil(err, "ValidateBasicMiddleware returned error on valid tx. err: %v", err)

// SimulateTx
_, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: validTx})
s.Require().Nil(err, "ValidateBasicMiddleware returned error on valid tx. err: %v", err)
}

func (s *MWTestSuite) TestConsumeGasForTxSize() {
Expand Down Expand Up @@ -214,8 +238,15 @@ func (s *MWTestSuite) TestTxHeightTimeoutMiddleware() {
s.Require().NoError(err)

ctx := ctx.WithBlockHeight(tc.height)

// DeliverTx
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx})
s.Require().Equal(tc.expectErr, err != nil, err)

// SimulateTx
_, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx})
s.Require().Equal(tc.expectErr, err != nil, err)

})
}
}
16 changes: 14 additions & 2 deletions x/auth/middleware/fee_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,27 @@ func (s *MWTestSuite) TestDeductFees() {
err = testutil.FundAccount(s.app.BankKeeper, ctx, addr1, coins)
s.Require().NoError(err)

// DeliverTx
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx})
s.Require().NotNil(err, "Tx did not error when fee payer had insufficient funds")
s.Require().NotNil(err, "Tx errored when fee payer had insufficient funds")

// SimulateTx
_, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx})
s.Require().NotNil(err, "Tx errored when fee payer had insufficient funds")

// Set account with sufficient funds
s.app.AccountKeeper.SetAccount(ctx, acc)
err = testutil.FundAccount(s.app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200))))
s.Require().NoError(err)

// DeliverTx
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx})
s.Require().Nil(err, "Tx did not error after account has been set with sufficient funds")

err = testutil.FundAccount(s.app.BankKeeper, ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200))))
s.Require().NoError(err)

s.Require().Nil(err, "Tx errored after account has been set with sufficient funds")
// SimulateTx
_, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx})
s.Require().Nil(err, "Tx did not error after account has been set with sufficient funds")
}
3 changes: 3 additions & 0 deletions x/auth/middleware/gas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,13 @@ func (s *MWTestSuite) TestSetup() {
for _, tc := range testcases {
s.Run(tc.name, func() {
res, _, err := txHandler.CheckTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: tc.tx}, tx.RequestCheckTx{})
_, simErr := txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: tc.tx})
if tc.expErr {
s.Require().EqualError(err, tc.errorStr)
s.Require().EqualError(simErr, tc.errorStr)
} else {
s.Require().Nil(err, "SetUpContextMiddleware returned error")
s.Require().Nil(simErr, "SetUpContextMiddleware returned error")
s.Require().Equal(tc.expGasLimit, uint64(res.GasWanted))
}
})
Expand Down
5 changes: 5 additions & 0 deletions x/auth/middleware/run_msgs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ func (s *MWTestSuite) TestRunMsgs() {
txBytes, err := s.clientCtx.TxConfig.TxEncoder()(testTx)
s.Require().NoError(err)

// DeliverTx
res, err := txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx, TxBytes: txBytes})
s.Require().NoError(err)
s.Require().Len(res.MsgResponses, 1)
s.Require().Equal(fmt.Sprintf("/%s", proto.MessageName(&testdata.MsgCreateDogResponse{})), res.MsgResponses[0].TypeUrl)

// SimulateTx
_, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx, TxBytes: txBytes})
s.Require().NoError(err)
}
5 changes: 5 additions & 0 deletions x/auth/middleware/sigverify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (s *MWTestSuite) TestSetPubKey() {
testTx, _, err := s.createTestTx(txBuilder, privs, accNums, accSeqs, ctx.ChainID())
require.NoError(err)

// DeliverTx
_, err = txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx})
require.NoError(err)

Expand All @@ -65,6 +66,10 @@ func (s *MWTestSuite) TestSetPubKey() {
require.True(pubs[i].Equals(pk),
"Wrong Pubkey retrieved from AccountKeeper, idx=%d\nexpected=%s\n got=%s", i, pubs[i], pk)
}

// SimulateTx
_, err = txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tx.Request{Tx: testTx})
require.NoError(err)
}

func (s *MWTestSuite) TestConsumeSignatureVerificationGas() {
Expand Down
7 changes: 7 additions & 0 deletions x/auth/middleware/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,18 @@ func (s *MWTestSuite) TestTxDecoderMiddleware() {
txReqChecker,
middleware.NewTxDecoderMiddleware(s.clientCtx.TxConfig.TxDecoder()),
)

// DeliverTx
_, err := txHandler.DeliverTx(sdk.WrapSDKContext(ctx), tc.req)

// SimulateTx
_, simErr := txHandler.SimulateTx(sdk.WrapSDKContext(ctx), tc.req)
if tc.expErr {
require.Error(err)
require.Error(simErr)
} else {
require.NoError(err)
require.NoError(simErr)
}
})
}
Expand Down