github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/crypto/keys/client/import_test.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strings"
     7  	"testing"
     8  
     9  	"github.com/gnolang/gno/tm2/pkg/commands"
    10  	"github.com/gnolang/gno/tm2/pkg/testutils"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  type testImportKeyOpts struct {
    15  	testCmdKeyOptsBase
    16  
    17  	armorPath string
    18  }
    19  
    20  // importKey runs the import private key command (from armor)
    21  func importKey(
    22  	importOpts testImportKeyOpts,
    23  	input io.Reader,
    24  ) error {
    25  	cfg := &ImportCfg{
    26  		RootCfg: &BaseCfg{
    27  			BaseOptions: BaseOptions{
    28  				Home:                  importOpts.kbHome,
    29  				InsecurePasswordStdin: true,
    30  			},
    31  		},
    32  		KeyName:   importOpts.keyName,
    33  		ArmorPath: importOpts.armorPath,
    34  		Unsafe:    importOpts.unsafe,
    35  	}
    36  
    37  	cmdIO := commands.NewTestIO()
    38  	cmdIO.SetIn(input)
    39  
    40  	return execImport(cfg, cmdIO)
    41  }
    42  
    43  // TestImport_ImportKey makes sure the key can be imported correctly
    44  func TestImport_ImportKey(t *testing.T) {
    45  	t.Parallel()
    46  
    47  	const (
    48  		keyName       = "key name"
    49  		importKeyName = "import key name"
    50  		password      = "password"
    51  	)
    52  
    53  	testTable := []struct {
    54  		name     string
    55  		baseOpts testCmdKeyOptsBase
    56  		input    io.Reader
    57  	}{
    58  		{
    59  			"encrypted private key",
    60  			testCmdKeyOptsBase{
    61  				unsafe: false, // explicit
    62  			},
    63  			strings.NewReader(
    64  				fmt.Sprintf(
    65  					"%s\n%s\n%s\n",
    66  					password, // decrypt
    67  					password, // key-base encrypt
    68  					password, // key-base encrypt confirm
    69  				),
    70  			),
    71  		},
    72  		{
    73  			"unencrypted private key",
    74  			testCmdKeyOptsBase{
    75  				unsafe: true,
    76  			},
    77  			strings.NewReader(
    78  				fmt.Sprintf(
    79  					"%s\n%s\n",
    80  					password, // key-base encrypt
    81  					password, // key-base encrypt confirm
    82  				),
    83  			),
    84  		},
    85  	}
    86  
    87  	for _, testCase := range testTable {
    88  		testCase := testCase
    89  
    90  		t.Run(testCase.name, func(t *testing.T) {
    91  			t.Parallel()
    92  
    93  			// Generate a temporary key-base directory
    94  			kb, kbHome := newTestKeybase(t)
    95  
    96  			// Add an initial key to the key base
    97  			info, err := addRandomKeyToKeybase(kb, keyName, password)
    98  			if err != nil {
    99  				t.Fatalf(
   100  					"unable to create a key base account, %v",
   101  					err,
   102  				)
   103  			}
   104  
   105  			outputFile, outputCleanupFn := testutils.NewTestFile(t)
   106  			defer outputCleanupFn()
   107  
   108  			// Make sure the export command executes correctly
   109  			if err := exportKey(
   110  				testExportKeyOpts{
   111  					testCmdKeyOptsBase: testCmdKeyOptsBase{
   112  						kbHome:  kbHome,
   113  						keyName: keyName,
   114  						unsafe:  testCase.baseOpts.unsafe,
   115  					},
   116  					outputPath: outputFile.Name(),
   117  				},
   118  				strings.NewReader(
   119  					fmt.Sprintf(
   120  						"%s\n%s\n%s\n",
   121  						password,
   122  						password,
   123  						password,
   124  					),
   125  				),
   126  			); err != nil {
   127  				t.Fatalf("unable to export private key, %v", err)
   128  			}
   129  
   130  			// Make sure the encrypted armor can be imported correctly
   131  			if err := importKey(
   132  				testImportKeyOpts{
   133  					testCmdKeyOptsBase: testCmdKeyOptsBase{
   134  						kbHome: kbHome,
   135  						// Change the import key name so the existing one (in the key-base)
   136  						// doesn't get overwritten
   137  						keyName: importKeyName,
   138  						unsafe:  testCase.baseOpts.unsafe,
   139  					},
   140  					armorPath: outputFile.Name(),
   141  				},
   142  				testCase.input,
   143  			); err != nil {
   144  				t.Fatalf("unable to import private key armor, %v", err)
   145  			}
   146  
   147  			// Make sure the key-base has the new key imported
   148  			info, err = kb.GetByName(importKeyName)
   149  
   150  			assert.NotNil(t, info)
   151  			assert.NoError(t, err)
   152  		})
   153  	}
   154  }
   155  
   156  func TestImport_ImportKeyWithEmptyName(t *testing.T) {
   157  	// Generate a temporary key-base directory
   158  	_, kbHome := newTestKeybase(t)
   159  	err := importKey(
   160  		testImportKeyOpts{
   161  			testCmdKeyOptsBase: testCmdKeyOptsBase{
   162  				kbHome:  kbHome,
   163  				keyName: "",
   164  			},
   165  		},
   166  		nil,
   167  	)
   168  	assert.Error(t, err)
   169  	assert.EqualError(t, err, "name shouldn't be empty")
   170  }