2018-05-01 23:45:06 +00:00
|
|
|
package tlsconfig
|
|
|
|
|
|
|
|
import (
|
2018-11-15 15:43:50 +00:00
|
|
|
"crypto/tls"
|
2018-05-01 23:45:06 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
// testcert.pem and testcert2.pem are Generated using `openssl req -newkey rsa:512 -nodes -x509 -days 3650`
|
|
|
|
const (
|
|
|
|
testcertCommonName = "localhost"
|
|
|
|
)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
func TestGetFromEmptyConfig(t *testing.T) {
|
|
|
|
c := &TLSParameters{}
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
tlsConfig, err := GetConfig(c)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Empty(t, tlsConfig.Certificates)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
assert.Empty(t, tlsConfig.NameToCertificate)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
assert.Nil(t, tlsConfig.ClientCAs)
|
|
|
|
assert.Equal(t, tls.NoClientCert, tlsConfig.ClientAuth)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
assert.Nil(t, tlsConfig.RootCAs)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
assert.Len(t, tlsConfig.CurvePreferences, 1)
|
|
|
|
assert.Equal(t, tls.CurveP256, tlsConfig.CurvePreferences[0])
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
func TestGetConfig(t *testing.T) {
|
|
|
|
cert, err := tls.LoadX509KeyPair("testcert.pem", "testkey.pem")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
c := &TLSParameters{
|
|
|
|
Cert: "testcert.pem",
|
|
|
|
Key: "testkey.pem",
|
|
|
|
ClientCAs: []string{"testcert.pem", "testcert2.pem"},
|
|
|
|
RootCAs: []string{"testcert.pem", "testcert2.pem"},
|
|
|
|
ServerName: "test",
|
|
|
|
CurvePreferences: []tls.CurveID{tls.CurveP384},
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2018-11-15 15:43:50 +00:00
|
|
|
tlsConfig, err := GetConfig(c)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, tlsConfig.Certificates, 1)
|
|
|
|
assert.Equal(t, cert, tlsConfig.Certificates[0])
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
assert.Equal(t, cert, *tlsConfig.NameToCertificate[testcertCommonName])
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
assert.NotNil(t, tlsConfig.ClientCAs)
|
|
|
|
assert.Equal(t, tls.RequireAndVerifyClientCert, tlsConfig.ClientAuth)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
assert.NotNil(t, tlsConfig.RootCAs)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
assert.Len(t, tlsConfig.CurvePreferences, 1)
|
|
|
|
assert.Equal(t, tls.CurveP384, tlsConfig.CurvePreferences[0])
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
func TestCertReloader(t *testing.T) {
|
|
|
|
expectedCert, err := tls.LoadX509KeyPair("testcert.pem", "testkey.pem")
|
|
|
|
assert.NoError(t, err)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
certReloader, err := NewCertReloader("testcert.pem", "testkey.pem")
|
|
|
|
assert.NoError(t, err)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
chi := &tls.ClientHelloInfo{ServerName: testcertCommonName}
|
|
|
|
cert, err := certReloader.Cert(chi)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedCert, *cert)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
c := &TLSParameters{
|
|
|
|
GetCertificate: certReloader,
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|
2018-11-15 15:43:50 +00:00
|
|
|
tlsConfig, err := GetConfig(c)
|
|
|
|
assert.NoError(t, err)
|
2018-05-01 23:45:06 +00:00
|
|
|
|
2018-11-15 15:43:50 +00:00
|
|
|
cert, err = tlsConfig.GetCertificate(chi)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedCert, *cert)
|
2018-05-01 23:45:06 +00:00
|
|
|
}
|