acme/autocert: fix racy tests
memCache used an unsynchronized map, which failed go test -race. Add a mutex and constructor function to fix it. Change-Id: Iddaa492ea1e3d7747965c6423368baa6556402ed Reviewed-on: https://go-review.googlesource.com/36545 Run-TryBot: Heschi Kreinick <heschi@google.com> Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
@@ -22,6 +22,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -51,26 +52,44 @@ var authzTmpl = template.Must(template.New("authz").Parse(`{
|
|||||||
]
|
]
|
||||||
}`))
|
}`))
|
||||||
|
|
||||||
type memCache map[string][]byte
|
type memCache struct {
|
||||||
|
mu sync.Mutex
|
||||||
|
keyData map[string][]byte
|
||||||
|
}
|
||||||
|
|
||||||
func (m memCache) Get(ctx context.Context, key string) ([]byte, error) {
|
func (m *memCache) Get(ctx context.Context, key string) ([]byte, error) {
|
||||||
v, ok := m[key]
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
v, ok := m.keyData[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, ErrCacheMiss
|
return nil, ErrCacheMiss
|
||||||
}
|
}
|
||||||
return v, nil
|
return v, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m memCache) Put(ctx context.Context, key string, data []byte) error {
|
func (m *memCache) Put(ctx context.Context, key string, data []byte) error {
|
||||||
m[key] = data
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
m.keyData[key] = data
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m memCache) Delete(ctx context.Context, key string) error {
|
func (m *memCache) Delete(ctx context.Context, key string) error {
|
||||||
delete(m, key)
|
m.mu.Lock()
|
||||||
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
|
delete(m.keyData, key)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newMemCache() *memCache {
|
||||||
|
return &memCache{
|
||||||
|
keyData: make(map[string][]byte),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func dummyCert(pub interface{}, san ...string) ([]byte, error) {
|
func dummyCert(pub interface{}, san ...string) ([]byte, error) {
|
||||||
return dateDummyCert(pub, time.Now(), time.Now().Add(90*24*time.Hour), san...)
|
return dateDummyCert(pub, time.Now(), time.Now().Add(90*24*time.Hour), san...)
|
||||||
}
|
}
|
||||||
@@ -124,7 +143,7 @@ func TestGetCertificate_trailingDot(t *testing.T) {
|
|||||||
func TestGetCertificate_ForceRSA(t *testing.T) {
|
func TestGetCertificate_ForceRSA(t *testing.T) {
|
||||||
man := &Manager{
|
man := &Manager{
|
||||||
Prompt: AcceptTOS,
|
Prompt: AcceptTOS,
|
||||||
Cache: make(memCache),
|
Cache: newMemCache(),
|
||||||
ForceRSA: true,
|
ForceRSA: true,
|
||||||
}
|
}
|
||||||
defer man.stopRenew()
|
defer man.stopRenew()
|
||||||
@@ -280,8 +299,7 @@ func testGetCertificate(t *testing.T, man *Manager, domain string, hello *tls.Cl
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAccountKeyCache(t *testing.T) {
|
func TestAccountKeyCache(t *testing.T) {
|
||||||
cache := make(memCache)
|
m := Manager{Cache: newMemCache()}
|
||||||
m := Manager{Cache: cache}
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
k1, err := m.accountKey(ctx)
|
k1, err := m.accountKey(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -315,8 +333,7 @@ func TestCache(t *testing.T) {
|
|||||||
PrivateKey: privKey,
|
PrivateKey: privKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
cache := make(memCache)
|
man := &Manager{Cache: newMemCache()}
|
||||||
man := &Manager{Cache: cache}
|
|
||||||
defer man.stopRenew()
|
defer man.stopRenew()
|
||||||
if err := man.cachePut("example.org", tlscert); err != nil {
|
if err := man.cachePut("example.org", tlscert); err != nil {
|
||||||
t.Fatalf("man.cachePut: %v", err)
|
t.Fatalf("man.cachePut: %v", err)
|
||||||
|
|||||||
@@ -111,7 +111,7 @@ func TestRenewFromCache(t *testing.T) {
|
|||||||
}
|
}
|
||||||
man := &Manager{
|
man := &Manager{
|
||||||
Prompt: AcceptTOS,
|
Prompt: AcceptTOS,
|
||||||
Cache: make(memCache),
|
Cache: newMemCache(),
|
||||||
RenewBefore: 24 * time.Hour,
|
RenewBefore: 24 * time.Hour,
|
||||||
Client: &acme.Client{
|
Client: &acme.Client{
|
||||||
Key: key,
|
Key: key,
|
||||||
|
|||||||
Reference in New Issue
Block a user