github.com/kubri/kubri@v0.5.1-0.20240317001612-bda2aaef967e/pkg/cmd/keys_import_test.go (about) 1 package cmd_test 2 3 import ( 4 "bytes" 5 "io" 6 "os" 7 "path/filepath" 8 "strings" 9 "testing" 10 11 "github.com/kubri/kubri/pkg/cmd" 12 "github.com/kubri/kubri/pkg/crypto/dsa" 13 "github.com/kubri/kubri/pkg/crypto/ed25519" 14 "github.com/kubri/kubri/pkg/crypto/pgp" 15 "github.com/kubri/kubri/pkg/crypto/rsa" 16 "github.com/kubri/kubri/pkg/secret" 17 ) 18 19 func TestKeysImportCmd(t *testing.T) { 20 dsaKey, _ := dsa.NewPrivateKey() 21 dsaBytes, _ := dsa.MarshalPrivateKey(dsaKey) 22 dsaPath := filepath.Join(t.TempDir(), "test") 23 os.WriteFile(dsaPath, dsaBytes, os.ModePerm) 24 25 ed25519Key, _ := ed25519.NewPrivateKey() 26 ed25519Bytes, _ := ed25519.MarshalPrivateKey(ed25519Key) 27 ed25519Path := filepath.Join(t.TempDir(), "test") 28 os.WriteFile(ed25519Path, ed25519Bytes, os.ModePerm) 29 30 pgpKey, _ := pgp.NewPrivateKey("test", "test@example.com") 31 pgpBytes, _ := pgp.MarshalPrivateKey(pgpKey) 32 pgpPath := filepath.Join(t.TempDir(), "test") 33 os.WriteFile(pgpPath, pgpBytes, os.ModePerm) 34 35 rsaKey, _ := rsa.NewPrivateKey() 36 rsaBytes, _ := rsa.MarshalPrivateKey(rsaKey) 37 rsaPath := filepath.Join(t.TempDir(), "test") 38 os.WriteFile(rsaPath, rsaBytes, os.ModePerm) 39 40 tests := [][]string{ 41 {"keys", "import", "dsa", dsaPath}, 42 {"keys", "import", "ed25519", ed25519Path}, 43 {"keys", "import", "pgp", pgpPath}, 44 {"keys", "import", "rsa", rsaPath}, 45 {"keys", "import", "dsa", dsaPath, "--force"}, 46 {"keys", "import", "ed25519", ed25519Path, "--force"}, 47 {"keys", "import", "pgp", pgpPath, "--force"}, 48 {"keys", "import", "rsa", rsaPath, "--force"}, 49 } 50 51 t.Setenv("KUBRI_PATH", t.TempDir()) 52 53 for _, test := range tests { 54 want, _ := os.ReadFile(test[3]) 55 err := cmd.Execute("", cmd.WithArgs(test...), cmd.WithStdout(io.Discard)) 56 if err != nil { 57 t.Errorf("%s: %s", test, err) 58 } else if b, _ := secret.Get(test[2] + "_key"); !bytes.Equal(want, b) { 59 t.Errorf("%s should be equal", test) 60 } 61 } 62 } 63 64 func TestKeysImportCmdErrors(t *testing.T) { 65 path := filepath.Join(t.TempDir(), "test") 66 os.WriteFile(path, []byte("test"), os.ModePerm) 67 68 dsaPath := filepath.Join(t.TempDir(), "test") 69 dsaKey, _ := dsa.NewPrivateKey() 70 dsaBytes, _ := dsa.MarshalPrivateKey(dsaKey) 71 os.WriteFile(dsaPath, dsaBytes, os.ModePerm) 72 73 ed25519Path := filepath.Join(t.TempDir(), "test") 74 ed25519Key, _ := ed25519.NewPrivateKey() 75 ed25519Bytes, _ := ed25519.MarshalPrivateKey(ed25519Key) 76 os.WriteFile(ed25519Path, ed25519Bytes, os.ModePerm) 77 78 pgpPath := filepath.Join(t.TempDir(), "test") 79 pgpKey, _ := pgp.NewPrivateKey("test", "test@example.com") 80 pgpBytes, _ := pgp.MarshalPrivateKey(pgpKey) 81 os.WriteFile(pgpPath, pgpBytes, os.ModePerm) 82 83 rsaPath := filepath.Join(t.TempDir(), "test") 84 rsaKey, _ := rsa.NewPrivateKey() 85 rsaBytes, _ := rsa.MarshalPrivateKey(rsaKey) 86 os.WriteFile(rsaPath, rsaBytes, os.ModePerm) 87 88 tests := []struct { 89 args []string 90 want string 91 }{ 92 { 93 args: []string{"keys", "import"}, 94 want: "accepts 2 arg(s), received 0", 95 }, 96 { 97 args: []string{"keys", "import", "dsa"}, 98 want: "accepts 2 arg(s), received 1", 99 }, 100 { 101 args: []string{"keys", "import", "foo", "bar"}, 102 want: "invalid argument", 103 }, 104 { 105 args: []string{"keys", "import", "dsa", dsaPath}, 106 want: "key already exists", 107 }, 108 { 109 args: []string{"keys", "import", "ed25519", ed25519Path}, 110 want: "key already exists", 111 }, 112 { 113 args: []string{"keys", "import", "pgp", pgpPath}, 114 want: "key already exists", 115 }, 116 { 117 args: []string{"keys", "import", "rsa", rsaPath}, 118 want: "key already exists", 119 }, 120 { 121 args: []string{"keys", "import", "dsa", path}, 122 want: "invalid key", 123 }, 124 { 125 args: []string{"keys", "import", "ed25519", path}, 126 want: "invalid key", 127 }, 128 { 129 args: []string{"keys", "import", "pgp", path}, 130 want: "invalid key", 131 }, 132 { 133 args: []string{"keys", "import", "rsa", path}, 134 want: "invalid key", 135 }, 136 { 137 args: []string{"keys", "import", "dsa", "foo", "--force"}, 138 want: "no such file or directory", 139 }, 140 } 141 142 t.Setenv("KUBRI_PATH", t.TempDir()) 143 secret.Put("dsa_key", nil) 144 secret.Put("ed25519_key", nil) 145 secret.Put("pgp_key", nil) 146 secret.Put("rsa_key", nil) 147 148 for _, test := range tests { 149 var stderr bytes.Buffer 150 err := cmd.Execute("", cmd.WithArgs(test.args...), cmd.WithStderr(&stderr), cmd.WithStdout(io.Discard)) 151 if err == nil || !strings.Contains(stderr.String(), test.want) { 152 t.Errorf("%s should fail with %q:\n%s", test.args, test.want, err) 153 } 154 } 155 }