2019-05-30 08:44:28 -07:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
2020-07-06 12:50:09 -07:00
|
|
|
"fmt"
|
2019-05-30 08:44:28 -07:00
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-03-25 08:20:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2020-07-09 05:21:20 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
2020-01-22 09:54:56 -08:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-05-30 08:44:28 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_runImportCmd(t *testing.T) {
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd := ImportKeyCommand()
|
2020-07-20 08:12:33 -07:00
|
|
|
cmd.Flags().AddFlagSet(Commands("home").PersistentFlags())
|
2020-07-09 23:55:48 -07:00
|
|
|
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
|
2019-05-30 08:44:28 -07:00
|
|
|
|
|
|
|
// Now add a temporary keybase
|
2020-07-09 05:21:20 -07:00
|
|
|
kbHome, cleanUp := testutil.NewTestCaseDir(t)
|
2020-03-01 16:16:23 -08:00
|
|
|
t.Cleanup(cleanUp)
|
2019-05-30 08:44:28 -07:00
|
|
|
|
2020-07-06 12:50:09 -07:00
|
|
|
kb, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendTest, kbHome, mockIn)
|
2020-03-30 11:30:50 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() {
|
2020-04-08 02:38:28 -07:00
|
|
|
kb.Delete("keyname1") // nolint:errcheck
|
2020-03-30 11:30:50 -07:00
|
|
|
})
|
2019-11-14 06:17:21 -08:00
|
|
|
|
2019-05-30 08:44:28 -07:00
|
|
|
keyfile := filepath.Join(kbHome, "key.asc")
|
|
|
|
armoredKey := `-----BEGIN TENDERMINT PRIVATE KEY-----
|
|
|
|
salt: A790BB721D1C094260EA84F5E5B72289
|
|
|
|
kdf: bcrypt
|
|
|
|
|
|
|
|
HbP+c6JmeJy9JXe2rbbF1QtCX1gLqGcDQPBXiCtFvP7/8wTZtVOPj8vREzhZ9ElO
|
|
|
|
3P7YnrzPQThG0Q+ZnRSbl9MAS8uFAM4mqm5r/Ys=
|
|
|
|
=f3l4
|
|
|
|
-----END TENDERMINT PRIVATE KEY-----
|
|
|
|
`
|
|
|
|
require.NoError(t, ioutil.WriteFile(keyfile, []byte(armoredKey), 0644))
|
|
|
|
|
2020-03-30 11:30:50 -07:00
|
|
|
mockIn.Reset("123456789\n")
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd.SetArgs([]string{
|
|
|
|
"keyname1", keyfile,
|
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagHome, kbHome),
|
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, keyring.BackendTest),
|
|
|
|
})
|
|
|
|
require.NoError(t, cmd.Execute())
|
2019-05-30 08:44:28 -07:00
|
|
|
}
|