From f03ad807012de81adff1d5442df2789bd1045646 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Mon, 10 Dec 2018 20:08:29 -0700 Subject: [PATCH] Update tests after large refactor --- caddy_test.go | 38 ------------------------------ caddyhttp/caddyhttp_test.go | 2 +- caddyhttp/httpserver/https.go | 3 ++- caddyhttp/httpserver/https_test.go | 38 +++++++++++++++++------------- caddyhttp/httpserver/plugin.go | 6 ++++- 5 files changed, 29 insertions(+), 58 deletions(-) diff --git a/caddy_test.go b/caddy_test.go index 0a18100f63e..9a68809d106 100644 --- a/caddy_test.go +++ b/caddy_test.go @@ -16,9 +16,7 @@ package caddy import ( "fmt" - "net" "reflect" - "strconv" "sync" "testing" @@ -204,39 +202,3 @@ func TestIsInternal(t *testing.T) { } } } - -func TestListenerAddrEqual(t *testing.T) { - ln1, err := net.Listen("tcp", "[::]:0") - if err != nil { - t.Fatal(err) - } - defer ln1.Close() - ln1port := strconv.Itoa(ln1.Addr().(*net.TCPAddr).Port) - - ln2, err := net.Listen("tcp", "127.0.0.1:0") - if err != nil { - t.Fatal(err) - } - defer ln2.Close() - ln2port := strconv.Itoa(ln2.Addr().(*net.TCPAddr).Port) - - for i, test := range []struct { - ln net.Listener - addr string - expect bool - }{ - {ln1, ":" + ln2port, false}, - {ln1, "0.0.0.0:" + ln2port, false}, - {ln1, "0.0.0.0", false}, - {ln1, ":" + ln1port, true}, - {ln1, "0.0.0.0:" + ln1port, true}, - {ln2, ":" + ln2port, false}, - {ln2, "127.0.0.1:" + ln1port, false}, - {ln2, "127.0.0.1", false}, - {ln2, "127.0.0.1:" + ln2port, true}, - } { - if got, want := listenerAddrEqual(test.ln, test.addr), test.expect; got != want { - t.Errorf("Test %d (%s == %s): expected %v but was %v", i, test.addr, test.ln.Addr().String(), want, got) - } - } -} diff --git a/caddyhttp/caddyhttp_test.go b/caddyhttp/caddyhttp_test.go index ca3b97e1913..d83e3eb457b 100644 --- a/caddyhttp/caddyhttp_test.go +++ b/caddyhttp/caddyhttp_test.go @@ -25,7 +25,7 @@ import ( // ensure that the standard plugins are in fact plugged in // and registered properly; this is a quick/naive way to do it. func TestStandardPlugins(t *testing.T) { - numStandardPlugins := 31 // importing caddyhttp plugs in this many plugins + numStandardPlugins := 30 // importing caddyhttp plugs in this many plugins s := caddy.DescribePlugins() if got, want := strings.Count(s, "\n"), numStandardPlugins+5; got != want { t.Errorf("Expected all standard plugins to be plugged in, got:\n%s", s) diff --git a/caddyhttp/httpserver/https.go b/caddyhttp/httpserver/https.go index f81fde1da7c..651f946ec44 100644 --- a/caddyhttp/httpserver/https.go +++ b/caddyhttp/httpserver/https.go @@ -101,7 +101,8 @@ func markQualifiedForAutoHTTPS(configs []*SiteConfig) { // value will always be nil. func enableAutoHTTPS(configs []*SiteConfig, loadCertificates bool) error { for _, cfg := range configs { - if cfg == nil || cfg.TLS == nil || !cfg.TLS.Managed || cfg.TLS.Manager.OnDemand != nil { + if cfg == nil || cfg.TLS == nil || !cfg.TLS.Managed || + cfg.TLS.Manager == nil || cfg.TLS.Manager.OnDemand != nil { continue } cfg.TLS.Enabled = true diff --git a/caddyhttp/httpserver/https_test.go b/caddyhttp/httpserver/https_test.go index 7ee44309748..52ce3f9fe95 100644 --- a/caddyhttp/httpserver/https_test.go +++ b/caddyhttp/httpserver/https_test.go @@ -151,18 +151,18 @@ func TestHostHasOtherPort(t *testing.T) { func TestMakePlaintextRedirects(t *testing.T) { configs := []*SiteConfig{ // Happy path = standard redirect from 80 to 443 - {Addr: Address{Host: "example.com"}, TLS: &caddytls.Config{Manager: &certmagic.Config{Managed: true}}}, + {Addr: Address{Host: "example.com"}, TLS: &caddytls.Config{Managed: true}}, // Host on port 80 already defined; don't change it (no redirect) {Addr: Address{Host: "sub1.example.com", Port: "80", Scheme: "http"}, TLS: new(caddytls.Config)}, - {Addr: Address{Host: "sub1.example.com"}, TLS: &caddytls.Config{Manager: &certmagic.Config{Managed: true}}}, + {Addr: Address{Host: "sub1.example.com"}, TLS: &caddytls.Config{Managed: true}}, // Redirect from port 80 to port 5000 in this case - {Addr: Address{Host: "sub2.example.com", Port: "5000"}, TLS: &caddytls.Config{Manager: &certmagic.Config{Managed: true}}}, + {Addr: Address{Host: "sub2.example.com", Port: "5000"}, TLS: &caddytls.Config{Managed: true}}, // Can redirect from 80 to either 443 or 5001, but choose 443 - {Addr: Address{Host: "sub3.example.com", Port: "443"}, TLS: &caddytls.Config{Manager: &certmagic.Config{Managed: true}}}, - {Addr: Address{Host: "sub3.example.com", Port: "5001", Scheme: "https"}, TLS: &caddytls.Config{Manager: &certmagic.Config{Managed: true}}}, + {Addr: Address{Host: "sub3.example.com", Port: "443"}, TLS: &caddytls.Config{Managed: true}}, + {Addr: Address{Host: "sub3.example.com", Port: "5001", Scheme: "https"}, TLS: &caddytls.Config{Managed: true}}, } result := makePlaintextRedirects(configs) @@ -176,7 +176,7 @@ func TestMakePlaintextRedirects(t *testing.T) { func TestEnableAutoHTTPS(t *testing.T) { configs := []*SiteConfig{ - {Addr: Address{Host: "example.com"}, TLS: &caddytls.Config{Manager: &certmagic.Config{Managed: true}}}, + {Addr: Address{Host: "example.com"}, TLS: &caddytls.Config{Managed: true, Manager: &certmagic.Config{}}}, {}, // not managed - no changes! } @@ -197,18 +197,18 @@ func TestEnableAutoHTTPS(t *testing.T) { func TestMarkQualifiedForAutoHTTPS(t *testing.T) { // TODO: caddytls.TestQualifiesForManagedTLS and this test share nearly the same config list... configs := []*SiteConfig{ - {Addr: Address{Host: ""}, TLS: new(caddytls.Config)}, - {Addr: Address{Host: "localhost"}, TLS: new(caddytls.Config)}, - {Addr: Address{Host: "123.44.3.21"}, TLS: new(caddytls.Config)}, - {Addr: Address{Host: "example.com"}, TLS: new(caddytls.Config)}, + {Addr: Address{Host: ""}, TLS: newManagedConfig()}, + {Addr: Address{Host: "localhost"}, TLS: newManagedConfig()}, + {Addr: Address{Host: "123.44.3.21"}, TLS: newManagedConfig()}, + {Addr: Address{Host: "example.com"}, TLS: newManagedConfig()}, {Addr: Address{Host: "example.com"}, TLS: &caddytls.Config{Manual: true}}, {Addr: Address{Host: "example.com"}, TLS: &caddytls.Config{ACMEEmail: "off"}}, - {Addr: Address{Host: "example.com"}, TLS: &caddytls.Config{ACMEEmail: "foo@bar.com"}}, - {Addr: Address{Host: "example.com", Scheme: "http"}, TLS: new(caddytls.Config)}, - {Addr: Address{Host: "example.com", Port: "80"}, TLS: new(caddytls.Config)}, - {Addr: Address{Host: "example.com", Port: "1234"}, TLS: new(caddytls.Config)}, - {Addr: Address{Host: "example.com", Scheme: "https"}, TLS: new(caddytls.Config)}, - {Addr: Address{Host: "example.com", Port: "80", Scheme: "https"}, TLS: new(caddytls.Config)}, + {Addr: Address{Host: "example.com"}, TLS: &caddytls.Config{ACMEEmail: "foo@bar.com", Manager: &certmagic.Config{}}}, + {Addr: Address{Host: "example.com", Scheme: "http"}, TLS: newManagedConfig()}, + {Addr: Address{Host: "example.com", Port: "80"}, TLS: newManagedConfig()}, + {Addr: Address{Host: "example.com", Port: "1234"}, TLS: newManagedConfig()}, + {Addr: Address{Host: "example.com", Scheme: "https"}, TLS: newManagedConfig()}, + {Addr: Address{Host: "example.com", Port: "80", Scheme: "https"}, TLS: newManagedConfig()}, } expectedManagedCount := 4 @@ -216,7 +216,7 @@ func TestMarkQualifiedForAutoHTTPS(t *testing.T) { count := 0 for _, cfg := range configs { - if cfg.TLS.Manager.Managed { + if cfg.TLS.Managed { count++ } } @@ -225,3 +225,7 @@ func TestMarkQualifiedForAutoHTTPS(t *testing.T) { t.Errorf("Expected %d managed configs, but got %d", expectedManagedCount, count) } } + +func newManagedConfig() *caddytls.Config { + return &caddytls.Config{Manager: &certmagic.Config{}} +} diff --git a/caddyhttp/httpserver/plugin.go b/caddyhttp/httpserver/plugin.go index 92dc80593e8..296ea2dad88 100644 --- a/caddyhttp/httpserver/plugin.go +++ b/caddyhttp/httpserver/plugin.go @@ -346,7 +346,11 @@ func GetConfig(c *caddy.Controller) *SiteConfig { // we should only get here during tests because directive // actions typically skip the server blocks where we make // the configs - cfg := &SiteConfig{Root: Root, TLS: new(caddytls.Config), IndexPages: staticfiles.DefaultIndexPages} + cfg := &SiteConfig{ + Root: Root, + TLS: &caddytls.Config{Manager: certmagic.NewDefault()}, + IndexPages: staticfiles.DefaultIndexPages, + } ctx.saveConfig(key, cfg) return cfg }