github.com/psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/crypto/ssh/agent/keyring_test.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 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 // add all test private keys 57 k := NewKeyring() 58 for _, keyName := range keyNames { 59 addTestKey(t, k, keyName) 60 } 61 validateListedKeys(t, k, keyNames) 62 63 // remove a key in the middle 64 keyToRemove := keyNames[1] 65 keyNames = append(keyNames[:1], keyNames[2:]...) 66 67 removeTestKey(t, k, keyToRemove) 68 validateListedKeys(t, k, keyNames) 69 70 // remove all keys 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 }