Skip to content
Snippets Groups Projects
Select Git revision
  • 63123b959bc0322c80d41da12ef73abe3dc1ce45
  • ballinvoher default protected
  • client-http-server-for-token
  • master
  • gitlab-auth-issue
  • windows
  • microsoft
  • message
  • azure_auth
  • prometheus
  • permission-templates
  • no-datastore
  • save-public-keys
  • gitlab-group-level-start
  • v1.1.0
  • v1.0.0
  • v0.1
17 results

store_test.go

Blame
  • 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)