...
1
2
3
4
5 package autocert
6
7 import (
8 "context"
9 "os"
10 "path/filepath"
11 "reflect"
12 "testing"
13 )
14
15
16 var _ Cache = DirCache("/")
17
18 func TestDirCache(t *testing.T) {
19 dir, err := os.MkdirTemp("", "autocert")
20 if err != nil {
21 t.Fatal(err)
22 }
23 defer os.RemoveAll(dir)
24 dir = filepath.Join(dir, "certs")
25 cache := DirCache(dir)
26 ctx := context.Background()
27
28
29 if _, err := cache.Get(ctx, "nonexistent"); err != ErrCacheMiss {
30 t.Errorf("get: %v; want ErrCacheMiss", err)
31 }
32
33
34 b1 := []byte{1}
35 if err := cache.Put(ctx, "dummy", b1); err != nil {
36 t.Fatalf("put: %v", err)
37 }
38 b2, err := cache.Get(ctx, "dummy")
39 if err != nil {
40 t.Fatalf("get: %v", err)
41 }
42 if !reflect.DeepEqual(b1, b2) {
43 t.Errorf("b1 = %v; want %v", b1, b2)
44 }
45 name := filepath.Join(dir, "dummy")
46 if _, err := os.Stat(name); err != nil {
47 t.Error(err)
48 }
49
50
51 tmp, err := filepath.Glob(name + "?*")
52 if err != nil {
53 t.Error(err)
54 }
55 if tmp != nil {
56 t.Errorf("temp file exists: %s", tmp)
57 }
58
59
60 if err := cache.Delete(ctx, "dummy"); err != nil {
61 t.Fatalf("delete: %v", err)
62 }
63 if _, err := cache.Get(ctx, "dummy"); err != ErrCacheMiss {
64 t.Errorf("get: %v; want ErrCacheMiss", err)
65 }
66 }
67
View as plain text