Select Git revision
store_test.go
-
Niall Sheridan authoredNiall Sheridan authored
store_test.go 3.96 KiB
package store
import (
"crypto/rand"
"crypto/rsa"
"database/sql"
"encoding/json"
"io/ioutil"
"os"
"os/user"
"strings"
"testing"
"time"
"github.com/nsheridan/cashier/server/store/types"
"github.com/nsheridan/cashier/testdata"
"github.com/stretchr/testify/assert"
"golang.org/x/crypto/ssh"
)
func TestParseCertificate(t *testing.T) {
t.Parallel()
a := assert.New(t)
now := uint64(time.Now().Unix())
r, _ := rsa.GenerateKey(rand.Reader, 1024)
pub, _ := ssh.NewPublicKey(r.Public())
c := &ssh.Certificate{
KeyId: "id",
ValidPrincipals: types.StringSlice{"principal"},
ValidBefore: now,
CertType: ssh.UserCert,
Key: pub,
}
s, _ := ssh.NewSignerFromKey(r)
c.SignCert(rand.Reader, s)
rec := parseCertificate(c)
a.Equal(c.KeyId, rec.KeyID)
a.Equal(c.ValidPrincipals, []string(rec.Principals))
a.Equal(c.ValidBefore, uint64(rec.Expires.Unix()))
a.Equal(c.ValidAfter, uint64(rec.CreatedAt.Unix()))
}
func testStore(t *testing.T, db CertStorer) {
defer db.Close()
r := &CertRecord{
KeyID: "a",
Principals: []string{"b"},
CreatedAt: time.Now().UTC(),
Expires: time.Now().UTC().Add(-1 * time.Second),
Raw: "AAAAAA",
}
if err := db.SetRecord(r); err != nil {
t.Error(err)
}
// includeExpired = false should return 0 results
recs, err := db.List(false)
if err != nil {
t.Error(err)
}
if len(recs) > 0 {
t.Errorf("Expected 0 results, got %d", len(recs))
}
// includeExpired = false should return 1 result
recs, err = db.List(true)
if err != nil {
t.Error(err)