## Description The `io/ioutil` package has been deprecated in Go 1.16 (See https://golang.org/doc/go1.16#ioutil). Since cosmos-sdk has upgraded to Go 1.17 (#9987), this PR replaces the existing `io/ioutil` functions with their new definitions in `io` and `os` packages. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
121 lines
3.0 KiB
Go
121 lines
3.0 KiB
Go
package keys
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
)
|
|
|
|
func Test_runImportCmd(t *testing.T) {
|
|
cdc := simapp.MakeTestEncodingConfig().Codec
|
|
testCases := []struct {
|
|
name string
|
|
keyringBackend string
|
|
userInput string
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "test backend success",
|
|
keyringBackend: keyring.BackendTest,
|
|
// key armor passphrase
|
|
userInput: "123456789\n",
|
|
},
|
|
{
|
|
name: "test backend fail with wrong armor pass",
|
|
keyringBackend: keyring.BackendTest,
|
|
userInput: "987654321\n",
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "file backend success",
|
|
keyringBackend: keyring.BackendFile,
|
|
// key armor passphrase + keyring password x2
|
|
userInput: "123456789\n12345678\n12345678\n",
|
|
},
|
|
{
|
|
name: "file backend fail with wrong armor pass",
|
|
keyringBackend: keyring.BackendFile,
|
|
userInput: "987654321\n12345678\n12345678\n",
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "file backend fail with wrong keyring pass",
|
|
keyringBackend: keyring.BackendFile,
|
|
userInput: "123465789\n12345678\n87654321\n",
|
|
expectError: true,
|
|
},
|
|
{
|
|
name: "file backend fail with no keyring pass",
|
|
keyringBackend: keyring.BackendFile,
|
|
userInput: "123465789\n",
|
|
expectError: true,
|
|
},
|
|
}
|
|
|
|
armoredKey := `-----BEGIN TENDERMINT PRIVATE KEY-----
|
|
salt: A790BB721D1C094260EA84F5E5B72289
|
|
kdf: bcrypt
|
|
|
|
HbP+c6JmeJy9JXe2rbbF1QtCX1gLqGcDQPBXiCtFvP7/8wTZtVOPj8vREzhZ9ElO
|
|
3P7YnrzPQThG0Q+ZnRSbl9MAS8uFAM4mqm5r/Ys=
|
|
=f3l4
|
|
-----END TENDERMINT PRIVATE KEY-----
|
|
`
|
|
|
|
for _, tc := range testCases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
cmd := ImportKeyCommand()
|
|
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
|
|
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
|
|
|
|
// Now add a temporary keybase
|
|
kbHome := t.TempDir()
|
|
kb, err := keyring.New(sdk.KeyringServiceName(), tc.keyringBackend, kbHome, nil, cdc)
|
|
require.NoError(t, err)
|
|
|
|
clientCtx := client.Context{}.
|
|
WithKeyringDir(kbHome).
|
|
WithKeyring(kb).
|
|
WithInput(mockIn).
|
|
WithCodec(cdc)
|
|
ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx)
|
|
|
|
t.Cleanup(func() {
|
|
kb.Delete("keyname1") // nolint:errcheck
|
|
})
|
|
|
|
keyfile := filepath.Join(kbHome, "key.asc")
|
|
|
|
require.NoError(t, os.WriteFile(keyfile, []byte(armoredKey), 0644))
|
|
|
|
defer func() {
|
|
_ = os.RemoveAll(kbHome)
|
|
}()
|
|
|
|
mockIn.Reset(tc.userInput)
|
|
cmd.SetArgs([]string{
|
|
"keyname1", keyfile,
|
|
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, tc.keyringBackend),
|
|
})
|
|
|
|
err = cmd.ExecuteContext(ctx)
|
|
if tc.expectError {
|
|
require.Error(t, err)
|
|
} else {
|
|
require.NoError(t, err)
|
|
}
|
|
})
|
|
}
|
|
}
|