Skip to content

Commit

Permalink
drive-by: unit tests had become much too slow, switch back to have a …
Browse files Browse the repository at this point in the history
…special path to generate test keys and use a shorter length there
  • Loading branch information
pedronis committed Jun 9, 2016
1 parent df0a6df commit 0644794
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
10 changes: 7 additions & 3 deletions asserts/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,19 @@ func OpenPGPPrivateKey(privk *packet.PrivateKey) PrivateKey {
return openpgpPrivateKey{privk}
}

// GenerateKey generates a private/public key pair.
func GenerateKey() (PrivateKey, error) {
priv, err := rsa.GenerateKey(rand.Reader, 4096)
func generateKey(bits int) (PrivateKey, error) {
priv, err := rsa.GenerateKey(rand.Reader, bits)
if err != nil {
return nil, err
}
return OpenPGPPrivateKey(packet.NewRSAPrivateKey(time.Now(), priv)), nil
}

// GenerateKey generates a private/public key pair.
func GenerateKey() (PrivateKey, error) {
return generateKey(4096)
}

func encodePrivateKey(privKey PrivateKey) ([]byte, error) {
return encodeKey(privKey, "private key")
}
Expand Down
3 changes: 3 additions & 0 deletions asserts/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (

// expose test-only things here

// GenerateTestKey lets the test generate keys of given length
var GenerateTestKey = generateKey

// access internal openpgp lib packet
func PrivateKeyPacket(pk PrivateKey) *packet.PrivateKey {
return pk.(openpgpPrivateKey).privk
Expand Down
6 changes: 5 additions & 1 deletion asserts/privkeys_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ var (
)

func genTestPrivKey() asserts.PrivateKey {
privKey, err := asserts.GenerateKey()
// use a shorter key length here for test key because otherwise
// they take too long to generate;
// the ones that care use pregenerated keys of the right length
// or use GenerateKey directly
privKey, err := asserts.GenerateTestKey(752)
if err != nil {
panic(fmt.Errorf("failed to create priv key for tests: %v", err))
}
Expand Down

0 comments on commit 0644794

Please sign in to comment.