2019-02-08 12:45:23 -08:00
package keys
import (
2021-06-25 04:55:58 -07:00
"bytes"
2020-10-08 10:41:35 -07:00
"context"
2020-07-06 12:50:09 -07:00
"fmt"
2021-06-25 04:55:58 -07:00
"io/ioutil"
2019-02-08 12:45:23 -08:00
"testing"
2019-11-14 06:17:21 -08:00
"github.com/stretchr/testify/require"
2019-05-28 01:44:04 -07:00
2019-02-08 12:45:23 -08:00
"github.com/tendermint/tendermint/libs/cli"
2020-10-08 10:41:35 -07:00
"github.com/cosmos/cosmos-sdk/client"
2019-05-28 01:44:04 -07:00
"github.com/cosmos/cosmos-sdk/client/flags"
2020-06-11 07:30:42 -07:00
"github.com/cosmos/cosmos-sdk/crypto/hd"
2020-03-25 08:20:36 -07:00
"github.com/cosmos/cosmos-sdk/crypto/keyring"
2021-06-25 04:55:58 -07:00
"github.com/cosmos/cosmos-sdk/simapp"
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"
2021-08-09 03:35:01 -07:00
bip39 "github.com/cosmos/go-bip39"
2019-02-08 12:45:23 -08:00
)
func Test_runAddCmdBasic ( t * testing . T ) {
2019-12-12 13:52:24 -08:00
cmd := AddKeyCommand ( )
2020-07-20 08:12:33 -07:00
cmd . Flags ( ) . AddFlagSet ( Commands ( "home" ) . PersistentFlags ( ) )
2020-06-11 07:30:42 -07:00
2020-07-09 23:55:48 -07:00
mockIn := testutil . ApplyMockIODiscardOutErr ( cmd )
2020-09-18 04:08:24 -07:00
kbHome := t . TempDir ( )
2021-09-20 05:02:15 -07:00
cdc := simapp . MakeTestEncodingConfig ( ) . Codec
kb , err := keyring . New ( sdk . KeyringServiceName ( ) , keyring . BackendTest , kbHome , mockIn , cdc )
2020-03-30 11:30:50 -07:00
require . NoError ( t , err )
2020-10-08 10:41:35 -07:00
2021-09-20 05:02:15 -07:00
clientCtx := client . Context { } . WithKeyringDir ( kbHome ) . WithInput ( mockIn ) . WithCodec ( cdc )
2020-10-08 10:41:35 -07:00
ctx := context . WithValue ( context . Background ( ) , client . ClientContextKey , & clientCtx )
2020-03-30 11:30:50 -07:00
t . Cleanup ( func ( ) {
2020-06-11 07:30:42 -07:00
_ = kb . Delete ( "keyname1" )
_ = kb . Delete ( "keyname2" )
2020-03-30 11:30:50 -07:00
} )
2020-07-06 12:50:09 -07:00
cmd . SetArgs ( [ ] string {
"keyname1" ,
fmt . Sprintf ( "--%s=%s" , flags . FlagHome , kbHome ) ,
fmt . Sprintf ( "--%s=%s" , cli . OutputFlag , OutputFormatText ) ,
2020-08-12 01:34:10 -07:00
fmt . Sprintf ( "--%s=%s" , flags . FlagKeyAlgorithm , string ( hd . Secp256k1Type ) ) ,
2020-07-06 12:50:09 -07:00
fmt . Sprintf ( "--%s=%s" , flags . FlagKeyringBackend , keyring . BackendTest ) ,
} )
mockIn . Reset ( "y\n" )
2020-10-08 10:41:35 -07:00
require . NoError ( t , cmd . ExecuteContext ( ctx ) )
2019-11-14 06:17:21 -08:00
2020-03-30 11:30:50 -07:00
mockIn . Reset ( "N\n" )
2020-10-08 10:41:35 -07:00
require . Error ( t , cmd . ExecuteContext ( ctx ) )
2020-07-06 12:50:09 -07:00
cmd . SetArgs ( [ ] string {
"keyname2" ,
fmt . Sprintf ( "--%s=%s" , flags . FlagHome , kbHome ) ,
fmt . Sprintf ( "--%s=%s" , cli . OutputFlag , OutputFormatText ) ,
2020-08-12 01:34:10 -07:00
fmt . Sprintf ( "--%s=%s" , flags . FlagKeyAlgorithm , string ( hd . Secp256k1Type ) ) ,
2020-07-06 12:50:09 -07:00
fmt . Sprintf ( "--%s=%s" , flags . FlagKeyringBackend , keyring . BackendTest ) ,
} )
2020-10-08 10:41:35 -07:00
require . NoError ( t , cmd . ExecuteContext ( ctx ) )
require . Error ( t , cmd . ExecuteContext ( ctx ) )
2019-11-14 06:17:21 -08:00
2020-03-30 11:30:50 -07:00
mockIn . Reset ( "y\n" )
2020-10-08 10:41:35 -07:00
require . NoError ( t , cmd . ExecuteContext ( ctx ) )
2020-07-06 12:50:09 -07:00
cmd . SetArgs ( [ ] string {
"keyname4" ,
fmt . Sprintf ( "--%s=%s" , flags . FlagHome , kbHome ) ,
fmt . Sprintf ( "--%s=%s" , cli . OutputFlag , OutputFormatText ) ,
2020-08-12 01:34:10 -07:00
fmt . Sprintf ( "--%s=%s" , flags . FlagKeyAlgorithm , string ( hd . Secp256k1Type ) ) ,
2020-07-06 12:50:09 -07:00
fmt . Sprintf ( "--%s=%s" , flags . FlagKeyringBackend , keyring . BackendTest ) ,
} )
2020-10-08 10:41:35 -07:00
require . NoError ( t , cmd . ExecuteContext ( ctx ) )
require . Error ( t , cmd . ExecuteContext ( ctx ) )
2020-03-30 11:30:50 -07:00
2020-07-06 12:50:09 -07:00
cmd . SetArgs ( [ ] string {
"keyname5" ,
fmt . Sprintf ( "--%s=%s" , flags . FlagHome , kbHome ) ,
fmt . Sprintf ( "--%s=true" , flags . FlagDryRun ) ,
fmt . Sprintf ( "--%s=%s" , cli . OutputFlag , OutputFormatText ) ,
2020-08-12 01:34:10 -07:00
fmt . Sprintf ( "--%s=%s" , flags . FlagKeyAlgorithm , string ( hd . Secp256k1Type ) ) ,
2020-07-06 12:50:09 -07:00
} )
2020-03-30 11:30:50 -07:00
2020-10-08 10:41:35 -07:00
require . NoError ( t , cmd . ExecuteContext ( ctx ) )
2020-08-28 09:29:32 -07:00
// In recovery mode
cmd . SetArgs ( [ ] string {
"keyname6" ,
fmt . Sprintf ( "--%s=true" , flagRecover ) ,
} )
// use valid mnemonic and complete recovery key generation successfully
mockIn . Reset ( "decide praise business actor peasant farm drastic weather extend front hurt later song give verb rhythm worry fun pond reform school tumble august one\n" )
2020-10-08 10:41:35 -07:00
require . NoError ( t , cmd . ExecuteContext ( ctx ) )
2020-08-28 09:29:32 -07:00
// use invalid mnemonic and fail recovery key generation
mockIn . Reset ( "invalid mnemonic\n" )
2020-10-08 10:41:35 -07:00
require . Error ( t , cmd . ExecuteContext ( ctx ) )
2020-08-28 09:29:32 -07:00
// In interactive mode
cmd . SetArgs ( [ ] string {
"keyname7" ,
"-i" ,
fmt . Sprintf ( "--%s=false" , flagRecover ) ,
} )
const password = "password1!"
// set password and complete interactive key generation successfully
mockIn . Reset ( "\n" + password + "\n" + password + "\n" )
2020-10-08 10:41:35 -07:00
require . NoError ( t , cmd . ExecuteContext ( ctx ) )
2020-08-28 09:29:32 -07:00
// passwords don't match and fail interactive key generation
mockIn . Reset ( "\n" + password + "\n" + "fail" + "\n" )
2020-10-08 10:41:35 -07:00
require . Error ( t , cmd . ExecuteContext ( ctx ) )
2019-02-08 12:45:23 -08:00
}
2021-06-25 04:55:58 -07:00
func Test_runAddCmdDryRun ( t * testing . T ) {
pubkey1 := ` { "@type":"/cosmos.crypto.secp256k1.PubKey","key":"AtObiFVE4s+9+RX5SP8TN9r2mxpoaT4eGj9CJfK7VRzN"} `
pubkey2 := ` { "@type":"/cosmos.crypto.secp256k1.PubKey","key":"A/se1vkqgdQ7VJQCM4mxN+L+ciGhnnJ4XYsQCRBMrdRi"} `
2021-09-20 05:02:15 -07:00
cdc := simapp . MakeTestEncodingConfig ( ) . Codec
2021-06-25 04:55:58 -07:00
testData := [ ] struct {
name string
args [ ] string
added bool
} {
{
name : "account is added" ,
args : [ ] string {
"testkey" ,
fmt . Sprintf ( "--%s=%s" , flags . FlagDryRun , "false" ) ,
} ,
added : true ,
} ,
{
name : "account is not added with dry run" ,
args : [ ] string {
"testkey" ,
fmt . Sprintf ( "--%s=%s" , flags . FlagDryRun , "true" ) ,
} ,
added : false ,
} ,
{
name : "multisig account is added" ,
args : [ ] string {
"testkey" ,
fmt . Sprintf ( "--%s=%s" , flags . FlagDryRun , "false" ) ,
fmt . Sprintf ( "--%s=%s" , flagMultisig , "subkey" ) ,
} ,
added : true ,
} ,
{
name : "multisig account is not added with dry run" ,
args : [ ] string {
"testkey" ,
fmt . Sprintf ( "--%s=%s" , flags . FlagDryRun , "true" ) ,
fmt . Sprintf ( "--%s=%s" , flagMultisig , "subkey" ) ,
} ,
added : false ,
} ,
{
name : "pubkey account is added" ,
args : [ ] string {
"testkey" ,
fmt . Sprintf ( "--%s=%s" , flags . FlagDryRun , "false" ) ,
fmt . Sprintf ( "--%s=%s" , FlagPublicKey , pubkey1 ) ,
} ,
added : true ,
} ,
{
name : "pubkey account is not added with dry run" ,
args : [ ] string {
"testkey" ,
fmt . Sprintf ( "--%s=%s" , flags . FlagDryRun , "true" ) ,
fmt . Sprintf ( "--%s=%s" , FlagPublicKey , pubkey2 ) ,
} ,
added : false ,
} ,
}
for _ , tt := range testData {
tt := tt
t . Run ( tt . name , func ( t * testing . T ) {
cmd := AddKeyCommand ( )
cmd . Flags ( ) . AddFlagSet ( Commands ( "home" ) . PersistentFlags ( ) )
kbHome := t . TempDir ( )
mockIn := testutil . ApplyMockIODiscardOutErr ( cmd )
2021-09-20 05:02:15 -07:00
kb , err := keyring . New ( sdk . KeyringServiceName ( ) , keyring . BackendTest , kbHome , mockIn , cdc )
2021-06-25 04:55:58 -07:00
require . NoError ( t , err )
clientCtx := client . Context { } .
2021-09-20 05:02:15 -07:00
WithCodec ( cdc ) .
2021-06-25 04:55:58 -07:00
WithKeyringDir ( kbHome ) .
WithKeyring ( kb )
ctx := context . WithValue ( context . Background ( ) , client . ClientContextKey , & clientCtx )
path := sdk . GetConfig ( ) . GetFullBIP44Path ( )
_ , err = kb . NewAccount ( "subkey" , testutil . TestMnemonic , "" , path , hd . Secp256k1 )
require . NoError ( t , err )
t . Cleanup ( func ( ) {
_ = kb . Delete ( "subkey" )
} )
b := bytes . NewBufferString ( "" )
cmd . SetOut ( b )
cmd . SetArgs ( tt . args )
require . NoError ( t , cmd . ExecuteContext ( ctx ) )
if tt . added {
2021-09-20 05:02:15 -07:00
_ , err := kb . Key ( "testkey" )
2021-06-25 04:55:58 -07:00
require . NoError ( t , err )
out , err := ioutil . ReadAll ( b )
require . NoError ( t , err )
require . Contains ( t , string ( out ) , "name: testkey" )
} else {
_ , err = kb . Key ( "testkey" )
require . Error ( t , err )
2021-09-20 05:02:15 -07:00
require . Equal ( t , "testkey: key not found" , err . Error ( ) )
2021-06-25 04:55:58 -07:00
}
} )
}
}
2021-08-09 03:35:01 -07:00
func TestAddRecoverFileBackend ( t * testing . T ) {
cmd := AddKeyCommand ( )
cmd . Flags ( ) . AddFlagSet ( Commands ( "home" ) . PersistentFlags ( ) )
2021-09-20 05:02:15 -07:00
cdc := simapp . MakeTestEncodingConfig ( ) . Codec
2021-08-09 03:35:01 -07:00
mockIn := testutil . ApplyMockIODiscardOutErr ( cmd )
kbHome := t . TempDir ( )
2021-09-20 05:02:15 -07:00
clientCtx := client . Context { } . WithKeyringDir ( kbHome ) . WithInput ( mockIn ) . WithCodec ( cdc )
2021-08-09 03:35:01 -07:00
ctx := context . WithValue ( context . Background ( ) , client . ClientContextKey , & clientCtx )
cmd . SetArgs ( [ ] string {
"keyname1" ,
fmt . Sprintf ( "--%s=%s" , flags . FlagHome , kbHome ) ,
fmt . Sprintf ( "--%s=%s" , cli . OutputFlag , OutputFormatText ) ,
fmt . Sprintf ( "--%s=%s" , flags . FlagKeyAlgorithm , string ( hd . Secp256k1Type ) ) ,
fmt . Sprintf ( "--%s=%s" , flags . FlagKeyringBackend , keyring . BackendFile ) ,
fmt . Sprintf ( "--%s" , flagRecover ) ,
} )
keyringPassword := "12345678"
entropySeed , err := bip39 . NewEntropy ( mnemonicEntropySize )
require . NoError ( t , err )
mnemonic , err := bip39 . NewMnemonic ( entropySeed )
require . NoError ( t , err )
mockIn . Reset ( fmt . Sprintf ( "%s\n%s\n%s\n" , mnemonic , keyringPassword , keyringPassword ) )
require . NoError ( t , cmd . ExecuteContext ( ctx ) )
2021-09-20 05:02:15 -07:00
kb , err := keyring . New ( sdk . KeyringServiceName ( ) , keyring . BackendFile , kbHome , mockIn , cdc )
2021-08-09 03:35:01 -07:00
require . NoError ( t , err )
t . Cleanup ( func ( ) {
mockIn . Reset ( fmt . Sprintf ( "%s\n%s\n" , keyringPassword , keyringPassword ) )
_ = kb . Delete ( "keyname1" )
} )
mockIn . Reset ( fmt . Sprintf ( "%s\n%s\n" , keyringPassword , keyringPassword ) )
2021-09-20 05:02:15 -07:00
k , err := kb . Key ( "keyname1" )
2021-08-09 03:35:01 -07:00
require . NoError ( t , err )
2021-09-20 05:02:15 -07:00
require . Equal ( t , "keyname1" , k . Name )
2021-08-09 03:35:01 -07:00
}