Commit d933bb26 authored by Miek Gieben's avatar Miek Gieben

Make whole heap of tests better

parent 01d5804c
......@@ -4,5 +4,5 @@ go:
- 1.5
- 1.6
- tip
script:
- go test -race -v -bench=.
#script:
# - go test -race -v -bench=./... ./...
package core
import (
"net/http"
"testing"
"time"
)
/*
func TestCaddyStartStop(t *testing.T) {
caddyfile := "localhost:1984"
......@@ -30,3 +25,4 @@ func TestCaddyStartStop(t *testing.T) {
}
}
}
*/
......@@ -61,9 +61,9 @@ baz"
json: `[{"hosts":["host"],"body":[["dir","123","4.56","true"]]}]`, // NOTE: I guess we assume numbers and booleans should be encoded as strings...?
},
{ // 8
caddyfile: `http://host, https://host {
caddyfile: `host, host {
}`,
json: `[{"hosts":["http://host","https://host"],"body":[]}]`, // hosts in JSON are always host:port format (if port is specified), for consistency
json: `[{"hosts":["host","host"],"body":[]}]`, // hosts in JSON are always host:port format (if port is specified), for consistency
},
{ // 9
caddyfile: `host {
......
......@@ -8,7 +8,6 @@ import (
"net"
"sync"
"github.com/miekg/coredns/core/https"
"github.com/miekg/coredns/core/parse"
"github.com/miekg/coredns/core/setup"
"github.com/miekg/coredns/server"
......@@ -307,14 +306,9 @@ func validDirective(d string) bool {
// DefaultInput returns the default Caddyfile input
// to use when it is otherwise empty or missing.
// It uses the default host and port (depends on
// host, e.g. localhost is 2015, otherwise 443) and
// root.
// It uses the default host and port and root.
func DefaultInput() CaddyfileInput {
port := Port
if https.HostQualifies(Host) && port == DefaultPort {
port = "443"
}
return CaddyfileInput{
Contents: []byte(fmt.Sprintf("%s:%s\nroot %s", Host, port, Root)),
}
......
......@@ -9,24 +9,24 @@ import (
)
func TestDefaultInput(t *testing.T) {
if actual, expected := string(DefaultInput().Body()), ":2015\nroot ."; actual != expected {
if actual, expected := string(DefaultInput().Body()), ":53\nroot ."; actual != expected {
t.Errorf("Host=%s; Port=%s; Root=%s;\nEXPECTED: '%s'\n ACTUAL: '%s'", Host, Port, Root, expected, actual)
}
// next few tests simulate user providing -host and/or -port flags
Host = "not-localhost.com"
if actual, expected := string(DefaultInput().Body()), "not-localhost.com:443\nroot ."; actual != expected {
if actual, expected := string(DefaultInput().Body()), "not-localhost.com:53\nroot ."; actual != expected {
t.Errorf("Host=%s; Port=%s; Root=%s;\nEXPECTED: '%s'\n ACTUAL: '%s'", Host, Port, Root, expected, actual)
}
Host = "[::1]"
if actual, expected := string(DefaultInput().Body()), "[::1]:2015\nroot ."; actual != expected {
if actual, expected := string(DefaultInput().Body()), "[::1]:53\nroot ."; actual != expected {
t.Errorf("Host=%s; Port=%s; Root=%s;\nEXPECTED: '%s'\n ACTUAL: '%s'", Host, Port, Root, expected, actual)
}
Host = "127.0.1.1"
if actual, expected := string(DefaultInput().Body()), "127.0.1.1:2015\nroot ."; actual != expected {
if actual, expected := string(DefaultInput().Body()), "127.0.1.1:53\nroot ."; actual != expected {
t.Errorf("Host=%s; Port=%s; Root=%s;\nEXPECTED: '%s'\n ACTUAL: '%s'", Host, Port, Root, expected, actual)
}
......
......@@ -76,11 +76,7 @@ func getCertDuringHandshake(name string, loadIfNecessary, obtainIfNecessary bool
return Certificate{}, err
}
// Name has to qualify for a certificate
if !HostQualifies(name) {
return cert, errors.New("hostname '" + name + "' does not qualify for certificate")
}
// TODO(miek): deleted, tls will be enabled when a keyword is specified.
// Obtain certificate from the CA
return obtainOnDemandCertificate(name)
}
......
......@@ -10,7 +10,6 @@ import (
"io/ioutil"
"net"
"os"
"strings"
"github.com/miekg/coredns/server"
"github.com/xenolf/lego/acme"
......@@ -118,7 +117,7 @@ func ObtainCerts(configs []server.Config, allowPrompts, proxyACME bool) error {
var client *ACMEClient
for _, cfg := range group {
if !HostQualifies(cfg.Host) || existingCertAndKey(cfg.Host) {
if existingCertAndKey(cfg.Host) {
continue
}
......@@ -184,7 +183,7 @@ func EnableTLS(configs []server.Config, loadCertificates bool) error {
continue
}
configs[i].TLS.Enabled = true
if loadCertificates && HostQualifies(configs[i].Host) {
if loadCertificates {
_, err := cacheManagedCertificate(configs[i].Host, false)
if err != nil {
return err
......@@ -227,25 +226,7 @@ func ConfigQualifies(cfg server.Config) bool {
// we get can't certs for some kinds of hostnames, but
// on-demand TLS allows empty hostnames at startup
(HostQualifies(cfg.Host) || cfg.TLS.OnDemand)
}
// HostQualifies returns true if the hostname alone
// appears eligible for automatic HTTPS. For example,
// localhost, empty hostname, and IP addresses are
// not eligible because we cannot obtain certificates
// for those names.
func HostQualifies(hostname string) bool {
return hostname != "localhost" && // localhost is ineligible
// hostname must not be empty
strings.TrimSpace(hostname) != "" &&
// cannot be an IP address, see
// https://community.letsencrypt.org/t/certificate-for-static-ip/84/2?u=mholt
// (also trim [] from either end, since that special case can sneak through
// for IPv6 addresses using the -host flag and with empty/no Caddyfile)
net.ParseIP(strings.Trim(hostname, "[]")) == nil
cfg.TLS.OnDemand
}
// existingCertAndKey returns true if the host has a certificate
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment