forked from Silverfish/proton-bridge
test: generate tls cert/key in test
This commit is contained in:
@ -64,7 +64,7 @@ func GetTLSConfig(cfg tlsConfiger) (tlsConfig *tls.Config, err error) {
|
||||
tlsConfig, err = loadTLSConfig(certPath, keyPath)
|
||||
if err != nil {
|
||||
log.WithError(err).Warn("Cannot load cert, generating a new one")
|
||||
tlsConfig, err = generateTLSConfig(certPath, keyPath)
|
||||
tlsConfig, err = GenerateTLSConfig(certPath, keyPath)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -126,8 +126,9 @@ func loadTLSConfig(certPath, keyPath string) (tlsConfig *tls.Config, err error)
|
||||
return
|
||||
}
|
||||
|
||||
// GenerateTLSConfig generates certs and keys at the given filepaths and returns a TLS Config which holds them.
|
||||
// See https://golang.org/src/crypto/tls/generate_cert.go
|
||||
func generateTLSConfig(certPath, keyPath string) (tlsConfig *tls.Config, err error) {
|
||||
func GenerateTLSConfig(certPath, keyPath string) (tlsConfig *tls.Config, err error) {
|
||||
priv, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("failed to generate private key: %s", err)
|
||||
|
||||
@ -43,7 +43,7 @@ func TestTLSKeyRenewal(t *testing.T) {
|
||||
// Put old key there.
|
||||
tlsTemplate.NotBefore = time.Now().Add(-365 * 24 * time.Hour)
|
||||
tlsTemplate.NotAfter = time.Now()
|
||||
cert, err := generateTLSConfig(certPath, keyPath)
|
||||
cert, err := GenerateTLSConfig(certPath, keyPath)
|
||||
require.Equal(t, err, ErrTLSCertExpireSoon)
|
||||
require.Equal(t, len(cert.Certificates), 1)
|
||||
time.Sleep(time.Second)
|
||||
|
||||
@ -23,8 +23,10 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/ProtonMail/proton-bridge/pkg/config"
|
||||
"github.com/ProtonMail/proton-bridge/pkg/constants"
|
||||
"github.com/ProtonMail/proton-bridge/pkg/pmapi"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type fakeConfig struct {
|
||||
@ -39,9 +41,16 @@ func newFakeConfig() *fakeConfig {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &fakeConfig{
|
||||
cfg := &fakeConfig{
|
||||
dir: dir,
|
||||
}
|
||||
|
||||
// We must generate cert.pem and key.pem to prevent errors when attempting to open them.
|
||||
if _, err = config.GenerateTLSConfig(cfg.GetTLSCertPath(), cfg.GetTLSKeyPath()); err != nil {
|
||||
logrus.WithError(err).Fatal()
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
func (c *fakeConfig) ClearData() error {
|
||||
|
||||
Reference in New Issue
Block a user