Skip to content

Commit 1f99b0e

Browse files
x1ddosAlex Vaghin
authored andcommitted
acme: prevent discovery network roundtrips in tests
Client's FetchCert now requires to do the discovery before fetching a cert from RFC 8555 compliant CA using POST-as-GET requests. Some tests initialized the client without providing a directory metadata, making it do network roundtrips to the default CA. This CL sets a non-nil directory metadata to prevent unnecessary network roundtrips to an actual CA during tests. Change-Id: Icaa6c2c2fd25ae8e5b1b44c6c3dafa246e6bdbb0 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/200637 Run-TryBot: Alex Vaghin <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent af544f3 commit 1f99b0e

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

acme/acme_test.go

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ import (
2727
"time"
2828
)
2929

30+
// newTestClient creates a client with a non-nil Directory so that it skips
31+
// the discovery which is otherwise done on the first call of almost every
32+
// exported method.
33+
func newTestClient() *Client {
34+
return &Client{
35+
Key: testKeyEC,
36+
dir: &Directory{}, // skip discovery
37+
}
38+
}
39+
3040
// Decodes a JWS-encoded request and unmarshals the decoded JSON into a provided
3141
// interface.
3242
func decodeJWSRequest(t *testing.T, v interface{}, r io.Reader) {
@@ -865,7 +875,7 @@ func TestFetchCert(t *testing.T) {
865875
w.Write([]byte{count})
866876
}))
867877
defer ts.Close()
868-
cl := &Client{dir: &Directory{}} // skip discovery
878+
cl := newTestClient()
869879
res, err := cl.FetchCert(context.Background(), ts.URL, true)
870880
if err != nil {
871881
t.Fatalf("FetchCert: %v", err)
@@ -888,7 +898,7 @@ func TestFetchCertRetry(t *testing.T) {
888898
w.Write([]byte{1})
889899
}))
890900
defer ts.Close()
891-
cl := &Client{dir: &Directory{}} // skip discovery
901+
cl := newTestClient()
892902
res, err := cl.FetchCert(context.Background(), ts.URL, false)
893903
if err != nil {
894904
t.Fatalf("FetchCert: %v", err)
@@ -909,7 +919,8 @@ func TestFetchCertCancel(t *testing.T) {
909919
done := make(chan struct{})
910920
var err error
911921
go func() {
912-
_, err = (&Client{}).FetchCert(ctx, ts.URL, false)
922+
cl := newTestClient()
923+
_, err = cl.FetchCert(ctx, ts.URL, false)
913924
close(done)
914925
}()
915926
cancel()
@@ -932,7 +943,8 @@ func TestFetchCertDepth(t *testing.T) {
932943
w.Write([]byte{count})
933944
}))
934945
defer ts.Close()
935-
_, err := (&Client{}).FetchCert(context.Background(), ts.URL, true)
946+
cl := newTestClient()
947+
_, err := cl.FetchCert(context.Background(), ts.URL, true)
936948
if err == nil {
937949
t.Errorf("err is nil")
938950
}
@@ -947,7 +959,8 @@ func TestFetchCertBreadth(t *testing.T) {
947959
w.Write([]byte{1})
948960
}))
949961
defer ts.Close()
950-
_, err := (&Client{}).FetchCert(context.Background(), ts.URL, true)
962+
cl := newTestClient()
963+
_, err := cl.FetchCert(context.Background(), ts.URL, true)
951964
if err == nil {
952965
t.Errorf("err is nil")
953966
}
@@ -959,7 +972,8 @@ func TestFetchCertSize(t *testing.T) {
959972
w.Write(b)
960973
}))
961974
defer ts.Close()
962-
_, err := (&Client{}).FetchCert(context.Background(), ts.URL, false)
975+
cl := newTestClient()
976+
_, err := cl.FetchCert(context.Background(), ts.URL, false)
963977
if err == nil {
964978
t.Errorf("err is nil")
965979
}
@@ -1044,7 +1058,7 @@ func TestNonce_fetch(t *testing.T) {
10441058
defer ts.Close()
10451059
for ; i < len(tests); i++ {
10461060
test := tests[i]
1047-
c := &Client{}
1061+
c := newTestClient()
10481062
n, err := c.fetchNonce(context.Background(), ts.URL)
10491063
if n != test.nonce {
10501064
t.Errorf("%d: n=%q; want %q", i, n, test.nonce)
@@ -1063,7 +1077,7 @@ func TestNonce_fetchError(t *testing.T) {
10631077
w.WriteHeader(http.StatusTooManyRequests)
10641078
}))
10651079
defer ts.Close()
1066-
c := &Client{}
1080+
c := newTestClient()
10671081
_, err := c.fetchNonce(context.Background(), ts.URL)
10681082
e, ok := err.(*Error)
10691083
if !ok {
@@ -1210,8 +1224,7 @@ func TestTLSSNI01ChallengeCert(t *testing.T) {
12101224
san = "dbbd5eefe7b4d06eb9d1d9f5acb4c7cd.a27d320e4b30332f0b6cb441734ad7b0.acme.invalid"
12111225
)
12121226

1213-
client := &Client{Key: testKeyEC}
1214-
tlscert, name, err := client.TLSSNI01ChallengeCert(token)
1227+
tlscert, name, err := newTestClient().TLSSNI01ChallengeCert(token)
12151228
if err != nil {
12161229
t.Fatal(err)
12171230
}
@@ -1243,8 +1256,7 @@ func TestTLSSNI02ChallengeCert(t *testing.T) {
12431256
sanB = "dbbd5eefe7b4d06eb9d1d9f5acb4c7cd.a27d320e4b30332f0b6cb441734ad7b0.ka.acme.invalid"
12441257
)
12451258

1246-
client := &Client{Key: testKeyEC}
1247-
tlscert, name, err := client.TLSSNI02ChallengeCert(token)
1259+
tlscert, name, err := newTestClient().TLSSNI02ChallengeCert(token)
12481260
if err != nil {
12491261
t.Fatal(err)
12501262
}
@@ -1284,8 +1296,7 @@ func TestTLSALPN01ChallengeCert(t *testing.T) {
12841296
t.Fatal(err)
12851297
}
12861298

1287-
client := &Client{Key: testKeyEC}
1288-
tlscert, err := client.TLSALPN01ChallengeCert(token, domain)
1299+
tlscert, err := newTestClient().TLSALPN01ChallengeCert(token, domain)
12891300
if err != nil {
12901301
t.Fatal(err)
12911302
}
@@ -1334,7 +1345,7 @@ func TestTLSChallengeCertOpt(t *testing.T) {
13341345
}
13351346
opts := []CertOption{WithKey(key), WithTemplate(tmpl)}
13361347

1337-
client := &Client{Key: testKeyEC}
1348+
client := newTestClient()
13381349
cert1, _, err := client.TLSSNI01ChallengeCert("token", opts...)
13391350
if err != nil {
13401351
t.Fatal(err)
@@ -1392,7 +1403,7 @@ func TestHTTP01Challenge(t *testing.T) {
13921403
value = token + "." + testKeyECThumbprint
13931404
urlpath = "/.well-known/acme-challenge/" + token
13941405
)
1395-
client := &Client{Key: testKeyEC}
1406+
client := newTestClient()
13961407
val, err := client.HTTP01ChallengeResponse(token)
13971408
if err != nil {
13981409
t.Fatal(err)
@@ -1411,8 +1422,7 @@ func TestDNS01ChallengeRecord(t *testing.T) {
14111422
// base64 | tr -d '=' | tr '/+' '_-'
14121423
const value = "8DERMexQ5VcdJ_prpPiA0mVdp7imgbCgjsG4SqqNMIo"
14131424

1414-
client := &Client{Key: testKeyEC}
1415-
val, err := client.DNS01ChallengeRecord("xxx")
1425+
val, err := newTestClient().DNS01ChallengeRecord("xxx")
14161426
if err != nil {
14171427
t.Fatal(err)
14181428
}

0 commit comments

Comments
 (0)