Files
fancy-dns/internal/app/accesslog_test.go
T
matsandClaude Sonnet 5.5 b513501773 Fix block-answer TTLs that were nanoseconds, and stop counting NXDOMAIN as an error
Both found by running the resolver locally against real upstreams.

response_ttl is a duration (60s) and a DNS TTL is seconds, but eight places
converted with a bare uint32(), which keeps the low 32 bits of the nanosecond
count. A blocked name's negative answer carried an SOA TTL and minimum of
997195776 (about 31 years), so any client or downstream cache that honoured it
would remember the block, and keep remembering it after the name was
unblocked. blocklist.TTLSeconds converts properly (whole seconds, clamped to
2^31-1) and is used by the rule parser and the block, sinkhole and redirect
answers. The tests read the TTL off the wire; with the old conversion they fail
with 4165425152.

An ordinary upstream NXDOMAIN was classified as an error. A name that does not
exist is the normal result of a typo, a search-domain expansion or a leaked mDNS
query, so on a real network the error metric counted mostly those and, with the
access log's errors-only default, every one wrote a line. Only the upstream
failing or refusing (SERVFAIL, REFUSED, FORMERR, NOTIMP) is an error now.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-16 10:26:49 +02:00

72 lines
2.0 KiB
Go

package app
import (
"bytes"
"encoding/json"
"strings"
"sync"
"testing"
"github.com/miekg/dns"
)
type lockedBuf struct {
mu sync.Mutex
b bytes.Buffer
}
func (l *lockedBuf) Write(p []byte) (int, error) {
l.mu.Lock()
defer l.mu.Unlock()
return l.b.Write(p)
}
func (l *lockedBuf) lines() []map[string]any {
l.mu.Lock()
defer l.mu.Unlock()
var out []map[string]any
for _, ln := range strings.Split(strings.TrimSpace(l.b.String()), "\n") {
if ln == "" {
continue
}
var m map[string]any
if err := json.Unmarshal([]byte(ln), &m); err != nil {
panic("access log line is not JSON: " + ln)
}
out = append(out, m)
}
return out
}
// TestAccessLogDefaultsToBlockedAndFailed runs the whole resolver with the
// shipped defaults: a blocked query leaves a line, an ordinary answer and a
// cache hit leave none, and the line carries what a log search needs.
func TestAccessLogDefaultsToBlockedAndFailed(t *testing.T) {
out := &lockedBuf{}
h := newHarness(t, nil, func(o *Options) { o.AccessLogOut = out })
h.query("www.example.test", dns.TypeA) // allowed
h.query("www.example.test", dns.TypeA) // cached
// A name that does not exist is an answer, not an error worth a line.
if m := h.query("no-such-name.example.org", dns.TypeA); m.Rcode != dns.RcodeNameError {
t.Fatalf("setup: no-such-name.example.org rcode %d, want NXDOMAIN", m.Rcode)
}
if m := h.query("ads.example.com", dns.TypeA); m.Rcode != dns.RcodeNameError {
t.Fatalf("setup: ads.example.com rcode %d, want a block", m.Rcode)
}
h.app.logger.Flush()
lines := out.lines()
if len(lines) != 1 {
t.Fatalf("%d access-log lines, want exactly the blocked query (not the allowed, cached or NXDOMAIN ones): %v", len(lines), lines)
}
l := lines[0]
for k, want := range map[string]any{
"log": "access", "msg": "query", "name": "ads.example.com.", "action": "block",
"list": "test-hosts", "qtype": "A", "client": "127.0.0.1", "protocol": "udp",
} {
if l[k] != want {
t.Errorf("%s = %v, want %v (line %v)", k, l[k], want, l)
}
}
}