2016-06-09 22:11:16 +03:00
|
|
|
// Copyright 2016 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package autocert
|
|
|
|
|
|
|
|
import (
|
2017-04-02 23:18:05 +03:00
|
|
|
"context"
|
2016-06-09 22:11:16 +03:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// make sure DirCache satisfies Cache interface
|
|
|
|
var _ Cache = DirCache("/")
|
|
|
|
|
|
|
|
func TestDirCache(t *testing.T) {
|
|
|
|
dir, err := ioutil.TempDir("", "autocert")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-02 04:54:02 +03:00
|
|
|
defer os.RemoveAll(dir)
|
2016-06-09 22:11:16 +03:00
|
|
|
dir = filepath.Join(dir, "certs") // a nonexistent dir
|
|
|
|
cache := DirCache(dir)
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// test cache miss
|
|
|
|
if _, err := cache.Get(ctx, "nonexistent"); err != ErrCacheMiss {
|
|
|
|
t.Errorf("get: %v; want ErrCacheMiss", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test put/get
|
|
|
|
b1 := []byte{1}
|
|
|
|
if err := cache.Put(ctx, "dummy", b1); err != nil {
|
|
|
|
t.Fatalf("put: %v", err)
|
|
|
|
}
|
|
|
|
b2, err := cache.Get(ctx, "dummy")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("get: %v", err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(b1, b2) {
|
|
|
|
t.Errorf("b1 = %v; want %v", b1, b2)
|
|
|
|
}
|
|
|
|
name := filepath.Join(dir, "dummy")
|
|
|
|
if _, err := os.Stat(name); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// test delete
|
|
|
|
if err := cache.Delete(ctx, "dummy"); err != nil {
|
|
|
|
t.Fatalf("delete: %v", err)
|
|
|
|
}
|
|
|
|
if _, err := cache.Get(ctx, "dummy"); err != ErrCacheMiss {
|
|
|
|
t.Errorf("get: %v; want ErrCacheMiss", err)
|
|
|
|
}
|
|
|
|
}
|