-
Notifications
You must be signed in to change notification settings - Fork 11
/
certcache_test.go
78 lines (66 loc) · 1.73 KB
/
certcache_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"bytes"
"context"
"crypto/tls"
"errors"
"testing"
"golang.org/x/crypto/acme/autocert"
)
func TestPrematureGet(t *testing.T) {
var (
err error
r = new(certRetriever)
)
_, err = r.get(new(tls.ClientHelloInfo))
assertEqual(t, err, errUninitializedCert)
}
func TestGet(t *testing.T) {
var err error
var key = "foo"
var expectedCert = []byte("bar")
c := newCertCache()
// Retrieve non-existing key.
_, err = c.Get(context.TODO(), key)
if !errors.Is(err, autocert.ErrCacheMiss) {
t.Fatalf("Expected error %v but got %v.", autocert.ErrCacheMiss, err)
}
// Retrieve existing key.
_ = c.Put(context.TODO(), key, expectedCert)
cert, err := c.Get(context.TODO(), key)
if err != nil {
t.Fatalf("Expected no error but got %v.", err)
}
if !bytes.Equal(expectedCert, cert) {
t.Fatalf("Expected value %s but got %s.", string(expectedCert), string(cert))
}
}
func TestPut(t *testing.T) {
var err error
var key = "foo"
var expectedCert = []byte("bar")
c := newCertCache()
if err = c.Put(context.TODO(), key, expectedCert); err != nil {
t.Fatalf("Expected no error but got %v.", err)
}
}
func TestDelete(t *testing.T) {
var key = "foo"
var err error
c := newCertCache()
_ = c.Put(context.TODO(), key, []byte("bar"))
if err = c.Delete(context.TODO(), key); err != nil {
t.Fatalf("Expected no error but got %v.", err)
}
if len(c.cache) != 0 {
t.Fatal("Expected cache to be empty but it's not.")
}
// Try deleting the same element again. This should not result in an error
// as our Delete never returns an error.
if err = c.Delete(context.TODO(), key); err != nil {
t.Fatalf("Expected no error but got %v.", err)
}
if len(c.cache) != 0 {
t.Fatal("Expected cache to be empty but it's not.")
}
}