diff --git a/plugin/loop/loop_test.go b/plugin/loop/loop_test.go index e7a4b06bb..d41dd73b9 100644 --- a/plugin/loop/loop_test.go +++ b/plugin/loop/loop_test.go @@ -1,6 +1,13 @@ package loop -import "testing" +import ( + "context" + "testing" + + "github.com/coredns/coredns/plugin/test" + + "github.com/miekg/dns" +) func TestLoop(t *testing.T) { l := New(".") @@ -9,3 +16,67 @@ func TestLoop(t *testing.T) { t.Errorf("Failed to inc loop, expected %d, got %d", 1, l.seen()) } } + +func TestLoop_NonHINFO(t *testing.T) { + l := New(".") + l.Next = test.NextHandler(dns.RcodeSuccess, nil) + w := &test.ResponseWriter{} + m := new(dns.Msg) + m.SetQuestion("example.org.", dns.TypeA) + if rc, _ := l.ServeDNS(context.Background(), w, m); rc != dns.RcodeSuccess { + t.Fatalf("expected %d, got %d", dns.RcodeSuccess, rc) + } +} + +func TestLoop_Disabled(t *testing.T) { + l := New(".") + l.setDisabled() + l.Next = test.NextHandler(dns.RcodeSuccess, nil) + w := &test.ResponseWriter{} + m := new(dns.Msg) + m.SetQuestion("example.org.", dns.TypeHINFO) + if rc, _ := l.ServeDNS(context.Background(), w, m); rc != dns.RcodeSuccess { + t.Fatalf("expected %d, got %d", dns.RcodeSuccess, rc) + } +} + +func TestLoop_ZoneMismatch(t *testing.T) { + l := New("example.org.") + l.Next = test.NextHandler(dns.RcodeSuccess, nil) + w := &test.ResponseWriter{} + m := new(dns.Msg) + m.SetQuestion("a.example.com.", dns.TypeHINFO) + if rc, _ := l.ServeDNS(context.Background(), w, m); rc != dns.RcodeSuccess { + t.Fatalf("expected %d, got %d", dns.RcodeSuccess, rc) + } +} + +func TestLoop_MatchAndInc(t *testing.T) { + l := New(".") + l.Next = test.NextHandler(dns.RcodeSuccess, nil) + l.qname = "1.2.example.org." + w := &test.ResponseWriter{} + m := new(dns.Msg) + m.SetQuestion(l.qname, dns.TypeHINFO) + + if l.seen() != 0 { + t.Fatalf("expected initial seen 0, got %d", l.seen()) + } + if _, err := l.ServeDNS(context.Background(), w, m); err != nil { + t.Fatalf("ServeDNS returned error: %v", err) + } + if l.seen() != 1 { + t.Fatalf("expected seen to be 1 after matching query, got %d", l.seen()) + } +} + +func TestLoop_SetAddressAndName(t *testing.T) { + l := New(".") + l.setAddress("127.0.0.1:1053") + if l.address() != "127.0.0.1:1053" { + t.Fatalf("expected address to be set") + } + if l.Name() != "loop" { + t.Fatalf("expected Name() to be 'loop', got %q", l.Name()) + } +} diff --git a/plugin/loop/setup.go b/plugin/loop/setup.go index 0a2c94aef..edef1b9de 100644 --- a/plugin/loop/setup.go +++ b/plugin/loop/setup.go @@ -73,7 +73,10 @@ func parse(c *caddy.Controller) (*Loop, error) { } if len(c.ServerBlockKeys) > 0 { - zones = plugin.Host(c.ServerBlockKeys[0]).NormalizeExact() + z := plugin.Host(c.ServerBlockKeys[0]).NormalizeExact() + if len(z) > 0 { + zones = z + } } } return New(zones[0]), nil diff --git a/plugin/loop/setup_test.go b/plugin/loop/setup_test.go index 6b80b9b5b..d3853beb1 100644 --- a/plugin/loop/setup_test.go +++ b/plugin/loop/setup_test.go @@ -17,3 +17,32 @@ func TestSetup(t *testing.T) { t.Fatal("Expected errors, but got none") } } + +func TestParseServerBlockKeys(t *testing.T) { + tests := []struct { + name string + key string + want string + wantOk bool + }{ + {name: "valid domain", key: "example.org", want: "example.org.", wantOk: true}, + {name: "invalid scheme", key: "unix://", want: ".", wantOk: true}, + {name: "empty", key: "", want: ".", wantOk: true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := caddy.NewTestController("dns", `loop`) + if tt.key != "" { + c.ServerBlockKeys = []string{tt.key} + } + l, err := parse(c) + if (err == nil) != tt.wantOk { + t.Fatalf("parse err=%v, wantOk=%v", err, tt.wantOk) + } + if l.zone != tt.want { + t.Fatalf("zone=%q, want %q", l.zone, tt.want) + } + }) + } +}