(t *testing.T)
| 21 | ) |
| 22 | |
| 23 | func TestTrustKeyLoadErrors(t *testing.T) { |
| 24 | noSuchFile := "stat iamnotakey: no such file or directory" |
| 25 | if runtime.GOOS == "windows" { |
| 26 | noSuchFile = "CreateFile iamnotakey: The system cannot find the file specified." |
| 27 | } |
| 28 | testCases := []struct { |
| 29 | name string |
| 30 | args []string |
| 31 | expectedError string |
| 32 | expectedOutput string |
| 33 | }{ |
| 34 | { |
| 35 | name: "not-enough-args", |
| 36 | expectedError: "1 argument", |
| 37 | args: []string{}, |
| 38 | expectedOutput: "", |
| 39 | }, |
| 40 | { |
| 41 | name: "too-many-args", |
| 42 | args: []string{"iamnotakey", "alsonotakey"}, |
| 43 | expectedError: "1 argument", |
| 44 | expectedOutput: "", |
| 45 | }, |
| 46 | { |
| 47 | name: "not-a-key", |
| 48 | args: []string{"iamnotakey"}, |
| 49 | expectedError: "refusing to load key from iamnotakey: " + noSuchFile, |
| 50 | expectedOutput: "Loading key from \"iamnotakey\"...\n", |
| 51 | }, |
| 52 | { |
| 53 | name: "bad-key-name", |
| 54 | args: []string{"iamnotakey", "--name", "KEYNAME"}, |
| 55 | expectedError: "key name \"KEYNAME\" must start with lowercase alphanumeric characters and can include \"-\" or \"_\" after the first character", |
| 56 | expectedOutput: "", |
| 57 | }, |
| 58 | } |
| 59 | config.SetDir(t.TempDir()) |
| 60 | |
| 61 | for _, tc := range testCases { |
| 62 | cli := test.NewFakeCli(&fakeClient{}) |
| 63 | cmd := newKeyLoadCommand(cli) |
| 64 | cmd.SetArgs(tc.args) |
| 65 | cmd.SetOut(io.Discard) |
| 66 | cmd.SetErr(io.Discard) |
| 67 | assert.ErrorContains(t, cmd.Execute(), tc.expectedError) |
| 68 | assert.Check(t, is.Contains(cli.OutBuffer().String(), tc.expectedOutput)) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | var rsaPrivKeyFixture = []byte(`-----BEGIN RSA PRIVATE KEY----- |
| 73 | MIIEpAIBAAKCAQEAs7yVMzCw8CBZPoN+QLdx3ZzbVaHnouHIKu+ynX60IZ3stpbb |
nothing calls this directly
no test coverage detected
searching dependent graphs…