Skip to content

Commit

Permalink
cleanup GeneratePrivateKeyInTest vs plan GenerateKey
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronis committed May 27, 2016
1 parent 1f4a2f2 commit d4f5b5c
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 48 deletions.
8 changes: 4 additions & 4 deletions asserts/account_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (aks *accountKeySuite) SetUpSuite(c *C) {
}
accDb, err := asserts.OpenDatabase(cfg1)
c.Assert(err, IsNil)
pk := asserts.OpenPGPPrivateKey(testPrivKey1)
pk := testPrivKey1
err = accDb.ImportKey("acc-id1", pk)
c.Assert(err, IsNil)
aks.fp = pk.PublicKey().Fingerprint()
Expand Down Expand Up @@ -195,7 +195,7 @@ func (aks *accountKeySuite) openDB(c *C) *asserts.Database {
cfg := &asserts.DatabaseConfig{
Backstore: bs,
KeypairManager: asserts.NewMemoryKeypairManager(),
TrustedKeys: []*asserts.AccountKey{asserts.BootstrapAccountKeyForTest("canonical", &trustedKey.PublicKey)},
TrustedKeys: []*asserts.AccountKey{asserts.BootstrapAccountKeyForTest("canonical", trustedKey.PublicKey())},
}
db, err := asserts.OpenDatabase(cfg)
c.Assert(err, IsNil)
Expand All @@ -213,7 +213,7 @@ func (aks *accountKeySuite) TestAccountKeyCheck(c *C) {
"since": aks.since.Format(time.RFC3339),
"until": aks.until.Format(time.RFC3339),
}
accKey, err := asserts.AssembleAndSignInTest(asserts.AccountKeyType, headers, []byte(aks.pubKeyBody), asserts.OpenPGPPrivateKey(trustedKey))
accKey, err := asserts.AssembleAndSignInTest(asserts.AccountKeyType, headers, []byte(aks.pubKeyBody), trustedKey)
c.Assert(err, IsNil)

db := aks.openDB(c)
Expand All @@ -233,7 +233,7 @@ func (aks *accountKeySuite) TestAccountKeyAddAndFind(c *C) {
"since": aks.since.Format(time.RFC3339),
"until": aks.until.Format(time.RFC3339),
}
accKey, err := asserts.AssembleAndSignInTest(asserts.AccountKeyType, headers, []byte(aks.pubKeyBody), asserts.OpenPGPPrivateKey(trustedKey))
accKey, err := asserts.AssembleAndSignInTest(asserts.AccountKeyType, headers, []byte(aks.pubKeyBody), trustedKey)
c.Assert(err, IsNil)

db := aks.openDB(c)
Expand Down
6 changes: 3 additions & 3 deletions asserts/asserts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ func (as *assertsSuite) TestSignFormatSanityEmptyBody(c *C) {
"authority-id": "auth-id1",
"primary-key": "0",
}
a, err := asserts.AssembleAndSignInTest(asserts.TestOnlyType, headers, nil, asserts.OpenPGPPrivateKey(testPrivKey1))
a, err := asserts.AssembleAndSignInTest(asserts.TestOnlyType, headers, nil, testPrivKey1)
c.Assert(err, IsNil)

_, err = asserts.Decode(asserts.Encode(a))
Expand All @@ -407,7 +407,7 @@ func (as *assertsSuite) TestSignFormatSanityNonEmptyBody(c *C) {
"primary-key": "0",
}
body := []byte("THE-BODY")
a, err := asserts.AssembleAndSignInTest(asserts.TestOnlyType, headers, body, asserts.OpenPGPPrivateKey(testPrivKey1))
a, err := asserts.AssembleAndSignInTest(asserts.TestOnlyType, headers, body, testPrivKey1)
c.Assert(err, IsNil)
c.Check(a.Body(), DeepEquals, body)

Expand Down Expand Up @@ -437,7 +437,7 @@ func (as *assertsSuite) TestSignFormatSanitySupportMultilineHeaderValues(c *C) {
headers["odd"] = "true"
}

a, err := asserts.AssembleAndSignInTest(asserts.TestOnlyType, headers, nil, asserts.OpenPGPPrivateKey(testPrivKey1))
a, err := asserts.AssembleAndSignInTest(asserts.TestOnlyType, headers, nil, testPrivKey1)
c.Assert(err, IsNil)

decoded, err := asserts.Decode(asserts.Encode(a))
Expand Down
34 changes: 17 additions & 17 deletions asserts/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ func (dbs *databaseSuite) SetUpTest(c *C) {
}

func (dbs *databaseSuite) TestImportKey(c *C) {
expectedFingerprint := hex.EncodeToString(testPrivKey1.PublicKey.Fingerprint[:])
expectedKeyID := hex.EncodeToString(testPrivKey1.PublicKey.Fingerprint[12:])
expectedFingerprint := hex.EncodeToString(testPrivKey1Pkt.PublicKey.Fingerprint[:])
expectedKeyID := hex.EncodeToString(testPrivKey1Pkt.PublicKey.Fingerprint[12:])

err := dbs.db.ImportKey("account0", asserts.OpenPGPPrivateKey(testPrivKey1))
err := dbs.db.ImportKey("account0", testPrivKey1)
c.Assert(err, IsNil)

keyPath := filepath.Join(dbs.topDir, "private-keys-v0/account0", expectedKeyID)
Expand All @@ -99,15 +99,15 @@ func (dbs *databaseSuite) TestImportKey(c *C) {
}

func (dbs *databaseSuite) TestImportKeyAlreadyExists(c *C) {
err := dbs.db.ImportKey("account0", asserts.OpenPGPPrivateKey(testPrivKey1))
err := dbs.db.ImportKey("account0", testPrivKey1)
c.Assert(err, IsNil)

err = dbs.db.ImportKey("account0", asserts.OpenPGPPrivateKey(testPrivKey1))
err = dbs.db.ImportKey("account0", testPrivKey1)
c.Check(err, ErrorMatches, "key pair with given key id already exists")
}

func (dbs *databaseSuite) TestPublicKey(c *C) {
pk := asserts.OpenPGPPrivateKey(testPrivKey1)
pk := testPrivKey1
fingerp := pk.PublicKey().Fingerprint()
keyid := pk.PublicKey().ID()
err := dbs.db.ImportKey("account0", pk)
Expand All @@ -127,11 +127,11 @@ func (dbs *databaseSuite) TestPublicKey(c *C) {
c.Assert(err, IsNil)
pubKey, ok := pkt.(*packet.PublicKey)
c.Assert(ok, Equals, true)
c.Assert(pubKey.Fingerprint, DeepEquals, testPrivKey1.PublicKey.Fingerprint)
c.Assert(pubKey.Fingerprint, DeepEquals, testPrivKey1Pkt.PublicKey.Fingerprint)
}

func (dbs *databaseSuite) TestPublicKeyNotFound(c *C) {
pk := asserts.OpenPGPPrivateKey(testPrivKey1)
pk := testPrivKey1
keyID := pk.PublicKey().ID()

_, err := dbs.db.PublicKey("account0", keyID)
Expand Down Expand Up @@ -162,7 +162,7 @@ func (chks *checkSuite) SetUpTest(c *C) {
"authority-id": "canonical",
"primary-key": "0",
}
chks.a, err = asserts.AssembleAndSignInTest(asserts.TestOnlyType, headers, nil, asserts.OpenPGPPrivateKey(testPrivKey0))
chks.a, err = asserts.AssembleAndSignInTest(asserts.TestOnlyType, headers, nil, testPrivKey0)
c.Assert(err, IsNil)
}

Expand All @@ -184,7 +184,7 @@ func (chks *checkSuite) TestCheckExpiredPubKey(c *C) {
cfg := &asserts.DatabaseConfig{
Backstore: chks.bs,
KeypairManager: asserts.NewMemoryKeypairManager(),
TrustedKeys: []*asserts.AccountKey{asserts.ExpiredAccountKeyForTest("canonical", &trustedKey.PublicKey)},
TrustedKeys: []*asserts.AccountKey{asserts.ExpiredAccountKeyForTest("canonical", trustedKey.PublicKey())},
}
db, err := asserts.OpenDatabase(cfg)
c.Assert(err, IsNil)
Expand All @@ -199,7 +199,7 @@ func (chks *checkSuite) TestCheckForgery(c *C) {
cfg := &asserts.DatabaseConfig{
Backstore: chks.bs,
KeypairManager: asserts.NewMemoryKeypairManager(),
TrustedKeys: []*asserts.AccountKey{asserts.BootstrapAccountKeyForTest("canonical", &trustedKey.PublicKey)},
TrustedKeys: []*asserts.AccountKey{asserts.BootstrapAccountKeyForTest("canonical", trustedKey.PublicKey())},
}
db, err := asserts.OpenDatabase(cfg)
c.Assert(err, IsNil)
Expand All @@ -208,13 +208,13 @@ func (chks *checkSuite) TestCheckForgery(c *C) {
content, encodedSig := chks.a.Signature()
// forgery
forgedSig := new(packet.Signature)
forgedSig.PubKeyAlgo = testPrivKey1.PubKeyAlgo
forgedSig.PubKeyAlgo = testPrivKey1Pkt.PubKeyAlgo
forgedSig.Hash = crypto.SHA256
forgedSig.CreationTime = time.Now()
forgedSig.IssuerKeyId = &testPrivKey0.KeyId
forgedSig.IssuerKeyId = &asserts.PrivateKeyPacket(testPrivKey0).KeyId
h := crypto.SHA256.New()
h.Write(content)
err = forgedSig.Sign(h, testPrivKey1, &packet.Config{DefaultHash: crypto.SHA256})
err = forgedSig.Sign(h, testPrivKey1Pkt, &packet.Config{DefaultHash: crypto.SHA256})
c.Assert(err, IsNil)
buf := new(bytes.Buffer)
forgedSig.Serialize(buf)
Expand Down Expand Up @@ -245,7 +245,7 @@ func (safs *signAddFindSuite) SetUpTest(c *C) {
c.Assert(err, IsNil)
safs.signingDB = db0

pk := asserts.OpenPGPPrivateKey(testPrivKey0)
pk := testPrivKey0
err = db0.ImportKey("canonical", pk)
c.Assert(err, IsNil)
safs.signingKeyID = pk.PublicKey().ID()
Expand All @@ -258,7 +258,7 @@ func (safs *signAddFindSuite) SetUpTest(c *C) {
cfg := &asserts.DatabaseConfig{
Backstore: bs,
KeypairManager: asserts.NewMemoryKeypairManager(),
TrustedKeys: []*asserts.AccountKey{asserts.BootstrapAccountKeyForTest("canonical", &trustedKey.PublicKey)},
TrustedKeys: []*asserts.AccountKey{asserts.BootstrapAccountKeyForTest("canonical", trustedKey.PublicKey())},
}
db, err := asserts.OpenDatabase(cfg)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -490,7 +490,7 @@ func (safs *signAddFindSuite) TestFindMany(c *C) {
}

func (safs *signAddFindSuite) TestFindFindsTrustedAccountKeys(c *C) {
pk1 := asserts.OpenPGPPrivateKey(testPrivKey1)
pk1 := testPrivKey1
pubKey1Encoded, err := asserts.EncodePublicKey(pk1.PublicKey())
c.Assert(err, IsNil)

Expand Down
2 changes: 1 addition & 1 deletion asserts/device_asserts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func (ss *serialSuite) SetUpSuite(c *C) {
ss.ts = time.Now().Truncate(time.Second).UTC()
ss.tsLine = "timestamp: " + ss.ts.Format(time.RFC3339) + "\n"

ss.deviceKey = asserts.OpenPGPPrivateKey(testPrivKey2)
ss.deviceKey = testPrivKey2
encodedPubKey, err := asserts.EncodePublicKey(ss.deviceKey.PublicKey())
c.Assert(err, IsNil)
ss.encodedDevKey = string(encodedPubKey)
Expand Down
17 changes: 6 additions & 11 deletions asserts/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,9 @@ import (

// expose test-only things here

// XXX: just use GenerateKey
func GeneratePrivateKeyInTest() (*packet.PrivateKey, error) {
priv, err := GenerateKey()
if err != nil {
return nil, err
}
return priv.(openpgpPrivateKey).privk, nil
// access internal openpgp lib packet
func PrivateKeyPacket(pk PrivateKey) *packet.PrivateKey {
return pk.(openpgpPrivateKey).privk
}

// assembleAndSign exposed for tests
Expand All @@ -59,8 +55,7 @@ func EncoderAppend(enc *Encoder, encoded []byte) error {
return enc.append(encoded)
}

func makeAccountKeyForTest(authorityID string, pubKey *packet.PublicKey, validYears int) *AccountKey {
openPGPPubKey := OpenPGPPublicKey(pubKey)
func makeAccountKeyForTest(authorityID string, openPGPPubKey PublicKey, validYears int) *AccountKey {
return &AccountKey{
assertionBase: assertionBase{
headers: map[string]string{
Expand All @@ -75,11 +70,11 @@ func makeAccountKeyForTest(authorityID string, pubKey *packet.PublicKey, validYe
}
}

func BootstrapAccountKeyForTest(authorityID string, pubKey *packet.PublicKey) *AccountKey {
func BootstrapAccountKeyForTest(authorityID string, pubKey PublicKey) *AccountKey {
return makeAccountKeyForTest(authorityID, pubKey, 9999)
}

func ExpiredAccountKeyForTest(authorityID string, pubKey *packet.PublicKey) *AccountKey {
func ExpiredAccountKeyForTest(authorityID string, pubKey PublicKey) *AccountKey {
return makeAccountKeyForTest(authorityID, pubKey, 1)
}

Expand Down
6 changes: 3 additions & 3 deletions asserts/memkeypairmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (mkms *memKeypairMgtSuite) SetUpTest(c *C) {
}

func (mkms *memKeypairMgtSuite) TestPutAndGet(c *C) {
pk1 := asserts.OpenPGPPrivateKey(testPrivKey1)
pk1 := testPrivKey1
keyID := pk1.PublicKey().ID()
err := mkms.keypairMgr.Put("auth-id1", pk1)
c.Assert(err, IsNil)
Expand All @@ -48,7 +48,7 @@ func (mkms *memKeypairMgtSuite) TestPutAndGet(c *C) {
}

func (mkms *memKeypairMgtSuite) TestPutAlreadyExists(c *C) {
pk1 := asserts.OpenPGPPrivateKey(testPrivKey1)
pk1 := testPrivKey1
err := mkms.keypairMgr.Put("auth-id1", pk1)
c.Assert(err, IsNil)

Expand All @@ -57,7 +57,7 @@ func (mkms *memKeypairMgtSuite) TestPutAlreadyExists(c *C) {
}

func (mkms *memKeypairMgtSuite) TestGetNotFound(c *C) {
pk1 := asserts.OpenPGPPrivateKey(testPrivKey1)
pk1 := testPrivKey1
keyID := pk1.PublicKey().ID()

got, err := mkms.keypairMgr.Get("auth-id1", keyID)
Expand Down
8 changes: 4 additions & 4 deletions asserts/privkeys_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ package asserts_test
import (
"fmt"

"golang.org/x/crypto/openpgp/packet"

"github.com/snapcore/snapd/asserts"
)

Expand All @@ -32,10 +30,12 @@ var (
testPrivKey0 = genTestPrivKey()
testPrivKey1 = genTestPrivKey()
testPrivKey2 = genTestPrivKey()

testPrivKey1Pkt = asserts.PrivateKeyPacket(testPrivKey1)
)

func genTestPrivKey() *packet.PrivateKey {
privKey, err := asserts.GeneratePrivateKeyInTest()
func genTestPrivKey() asserts.PrivateKey {
privKey, err := asserts.GenerateKey()
if err != nil {
panic(fmt.Errorf("failed to create priv key for tests: %v", err))
}
Expand Down
8 changes: 4 additions & 4 deletions asserts/snap_asserts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ func makeSignAndCheckDbWithAccountKey(c *C, accountID string) (signingKeyID stri
}
accSignDB, err := asserts.OpenDatabase(cfg1)
c.Assert(err, IsNil)
pk1 := asserts.OpenPGPPrivateKey(testPrivKey1)
err = accSignDB.ImportKey(accountID, asserts.OpenPGPPrivateKey(testPrivKey1))
pk1 := testPrivKey1
err = accSignDB.ImportKey(accountID, testPrivKey1)
c.Assert(err, IsNil)
accFingerp := pk1.PublicKey().Fingerprint()
accKeyID := pk1.PublicKey().ID()
Expand All @@ -232,7 +232,7 @@ func makeSignAndCheckDbWithAccountKey(c *C, accountID string) (signingKeyID stri
"since": "2015-11-20T15:04:00Z",
"until": "2500-11-20T15:04:00Z",
}
accKey, err := asserts.AssembleAndSignInTest(asserts.AccountKeyType, headers, []byte(accPubKeyBody), asserts.OpenPGPPrivateKey(trustedKey))
accKey, err := asserts.AssembleAndSignInTest(asserts.AccountKeyType, headers, []byte(accPubKeyBody), trustedKey)
c.Assert(err, IsNil)

topDir := filepath.Join(c.MkDir(), "asserts-db")
Expand All @@ -241,7 +241,7 @@ func makeSignAndCheckDbWithAccountKey(c *C, accountID string) (signingKeyID stri
cfg := &asserts.DatabaseConfig{
Backstore: bs,
KeypairManager: asserts.NewMemoryKeypairManager(),
TrustedKeys: []*asserts.AccountKey{asserts.BootstrapAccountKeyForTest("canonical", &trustedKey.PublicKey)},
TrustedKeys: []*asserts.AccountKey{asserts.BootstrapAccountKeyForTest("canonical", trustedKey.PublicKey())},
}
checkDB, err = asserts.OpenDatabase(cfg)
c.Assert(err, IsNil)
Expand Down
2 changes: 1 addition & 1 deletion asserts/sysdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ var _ = Suite(&sysDBSuite{})
func (sdbs *sysDBSuite) SetUpTest(c *C) {
tmpdir := c.MkDir()

pk := asserts.OpenPGPPrivateKey(testPrivKey0)
pk := testPrivKey0
trustedPubKey := pk.PublicKey()
trustedPubKeyEncoded, err := asserts.EncodePublicKey(trustedPubKey)
c.Assert(err, IsNil)
Expand Down

0 comments on commit d4f5b5c

Please sign in to comment.