...
1
2
3
4
5 package agent
6
7 import "testing"
8
9 func addTestKey(t *testing.T, a Agent, keyName string) {
10 err := a.Add(AddedKey{
11 PrivateKey: testPrivateKeys[keyName],
12 Comment: keyName,
13 })
14 if err != nil {
15 t.Fatalf("failed to add key %q: %v", keyName, err)
16 }
17 }
18
19 func removeTestKey(t *testing.T, a Agent, keyName string) {
20 err := a.Remove(testPublicKeys[keyName])
21 if err != nil {
22 t.Fatalf("failed to remove key %q: %v", keyName, err)
23 }
24 }
25
26 func validateListedKeys(t *testing.T, a Agent, expectedKeys []string) {
27 listedKeys, err := a.List()
28 if err != nil {
29 t.Fatalf("failed to list keys: %v", err)
30 return
31 }
32 actualKeys := make(map[string]bool)
33 for _, key := range listedKeys {
34 actualKeys[key.Comment] = true
35 }
36
37 matchedKeys := make(map[string]bool)
38 for _, expectedKey := range expectedKeys {
39 if !actualKeys[expectedKey] {
40 t.Fatalf("expected key %q, but was not found", expectedKey)
41 } else {
42 matchedKeys[expectedKey] = true
43 }
44 }
45
46 for actualKey := range actualKeys {
47 if !matchedKeys[actualKey] {
48 t.Fatalf("key %q was found, but was not expected", actualKey)
49 }
50 }
51 }
52
53 func TestKeyringAddingAndRemoving(t *testing.T) {
54 keyNames := []string{"dsa", "ecdsa", "rsa", "user"}
55
56
57 k := NewKeyring()
58 for _, keyName := range keyNames {
59 addTestKey(t, k, keyName)
60 }
61 validateListedKeys(t, k, keyNames)
62
63
64 keyToRemove := keyNames[1]
65 keyNames = append(keyNames[:1], keyNames[2:]...)
66
67 removeTestKey(t, k, keyToRemove)
68 validateListedKeys(t, k, keyNames)
69
70
71 err := k.RemoveAll()
72 if err != nil {
73 t.Fatalf("failed to remove all keys: %v", err)
74 }
75 validateListedKeys(t, k, []string{})
76 }
77
View as plain text