From da194c7ca4e486880a6a1f6d1bab85c16c06ecee Mon Sep 17 00:00:00 2001 From: Youngtaek Yoon Date: Wed, 19 Oct 2022 11:47:29 +0000 Subject: [PATCH] fix: add restrictions on the number of args in the CLIs (#734) * Add restrictions on the number of args in the CLIs * Add unit tests * Update CHANGELOG.md * Update CHANGELOG.md --- CHANGELOG.md | 1 + x/auth/client/cli/query.go | 2 ++ x/auth/client/testutil/suite.go | 57 +++++++++++++++++++++++++++++---- x/bank/client/cli/query.go | 2 ++ x/bank/client/testutil/suite.go | 24 ++++++++++++++ x/gov/client/cli/query.go | 1 + x/gov/client/testutil/suite.go | 8 +++++ 7 files changed, 88 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27e03396a6..5bad0ae4e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -113,6 +113,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (crypto) [\#731](https://github.com/line/lbm-sdk/pull/731) remove VRFProve function * (x/foundation) [\#732](https://github.com/line/lbm-sdk/pull/732) add verification on accounts into x/foundation Grants cli * (x/foundation) [\#730](https://github.com/line/lbm-sdk/pull/730) prune stale x/foundation proposals at voting period end +* (cli) [\#734](https://github.com/line/lbm-sdk/pull/734) add restrictions on the number of args in the CLIs ### Breaking Changes * (proto) [\#564](https://github.com/line/lbm-sdk/pull/564) change gRPC path to original cosmos path diff --git a/x/auth/client/cli/query.go b/x/auth/client/cli/query.go index 2480b67b1c..9438db9000 100644 --- a/x/auth/client/cli/query.go +++ b/x/auth/client/cli/query.go @@ -115,6 +115,7 @@ func GetAccountCmd() *cobra.Command { func GetAccountsCmd() *cobra.Command { cmd := &cobra.Command{ Use: "accounts", + Args: cobra.NoArgs, Short: "Query all the accounts", RunE: func(cmd *cobra.Command, args []string) error { clientCtx, err := client.GetClientQueryContext(cmd) @@ -147,6 +148,7 @@ func GetAccountsCmd() *cobra.Command { func QueryTxsByEventsCmd() *cobra.Command { cmd := &cobra.Command{ Use: "txs", + Args: cobra.NoArgs, Short: "Query for paginated transactions that match a set of events", Long: strings.TrimSpace( fmt.Sprintf(` diff --git a/x/auth/client/testutil/suite.go b/x/auth/client/testutil/suite.go index 4545ba9619..6c7f6017e8 100644 --- a/x/auth/client/testutil/suite.go +++ b/x/auth/client/testutil/suite.go @@ -453,6 +453,7 @@ func (s *IntegrationTestSuite) TestCLIQueryTxsCmdByEvents() { testCases := []struct { name string args []string + expectErr bool expectEmpty bool }{ { @@ -463,6 +464,7 @@ func (s *IntegrationTestSuite) TestCLIQueryTxsCmdByEvents() { fmt.Sprintf("--%s=json", ostcli.OutputFlag), }, false, + false, }, { "no matching fee event", @@ -471,6 +473,16 @@ func (s *IntegrationTestSuite) TestCLIQueryTxsCmdByEvents() { sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(0))).String()), fmt.Sprintf("--%s=json", ostcli.OutputFlag), }, + false, + true, + }, + { + "wrong number of arguments", + []string{ + "extra", + fmt.Sprintf("--%s=json", ostcli.OutputFlag), + }, + true, true, }, } @@ -482,6 +494,10 @@ func (s *IntegrationTestSuite) TestCLIQueryTxsCmdByEvents() { clientCtx := val.ClientCtx out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) + if tc.expectErr { + s.Require().Error(err) + return + } s.Require().NoError(err) var result sdk.SearchTxsResult @@ -1122,16 +1138,43 @@ func (s *IntegrationTestSuite) TestGetAccountCmd() { func (s *IntegrationTestSuite) TestGetAccountsCmd() { val := s.network.Validators[0] - clientCtx := val.ClientCtx - out, err := clitestutil.ExecTestCLICmd(clientCtx, authcli.GetAccountsCmd(), []string{ + commonArgs := []string{ fmt.Sprintf("--%s=json", ostcli.OutputFlag), - }) - s.Require().NoError(err) + } - var res authtypes.QueryAccountsResponse - s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res)) - s.Require().NotEmpty(res.Accounts) + testCases := map[string]struct { + args []string + valid bool + }{ + "valid request": { + valid: true, + }, + "wrong number of args": { + args: []string{ + "extra", + }, + }, + } + + for name, tc := range testCases { + tc := tc + s.Run(name, func() { + cmd := authcli.GetAccountsCmd() + clientCtx := val.ClientCtx + + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, append(tc.args, commonArgs...)) + if !tc.valid { + s.Require().Error(err) + return + } + s.Require().NoError(err) + + var res authtypes.QueryAccountsResponse + s.Require().NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &res)) + s.Require().NotEmpty(res.Accounts) + }) + } } func TestGetBroadcastCommandOfflineFlag(t *testing.T) { diff --git a/x/bank/client/cli/query.go b/x/bank/client/cli/query.go index 50a7b3a19f..0397dd5353 100644 --- a/x/bank/client/cli/query.go +++ b/x/bank/client/cli/query.go @@ -105,6 +105,7 @@ Example: func GetCmdDenomsMetadata() *cobra.Command { cmd := &cobra.Command{ Use: "denom-metadata", + Args: cobra.NoArgs, Short: "Query the client metadata for coin denominations", Long: strings.TrimSpace( fmt.Sprintf(`Query the client metadata for all the registered coin denominations @@ -158,6 +159,7 @@ To query for the client metadata of a specific coin denomination use: func GetCmdQueryTotalSupply() *cobra.Command { cmd := &cobra.Command{ Use: "total", + Args: cobra.NoArgs, Short: "Query the total supply of coins of the chain", Long: strings.TrimSpace( fmt.Sprintf(`Query total supply of coins that are held by accounts in the chain. diff --git a/x/bank/client/testutil/suite.go b/x/bank/client/testutil/suite.go index 6f82ac76d3..6348268750 100644 --- a/x/bank/client/testutil/suite.go +++ b/x/bank/client/testutil/suite.go @@ -214,6 +214,15 @@ func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() { Amount: sdk.ZeroInt(), }, }, + { + name: "wrong number of arguments", + args: []string{ + "extra", + fmt.Sprintf("--%s=1", flags.FlagHeight), + fmt.Sprintf("--%s=json", ostcli.OutputFlag), + }, + expectErr: true, + }, } for _, tc := range testCases { @@ -341,6 +350,21 @@ func (s *IntegrationTestSuite) TestGetCmdQueryDenomsMetadata() { }, }, }, + { + name: "wrong number of arguments", + args: []string{ + "extra", + fmt.Sprintf("--%s=1", flags.FlagHeight), + fmt.Sprintf("--%s=json", ostcli.OutputFlag), + }, + expectErr: true, + respType: &types.QueryDenomMetadataResponse{}, + expected: &types.QueryDenomMetadataResponse{ + Metadata: types.Metadata{ + DenomUnits: []*types.DenomUnit{}, + }, + }, + }, } for _, tc := range testCases { diff --git a/x/gov/client/cli/query.go b/x/gov/client/cli/query.go index d668314b38..27888c6ddf 100644 --- a/x/gov/client/cli/query.go +++ b/x/gov/client/cli/query.go @@ -94,6 +94,7 @@ $ %s query gov proposal 1 func GetCmdQueryProposals() *cobra.Command { cmd := &cobra.Command{ Use: "proposals", + Args: cobra.NoArgs, Short: "Query proposals with optional filters", Long: strings.TrimSpace( fmt.Sprintf(`Query for a all paginated proposals that match optional filters: diff --git a/x/gov/client/testutil/suite.go b/x/gov/client/testutil/suite.go index a1162a2e56..209549da87 100644 --- a/x/gov/client/testutil/suite.go +++ b/x/gov/client/testutil/suite.go @@ -435,6 +435,14 @@ func (s *IntegrationTestSuite) TestCmdGetProposals() { }, true, }, + { + "wrong number of arguments", + []string{ + "extra", + fmt.Sprintf("--%s=json", ostcli.OutputFlag), + }, + true, + }, } for _, tc := range testCases {