Skip to content

Commit af32e34

Browse files
committed
Speedup tests by using insecure key profile.
1 parent 36aef08 commit af32e34

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

pki/pki_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ import (
77
"testing"
88

99
gc "gopkg.in/check.v1"
10+
11+
"github.com/juju/juju/pki"
12+
pki_test "github.com/juju/juju/pki/test"
1013
)
1114

12-
func TestSuite(t *testing.T) { gc.TestingT(t) }
15+
func TestSuite(t *testing.T) {
16+
if pki_test.OriginalDefaultKeyProfile == nil {
17+
panic("pki_test.OriginalDefaultKeyProfile not set")
18+
}
19+
// Restore the correct key profile.
20+
pki.DefaultKeyProfile = pki_test.OriginalDefaultKeyProfile
21+
gc.TestingT(t)
22+
}

pki/signer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ func ECDSAP384() (crypto.Signer, error) {
3939
return ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
4040
}
4141

42-
// ECDSA384 returns a RSA 2048 private key
42+
// RSA2048 returns a RSA 2048 private key
4343
func RSA2048() (crypto.Signer, error) {
4444
return rsa.GenerateKey(rand.Reader, 2048)
4545
}
4646

47-
// ECDSA384 returns a RSA 3072 private key
47+
// RSA3072 returns a RSA 3072 private key
4848
func RSA3072() (crypto.Signer, error) {
4949
return rsa.GenerateKey(rand.Reader, 3072)
5050
}

pki/test/keyprofile.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2020 Canonical Ltd.
2+
// Licensed under the AGPLv3, see LICENCE file for details.
3+
4+
package test
5+
6+
import (
7+
"crypto"
8+
"crypto/rsa"
9+
"flag"
10+
"math/rand"
11+
12+
"github.com/juju/juju/pki"
13+
)
14+
15+
var insecureRand = rand.New(rand.NewSource(0))
16+
17+
// InsecureKeyProfile for tests. Will panic if used outside tests.
18+
func InsecureKeyProfile() (crypto.Signer, error) {
19+
if flag.Lookup("test.v") == nil {
20+
panic("InsecureKeyProfile cannot be used outside tests")
21+
}
22+
return rsa.GenerateKey(insecureRand, 512)
23+
}
24+
25+
// OriginalDefaultKeyProfile is the pre-patched pki.DefaultKeyProfile
26+
// value.
27+
var OriginalDefaultKeyProfile = pki.DefaultKeyProfile
28+
29+
func init() {
30+
pki.DefaultKeyProfile = InsecureKeyProfile
31+
}

testing/pki.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2020 Canonical Ltd.
2+
// Licensed under the AGPLv3, see LICENCE file for details.
3+
4+
package testing
5+
6+
import (
7+
// Force inclusion of pki/test for fast test keyprofiles.
8+
_ "github.com/juju/juju/pki/test"
9+
)

0 commit comments

Comments
 (0)