2016-07-08 21:32:31 +08:00
|
|
|
package caddytls
|
|
|
|
|
|
|
|
import (
|
2016-07-20 01:48:39 +08:00
|
|
|
"crypto/tls"
|
2016-07-08 21:32:31 +08:00
|
|
|
"errors"
|
|
|
|
"net/url"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2017-02-22 00:49:22 +08:00
|
|
|
func TestConvertTLSConfigProtocolVersions(t *testing.T) {
|
2016-07-20 01:48:39 +08:00
|
|
|
// same min and max protocol versions
|
2017-02-22 00:49:22 +08:00
|
|
|
config := &Config{
|
2017-02-19 06:26:23 +08:00
|
|
|
Enabled: true,
|
|
|
|
ProtocolMinVersion: tls.VersionTLS12,
|
|
|
|
ProtocolMaxVersion: tls.VersionTLS12,
|
2016-07-20 01:48:39 +08:00
|
|
|
}
|
2017-02-22 00:49:22 +08:00
|
|
|
err := config.buildStandardTLSConfig()
|
2016-07-20 01:48:39 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Did not expect an error, but got %v", err)
|
|
|
|
}
|
2017-02-22 00:49:22 +08:00
|
|
|
if got, want := config.tlsConfig.MinVersion, uint16(tls.VersionTLS12); got != want {
|
2016-07-20 01:48:39 +08:00
|
|
|
t.Errorf("Expected min version to be %x, got %x", want, got)
|
|
|
|
}
|
2017-02-22 00:49:22 +08:00
|
|
|
if got, want := config.tlsConfig.MaxVersion, uint16(tls.VersionTLS12); got != want {
|
2016-07-20 01:48:39 +08:00
|
|
|
t.Errorf("Expected max version to be %x, got %x", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 00:49:22 +08:00
|
|
|
func TestConvertTLSConfigPreferServerCipherSuites(t *testing.T) {
|
2016-08-26 07:13:27 +08:00
|
|
|
// prefer server cipher suites
|
2017-02-19 06:26:23 +08:00
|
|
|
config := Config{Enabled: true, PreferServerCipherSuites: true}
|
2017-02-22 00:49:22 +08:00
|
|
|
err := config.buildStandardTLSConfig()
|
2016-08-26 07:13:27 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Did not expect an error, but got %v", err)
|
|
|
|
}
|
2017-02-22 00:49:22 +08:00
|
|
|
if got, want := config.tlsConfig.PreferServerCipherSuites, true; got != want {
|
2016-08-26 07:13:27 +08:00
|
|
|
t.Errorf("Expected PreferServerCipherSuites==%v but got %v", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 00:49:22 +08:00
|
|
|
func TestMakeTLSConfigTLSEnabledDisabledError(t *testing.T) {
|
2016-08-26 07:13:27 +08:00
|
|
|
// verify handling when Enabled is true and false
|
|
|
|
configs := []*Config{
|
|
|
|
{Enabled: true},
|
|
|
|
{Enabled: false},
|
|
|
|
}
|
2017-02-22 00:49:22 +08:00
|
|
|
_, err := MakeTLSConfig(configs)
|
2016-08-26 07:13:27 +08:00
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("Expected an error, but got %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 00:49:22 +08:00
|
|
|
func TestConvertTLSConfigCipherSuites(t *testing.T) {
|
2016-08-26 07:13:27 +08:00
|
|
|
// ensure cipher suites are unioned and
|
|
|
|
// that TLS_FALLBACK_SCSV is prepended
|
|
|
|
configs := []*Config{
|
|
|
|
{Enabled: true, Ciphers: []uint16{0xc02c, 0xc030}},
|
|
|
|
{Enabled: true, Ciphers: []uint16{0xc012, 0xc030, 0xc00a}},
|
2017-02-19 06:26:23 +08:00
|
|
|
{Enabled: true, Ciphers: nil},
|
2016-08-26 07:13:27 +08:00
|
|
|
}
|
|
|
|
|
2017-02-19 06:26:23 +08:00
|
|
|
expectedCiphers := [][]uint16{
|
|
|
|
{tls.TLS_FALLBACK_SCSV, 0xc02c, 0xc030},
|
|
|
|
{tls.TLS_FALLBACK_SCSV, 0xc012, 0xc030, 0xc00a},
|
|
|
|
append([]uint16{tls.TLS_FALLBACK_SCSV}, defaultCiphers...),
|
2016-08-26 07:13:27 +08:00
|
|
|
}
|
2017-02-19 06:26:23 +08:00
|
|
|
|
|
|
|
for i, config := range configs {
|
2017-02-22 00:49:22 +08:00
|
|
|
err := config.buildStandardTLSConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Test %d: Expected no error, got: %v", i, err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(config.tlsConfig.CipherSuites, expectedCiphers[i]) {
|
|
|
|
t.Errorf("Test %d: Expected ciphers %v but got %v",
|
|
|
|
i, expectedCiphers[i], config.tlsConfig.CipherSuites)
|
2017-02-19 06:26:23 +08:00
|
|
|
}
|
|
|
|
|
2016-08-26 07:13:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 21:32:31 +08:00
|
|
|
func TestStorageForNoURL(t *testing.T) {
|
|
|
|
c := &Config{}
|
|
|
|
if _, err := c.StorageFor(""); err == nil {
|
|
|
|
t.Fatal("Expected error on empty URL")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageForLowercasesAndPrefixesScheme(t *testing.T) {
|
|
|
|
resultStr := ""
|
2016-08-24 04:58:33 +08:00
|
|
|
RegisterStorageProvider("fake-TestStorageForLowercasesAndPrefixesScheme", func(caURL *url.URL) (Storage, error) {
|
|
|
|
resultStr = caURL.String()
|
|
|
|
return nil, nil
|
|
|
|
})
|
2016-07-08 21:32:31 +08:00
|
|
|
c := &Config{
|
2016-08-24 04:58:33 +08:00
|
|
|
StorageProvider: "fake-TestStorageForLowercasesAndPrefixesScheme",
|
2016-07-08 21:32:31 +08:00
|
|
|
}
|
|
|
|
if _, err := c.StorageFor("EXAMPLE.COM/BLAH"); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if resultStr != "https://example.com/blah" {
|
|
|
|
t.Fatalf("Unexpected CA URL string: %v", resultStr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageForBadURL(t *testing.T) {
|
|
|
|
c := &Config{}
|
|
|
|
if _, err := c.StorageFor("http://192.168.0.%31/"); err == nil {
|
|
|
|
t.Fatal("Expected error for bad URL")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageForDefault(t *testing.T) {
|
|
|
|
c := &Config{}
|
|
|
|
s, err := c.StorageFor("example.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-20 07:24:34 +08:00
|
|
|
if _, ok := s.(*FileStorage); !ok {
|
|
|
|
t.Fatalf("Unexpected storage type: %#v", s)
|
2016-07-08 21:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageForCustom(t *testing.T) {
|
2016-08-24 04:58:33 +08:00
|
|
|
storage := fakeStorage("fake-TestStorageForCustom")
|
|
|
|
RegisterStorageProvider("fake-TestStorageForCustom", func(caURL *url.URL) (Storage, error) { return storage, nil })
|
2016-07-08 21:32:31 +08:00
|
|
|
c := &Config{
|
2016-08-24 04:58:33 +08:00
|
|
|
StorageProvider: "fake-TestStorageForCustom",
|
2016-07-08 21:32:31 +08:00
|
|
|
}
|
|
|
|
s, err := c.StorageFor("example.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if s != storage {
|
|
|
|
t.Fatal("Unexpected storage")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageForCustomError(t *testing.T) {
|
2016-08-24 04:58:33 +08:00
|
|
|
RegisterStorageProvider("fake-TestStorageForCustomError", func(caURL *url.URL) (Storage, error) { return nil, errors.New("some error") })
|
2016-07-08 21:32:31 +08:00
|
|
|
c := &Config{
|
2016-08-24 04:58:33 +08:00
|
|
|
StorageProvider: "fake-TestStorageForCustomError",
|
2016-07-08 21:32:31 +08:00
|
|
|
}
|
|
|
|
if _, err := c.StorageFor("example.com"); err == nil {
|
|
|
|
t.Fatal("Expecting error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageForCustomNil(t *testing.T) {
|
|
|
|
// Should fall through to the default
|
2016-08-24 04:58:33 +08:00
|
|
|
c := &Config{StorageProvider: ""}
|
2016-07-08 21:32:31 +08:00
|
|
|
s, err := c.StorageFor("example.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2016-09-20 07:24:34 +08:00
|
|
|
if _, ok := s.(*FileStorage); !ok {
|
|
|
|
t.Fatalf("Unexpected storage type: %#v", s)
|
2016-07-08 21:32:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeStorage string
|
|
|
|
|
2016-08-19 00:28:43 +08:00
|
|
|
func (s fakeStorage) SiteExists(domain string) (bool, error) {
|
2016-07-08 21:32:31 +08:00
|
|
|
panic("no impl")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s fakeStorage) LoadSite(domain string) (*SiteData, error) {
|
|
|
|
panic("no impl")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s fakeStorage) StoreSite(domain string, data *SiteData) error {
|
|
|
|
panic("no impl")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s fakeStorage) DeleteSite(domain string) error {
|
|
|
|
panic("no impl")
|
|
|
|
}
|
|
|
|
|
2016-09-20 07:24:34 +08:00
|
|
|
func (s fakeStorage) TryLock(domain string) (Waiter, error) {
|
2016-07-08 21:32:31 +08:00
|
|
|
panic("no impl")
|
|
|
|
}
|
|
|
|
|
2016-09-20 07:24:34 +08:00
|
|
|
func (s fakeStorage) Unlock(domain string) error {
|
2016-07-08 21:32:31 +08:00
|
|
|
panic("no impl")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s fakeStorage) LoadUser(email string) (*UserData, error) {
|
|
|
|
panic("no impl")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s fakeStorage) StoreUser(email string, data *UserData) error {
|
|
|
|
panic("no impl")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s fakeStorage) MostRecentUserEmail() string {
|
|
|
|
panic("no impl")
|
|
|
|
}
|