Fixed unit tests and updated limits

This commit is contained in:
Pierre Souchay 2018-05-16 12:11:49 +02:00
parent 6e80b6b127
commit cfa5986df7
2 changed files with 33 additions and 26 deletions

View File

@ -755,9 +755,9 @@ func (d *DNSServer) trimTCPResponse(req, resp *dns.Msg) (trimmed bool) {
// Beyond 2500 records, performance gets bad // Beyond 2500 records, performance gets bad
// Limit the number of records at once, anyway, it won't fit in 64k // Limit the number of records at once, anyway, it won't fit in 64k
// For SRV Records, the max is around 500 records, for A, less than 2k // For SRV Records, the max is around 500 records, for A, less than 2k
truncateAt := 2048 truncateAt := 4096
if req.Question[0].Qtype == dns.TypeSRV { if req.Question[0].Qtype == dns.TypeSRV {
truncateAt = 640 truncateAt = 1024
} }
if len(resp.Answer) > truncateAt { if len(resp.Answer) > truncateAt {
resp.Answer = resp.Answer[:truncateAt] resp.Answer = resp.Answer[:truncateAt]

View File

@ -3080,32 +3080,39 @@ func TestDNS_TCP_and_UDP_Truncate(t *testing.T) {
"tcp", "tcp",
"udp", "udp",
} }
for _, qType := range []uint16{dns.TypeANY, dns.TypeA, dns.TypeSRV} { for _, maxSize := range []uint16{8192, 65535} {
for _, question := range questions { for _, qType := range []uint16{dns.TypeANY, dns.TypeA, dns.TypeSRV} {
for _, protocol := range protocols { for _, question := range questions {
for _, compress := range []bool{true, false} { for _, protocol := range protocols {
t.Run(fmt.Sprintf("lookup %s %s (qType:=%d) compressed=%v", question, protocol, qType, compress), func(t *testing.T) { for _, compress := range []bool{true, false} {
m := new(dns.Msg) t.Run(fmt.Sprintf("lookup %s %s (qType:=%d) compressed=%v", question, protocol, qType, compress), func(t *testing.T) {
m.SetQuestion(question, dns.TypeANY) m := new(dns.Msg)
if protocol == "udp" { m.SetQuestion(question, dns.TypeANY)
m.SetEdns0(8192, true) maxSz := maxSize
} if protocol == "udp" {
c := new(dns.Client) maxSz = 8192
c.Net = protocol }
m.Compress = compress m.SetEdns0(uint16(maxSz), true)
in, out, err := c.Exchange(m, a.DNSAddr()) c := new(dns.Client)
if err != nil && err != dns.ErrTruncated { c.Net = protocol
t.Fatalf("err: %v", err) m.Compress = compress
} in, _, err := c.Exchange(m, a.DNSAddr())
// Check for the truncate bit if err != nil && err != dns.ErrTruncated {
shouldBeTruncated := numServices > 5000 t.Fatalf("err: %v", err)
}
if shouldBeTruncated != in.Truncated || len(in.Answer) > 2000 || len(in.Answer) < 1 || in.Len() > 65535 { // Check for the truncate bit
buf, err := m.Pack()
info := fmt.Sprintf("service %s question:=%s (%s) (%d total records) sz:= %d in %v", info := fmt.Sprintf("service %s question:=%s (%s) (%d total records) sz:= %d in %v",
service, question, protocol, numServices, len(in.Answer), out) service, question, protocol, numServices, len(in.Answer), in)
t.Fatalf("Should have truncated:=%v for %s", shouldBeTruncated, info) if err != nil {
} t.Fatalf("Error while packing: %v ; info:=%s", err, info)
}) }
if len(buf) > int(maxSz) {
t.Fatalf("len(buf) := %d > maxSz=%d for %v", len(buf), maxSz, info)
}
})
}
} }
} }
} }