@@ -2,10 +2,14 @@ package scan
22
33import (
44 "context"
5+ "net"
6+ "slices"
57 "strings"
68 "testing"
79 "time"
810
11+ "golang.org/x/net/dns/dnsmessage"
12+
913 "github.com/TMHSDigital/subenum/internal/dns"
1014 "github.com/TMHSDigital/subenum/internal/dnstest"
1115)
@@ -149,3 +153,92 @@ func TestDelegationCheckWithoutNameServers(t *testing.T) {
149153 t .Errorf ("parent checks %d, queries %d; want 0 and 1 (the NS lookup)" , st .ParentChecks , st .ParentQueries )
150154 }
151155}
156+
157+ // zoneLookupConfig is a delegation-check config that finds the zone's name
158+ // servers the way a real scan does: through resolver, with no ZoneServer,
159+ // and with their addresses reached on the zone server's port (#468).
160+ func zoneLookupConfig (resolver , zone * dnstest.Server ) Config {
161+ _ , port , _ := net .SplitHostPort (zone .Addr )
162+ return Config {
163+ Domain : "example.com" , Types : []string {"A" , "NS" }, Timeout : time .Second ,
164+ DNSServer : resolver .Addr , Resolver : dns .NewResolver (time .Second , resolver .Addr ),
165+ zonePort : port , scope : newScope (nil ),
166+ }
167+ }
168+
169+ // TestDelegationFindsZoneNameServers covers #468: without the lab shortcut,
170+ // the check looks up the zone's name servers through the resolver, keeps
171+ // every address of each, asks them in turn, moves past one that fails, and
172+ // counts the lookups and queries it sent.
173+ func TestDelegationFindsZoneNameServers (t * testing.T ) {
174+ zone := delegationZone (t , false )
175+ resolver := dnstest .Start (t , func (q dnstest.Query ) dnstest.Reply {
176+ switch {
177+ case q .Name == "example.com" && q .Type == dnsmessage .TypeNS :
178+ return dnstest.Reply {NS : []string {"ns1.example.com" , "ns2.example.com" }}
179+ case q .Name == "ns1.example.com" && q .Type == dnsmessage .TypeA :
180+ return dnstest.Reply {A : []string {"127.0.0.1" }}
181+ case q .Name == "ns2.example.com" && q .Type == dnsmessage .TypeA :
182+ return dnstest.Reply {A : []string {"127.0.0.2" }} // nothing answers there
183+ case q .Name == "example.com" || strings .HasPrefix (q .Name , "ns" ):
184+ return dnstest.Reply {}
185+ }
186+ return dnstest .NXDomain
187+ })
188+ p := & parentZone {cfg : zoneLookupConfig (resolver , zone )}
189+ _ , port , _ := net .SplitHostPort (zone .Addr )
190+ endpoints , errMsg := p .servers (context .Background ())
191+ good , bad := net .JoinHostPort ("127.0.0.1" , port ), net .JoinHostPort ("127.0.0.2" , port )
192+ if errMsg != "" || len (endpoints ) != 2 || ! slices .Contains (endpoints , good ) || ! slices .Contains (endpoints , bad ) {
193+ t .Fatalf ("endpoints %v (err %q), want %s and %s" , endpoints , errMsg , good , bad )
194+ }
195+ lookups := p .queries .Load ()
196+ if lookups != 5 { // NS, then A and AAAA for each host
197+ t .Errorf ("name server lookups counted %d queries, want 5" , lookups )
198+ }
199+
200+ // The one that fails is asked first, then the next one answers.
201+ p .endpoints = []string {bad , good }
202+ var notices []string
203+ records := p .delegation (context .Background (), "old.example.com" , func (m string ) { notices = append (notices , m ) })
204+ if want := (dns.Record {Type : "NS" , Value : "ns1.gone.example.net" }); len (records ) != 1 || records [0 ] != want {
205+ t .Errorf ("records %v, want %v from the second name server" , records , want )
206+ }
207+ if len (notices ) != 0 || p .checks .Load () != 1 {
208+ t .Errorf ("notices %q, checks %d; want none and 1" , notices , p .checks .Load ())
209+ }
210+ if n := zone .QueriesFor ("old.example.com" , dnsmessage .TypeNS ); n != 1 {
211+ t .Errorf ("the answering name server saw %d delegation queries, want 1" , n )
212+ }
213+ if p .queries .Load () <= lookups {
214+ t .Errorf ("queries %d after the check, want the delegation queries counted on top of %d" , p .queries .Load (), lookups )
215+ }
216+ }
217+
218+ // TestDelegationUnresolvedNameServers covers #468: when none of the zone's
219+ // name servers resolves, no check is made and the reason is noticed once.
220+ func TestDelegationUnresolvedNameServers (t * testing.T ) {
221+ zone := delegationZone (t , false )
222+ resolver := dnstest .Start (t , func (q dnstest.Query ) dnstest.Reply {
223+ if q .Name == "example.com" && q .Type == dnsmessage .TypeNS {
224+ return dnstest.Reply {NS : []string {"ns1.gone.example.net" }}
225+ }
226+ if q .Name == "example.com" {
227+ return dnstest.Reply {}
228+ }
229+ return dnstest .NXDomain
230+ })
231+ p := & parentZone {cfg : zoneLookupConfig (resolver , zone )}
232+ var notices []string
233+ for _ , name := range []string {"old.example.com" , "old2.example.com" } {
234+ if r := p .delegation (context .Background (), name , func (m string ) { notices = append (notices , m ) }); r != nil {
235+ t .Errorf ("%s: records %v, want none" , name , r )
236+ }
237+ }
238+ if len (notices ) != 1 || ! strings .Contains (notices [0 ], "none of the name servers of example.com could be resolved" ) {
239+ t .Errorf ("notices %q, want one naming the unresolved name servers" , notices )
240+ }
241+ if p .checks .Load () != 0 || zone .Queries () != 0 {
242+ t .Errorf ("checks %d, zone queries %d; want 0 and 0" , p .checks .Load (), zone .Queries ())
243+ }
244+ }
0 commit comments