2019-02-08 12:45:23 -08:00
|
|
|
package keys
|
|
|
|
|
|
|
|
import (
|
2020-07-06 12:50:09 -07:00
|
|
|
"fmt"
|
2019-02-08 12:45:23 -08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-06 12:50:09 -07:00
|
|
|
|
2020-07-09 05:21:20 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
2019-02-08 12:45:23 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_RunMnemonicCmdNormal(t *testing.T) {
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd := MnemonicKeyCommand()
|
2020-07-09 23:55:48 -07:00
|
|
|
_ = testutil.ApplyMockIODiscardOutErr(cmd)
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd.SetArgs([]string{})
|
|
|
|
require.NoError(t, cmd.Execute())
|
2019-02-08 12:45:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RunMnemonicCmdUser(t *testing.T) {
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd := MnemonicKeyCommand()
|
2020-07-09 23:55:48 -07:00
|
|
|
_ = testutil.ApplyMockIODiscardOutErr(cmd)
|
2019-02-08 12:45:23 -08:00
|
|
|
|
2020-07-06 12:50:09 -07:00
|
|
|
cmd.SetArgs([]string{fmt.Sprintf("--%s=1", flagUserEntropy)})
|
|
|
|
err := cmd.Execute()
|
2019-02-08 12:45:23 -08:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, "EOF", err.Error())
|
|
|
|
|
|
|
|
// Try again
|
2020-07-09 23:55:48 -07:00
|
|
|
mockIn := testutil.ApplyMockIODiscardOutErr(cmd)
|
2019-06-22 02:24:59 -07:00
|
|
|
mockIn.Reset("Hi!\n")
|
2020-07-06 12:50:09 -07:00
|
|
|
err = cmd.Execute()
|
2019-02-08 12:45:23 -08:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t,
|
|
|
|
"256-bits is 43 characters in Base-64, and 100 in Base-6. You entered 3, and probably want more",
|
|
|
|
err.Error())
|
|
|
|
|
|
|
|
// Now provide "good" entropy :)
|
|
|
|
fakeEntropy := strings.Repeat(":)", 40) + "\ny\n" // entropy + accept count
|
2019-06-22 02:24:59 -07:00
|
|
|
mockIn.Reset(fakeEntropy)
|
2020-07-06 12:50:09 -07:00
|
|
|
require.NoError(t, cmd.Execute())
|
2019-02-08 12:45:23 -08:00
|
|
|
|
|
|
|
// Now provide "good" entropy but no answer
|
|
|
|
fakeEntropy = strings.Repeat(":)", 40) + "\n" // entropy + accept count
|
2019-06-22 02:24:59 -07:00
|
|
|
mockIn.Reset(fakeEntropy)
|
2020-07-06 12:50:09 -07:00
|
|
|
require.Error(t, cmd.Execute())
|
2019-02-08 12:45:23 -08:00
|
|
|
|
|
|
|
// Now provide "good" entropy but say no
|
|
|
|
fakeEntropy = strings.Repeat(":)", 40) + "\nn\n" // entropy + accept count
|
2019-06-22 02:24:59 -07:00
|
|
|
mockIn.Reset(fakeEntropy)
|
2020-07-06 12:50:09 -07:00
|
|
|
require.NoError(t, cmd.Execute())
|
2019-02-08 12:45:23 -08:00
|
|
|
}
|