add GetKeyInfo test for memory store

Signed-off-by: Riyaz Faizullabhoy <riyaz.faizullabhoy@docker.com>
This commit is contained in:
Riyaz Faizullabhoy 2016-02-11 09:28:20 -08:00
parent 97e845e295
commit 0f39dd7aa8
2 changed files with 46 additions and 3 deletions

View File

@ -74,9 +74,6 @@ func (cs *CryptoService) Create(role, algorithm string) (data.PublicKey, error)
}
// GetPrivateKey returns a private key and role if present by ID.
// It tries to get the key first without a GUN (in which case it's a root key).
// If that fails, try to get the key with the GUN (non-root key).
// If that fails, then we don't have the key.
func (cs *CryptoService) GetPrivateKey(keyID string) (k data.PrivateKey, role string, err error) {
for _, ks := range cs.keyStores {
k, role, err = ks.GetKey(keyID)

View File

@ -390,6 +390,52 @@ func TestAddGetKeyMemStore(t *testing.T) {
assert.Equal(t, retrievedKey.Private(), privKey.Private())
}
func TestAddGetKeyInfoMemStore(t *testing.T) {
gun := "docker.com/notary"
// Create our store
store := NewKeyMemoryStore(passphraseRetriever)
rootKey, err := GenerateECDSAKey(rand.Reader)
assert.NoError(t, err, "could not generate private key")
// Call the AddKey function
err = store.AddKey(rootKey.ID(), data.CanonicalRootRole, rootKey)
assert.NoError(t, err, "failed to add key to store")
// Get and validate key info
rootInfo, err := store.GetKeyInfo(rootKey.ID())
assert.NoError(t, err)
assert.Equal(t, data.CanonicalRootRole, rootInfo.Role)
assert.Equal(t, "", rootInfo.Gun)
targetsKey, err := GenerateECDSAKey(rand.Reader)
assert.NoError(t, err, "could not generate private key")
// Call the AddKey function
err = store.AddKey(filepath.Join(gun, targetsKey.ID()), data.CanonicalTargetsRole, targetsKey)
assert.NoError(t, err, "failed to add key to store")
// Get and validate key info
targetsInfo, err := store.GetKeyInfo(targetsKey.ID())
assert.NoError(t, err)
assert.Equal(t, data.CanonicalTargetsRole, targetsInfo.Role)
assert.Equal(t, gun, targetsInfo.Gun)
delgKey, err := GenerateECDSAKey(rand.Reader)
assert.NoError(t, err, "could not generate private key")
// Call the AddKey function
err = store.AddKey(filepath.Join(gun, delgKey.ID()), "targets/delegation", delgKey)
assert.NoError(t, err, "failed to add key to store")
// Get and validate key info
delgInfo, err := store.GetKeyInfo(delgKey.ID())
assert.NoError(t, err)
assert.Equal(t, "targets/delegation", delgInfo.Role)
assert.Equal(t, gun, delgInfo.Gun)
}
func TestGetDecryptedWithTamperedCipherText(t *testing.T) {
testExt := "key"
testAlias := "root"