From cff0c9fb4c43eb084da2c9c318af8f83ecba3de7 Mon Sep 17 00:00:00 2001 From: Miek Gieben Date: Fri, 13 Apr 2018 17:02:54 +0100 Subject: [PATCH] plugin/forward: test TLS setup (#1677) --- plugin/forward/setup.go | 2 +- plugin/forward/setup_test.go | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/plugin/forward/setup.go b/plugin/forward/setup.go index 137137d45..3582093ce 100644 --- a/plugin/forward/setup.go +++ b/plugin/forward/setup.go @@ -114,7 +114,7 @@ func parseForward(c *caddy.Controller) (*Forward, error) { break } - // This is more of a bug in // dnsutil.ParseHostPortOrFile that defaults to + // This is more of a bug in dnsutil.ParseHostPortOrFile that defaults to // 53 because it doesn't know about the tls:// // and friends (that should be fixed). Hence // Fix the port number here, back to what the user intended. if p == "53" { diff --git a/plugin/forward/setup_test.go b/plugin/forward/setup_test.go index bf77bb932..d787a59d0 100644 --- a/plugin/forward/setup_test.go +++ b/plugin/forward/setup_test.go @@ -68,3 +68,40 @@ func TestSetup(t *testing.T) { } } } + +func TestSetupTLS(t *testing.T) { + tests := []struct { + input string + shouldErr bool + expectedServerName string + expectedErr string + }{ + // positive + {`forward . 127.0.0.1 { + tls_servername dns + }`, false, "dns", ""}, + } + + for i, test := range tests { + c := caddy.NewTestController("dns", test.input) + f, err := parseForward(c) + + if test.shouldErr && err == nil { + t.Errorf("Test %d: expected error but found %s for input %s", i, err, test.input) + } + + if err != nil { + if !test.shouldErr { + t.Errorf("Test %d: expected no error but found one for input %s, got: %v", i, test.input, err) + } + + if !strings.Contains(err.Error(), test.expectedErr) { + t.Errorf("Test %d: expected error to contain: %v, found error: %v, input: %s", i, test.expectedErr, err, test.input) + } + } + + if !test.shouldErr && test.expectedServerName != f.tlsConfig.ServerName { + t.Errorf("Test %d: expected: %q, actual: %q", i, test.expectedServerName, f.tlsConfig.ServerName) + } + } +}