test / test (pull_request) Successful in 10s
Malformed COOKIE options (RFC 7873 section 5.2.2): option lengths other than 8 and 16-40 are now answered with FORMERR and never get a COOKIE in the response; getQueryCookie classifies absent/valid/malformed. Server-cookie learning probes (RFC 7873 section 5.4): question-less queries carrying a COOKIE option are answered NOERROR with a freshly minted server cookie, or BADCOOKIE when re-establishing an invalid server cookie. miekg's ServeMux refuses question-less queries, so the servers now use handleQuery directly as their handler, and msgAcceptFunc lets QDCOUNT=0 QUERY requests through. EDNS/OPT handling (RFC 6891): the upstream OPT record is no longer cached or forwarded (sections 6.1.1 and 6.2.1) — it is removed before caching and rebuilt per query. Responses to EDNS queries always carry an OPT record advertising the responder's payload size and echoing the query's DO bit (sections 6.1.1 and 6.2.4); responses to non-EDNS queries never carry one (section 7). applyCookie is now applyEDNS. Tests: malformed-COOKIE FORMERR for lengths 4/12/41 and normal answers for 8/16/40, cookie probe (NOERROR/BADCOOKIE) at the handler level and through a real miekg server over UDP (independent raw-socket verification included), msgAcceptFunc unit test, OPT-never-cached and OPT-rebuild assertions, non-EDNS responses carry no OPT. README documents the new EDNS and COOKIE behaviors.
480 lines
16 KiB
Go
480 lines
16 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"net"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
// startMockUpstream runs a mock nameserver on 127.0.0.1 with a shared
|
|
// UDP/TCP port, returning its address.
|
|
func startMockUpstream(t *testing.T, udpHandler, tcpHandler dns.HandlerFunc) string {
|
|
t.Helper()
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
port := ln.Addr().(*net.TCPAddr).Port
|
|
pc, err := net.ListenPacket("udp", net.JoinHostPort("127.0.0.1", strconv.Itoa(port)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
udpSrv := &dns.Server{PacketConn: pc, Handler: udpHandler}
|
|
tcpSrv := &dns.Server{Listener: ln, Handler: tcpHandler}
|
|
go func() { _ = udpSrv.ActivateAndServe() }()
|
|
go func() { _ = tcpSrv.ActivateAndServe() }()
|
|
t.Cleanup(func() {
|
|
_ = udpSrv.Shutdown()
|
|
_ = tcpSrv.Shutdown()
|
|
})
|
|
return net.JoinHostPort("127.0.0.1", strconv.Itoa(port))
|
|
}
|
|
|
|
// useUpstream points the resolver at a mock nameserver address and
|
|
// restores the configuration afterwards.
|
|
func useUpstream(t *testing.T, addr string) {
|
|
t.Helper()
|
|
oldNS, oldPort := *ns, upstreamDNSPort
|
|
host, port, err := net.SplitHostPort(addr)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
*ns = host
|
|
upstreamDNSPort = port
|
|
t.Cleanup(func() { *ns, upstreamDNSPort = oldNS, oldPort })
|
|
}
|
|
|
|
func mockUDPWriter(t *testing.T) *mockWriter {
|
|
return &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.10"), Port: 5300}}
|
|
}
|
|
|
|
// TestHandleQueryUpstreamCookieAndAD exercises the full forwarding path:
|
|
// the upstream response's COOKIE is replaced with a freshly minted one,
|
|
// the upstream AD bit is cleared, the cache entry is stored without the
|
|
// upstream OPT record (RFC 6891 section 6.2.1), and cache hits rebuild
|
|
// both the OPT record and the cookie for the new client.
|
|
func TestHandleQueryUpstreamCookieAndAD(t *testing.T) {
|
|
resetMaps()
|
|
qcache.Flush()
|
|
|
|
upstreamServerCookie := []byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}
|
|
handler := func(w dns.ResponseWriter, q *dns.Msg) {
|
|
r := new(dns.Msg)
|
|
r.SetReply(q)
|
|
r.MsgHdr.AuthenticatedData = true // upstream claims validation
|
|
r.Answer = append(r.Answer, &dns.A{
|
|
Hdr: dns.RR_Header{Name: q.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 300},
|
|
A: net.ParseIP("192.0.2.1"),
|
|
})
|
|
r.SetEdns0(512, false)
|
|
// An upstream EDE option (info code 0, "Other Error"), which must
|
|
// not survive into the cache or the client response (OPT records
|
|
// are never cached or forwarded, RFC 6891 section 6.1.1).
|
|
r.IsEdns0().Option = append(r.IsEdns0().Option, &dns.EDNS0_EDE{
|
|
InfoCode: 0, // RFC 8914: Other Error
|
|
ExtraText: "upstream ede",
|
|
})
|
|
if clientCookie, _, state := getQueryCookie(q); state == cookieValid {
|
|
cookie := append(append([]byte{}, clientCookie...), upstreamServerCookie...)
|
|
r.IsEdns0().Option = append(r.IsEdns0().Option, &dns.EDNS0_COOKIE{
|
|
Code: dns.EDNS0COOKIE,
|
|
Cookie: hex.EncodeToString(cookie),
|
|
})
|
|
}
|
|
if err := w.WriteMsg(r); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
useUpstream(t, startMockUpstream(t, handler, handler))
|
|
|
|
clientCookie1 := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
|
|
clientCookie2 := []byte{0xde, 0xad, 0xbe, 0xef, 0x00, 0x11, 0x22, 0x33}
|
|
|
|
// First query: forwarded upstream.
|
|
w1 := mockUDPWriter(t)
|
|
q1 := newCookieQuery("upstream.test", dns.TypeA, clientCookie1)
|
|
handleQuery(w1, q1)
|
|
if w1.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if len(w1.msg.Answer) != 1 {
|
|
t.Fatalf("answers = %d, want 1", len(w1.msg.Answer))
|
|
}
|
|
if !w1.msg.RecursionAvailable {
|
|
t.Error("RecursionAvailable not set")
|
|
}
|
|
if w1.msg.AuthenticatedData {
|
|
t.Error("AD bit must be cleared: rdns does not validate DNSSEC (RFC 6840 section 5.8)")
|
|
}
|
|
if w1.msg.Answer[0].Header().Ttl != 300 {
|
|
t.Errorf("fresh response TTL = %d, want 300", w1.msg.Answer[0].Header().Ttl)
|
|
}
|
|
c1 := responseCookie(w1.msg)
|
|
if c1 == nil {
|
|
t.Fatal("response has no COOKIE option")
|
|
}
|
|
gotClient, gotServer := decodeCookie(t, c1)
|
|
mustEqual(t, "upstream response client cookie", gotClient, clientCookie1)
|
|
if bytes.Equal(gotServer, upstreamServerCookie) {
|
|
t.Error("upstream server cookie must not be relayed to the client")
|
|
}
|
|
if !validateServerCookie("192.0.2.10", clientCookie1, gotServer, time.Now()) {
|
|
t.Error("response server cookie does not validate for the querying client")
|
|
}
|
|
|
|
// The cache entry must be stored without the upstream OPT record:
|
|
// OPT records MUST NOT be cached (RFC 6891 section 6.2.1).
|
|
key := cacheKey("upstream.test", "IN", "A")
|
|
cached, found := qcache.Get(key)
|
|
if !found {
|
|
t.Fatal("response was not cached")
|
|
}
|
|
if cached.(*dns.Msg).IsEdns0() != nil {
|
|
t.Error("cached entry carries an OPT record, want none (RFC 6891 section 6.2.1)")
|
|
}
|
|
|
|
// The client response carries a freshly built OPT record: the
|
|
// responder's payload size, the query's DO bit, and our COOKIE —
|
|
// but not the upstream's EDE option.
|
|
if w1.msg.IsEdns0() == nil {
|
|
t.Fatal("response has no OPT record")
|
|
}
|
|
if size := w1.msg.IsEdns0().UDPSize(); size != serverUDPSize {
|
|
t.Errorf("response OPT UDP size = %d, want %d (responder's payload size)", size, serverUDPSize)
|
|
}
|
|
if len(w1.msg.IsEdns0().Option) != 1 {
|
|
t.Errorf("response OPT options = %d, want 1 (COOKIE only, upstream options not forwarded)", len(w1.msg.IsEdns0().Option))
|
|
}
|
|
|
|
// Second query from a different client (different cookie, different
|
|
// question case): cache hit with a rebuilt cookie.
|
|
w2 := &mockWriter{addr: &net.UDPAddr{IP: net.ParseIP("192.0.2.99"), Port: 5300}}
|
|
q2 := newCookieQuery("UPSTREAM.test", dns.TypeA, clientCookie2)
|
|
q2.Id = 999
|
|
handleQuery(w2, q2)
|
|
if w2.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if w2.msg.Id != 999 {
|
|
t.Errorf("cached response ID = %d, want 999", w2.msg.Id)
|
|
}
|
|
if w2.msg.Question[0].Name != "UPSTREAM.test." {
|
|
t.Errorf("cached response question = %q, want the current query's question", w2.msg.Question[0].Name)
|
|
}
|
|
if w2.msg.AuthenticatedData {
|
|
t.Error("AD bit must be cleared on cache hits too")
|
|
}
|
|
c2 := responseCookie(w2.msg)
|
|
if c2 == nil {
|
|
t.Fatal("cached response has no COOKIE option")
|
|
}
|
|
gotClient2, gotServer2 := decodeCookie(t, c2)
|
|
mustEqual(t, "cached response client cookie", gotClient2, clientCookie2)
|
|
if bytes.Equal(gotServer2, gotServer) {
|
|
t.Error("cached response must carry a fresh server cookie for the new client")
|
|
}
|
|
if !validateServerCookie("192.0.2.99", clientCookie2, gotServer2, time.Now()) {
|
|
t.Error("cached response server cookie does not validate for the new client")
|
|
}
|
|
if len(w2.msg.Answer) != 1 {
|
|
t.Errorf("cached response answers = %d, want 1", len(w2.msg.Answer))
|
|
}
|
|
}
|
|
|
|
// TestHandleQueryUpstreamQuestionMismatch verifies that an upstream
|
|
// response whose question section does not match the query is rejected.
|
|
func TestHandleQueryUpstreamQuestionMismatch(t *testing.T) {
|
|
resetMaps()
|
|
qcache.Flush()
|
|
|
|
handler := func(w dns.ResponseWriter, q *dns.Msg) {
|
|
r := new(dns.Msg)
|
|
r.SetReply(q)
|
|
r.Question[0].Name = "evil.example."
|
|
r.Answer = append(r.Answer, &dns.A{
|
|
Hdr: dns.RR_Header{Name: "evil.example.", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 300},
|
|
A: net.ParseIP("192.0.2.1"),
|
|
})
|
|
if err := w.WriteMsg(r); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
useUpstream(t, startMockUpstream(t, handler, handler))
|
|
|
|
w := mockUDPWriter(t)
|
|
handleQuery(w, newQuery("mismatch.test", dns.TypeA, dns.ClassINET))
|
|
if w.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if w.msg.Rcode != dns.RcodeServerFailure {
|
|
t.Errorf("rcode = %d, want SERVFAIL for question-mismatched upstream response", w.msg.Rcode)
|
|
}
|
|
}
|
|
|
|
// TestHandleQueryTruncatedRetryTCP verifies that a truncated UDP response
|
|
// from upstream is retried over TCP to get the full answer.
|
|
func TestHandleQueryTruncatedRetryTCP(t *testing.T) {
|
|
resetMaps()
|
|
qcache.Flush()
|
|
|
|
fullAnswer := func(q *dns.Msg) *dns.Msg {
|
|
r := new(dns.Msg)
|
|
r.SetReply(q)
|
|
r.Answer = append(r.Answer, &dns.A{
|
|
Hdr: dns.RR_Header{Name: q.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 300},
|
|
A: net.ParseIP("192.0.2.1"),
|
|
})
|
|
return r
|
|
}
|
|
udpHandler := func(w dns.ResponseWriter, q *dns.Msg) {
|
|
r := fullAnswer(q)
|
|
r.Truncated = true
|
|
if err := w.WriteMsg(r); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
tcpHandler := func(w dns.ResponseWriter, q *dns.Msg) {
|
|
if err := w.WriteMsg(fullAnswer(q)); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
useUpstream(t, startMockUpstream(t, udpHandler, tcpHandler))
|
|
|
|
w := mockUDPWriter(t)
|
|
handleQuery(w, newQuery("truncated.test", dns.TypeA, dns.ClassINET))
|
|
if w.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if w.msg.Truncated {
|
|
t.Error("response must not be truncated after the TCP retry")
|
|
}
|
|
if len(w.msg.Answer) != 1 {
|
|
t.Fatalf("answers = %d, want 1 (full answer over TCP)", len(w.msg.Answer))
|
|
}
|
|
}
|
|
|
|
// TestHandleQueryUDPTruncation verifies that UDP responses exceeding the
|
|
// client's advertised payload size are truncated with the TC bit set, and
|
|
// that a client with a larger buffer gets the full cached answer.
|
|
func TestHandleQueryUDPTruncation(t *testing.T) {
|
|
resetMaps()
|
|
qcache.Flush()
|
|
|
|
handler := func(w dns.ResponseWriter, q *dns.Msg) {
|
|
r := new(dns.Msg)
|
|
r.SetReply(q)
|
|
for i := 0; i < 40; i++ {
|
|
r.Answer = append(r.Answer, &dns.A{
|
|
Hdr: dns.RR_Header{Name: q.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 300},
|
|
A: net.ParseIP(fmt.Sprintf("192.0.2.%d", i+1)),
|
|
})
|
|
}
|
|
if err := w.WriteMsg(r); err != nil {
|
|
t.Error(err)
|
|
}
|
|
}
|
|
useUpstream(t, startMockUpstream(t, handler, handler))
|
|
|
|
// Client without EDNS: responses are limited to 512 bytes.
|
|
w1 := mockUDPWriter(t)
|
|
handleQuery(w1, newQuery("big.test", dns.TypeA, dns.ClassINET))
|
|
if w1.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if !w1.msg.Truncated {
|
|
t.Error("oversized UDP response must be truncated (TC bit set) for a non-EDNS client")
|
|
}
|
|
if len(w1.msg.Answer) == 0 || len(w1.msg.Answer) == 40 {
|
|
t.Errorf("answers = %d, want a partial answer", len(w1.msg.Answer))
|
|
}
|
|
|
|
// Client with a large EDNS buffer: full answer from the cache.
|
|
w2 := mockUDPWriter(t)
|
|
q2 := newQuery("big.test", dns.TypeA, dns.ClassINET)
|
|
q2.SetEdns0(4096, false)
|
|
handleQuery(w2, q2)
|
|
if w2.msg == nil {
|
|
t.Fatal("no response written")
|
|
}
|
|
if w2.msg.Truncated {
|
|
t.Error("response within the client's buffer must not be truncated")
|
|
}
|
|
if len(w2.msg.Answer) != 40 {
|
|
t.Errorf("answers = %d, want 40 (full cached answer)", len(w2.msg.Answer))
|
|
}
|
|
}
|
|
|
|
// TestHandleCacheDecrementsTTL verifies that TTLs of cached responses are
|
|
// decremented by the time the entry spent in the cache, for both positive
|
|
// and negative answers, and that the OPT pseudo-record is left untouched.
|
|
func TestHandleCacheDecrementsTTL(t *testing.T) {
|
|
qcache.Flush()
|
|
|
|
cached := new(dns.Msg)
|
|
cached.SetReply(newQuery("ttl.test", dns.TypeA, dns.ClassINET))
|
|
cached.Answer = []dns.RR{&dns.A{
|
|
Hdr: dns.RR_Header{Name: "ttl.test.", Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 300},
|
|
A: net.ParseIP("192.0.2.1"),
|
|
}}
|
|
cached.Ns = []dns.RR{&dns.SOA{
|
|
Hdr: dns.RR_Header{Name: "ttl.test.", Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 120},
|
|
Ns: "ns.ttl.test.",
|
|
Mbox: "hostmaster.ttl.test.",
|
|
Serial: 1,
|
|
Refresh: 3600,
|
|
Retry: 600,
|
|
Expire: 86400,
|
|
Minttl: 60,
|
|
}}
|
|
cached.SetEdns0(4096, false)
|
|
cached.IsEdns0().Hdr.Ttl = 0x00008000 // DO bit: must survive untouched
|
|
key := cacheKey("ttl.test", "IN", "A")
|
|
|
|
// Store the entry as if it had been cached 30 seconds ago.
|
|
qcache.Set(key, cached, cacheTTL()-30*time.Second)
|
|
|
|
req := newQuery("TTL.test", dns.TypeA, dns.ClassINET)
|
|
req.Id = 4242
|
|
msg, rcode, ok := handleCache(req, key)
|
|
if !ok {
|
|
t.Fatal("expected cache hit")
|
|
}
|
|
if rcode != dns.RcodeSuccess {
|
|
t.Errorf("rcode = %d, want NOERROR", rcode)
|
|
}
|
|
if msg.Id != 4242 {
|
|
t.Errorf("response ID = %d, want 4242", msg.Id)
|
|
}
|
|
if msg.Question[0].Name != "TTL.test." {
|
|
t.Errorf("question = %q, want the current query's question", msg.Question[0].Name)
|
|
}
|
|
if ttl := msg.Answer[0].Header().Ttl; ttl != 270 {
|
|
t.Errorf("answer TTL = %d, want 270 (300 decremented by 30s)", ttl)
|
|
}
|
|
if ttl := msg.Ns[0].Header().Ttl; ttl != 90 {
|
|
t.Errorf("authority TTL = %d, want 90 (120 decremented by 30s)", ttl)
|
|
}
|
|
if msg.IsEdns0().Hdr.Ttl != 0x00008000 {
|
|
t.Errorf("OPT TTL/flags = %#x, want 0x8000 (untouched)", msg.IsEdns0().Hdr.Ttl)
|
|
}
|
|
|
|
// The cached entry itself must not be mutated.
|
|
if ttl := cached.Answer[0].Header().Ttl; ttl != 300 {
|
|
t.Errorf("cached entry answer TTL = %d, want 300 (unmutated)", ttl)
|
|
}
|
|
|
|
// A negative (NXDOMAIN) entry is decremented too.
|
|
nxKey := cacheKey("nx.ttl.test", "IN", "A")
|
|
nx := new(dns.Msg)
|
|
nx.SetReply(newQuery("nx.ttl.test", dns.TypeA, dns.ClassINET))
|
|
nx.Rcode = dns.RcodeNameError
|
|
nx.Ns = []dns.RR{&dns.SOA{
|
|
Hdr: dns.RR_Header{Name: "ttl.test.", Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 120},
|
|
Ns: "ns.ttl.test.",
|
|
Mbox: "hostmaster.ttl.test.",
|
|
Serial: 1,
|
|
Refresh: 3600,
|
|
Retry: 600,
|
|
Expire: 86400,
|
|
Minttl: 60,
|
|
}}
|
|
qcache.Set(nxKey, nx, cacheTTL()-30*time.Second)
|
|
nxMsg, nxRcode, ok := handleCache(newQuery("nx.ttl.test", dns.TypeA, dns.ClassINET), nxKey)
|
|
if !ok {
|
|
t.Fatal("expected cache hit for NXDOMAIN entry")
|
|
}
|
|
if nxRcode != dns.RcodeNameError {
|
|
t.Errorf("rcode = %d, want NXDOMAIN", nxRcode)
|
|
}
|
|
if ttl := nxMsg.Ns[0].Header().Ttl; ttl != 90 {
|
|
t.Errorf("NXDOMAIN SOA TTL = %d, want 90 (120 decremented by 30s)", ttl)
|
|
}
|
|
}
|
|
|
|
func TestMsgAcceptFunc(t *testing.T) {
|
|
header := func(qdcount uint16, opcode int) dns.Header {
|
|
return dns.Header{Bits: uint16(opcode)<<11 | 0x0100, Qdcount: qdcount} // RD set
|
|
}
|
|
// Question-less QUERY requests are accepted so that RFC 7873
|
|
// section 5.4 server-cookie learning probes reach the handler.
|
|
if got := msgAcceptFunc(header(0, dns.OpcodeQuery)); got != dns.MsgAccept {
|
|
t.Errorf("msgAcceptFunc(QDCOUNT=0 QUERY) = %v, want MsgAccept", got)
|
|
}
|
|
// Everything else follows the default rules.
|
|
if got := msgAcceptFunc(header(1, dns.OpcodeQuery)); got != dns.MsgAccept {
|
|
t.Errorf("msgAcceptFunc(QDCOUNT=1 QUERY) = %v, want MsgAccept", got)
|
|
}
|
|
if got := msgAcceptFunc(header(2, dns.OpcodeQuery)); got != dns.MsgReject {
|
|
t.Errorf("msgAcceptFunc(QDCOUNT=2 QUERY) = %v, want MsgReject", got)
|
|
}
|
|
if got := msgAcceptFunc(header(0, dns.OpcodeUpdate)); got != dns.MsgRejectNotImplemented {
|
|
t.Errorf("msgAcceptFunc(QDCOUNT=0 UPDATE) = %v, want MsgRejectNotImplemented", got)
|
|
}
|
|
}
|
|
|
|
// TestServerCookieProbe runs the real server stack (miekg/dns server with
|
|
// msgAcceptFunc and handleQuery) over UDP and exercises the RFC 7873
|
|
// section 5.4 server-cookie learning probe and the malformed-COOKIE FORMERR
|
|
// end to end.
|
|
func TestServerCookieProbe(t *testing.T) {
|
|
resetMaps()
|
|
qcache.Flush()
|
|
|
|
pc, err := net.ListenPacket("udp", "127.0.0.1:0")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
srv := &dns.Server{
|
|
PacketConn: pc,
|
|
Handler: dns.HandlerFunc(handleQuery),
|
|
MsgAcceptFunc: msgAcceptFunc,
|
|
UDPSize: serverUDPSize,
|
|
}
|
|
go func() { _ = srv.ActivateAndServe() }()
|
|
t.Cleanup(func() { _ = srv.Shutdown() })
|
|
addr := pc.LocalAddr().String()
|
|
|
|
client := &dns.Client{Net: "udp", UDPSize: dns.MaxMsgSize}
|
|
clientCookie := []byte{0x6d, 0x36, 0x72, 0xdf, 0xf3, 0xc4, 0x63, 0x75}
|
|
|
|
// QDCOUNT=0 probe with a client cookie: NOERROR, empty answer
|
|
// section, fresh server cookie.
|
|
r, _, err := client.Exchange(newProbeQuery(clientCookie, nil), addr)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.Rcode != dns.RcodeSuccess {
|
|
t.Errorf("probe rcode = %d, want NOERROR", r.Rcode)
|
|
}
|
|
if len(r.Answer) != 0 {
|
|
t.Errorf("probe answer count = %d, want 0", len(r.Answer))
|
|
}
|
|
c := responseCookie(r)
|
|
if c == nil {
|
|
t.Fatal("probe response has no COOKIE option")
|
|
}
|
|
gotClient, gotServer := decodeCookie(t, c)
|
|
mustEqual(t, "probe response client cookie", gotClient, clientCookie)
|
|
if !validateServerCookie("127.0.0.1", clientCookie, gotServer, time.Now()) {
|
|
t.Error("probe response server cookie does not validate for the client address")
|
|
}
|
|
|
|
// A query with a malformed COOKIE option gets FORMERR.
|
|
r2, _, err := client.Exchange(newCookieQuery("malformed.test", dns.TypeA, []byte{1, 2, 3, 4}), addr)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r2.Rcode != dns.RcodeFormatError {
|
|
t.Errorf("malformed COOKIE rcode = %d, want FORMERR", r2.Rcode)
|
|
}
|
|
if c := responseCookie(r2); c != nil {
|
|
t.Error("FORMERR response to a malformed COOKIE option carries a COOKIE option")
|
|
}
|
|
}
|