restore 1.8.3 question revert logic; add tests (#4840)

Signed-off-by: Chris O'Haver <cohaver@infoblox.com>
This commit is contained in:
Chris O'Haver
2021-08-31 04:34:26 -04:00
committed by GitHub
parent b143cd49b8
commit 9b3b2679f5
2 changed files with 57 additions and 1 deletions

View File

@@ -54,7 +54,10 @@ func (rw Rewrite) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg
}
wr.ResponseRules = append(wr.ResponseRules, respRules...)
if rule.Mode() == Stop {
break
if !rw.RevertPolicy.DoRevert() {
return plugin.NextOrFailure(rw.Name(), rw.Next, ctx, w, r)
}
return plugin.NextOrFailure(rw.Name(), rw.Next, ctx, wr, r)
}
}
}

View File

@@ -163,6 +163,59 @@ func TestNewRule(t *testing.T) {
}
}
func TestRewriteDefaultRevertPolicy(t *testing.T) {
rules := []Rule{}
r, _ := newNameRule("stop", "prefix", "prefix", "to")
rules = append(rules, r)
r, _ = newNameRule("stop", "suffix", ".suffix.", ".nl.")
rules = append(rules, r)
r, _ = newNameRule("stop", "substring", "from.substring", "to")
rules = append(rules, r)
r, _ = newNameRule("stop", "regex", "(f.*m)\\.regex\\.(nl)", "to.{2}")
rules = append(rules, r)
rw := Rewrite{
Next: plugin.HandlerFunc(msgPrinter),
Rules: rules,
// use production (default) RevertPolicy
}
tests := []struct {
from string
fromT uint16
fromC uint16
to string
toT uint16
toC uint16
}{
{"prefix.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET},
{"to.suffix.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET},
{"from.substring.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET},
{"from.regex.nl.", dns.TypeA, dns.ClassINET, "to.nl.", dns.TypeA, dns.ClassINET},
}
ctx := context.TODO()
for i, tc := range tests {
m := new(dns.Msg)
m.SetQuestion(tc.from, tc.fromT)
m.Question[0].Qclass = tc.fromC
rec := dnstest.NewRecorder(&test.ResponseWriter{})
rw.ServeDNS(ctx, rec, m)
resp := rec.Msg
if resp.Question[0].Name != tc.from {
t.Errorf("Test %d: Expected Name in Question to be %q but was %q", i, tc.from, resp.Question[0].Name)
}
if resp.Answer[0].Header().Name != tc.to {
t.Errorf("Test %d: Expected Name in Answer to be %q but was %q", i, tc.to, resp.Answer[0].Header().Name)
}
}
}
func TestRewrite(t *testing.T) {
rules := []Rule{}
r, _ := newNameRule("stop", "from.nl.", "to.nl.")