Files
core/autocert/storage_test.go
Ingo Oppermann adcbd98467 Add CORE_TLS_SECRET configuration
This secret will be used to encrypt automatically obtained secrets at
rest, i.e. in a storage. They will be decrypted on demand. If the
secret is wrong, stored certificates can't be decrypted. For changing
the secret, the stored certificated must be deleted first in order
to obtain new ones that will be encrypted with the new secret.
2023-07-03 16:02:39 +02:00

99 lines
1.8 KiB
Go

package autocert
import (
"context"
"io/fs"
"os"
"testing"
"github.com/caddyserver/certmagic"
"github.com/stretchr/testify/require"
)
func getCryptoStorage() certmagic.Storage {
s := &certmagic.FileStorage{
Path: "./testing",
}
c := NewCrypto("secret")
sc := NewCryptoStorage(s, c)
return sc
}
func TestFileStorageStoreLoad(t *testing.T) {
s := getCryptoStorage()
defer os.RemoveAll("./testing/")
data := []byte("some data")
ctx := context.Background()
err := s.Store(ctx, "foo", data)
require.NoError(t, err)
loadedData, err := s.Load(ctx, "foo")
require.NoError(t, err)
require.Equal(t, data, loadedData)
}
func TestFileStorageDelete(t *testing.T) {
s := getCryptoStorage()
defer os.RemoveAll("./testing/")
data := []byte("some data")
ctx := context.Background()
err := s.Delete(ctx, "foo")
require.ErrorIs(t, err, fs.ErrNotExist)
err = s.Store(ctx, "foo", data)
require.NoError(t, err)
err = s.Delete(ctx, "foo")
require.NoError(t, err)
_, err = s.Load(ctx, "foo")
require.Error(t, err, fs.ErrNotExist)
}
func TestFileStorageExists(t *testing.T) {
s := getCryptoStorage()
defer os.RemoveAll("./testing/")
data := []byte("some data")
ctx := context.Background()
b := s.Exists(ctx, "foo")
require.False(t, b)
err := s.Store(ctx, "foo", data)
require.NoError(t, err)
b = s.Exists(ctx, "foo")
require.True(t, b)
err = s.Delete(ctx, "foo")
require.NoError(t, err)
b = s.Exists(ctx, "foo")
require.False(t, b)
}
func TestFileStorageStat(t *testing.T) {
s := getCryptoStorage()
defer os.RemoveAll("./testing/")
data := []byte("some data")
ctx := context.Background()
err := s.Store(ctx, "foo", data)
require.NoError(t, err)
info, err := s.Stat(ctx, "foo")
require.NoError(t, err)
require.Equal(t, "foo", info.Key)
require.Equal(t, int64(len(data)), info.Size)
require.Equal(t, true, info.IsTerminal)
}