plugin/hosts: make unsupported type fallthrough opt-in (#8282)

This commit is contained in:
houyuwushang
2026-07-20 10:59:12 +08:00
committed by GitHub
parent 201d86a098
commit 98bdb44f4d
6 changed files with 154 additions and 22 deletions

View File

@@ -58,31 +58,81 @@ func TestLookupA(t *testing.T) {
}
func TestFallthroughUnsupportedType(t *testing.T) {
h := Hosts{
Next: test.NextHandler(dns.RcodeRefused, nil),
Hostsfile: &Hostsfile{
Origins: []string{"."},
hmap: newMap(),
inline: newMap(),
options: newOptions(),
tests := []struct {
name string
qname string
fall fall.F
unsupported bool
expectFallthrough bool
}{
{
name: "existing name returns nodata by default",
qname: "example.org.",
fall: fall.Root,
},
{
name: "existing name falls through with opt-in",
qname: "example.org.",
fall: fall.Root,
unsupported: true,
expectFallthrough: true,
},
{
name: "opt-in respects fallthrough zones",
qname: "example.org.",
fall: fall.F{Zones: []string{"example.net."}},
unsupported: true,
},
{
name: "missing name still falls through",
qname: "missing.example.org.",
fall: fall.Root,
expectFallthrough: true,
},
Fall: fall.Root,
}
h.hmap = h.parse(strings.NewReader(hostsExample))
m := new(dns.Msg)
m.SetQuestion("example.org.", dns.TypeTXT)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
h := Hosts{
Next: test.NextHandler(dns.RcodeRefused, nil),
Hostsfile: &Hostsfile{
Origins: []string{"."},
hmap: newMap(),
inline: newMap(),
options: newOptions(),
},
Fall: tc.fall,
fallthroughUnsupported: tc.unsupported,
}
h.hmap = h.parse(strings.NewReader(hostsExample))
rcode, err := h.ServeDNS(context.Background(), rec, m)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if rcode != dns.RcodeRefused {
t.Fatalf("Expected fallthrough rcode %d, got %d", dns.RcodeRefused, rcode)
}
if rec.Msg != nil {
t.Fatalf("Expected no response from hosts after fallthrough, got %#v", rec.Msg)
m := new(dns.Msg)
m.SetQuestion(tc.qname, dns.TypeTXT)
rec := dnstest.NewRecorder(&test.ResponseWriter{})
rcode, err := h.ServeDNS(context.Background(), rec, m)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if tc.expectFallthrough {
if rcode != dns.RcodeRefused {
t.Fatalf("Expected fallthrough rcode %d, got %d", dns.RcodeRefused, rcode)
}
if rec.Msg != nil {
t.Fatalf("Expected no response from hosts after fallthrough, got %#v", rec.Msg)
}
return
}
if rcode != dns.RcodeSuccess {
t.Fatalf("Expected authoritative NODATA rcode %d, got %d", dns.RcodeSuccess, rcode)
}
if rec.Msg == nil {
t.Fatal("Expected authoritative NODATA response from hosts, got no response")
}
if !rec.Msg.Authoritative || len(rec.Msg.Answer) != 0 {
t.Fatalf("Expected authoritative NODATA response, got %#v", rec.Msg)
}
})
}
}