internal/database: add cert tests

This commit is contained in:
Cadey Ratio 2018-01-21 11:37:28 -08:00
parent 34eab0cf25
commit 0088a7dc09
2 changed files with 79 additions and 5 deletions

View File

@ -1,8 +1,6 @@
package database package database
import ( import (
"io"
"github.com/asdine/storm" "github.com/asdine/storm"
"golang.org/x/crypto/acme/autocert" "golang.org/x/crypto/acme/autocert"
"golang.org/x/net/context" "golang.org/x/net/context"
@ -14,7 +12,6 @@ import (
// to implement certificate decryption, as that will be handled in the layer // to implement certificate decryption, as that will be handled in the layer
// above this DAO. // above this DAO.
type Certs interface { type Certs interface {
io.Closer
autocert.Cache autocert.Cache
GetAll(ctx context.Context) ([]CachedCert, error) GetAll(ctx context.Context) ([]CachedCert, error)
@ -22,10 +19,9 @@ type Certs interface {
// CachedCert is an individual cached certificate in the database. // CachedCert is an individual cached certificate in the database.
type CachedCert struct { type CachedCert struct {
Key string `storm:"id" db:"domain"` Key string `storm:"id"`
// Encrypted data // Encrypted data
Data string `storm:"-" db:"data"`
Body []byte // above as a byte slice Body []byte // above as a byte slice
} }

View File

@ -0,0 +1,78 @@
package database
import (
"context"
"os"
"testing"
"git.xeserv.us/xena/route/internal/routecrypto"
"github.com/Xe/uuid"
)
const (
cryptoKey = `I7EzBRcoFtbW6RuqKdFjxYTHC_bpQLPJO3zyS2R-sf8=`
certNameBase = "cert."
certValue = "hunter2"
)
func testCerts(ctx context.Context, t *testing.T, c Certs) {
cn := certNameBase + uuid.New()
t.Run("put", func(t *testing.T) {
data := certValue + uuid.New()
err := c.Put(ctx, cn, []byte(data))
if err != nil {
t.Fatal(err)
}
data2, err := c.Get(ctx, cn)
if err != nil {
t.Fatal(err)
}
if data != string(data2) {
t.Fatalf("expected data to be %q, got %q", data, string(data2))
}
})
t.Run("get", func(t *testing.T) {
_, err := c.Get(ctx, cn)
if err != nil {
t.Fatal(err)
}
})
t.Run("delete", func(t *testing.T) {
err := c.Delete(ctx, cn)
if err != nil {
t.Fatal(err)
}
_, err = c.Get(ctx, cn)
if err == nil {
t.Fatal("able to fetch a deleted item")
}
})
}
func TestBoltDBCertStorage(t *testing.T) {
k, err := routecrypto.ParseKey(cryptoKey)
if err != nil {
t.Fatal(err)
}
p := uuid.New()
defer os.RemoveAll(p)
st, err := NewBoltStorage(p, k)
if err != nil {
t.Fatal(err)
}
defer st.Close()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
testCerts(ctx, t, st.Certs())
}