@@ -15,7 +15,6 @@ import (
1515)
1616
1717func TestCLI (t * testing.T ) {
18- cmd := cli .NewRootCmd ()
1918 tempDir := t .TempDir ()
2019 defer os .RemoveAll (tempDir )
2120 keystoreFile := filepath .Join (tempDir , "keystore.json" )
@@ -26,70 +25,58 @@ func TestCLI(t *testing.T) {
2625 os .Setenv ("KEYSTORE_PASSWORD" , "testpassword" )
2726
2827 // No error just listing help.
29- buf := new (bytes.Buffer )
30- cmd .SetOutput (buf )
31- cmd .SetErr (buf )
32- cmd .SetArgs ([]string {})
33- require .NoError (t , cmd .ExecuteContext (t .Context ()))
28+ _ , err = runCommand (t , nil , "" )
29+ require .NoError (t , err )
3430
3531 // Create a key.
36- buf .Reset ()
37- cmd .SetArgs ([]string {"create" , "-d" , `{"Keys": [{"KeyName": "testkey", "KeyType": "X25519"}]}` })
38- require .NoError (t , cmd .ExecuteContext (t .Context ()))
32+ _ , err = runCommand (t , nil , "create" , "-d" , `{"Keys": [{"KeyName": "testkey", "KeyType": "X25519"}]}` )
33+ require .NoError (t , err )
3934
4035 // List keys.
41- buf .Reset ()
42- cmd .SetArgs ([]string {"get" , "-d" , `{"KeyNames": ["testkey"]}` })
43- require .NoError (t , cmd .ExecuteContext (t .Context ()))
36+ out , err := runCommand (t , nil , "get" , "-d" , `{"KeyNames": ["testkey"]}` )
37+ require .NoError (t , err )
4438 resp := ks.GetKeysResponse {}
45- err = json .Unmarshal (buf .Bytes (), & resp )
39+ err = json .Unmarshal (out .Bytes (), & resp )
4640 require .NoError (t , err )
4741 require .Len (t , resp .Keys , 1 )
4842 require .Equal (t , "testkey" , resp .Keys [0 ].KeyInfo .Name )
4943 require .Equal (t , ks .X25519 , resp .Keys [0 ].KeyInfo .KeyType )
5044
5145 // Create a second key we export.
52- buf .Reset ()
53- cmd .SetArgs ([]string {"create" , "-d" , `{"Keys": [{"KeyName": "testkey2", "KeyType": "ECDSA_S256"}]}` })
54- require .NoError (t , cmd .ExecuteContext (t .Context ()))
46+ _ , err = runCommand (t , nil , "create" , "-d" , `{"Keys": [{"KeyName": "testkey2", "KeyType": "ECDSA_S256"}]}` )
47+ require .NoError (t , err )
5548
5649 // Export the second key.
57- buf .Reset ()
58- cmd .SetArgs ([]string {"export" , "-d" , `{"Keys": [{"KeyName": "testkey2", "Enc": {"Password": "testpassword2", "ScryptParams": {"N": 1024, "P": 1, "R": 8}}}]}` })
59- require .NoError (t , cmd .ExecuteContext (t .Context ()))
50+ out , err = runCommand (t , nil , "export" , "-d" , `{"Keys": [{"KeyName": "testkey2", "Enc": {"Password": "testpassword2", "ScryptParams": {"N": 1024, "P": 1, "R": 8}}}]}` )
51+ require .NoError (t , err )
6052 exportResp := ks.ExportKeysResponse {}
61- err = json .Unmarshal (buf .Bytes (), & exportResp )
53+ err = json .Unmarshal (out .Bytes (), & exportResp )
6254 require .NoError (t , err )
6355 exportedKey2Data := base64 .StdEncoding .EncodeToString (exportResp .Keys [0 ].Data )
6456
6557 // Delete the second key.
66- buf .Reset ()
67- // Force deletion without confirmation.
68- cmd .SetArgs ([]string {"delete" , "-d" , `{"KeyNames": ["testkey2"]}` , "--yes" })
69- require .NoError (t , cmd .ExecuteContext (t .Context ()))
58+ _ , err = runCommand (t , nil , "delete" , "-d" , `{"KeyNames": ["testkey2"]}` , "--yes" )
59+ require .NoError (t , err )
7060
7161 // List key should only see first.
72- buf .Reset ()
73- cmd .SetArgs ([]string {"list" })
74- require .NoError (t , cmd .ExecuteContext (t .Context ()))
62+ out , err = runCommand (t , nil , "list" )
63+ require .NoError (t , err )
7564 resp = ks.GetKeysResponse {}
76- err = json .Unmarshal (buf .Bytes (), & resp )
65+ err = json .Unmarshal (out .Bytes (), & resp )
7766 require .NoError (t , err )
7867 require .Len (t , resp .Keys , 1 )
7968 require .Equal (t , "testkey" , resp .Keys [0 ].KeyInfo .Name )
8069 require .Equal (t , ks .X25519 , resp .Keys [0 ].KeyInfo .KeyType )
8170
8271 // Import the exported key.
83- buf .Reset ()
84- cmd .SetArgs ([]string {"import" , "-d" , `{"Keys": [{"KeyName": "testkey2", "Data": "` + exportedKey2Data + `", "Password": "testpassword2"}]}` })
85- require .NoError (t , cmd .ExecuteContext (t .Context ()))
72+ _ , err = runCommand (t , nil , "import" , "-d" , `{"Keys": [{"KeyName": "testkey2", "Data": "` + exportedKey2Data + `", "Password": "testpassword2"}]}` )
73+ require .NoError (t , err )
8674
8775 // List keys.
88- buf .Reset ()
89- cmd .SetArgs ([]string {"list" })
90- require .NoError (t , cmd .ExecuteContext (t .Context ()))
76+ out , err = runCommand (t , nil , "list" )
77+ require .NoError (t , err )
9178 resp = ks.GetKeysResponse {}
92- err = json .Unmarshal (buf .Bytes (), & resp )
79+ err = json .Unmarshal (out .Bytes (), & resp )
9380 require .NoError (t , err )
9481 require .Len (t , resp .Keys , 2 )
9582 require .Equal (t , "testkey" , resp .Keys [0 ].KeyInfo .Name )
@@ -98,20 +85,46 @@ func TestCLI(t *testing.T) {
9885 require .Equal (t , ks .ECDSA_S256 , resp .Keys [1 ].KeyInfo .KeyType )
9986
10087 // Set metadata on testkey.
101- buf .Reset ()
10288 metadata := base64 .StdEncoding .EncodeToString ([]byte ("my-custom-metadata" ))
103- cmd . SetArgs ([] string { "set-metadata" , "-d" , `{"Updates": [{"KeyName": "testkey", "Metadata": "` + metadata + `"}]}` } )
104- require .NoError (t , cmd . ExecuteContext ( t . Context ()) )
89+ _ , err = runCommand ( t , nil , "set-metadata" , "-d" , `{"Updates": [{"KeyName": "testkey", "Metadata": "` + metadata + `"}]}` )
90+ require .NoError (t , err )
10591
10692 // Verify metadata was set.
107- buf .Reset ()
108- cmd .SetArgs ([]string {"get" , "-d" , `{"KeyNames": ["testkey"]}` })
109- require .NoError (t , cmd .ExecuteContext (t .Context ()))
93+ out , err = runCommand (t , nil , "get" , "-d" , `{"KeyNames": ["testkey"]}` )
94+ require .NoError (t , err )
11095 resp = ks.GetKeysResponse {}
111- err = json .Unmarshal (buf .Bytes (), & resp )
96+ err = json .Unmarshal (out .Bytes (), & resp )
11297 require .NoError (t , err )
11398 require .Len (t , resp .Keys , 1 )
11499 require .Equal (t , "testkey" , resp .Keys [0 ].KeyInfo .Name )
115100 // Metadata is []byte, Go's JSON unmarshaler automatically decodes base64 strings
116101 require .Equal (t , "my-custom-metadata" , string (resp .Keys [0 ].KeyInfo .Metadata ))
102+
103+ // Delete the keys with confirmation.
104+ out , err = runCommand (t , bytes .NewBufferString ("yes\n " ), "delete" , "-d" , `{"KeyNames": ["testkey", "testkey2"]}` )
105+ require .NoError (t , err )
106+ t .Log ("out" , out .String ())
107+
108+ // List keys should be empty.
109+ out , err = runCommand (t , nil , "list" )
110+ require .NoError (t , err )
111+ resp = ks.GetKeysResponse {}
112+ err = json .Unmarshal (out .Bytes (), & resp )
113+ require .NoError (t , err )
114+ require .Empty (t , resp .Keys )
115+ }
116+
117+ func runCommand (t * testing.T , in * bytes.Buffer , args ... string ) (bytes.Buffer , error ) {
118+ // Cobra commands are stateful which can cause subtle bugs if not reset.
119+ // For simplicity just create a fresh object.
120+ cmd := cli .NewRootCmd ()
121+ buf := new (bytes.Buffer )
122+ cmd .SetOutput (buf )
123+ cmd .SetArgs (args )
124+ cmd .SetIn (in )
125+ err := cmd .ExecuteContext (t .Context ())
126+ if err != nil {
127+ return bytes.Buffer {}, err
128+ }
129+ return * buf , nil
117130}
0 commit comments