MCPcopy
hub / github.com/caddyserver/certmagic / TestCacheCertificate

Function TestCacheCertificate

certificates_test.go:55–82  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

53}
54
55func TestCacheCertificate(t *testing.T) {
56 certCache := &Cache{cache: make(map[string]Certificate), cacheIndex: make(map[string][]string), logger: defaultTestLogger}
57
58 certCache.cacheCertificate(Certificate{Names: []string{"example.com", "sub.example.com"}, hash: "foobar", Certificate: tls.Certificate{Leaf: &x509.Certificate{NotAfter: time.Now()}}})
59 if len(certCache.cache) != 1 {
60 t.Errorf("Expected length of certificate cache to be 1")
61 }
62 if _, ok := certCache.cache["foobar"]; !ok {
63 t.Error("Expected first cert to be cached by key 'foobar', but it wasn't")
64 }
65 if _, ok := certCache.cacheIndex["example.com"]; !ok {
66 t.Error("Expected first cert to be keyed by 'example.com', but it wasn't")
67 }
68 if _, ok := certCache.cacheIndex["sub.example.com"]; !ok {
69 t.Error("Expected first cert to be keyed by 'sub.example.com', but it wasn't")
70 }
71
72 // using same cache; and has cert with overlapping name, but different hash
73 certCache.cacheCertificate(Certificate{Names: []string{"example.com"}, hash: "barbaz", Certificate: tls.Certificate{Leaf: &x509.Certificate{NotAfter: time.Now()}}})
74 if _, ok := certCache.cache["barbaz"]; !ok {
75 t.Error("Expected second cert to be cached by key 'barbaz.com', but it wasn't")
76 }
77 if hashes, ok := certCache.cacheIndex["example.com"]; !ok {
78 t.Error("Expected second cert to be keyed by 'example.com', but it wasn't")
79 } else if !reflect.DeepEqual(hashes, []string{"foobar", "barbaz"}) {
80 t.Errorf("Expected second cert to map to 'barbaz' but it was %v instead", hashes)
81 }
82}
83
84func TestSubjectQualifiesForCert(t *testing.T) {
85 for i, test := range []struct {

Callers

nothing calls this directly

Calls 2

cacheCertificateMethod · 0.95
ErrorMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…