Commit b813706b authored by Miek Gieben's avatar Miek Gieben Committed by GitHub

request: add match function (#1615)

parent 3e6489ef
...@@ -361,6 +361,24 @@ func (r *Request) Clear() { ...@@ -361,6 +361,24 @@ func (r *Request) Clear() {
r.name = "" r.name = ""
} }
// Match checks if the reply matches the qname and qtype from the request, it returns
// false when they don't match.
func (r *Request) Match(reply *dns.Msg) bool {
if len(reply.Question) != 1 {
return false
}
if strings.ToLower(reply.Question[0].Name) != r.Name() {
return false
}
if reply.Question[0].Qtype != r.QType() {
return false
}
return true
}
const ( const (
// TODO(miek): make this less awkward. // TODO(miek): make this less awkward.
doTrue = 1 doTrue = 1
......
...@@ -109,6 +109,26 @@ func TestRequestScrubExtra(t *testing.T) { ...@@ -109,6 +109,26 @@ func TestRequestScrubExtra(t *testing.T) {
} }
} }
func TestRequestMatch(t *testing.T) {
st := testRequest()
reply := new(dns.Msg)
reply.SetQuestion("example.com.", dns.TypeMX)
if b := st.Match(reply); b {
t.Errorf("failed to match %s %d, got %t, expected %t", "example.com.", dns.TypeMX, b, false)
}
reply.SetQuestion("example.com.", dns.TypeA)
if b := st.Match(reply); !b {
t.Errorf("failed to match %s %d, got %t, expected %t", "example.com.", dns.TypeA, b, true)
}
reply.SetQuestion("example.org.", dns.TypeA)
if b := st.Match(reply); b {
t.Errorf("failed to match %s %d, got %t, expected %t", "example.org.", dns.TypeA, b, false)
}
}
func BenchmarkRequestDo(b *testing.B) { func BenchmarkRequestDo(b *testing.B) {
st := testRequest() st := testRequest()
......
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