Add test for UnlockedCryptoService

Brings the test coverage for the cryptoservice package from 54.5% to
72.3% (based only on tests inside the package).

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
This commit is contained in:
Aaron Lehmann 2015-07-17 17:22:52 -07:00
parent f5d1a1fbf5
commit 16f57a6f4f
1 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,45 @@
package cryptoservice
import (
"crypto/rand"
"crypto/x509"
"testing"
"github.com/docker/notary/trustmanager"
"github.com/stretchr/testify/assert"
)
func TestUnlockedSigner(t *testing.T) {
privKey, err := trustmanager.GenerateECDSAKey(rand.Reader)
assert.NoError(t, err, "could not generate key")
keyStore := trustmanager.NewKeyMemoryStore()
passphrase := "passphrase"
err = keyStore.AddEncryptedKey(privKey.ID(), privKey, passphrase)
assert.NoError(t, err, "could not add key to store")
cryptoService := NewCryptoService("", keyStore, passphrase)
uCryptoService := NewUnlockedCryptoService(privKey, cryptoService)
// Check ID method
assert.Equal(t, privKey.ID(), uCryptoService.ID())
// Check Public method
assert.Equal(t, privKey.Public(), uCryptoService.PublicKey().Public())
assert.Equal(t, privKey.ID(), uCryptoService.PublicKey().ID())
// Check GenerateCertificate method
gun := "docker.com/notary"
cert, err := uCryptoService.GenerateCertificate(gun)
assert.NoError(t, err, "could not generate certificate")
// Check public key
ecdsaPrivateKey, err := x509.ParseECPrivateKey(privKey.Private())
assert.NoError(t, err)
ecdsaPublicKey := ecdsaPrivateKey.Public()
assert.Equal(t, ecdsaPublicKey, cert.PublicKey)
// Check CommonName
assert.Equal(t, cert.Subject.CommonName, gun)
}