|
| 1 | +package rpz |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/miekg/dns" |
| 9 | + "github.com/prometheus/client_golang/prometheus/testutil" |
| 10 | + "github.com/semihalev/sdns/config" |
| 11 | + "github.com/semihalev/sdns/internal/mock" |
| 12 | + rpzengine "github.com/semihalev/sdns/internal/rpz" |
| 13 | + "github.com/semihalev/sdns/middleware" |
| 14 | +) |
| 15 | + |
| 16 | +// The coverage-gap round: paths the arc's tests drove only indirectly |
| 17 | +// or not at all — the feed's real lifecycle loop, the neutral global |
| 18 | +// gate's exempt-query contract, the response wrap's remaining actions, |
| 19 | +// and the decoded request path. |
| 20 | + |
| 21 | +// serveProto is serve with the transport under test. |
| 22 | +func (h *sidecarHarness) serveProto(t *testing.T, qname, proto string) *mock.Writer { |
| 23 | + t.Helper() |
| 24 | + q := new(dns.Msg) |
| 25 | + q.SetQuestion(qname, dns.TypeA) |
| 26 | + q.RecursionDesired = true |
| 27 | + raw, err := q.Pack() |
| 28 | + if err != nil { |
| 29 | + t.Fatal(err) |
| 30 | + } |
| 31 | + req := new(middleware.Request) |
| 32 | + if !req.ParseWire(raw, time.Now(), nil) { |
| 33 | + t.Fatal("refused") |
| 34 | + } |
| 35 | + w := mock.NewWriter(proto, "192.0.2.1:40000") |
| 36 | + ch := middleware.NewChain([]middleware.Handler{h.r, h.c, h.authority()}) |
| 37 | + ch.ResetWire(w, req) |
| 38 | + ch.AllowDirectPack() |
| 39 | + ch.Next(context.Background()) |
| 40 | + return w |
| 41 | +} |
| 42 | + |
| 43 | +// TestResponseActionsEndToEnd drives the wrap's remaining actions over |
| 44 | +// real resolutions: DROP swallows the reply, TCP-Only truncates UDP and |
| 45 | +// passes other transports, NODATA empties the answer, and Local Data |
| 46 | +// answers with the rule's records on the client's qname. |
| 47 | +func TestResponseActionsEndToEnd(t *testing.T) { |
| 48 | + const actionZone = ` |
| 49 | +rpz.test. IN SOA ns.rpz.test. admin.rpz.test. 1 3600 900 604800 300 |
| 50 | +24.0.113.0.203.rpz-ip.rpz.test. IN CNAME rpz-drop. |
| 51 | +24.0.100.51.198.rpz-ip.rpz.test. IN CNAME rpz-tcp-only. |
| 52 | +24.0.2.0.192.rpz-ip.rpz.test. IN CNAME *. |
| 53 | +16.0.0.0.10.rpz-ip.rpz.test. IN A 203.0.113.53 |
| 54 | +` |
| 55 | + h := newSidecarHarness(t, "enforce", config.RPZZone{Name: "act", File: writeZone(t, actionZone)}) |
| 56 | + h.truth["dropme.test."] = "203.0.113.5" |
| 57 | + h.truth["tcpme.test."] = "198.51.100.5" |
| 58 | + h.truth["nodatame.test."] = "192.0.2.5" |
| 59 | + h.truth["localme.test."] = "10.0.1.2" |
| 60 | + |
| 61 | + for _, pass := range []string{"miss", "hit"} { |
| 62 | + if w := h.serve(t, "dropme.test.", "192.0.2.1:40000", true); w.Written() { |
| 63 | + t.Fatalf("%s: DROP wrote a reply", pass) |
| 64 | + } |
| 65 | + w := h.serve(t, "tcpme.test.", "192.0.2.1:40000", true) |
| 66 | + if !w.Written() || !w.Msg().Truncated || len(w.Msg().Answer) != 0 { |
| 67 | + t.Fatalf("%s: TCP-Only over UDP must truncate emptily: tc=%v answers=%d", pass, w.Msg().Truncated, len(w.Msg().Answer)) |
| 68 | + } |
| 69 | + w = h.serve(t, "nodatame.test.", "192.0.2.1:40000", true) |
| 70 | + if w.Rcode() != dns.RcodeSuccess || len(w.Msg().Answer) != 0 || w.Msg().AuthenticatedData { |
| 71 | + t.Fatalf("%s: NODATA broken: rcode=%d answers=%d", pass, w.Rcode(), len(w.Msg().Answer)) |
| 72 | + } |
| 73 | + w = h.serve(t, "localme.test.", "192.0.2.1:40000", true) |
| 74 | + if w.Rcode() != dns.RcodeSuccess || len(w.Msg().Answer) != 1 { |
| 75 | + t.Fatalf("%s: Local Data broken: rcode=%d answers=%d", pass, w.Rcode(), len(w.Msg().Answer)) |
| 76 | + } |
| 77 | + a := w.Msg().Answer[0].(*dns.A) |
| 78 | + if a.Hdr.Name != "localme.test." || a.A.String() != "203.0.113.53" { |
| 79 | + t.Fatalf("%s: Local Data owner/rdata: %v", pass, a) |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + // A non-UDP transport already satisfies TCP-Only: the truth flows. |
| 84 | + w := h.serveProto(t, "tcpme.test.", "tcp") |
| 85 | + if w.Rcode() != dns.RcodeSuccess || len(w.Msg().Answer) != 1 || w.Msg().Truncated { |
| 86 | + t.Fatalf("TCP-Only over TCP must pass the truth: rcode=%d tc=%v", w.Rcode(), w.Msg().Truncated) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// TestNeutralGateServesExemptQueriesUntouched pins the global gate's |
| 91 | +// contract — the gate un-wrapped (exempt) queries reach: entries stay |
| 92 | +// healthy (stale restamps) but even a matching sidecar serves the truth |
| 93 | +// and counts nothing, because policy does not apply to those queries. |
| 94 | +func TestNeutralGateServesExemptQueriesUntouched(t *testing.T) { |
| 95 | + h := newSidecarHarness(t, "enforce", config.RPZZone{Name: "resp", File: writeZone(t, respTestZone)}) |
| 96 | + s := h.r.store.Load() |
| 97 | + |
| 98 | + matching := &middleware.Sidecar{Value: &rpzengine.ResponseMatches{ |
| 99 | + Gen: s.Gen, |
| 100 | + List: s.EvaluateResponseList([]dns.RR{testA("x.test.", "203.0.113.10")}), |
| 101 | + }} |
| 102 | + if v := h.r.JudgeWireHit(matching); v != middleware.WireHitServe { |
| 103 | + t.Fatalf("a matching entry must still serve an exempt query: %v", v) |
| 104 | + } |
| 105 | + if v := h.r.JudgeWireHit(nil); v != middleware.WireHitRestamp { |
| 106 | + t.Fatalf("an unevaluated entry must restamp: %v", v) |
| 107 | + } |
| 108 | + stale := &middleware.Sidecar{Value: &rpzengine.ResponseMatches{Gen: s.Gen - 1}} |
| 109 | + if v := h.r.JudgeWireHit(stale); v != middleware.WireHitRestamp { |
| 110 | + t.Fatalf("a stale entry must restamp: %v", v) |
| 111 | + } |
| 112 | + |
| 113 | + var chain middleware.SidecarChain |
| 114 | + chain.Append(matching) |
| 115 | + if v := h.r.JudgeWireChase(chain); v != middleware.WireHitServe { |
| 116 | + t.Fatalf("chase with fresh segments must serve: %v", v) |
| 117 | + } |
| 118 | + var staleChain middleware.SidecarChain |
| 119 | + staleChain.Append(stale) |
| 120 | + if v := h.r.JudgeWireChase(staleChain); v != middleware.WireHitRestamp { |
| 121 | + t.Fatalf("chase with a stale segment must restamp: %v", v) |
| 122 | + } |
| 123 | + |
| 124 | + // Counting nothing is the whole point. |
| 125 | + counter := actionTotal.WithLabelValues("resp", rpzengine.TriggerResponseIP, "nxdomain", "enforced") |
| 126 | + base := testutil.ToFloat64(counter) |
| 127 | + h.r.CountWireHit(matching) |
| 128 | + h.r.CountWireChase(chain) |
| 129 | + if d := testutil.ToFloat64(counter) - base; d != 0 { |
| 130 | + t.Fatalf("the neutral gate counted an exempt query: %v", d) |
| 131 | + } |
| 132 | + |
| 133 | + // Without response rules everything serves, whatever the sidecar. |
| 134 | + bare := newRPZ(t, "enforce", config.RPZZone{Name: "plain", File: writeZone(t, testZone)}) |
| 135 | + if v := bare.JudgeWireHit(nil); v != middleware.WireHitServe { |
| 136 | + t.Fatalf("no response rules must mean Serve: %v", v) |
| 137 | + } |
| 138 | + if v := bare.WireHitGate().JudgeWireChase(middleware.SidecarChain{}); v != middleware.WireHitServe { |
| 139 | + t.Fatalf("no response rules must mean Serve for chases too: %v", v) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +// TestDecodedRequestsSeeTheSamePolicy drives the Msg-born request path |
| 144 | +// (ch.Reset, not ResetWire): the decoded key build must reach the same |
| 145 | +// verdicts the wire-born path does. |
| 146 | +func TestDecodedRequestsSeeTheSamePolicy(t *testing.T) { |
| 147 | + r := testRPZ(t, "enforce") |
| 148 | + |
| 149 | + q := new(dns.Msg) |
| 150 | + q.SetQuestion("nx.example.com.", dns.TypeA) |
| 151 | + q.RecursionDesired = true |
| 152 | + passed := false |
| 153 | + next := middleware.HandlerFunc(func(_ context.Context, ch *middleware.Chain) { |
| 154 | + passed = true |
| 155 | + ch.Cancel() |
| 156 | + }) |
| 157 | + w := mock.NewWriter("udp", "192.0.2.1:40000") |
| 158 | + ch := middleware.NewChain([]middleware.Handler{r, next}) |
| 159 | + ch.Reset(w, q) |
| 160 | + ch.Next(context.Background()) |
| 161 | + |
| 162 | + if passed || !w.Written() || w.Rcode() != dns.RcodeNameError { |
| 163 | + t.Fatalf("decoded request escaped policy: passed=%v rcode=%d", passed, w.Rcode()) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// TestAXFRFeedRunLoopLifecycle runs the feed's real schedule loop: the |
| 168 | +// first transfer lands without being hand-driven, and cancelling the |
| 169 | +// context ends the loop. |
| 170 | +func TestAXFRFeedRunLoopLifecycle(t *testing.T) { |
| 171 | + srv := startFeedServer(t, "", "") |
| 172 | + r, feed := feedUnderTest(t, srv, "") |
| 173 | + |
| 174 | + ctx, cancel := context.WithCancel(context.Background()) |
| 175 | + done := make(chan struct{}) |
| 176 | + go func() { defer close(done); feed.run(ctx) }() |
| 177 | + |
| 178 | + deadline := time.Now().Add(3 * time.Second) |
| 179 | + for { |
| 180 | + if _, passed := serve(t, r, "blocked.example.com.", dns.TypeA, "udp", true); !passed { |
| 181 | + break // the first transfer landed and the rule enforces |
| 182 | + } |
| 183 | + if time.Now().After(deadline) { |
| 184 | + t.Fatal("the run loop never completed its first transfer") |
| 185 | + } |
| 186 | + time.Sleep(10 * time.Millisecond) |
| 187 | + } |
| 188 | + |
| 189 | + cancel() |
| 190 | + select { |
| 191 | + case <-done: |
| 192 | + case <-time.After(2 * time.Second): |
| 193 | + t.Fatal("cancelling the context did not end the run loop") |
| 194 | + } |
| 195 | +} |
0 commit comments