Files
jarl/internal/stats/blocking_test.go
T
adamandClaude Opus 5.5 9fc23310bd Add blocking: blocklists and block/allow rules with BIND response policy zones
jarld downloads blocklists (Adblock, hosts or plain domains), checks them
daily and reloads named only when a list changed; your own block and
allow rules take precedence, and the most specific rule wins. Lists and
rules apply per view (default: every view that offers recursion). Web UI
Blocking page with name checks, jarl blocklist and jarl block, the
/blocking API, blocked counts in the statistics. Database schema 6.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 09:24:19 +00:00

112 lines
5.0 KiB
Go

package stats
import (
"context"
"fmt"
"io"
"log/slog"
"net/netip"
"os"
"path/filepath"
"testing"
"time"
)
func TestParseRewrite(t *testing.T) {
loc := time.UTC
// Real lines from named 9.20.24.
rw, ok := ParseRewrite(`09-Oct-2026 23:49:13.089 client @0x7bd607320000 127.0.0.1#56905 (x.ads.example): view internal: rpz QNAME NXDOMAIN rewrite x.ads.example/A/IN via x.ads.example.rules.jarl.invalid`, loc)
if !ok || rw.Client.String() != "127.0.0.1" || rw.Port != "56905" || rw.View != "internal" || rw.Name != "x.ads.example" || rw.Action != "NXDOMAIN" || policySource(rw.Via) != "rules" {
t.Errorf("rules rewrite: %+v (%s)", rw, policySource(rw.Via))
}
rw, ok = ParseRewrite(`09-Oct-2026 23:49:13.378 client @0x7bd607320000 10.0.0.9#57347 (Tracker.Example): rpz QNAME NXDOMAIN rewrite tracker.example/AAAA/IN via tracker.example.hagezi-pro.blocklist.jarl.invalid`, loc)
if !ok || rw.View != "" || rw.Name != "tracker.example" || policySource(rw.Via) != "hagezi-pro" {
t.Errorf("list rewrite: %+v (%s)", rw, policySource(rw.Via))
}
if policySource("ads.example.my-own-rpz.") != "ads.example.my-own-rpz" {
t.Error("a policy zone that isn't Jarl's")
}
if _, ok := ParseRewrite(`09-Oct-2026 23:49:13.378 client @0x7bd607320000 10.0.0.9#57347 (x): view internal: response: x IN A NOERROR 1 0 1 + (10.0.0.2)`, loc); ok {
t.Error("a response line parsed as a rewrite")
}
}
func TestCollectorBlocking(t *testing.T) {
ctx := context.Background()
loc := time.UTC
dir := t.TempDir()
logFile := filepath.Join(dir, "responses.log")
store, err := OpenStore(filepath.Join(dir, "stats.db"))
if err != nil {
t.Fatal(err)
}
defer store.Close()
named := newFakeNamed(t)
clk := &clock{time.Date(2026, 10, 5, 10, 0, 30, 0, loc)}
opts := Options{QueryLog: true, LogFile: logFile, StatisticsChannel: "unused", TopN: 5,
RetainMinute: 7 * 24 * time.Hour, RetainHour: 90 * 24 * time.Hour, RetainDay: 730 * 24 * time.Hour,
Exclude: []netip.Prefix{netip.MustParsePrefix("10.0.0.250/32")}, Location: loc}
c := NewCollector(store, opts, func() map[string][]string { return nil }, slog.New(slog.NewTextHandler(io.Discard, nil)))
c.poller.URL = named.srv.URL
c.now = clk.now
c.start(ctx)
at := time.Date(2026, 10, 5, 10, 0, 40, 0, loc).Format(logTimeLayout)
rewrite := func(client, port, name, action, via string) string {
return fmt.Sprintf("%s client @0x7f0000000000 %s#%s (%s): view internal: rpz QNAME %s rewrite %s/A/IN via %s\n", at, client, port, name, action, name, via)
}
response := func(client, port, name, rcode string) string {
return fmt.Sprintf("%s client @0x7f0000000000 %s#%s (%s): view internal: response: %s IN A %s 0 0 2 +E(0)K (10.0.0.2)\n", at, client, port, name, name, rcode)
}
f, _ := os.Create(logFile)
for _, l := range []string{
rewrite("10.0.0.5", "1001", "ads.example", "NXDOMAIN", "ads.example.hagezi.blocklist.jarl.invalid"),
response("10.0.0.5", "1001", "ads.example", "NXDOMAIN"),
rewrite("10.0.0.5", "1002", "x.ads.example", "NXDOMAIN", "x.ads.example.hagezi.blocklist.jarl.invalid"),
response("10.0.0.5", "1002", "x.ads.example", "NXDOMAIN"),
rewrite("10.0.0.6", "1003", "ads.example", "NXDOMAIN", "ads.example.rules.jarl.invalid"),
response("10.0.0.6", "1003", "ads.example", "NXDOMAIN"),
// An allow rule: answered normally, not blocked.
rewrite("10.0.0.6", "1004", "cdn.ads.example", "PASSTHRU", "cdn.ads.example.rules.jarl.invalid"),
response("10.0.0.6", "1004", "cdn.ads.example", "NOERROR"),
// A real NXDOMAIN.
response("10.0.0.6", "1005", "typo.example", "NXDOMAIN"),
// An excluded client.
rewrite("10.0.0.250", "1006", "ads.example", "NXDOMAIN", "ads.example.hagezi.blocklist.jarl.invalid"),
response("10.0.0.250", "1006", "ads.example", "NXDOMAIN"),
} {
f.WriteString(l)
}
f.Close()
c.readLog()
named.add(6, 4)
clk.t = time.Date(2026, 10, 5, 10, 1, 2, 0, loc)
c.flush(ctx, false)
m := time.Date(2026, 10, 5, 10, 0, 0, 0, loc).Unix()
got, _ := store.sums(ctx, ResMinute, m, m+60, "")
for k, v := range map[string]int64{"blocked": 3, "blocklist:hagezi": 2, "blocklist:rules": 1, "responses": 5} {
if got[k] != v {
t.Errorf("%s = %d, want %d (all: %v)", k, got[k], v, got)
}
}
if b, _ := store.TopsAt(ctx, ResHour, m, KindBlocked); len(b) != 2 || b[0] != (Entry{Key: "ads.example", Count: 2}) {
t.Errorf("blocked names = %v", b)
}
if nx, _ := store.TopsAt(ctx, ResHour, m, KindNXDomain); len(nx) != 1 || nx[0].Key != "typo.example" {
t.Errorf("NXDOMAIN names should leave out blocked ones: %v", nx)
}
q := &Querier{Store: store, Opts: opts, Now: clk.now}
sum, err := q.Summary(ctx, time.Hour, 5)
if err != nil {
t.Fatal(err)
}
// 6 queries, 4 NXDOMAIN of which 3 blocked.
if sum.Blocked != 3 || sum.BlockedRatio == nil || *sum.BlockedRatio != 0.5 || sum.NXDomainRatio == nil || *sum.NXDomainRatio != 1.0/6 {
t.Errorf("summary: blocked %d ratio %v nxdomain %v", sum.Blocked, sum.BlockedRatio, sum.NXDomainRatio)
}
if len(sum.Blocklists) != 2 || sum.Blocklists[0] != (Entry{Key: "hagezi", Count: 2}) || len(sum.Tops[KindBlocked]) != 2 {
t.Errorf("summary lists %v, tops %v", sum.Blocklists, sum.Tops[KindBlocked])
}
}