Skip to content

Commit

Permalink
fix store/load certificate test
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Sep 4, 2021
1 parent 69c7ca8 commit c51be9e
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions internal/cert_manager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/http"
"testing"
Expand Down Expand Up @@ -105,28 +104,39 @@ func TestGetKeyType(t *testing.T) {
}

func TestStoreCertificate(t *testing.T) {
ctx, flush := th.TestContext(t)
e, ctx, flush := th.NewEnv(t)
defer flush()

//nolint:gosec
key, _ := rsa.GenerateKey(rand.Reader, 512)

cert := &tls.Certificate{Certificate: [][]byte{
{1, 2, 3},
{4, 5, 6},
},
PrivateKey: key,
}
certBytes, keyBytes := fastCreateTestCert([]string{"domain.com"}, time.Now())
cert, err := tls.X509KeyPair(certBytes, keyBytes)
e.CmpNoError(err)
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
e.CmpNoError(err)

mc := minimock.NewController(t)
cacheMock := NewBytesMock(mc).PutMock.Set(func(ctx context.Context, key string, data []byte) (err error) {
fmt.Println(key)
fmt.Println(string(data))
c := make(map[string][]byte)
cacheMock := NewBytesMock(mc)
cacheMock.PutMock.Set(func(ctx context.Context, key string, data []byte) (err error) {
c[key] = data
return nil
})
cacheMock.GetMock.Return(nil, cache.ErrCacheMiss)
cacheMock.GetMock.Set(func(ctx context.Context, key string) (ba1 []byte, err error) {
data, ok := c[key]
if ok {
return data, nil
}
return nil, cache.ErrCacheMiss
})

cd := CertDescription{MainDomain: "asd", KeyType: KeyRSA}
err = storeCertificate(ctx, cacheMock, cd, &cert)
e.CmpNoError(err)

resCert, err := loadCertificateFromCache(ctx, cacheMock, cd)
e.CmpNoError(err)

storeCertificate(ctx, cacheMock, CertDescription{MainDomain: "asd", KeyType: KeyRSA}, cert)
e.CmpNoError(err)
e.Cmp(resCert, &cert)
}

func TestIsNeedRenew(t *testing.T) {
Expand Down

0 comments on commit c51be9e

Please sign in to comment.