github.com/Finschia/finschia-sdk@v0.48.1/client/keys/import_test.go (about) 1 package keys 2 3 import ( 4 "context" 5 "fmt" 6 "os" 7 "path/filepath" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 12 "github.com/Finschia/finschia-sdk/client" 13 "github.com/Finschia/finschia-sdk/client/flags" 14 "github.com/Finschia/finschia-sdk/crypto/keyring" 15 "github.com/Finschia/finschia-sdk/testutil" 16 sdk "github.com/Finschia/finschia-sdk/types" 17 ) 18 19 func Test_runImportCmd(t *testing.T) { 20 testCases := []struct { 21 name string 22 keyringBackend string 23 userInput string 24 expectError bool 25 }{ 26 { 27 name: "test backend success", 28 keyringBackend: keyring.BackendTest, 29 // key armor passphrase 30 userInput: "123456789\n", 31 }, 32 { 33 name: "test backend fail with wrong armor pass", 34 keyringBackend: keyring.BackendTest, 35 userInput: "987654321\n", 36 expectError: true, 37 }, 38 { 39 name: "file backend success", 40 keyringBackend: keyring.BackendFile, 41 // key armor passphrase + keyring password x2 42 userInput: "123456789\n12345678\n12345678\n", 43 }, 44 { 45 name: "file backend fail with wrong armor pass", 46 keyringBackend: keyring.BackendFile, 47 userInput: "987654321\n12345678\n12345678\n", 48 expectError: true, 49 }, 50 { 51 name: "file backend fail with wrong keyring pass", 52 keyringBackend: keyring.BackendFile, 53 userInput: "123465789\n12345678\n87654321\n", 54 expectError: true, 55 }, 56 { 57 name: "file backend fail with no keyring pass", 58 keyringBackend: keyring.BackendFile, 59 userInput: "123465789\n", 60 expectError: true, 61 }, 62 } 63 64 armoredKey := `-----BEGIN OSTRACON PRIVATE KEY----- 65 kdf: bcrypt 66 salt: A53F628182B827E07DD11A96EAB9D526 67 type: secp256k1 68 69 Ax9IQsSq+jOWkPRDJQ69a5/uUm4XliPim/CbYDVoXO6D3fts5IEXcUTmIa60ynC/ 70 8hzYAawzYMO95Kwi0NI8WW9wUv3TseSWFv6/RpU= 71 =umYd 72 -----END OSTRACON PRIVATE KEY-----` 73 74 for _, tc := range testCases { 75 t.Run(tc.name, func(t *testing.T) { 76 cmd := ImportKeyCommand() 77 cmd.Flags().AddFlagSet(Commands("home").PersistentFlags()) 78 mockIn := testutil.ApplyMockIODiscardOutErr(cmd) 79 80 // Now add a temporary keybase 81 kbHome := t.TempDir() 82 kb, err := keyring.New(sdk.KeyringServiceName(), tc.keyringBackend, kbHome, nil) 83 84 clientCtx := client.Context{}. 85 WithKeyringDir(kbHome). 86 WithKeyring(kb). 87 WithInput(mockIn) 88 ctx := context.WithValue(context.Background(), client.ClientContextKey, &clientCtx) 89 90 require.NoError(t, err) 91 t.Cleanup(func() { 92 kb.Delete("keyname1") // nolint:errcheck 93 }) 94 95 keyfile := filepath.Join(kbHome, "key.asc") 96 97 require.NoError(t, os.WriteFile(keyfile, []byte(armoredKey), 0o644)) 98 99 defer func() { 100 _ = os.RemoveAll(kbHome) 101 }() 102 103 mockIn.Reset(tc.userInput) 104 cmd.SetArgs([]string{ 105 "keyname1", keyfile, 106 fmt.Sprintf("--%s=%s", flags.FlagKeyringBackend, tc.keyringBackend), 107 }) 108 109 err = cmd.ExecuteContext(ctx) 110 if tc.expectError { 111 require.Error(t, err) 112 } else { 113 require.NoError(t, err) 114 } 115 }) 116 } 117 }