cache_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package autocert
  5. import (
  6. "context"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "reflect"
  11. "testing"
  12. )
  13. // make sure DirCache satisfies Cache interface
  14. var _ Cache = DirCache("/")
  15. func TestDirCache(t *testing.T) {
  16. dir, err := ioutil.TempDir("", "autocert")
  17. if err != nil {
  18. t.Fatal(err)
  19. }
  20. dir = filepath.Join(dir, "certs") // a nonexistent dir
  21. cache := DirCache(dir)
  22. ctx := context.Background()
  23. // test cache miss
  24. if _, err := cache.Get(ctx, "nonexistent"); err != ErrCacheMiss {
  25. t.Errorf("get: %v; want ErrCacheMiss", err)
  26. }
  27. // test put/get
  28. b1 := []byte{1}
  29. if err := cache.Put(ctx, "dummy", b1); err != nil {
  30. t.Fatalf("put: %v", err)
  31. }
  32. b2, err := cache.Get(ctx, "dummy")
  33. if err != nil {
  34. t.Fatalf("get: %v", err)
  35. }
  36. if !reflect.DeepEqual(b1, b2) {
  37. t.Errorf("b1 = %v; want %v", b1, b2)
  38. }
  39. name := filepath.Join(dir, "dummy")
  40. if _, err := os.Stat(name); err != nil {
  41. t.Error(err)
  42. }
  43. // test delete
  44. if err := cache.Delete(ctx, "dummy"); err != nil {
  45. t.Fatalf("delete: %v", err)
  46. }
  47. if _, err := cache.Get(ctx, "dummy"); err != ErrCacheMiss {
  48. t.Errorf("get: %v; want ErrCacheMiss", err)
  49. }
  50. }