github.com/cosmos/cosmos-sdk@v0.50.10/client/keys/export_test.go (about) 1 package keys 2 3 import ( 4 "bufio" 5 "context" 6 "fmt" 7 "testing" 8 9 "github.com/stretchr/testify/require" 10 11 "github.com/cosmos/cosmos-sdk/client" 12 "github.com/cosmos/cosmos-sdk/client/flags" 13 "github.com/cosmos/cosmos-sdk/crypto/hd" 14 "github.com/cosmos/cosmos-sdk/crypto/keyring" 15 "github.com/cosmos/cosmos-sdk/testutil" 16 "github.com/cosmos/cosmos-sdk/testutil/testdata" 17 sdk "github.com/cosmos/cosmos-sdk/types" 18 moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" 19 ) 20 21 func Test_runExportCmd(t *testing.T) { 22 cdc := moduletestutil.MakeTestEncodingConfig().Codec 23 testCases := []struct { 24 name string 25 keyringBackend string 26 extraArgs []string 27 userInput string 28 mustFail bool 29 expectedOutput string 30 }{ 31 { 32 name: "--unsafe only must fail", 33 keyringBackend: keyring.BackendTest, 34 extraArgs: []string{"--unsafe"}, 35 mustFail: true, 36 }, 37 { 38 name: "--unarmored-hex must fail", 39 keyringBackend: keyring.BackendTest, 40 extraArgs: []string{"--unarmored-hex"}, 41 mustFail: true, 42 }, 43 { 44 name: "--unsafe --unarmored-hex fail with no user confirmation", 45 keyringBackend: keyring.BackendTest, 46 extraArgs: []string{"--unsafe", "--unarmored-hex"}, 47 userInput: "", 48 mustFail: true, 49 expectedOutput: "", 50 }, 51 { 52 name: "--unsafe --unarmored-hex succeed", 53 keyringBackend: keyring.BackendTest, 54 extraArgs: []string{"--unsafe", "--unarmored-hex"}, 55 userInput: "y\n", 56 mustFail: false, 57 expectedOutput: "2485e33678db4175dc0ecef2d6e1fc493d4a0d7f7ce83324b6ed70afe77f3485\n", 58 }, 59 { 60 name: "file keyring backend properly read password and user confirmation", 61 keyringBackend: keyring.BackendFile, 62 extraArgs: []string{"--unsafe", "--unarmored-hex"}, 63 // first 2 pass for creating the key, then unsafe export confirmation, then unlock keyring pass 64 userInput: "12345678\n12345678\ny\n12345678\n", 65 mustFail: false, 66 expectedOutput: "2485e33678db4175dc0ecef2d6e1fc493d4a0d7f7ce83324b6ed70afe77f3485\n", 67 }, 68 } 69 70 for _, tc := range testCases { 71 t.Run(tc.name, func(t *testing.T) { 72 kbHome := t.TempDir() 73 defaultArgs := []string{ 74 "keyname1", 75 fmt.Sprintf("--%s=%s", flags.FlagKeyringDir, kbHome), 76 fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, tc.keyringBackend), 77 } 78 79 cmd := ExportKeyCommand() 80 cmd.Flags().AddFlagSet(Commands().PersistentFlags()) 81 82 cmd.SetArgs(append(defaultArgs, tc.extraArgs...)) 83 mockIn, mockOut := testutil.ApplyMockIO(cmd) 84 85 mockIn.Reset(tc.userInput) 86 mockInBuf := bufio.NewReader(mockIn) 87 88 // create a key 89 kb, err := keyring.New(sdk.KeyringServiceName(), tc.keyringBackend, kbHome, bufio.NewReader(mockInBuf), cdc) 90 require.NoError(t, err) 91 t.Cleanup(cleanupKeys(t, kb, "keyname1")) 92 93 path := sdk.GetConfig().GetFullBIP44Path() 94 _, err = kb.NewAccount("keyname1", testdata.TestMnemonic, "", path, hd.Secp256k1) 95 require.NoError(t, err) 96 97 clientCtx := client.Context{}. 98 WithKeyringDir(kbHome). 99 WithKeyring(kb). 100 WithInput(mockInBuf). 101 WithCodec(cdc) 102 ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) 103 104 err = cmd.ExecuteContext(ctx) 105 if tc.mustFail { 106 require.Error(t, err) 107 } else { 108 require.NoError(t, err) 109 require.Equal(t, tc.expectedOutput, mockOut.String()) 110 } 111 }) 112 } 113 }