|
| 1 | +package scan |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/TMHSDigital/subenum/internal/dns" |
| 9 | + "github.com/TMHSDigital/subenum/internal/dnstest" |
| 10 | +) |
| 11 | + |
| 12 | +// TestIsWildcardAnswer covers every branch of the near-miss revalidation |
| 13 | +// added for #48 (#169), against the in-repo DNS server. Every name under |
| 14 | +// example.com answers like a rotating wildcard pool: A 192.0.2.2 and |
| 15 | +// 192.0.2.3, while the fingerprint from preflight only saw 192.0.2.1 and |
| 16 | +// 192.0.2.2. |
| 17 | +func TestIsWildcardAnswer(t *testing.T) { |
| 18 | + srv := dnstest.Start(t, func(dnstest.Query) dnstest.Reply { |
| 19 | + return dnstest.Reply{A: []string{"192.0.2.2", "192.0.2.3"}} |
| 20 | + }) |
| 21 | + a := func(ips ...string) []dns.Record { |
| 22 | + var out []dns.Record |
| 23 | + for _, ip := range ips { |
| 24 | + out = append(out, dns.Record{Type: "A", Value: ip}) |
| 25 | + } |
| 26 | + return out |
| 27 | + } |
| 28 | + live := Config{ |
| 29 | + Resolver: dns.NewResolver(2*time.Second, srv.Addr), DNSServer: srv.Addr, |
| 30 | + Timeout: 2 * time.Second, Attempts: 1, Types: []string{"A"}, |
| 31 | + } |
| 32 | + canceled, cancel := context.WithCancel(context.Background()) |
| 33 | + cancel() |
| 34 | + |
| 35 | + for _, c := range []struct { |
| 36 | + name string |
| 37 | + cfg Config |
| 38 | + ctx context.Context |
| 39 | + records []dns.Record |
| 40 | + want bool |
| 41 | + probes int64 // queries the check may send |
| 42 | + grows bool // the fingerprint learns the pool's third address |
| 43 | + }{ |
| 44 | + {name: "covered", cfg: live, records: a("192.0.2.1"), want: true}, |
| 45 | + {name: "nothing shared: a real name, no probes", cfg: live, records: a("198.51.100.7"), want: false}, |
| 46 | + {name: "near miss the pool explains", cfg: live, records: a("192.0.2.2", "192.0.2.3"), want: true, probes: 1, grows: true}, |
| 47 | + {name: "real name sharing one record", cfg: live, records: a("192.0.2.2", "198.51.100.7"), want: false, probes: nearMissProbes, grows: true}, |
| 48 | + {name: "cancelled mid-check keeps the result", cfg: live, ctx: canceled, records: a("192.0.2.2", "198.51.100.7"), want: false}, |
| 49 | + {name: "simulated runs never probe", cfg: Config{Simulate: true}, records: a("192.0.2.2", "198.51.100.7"), want: false}, |
| 50 | + } { |
| 51 | + t.Run(c.name, func(t *testing.T) { |
| 52 | + ctx := c.ctx |
| 53 | + if ctx == nil { |
| 54 | + ctx = context.Background() |
| 55 | + } |
| 56 | + fp := newFingerprint(a("192.0.2.1", "192.0.2.2")) |
| 57 | + before := srv.Queries() |
| 58 | + if got := isWildcardAnswer(ctx, c.cfg, fp, "host.example.com", c.records); got != c.want { |
| 59 | + t.Errorf("isWildcardAnswer = %v, want %v", got, c.want) |
| 60 | + } |
| 61 | + if sent := srv.Queries() - before; sent != c.probes { |
| 62 | + t.Errorf("%d probe queries, want %d", sent, c.probes) |
| 63 | + } |
| 64 | + if grew := len(fp.records()) > 2; grew != c.grows { |
| 65 | + t.Errorf("fingerprint %v, grown = %v, want %v", fp.records(), grew, c.grows) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
0 commit comments