|
| 1 | +package dns |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "sync/atomic" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "golang.org/x/net/dns/dnsmessage" |
| 10 | + |
| 11 | + "github.com/TMHSDigital/subenum/internal/dnstest" |
| 12 | +) |
| 13 | + |
| 14 | +// TestNoDataIsDefinitiveNegative (#67): NOERROR with no records and an SOA |
| 15 | +// (NODATA) is a definitive negative, never retried, and not a failure. |
| 16 | +func TestNoDataIsDefinitiveNegative(t *testing.T) { |
| 17 | + srv := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply { |
| 18 | + if q.Name == "v4only.example.com" && q.Type == dnsmessage.TypeA { |
| 19 | + return dnstest.Reply{A: []string{"192.0.2.1"}} |
| 20 | + } |
| 21 | + if q.Name == "v4only.example.com" { |
| 22 | + return dnstest.Reply{} // NODATA |
| 23 | + } |
| 24 | + return dnstest.NXDomain |
| 25 | + }) |
| 26 | + r := NewResolver(time.Second, srv.Addr) |
| 27 | + recs, outcome := ResolveDomainWithRetry(context.Background(), r, "v4only.example.com", time.Second, nil, 3, []string{"AAAA"}) |
| 28 | + if len(recs) != 0 || outcome != OutcomeNXDomain { |
| 29 | + t.Fatalf("AAAA on an A-only name: records %v, outcome %v; want none, NXDOMAIN", recs, outcome) |
| 30 | + } |
| 31 | + if n := srv.QueriesFor("v4only.example.com", dnsmessage.TypeAAAA); n != 1 { |
| 32 | + t.Fatalf("AAAA queries = %d, want 1 (NODATA must not be retried)", n) |
| 33 | + } |
| 34 | + // With A requested too, the name resolves despite the AAAA NODATA. |
| 35 | + recs, outcome = ResolveDomainWithRetry(context.Background(), r, "v4only.example.com", time.Second, nil, 1, DefaultTypes) |
| 36 | + if outcome != OutcomeFound || len(recs) != 1 || recs[0].Value != "192.0.2.1" { |
| 37 | + t.Fatalf("A+AAAA: records %v, outcome %v", recs, outcome) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// TestResolverAtIPv6Address (#67): -dns-server can be an IPv6 address. |
| 42 | +func TestResolverAtIPv6Address(t *testing.T) { |
| 43 | + srv := dnstest.StartWith(t, dnstest.Options{IPv6: true}, func(q dnstest.Query) dnstest.Reply { |
| 44 | + if q.Name == "www.example.com" && q.Type == dnsmessage.TypeA { |
| 45 | + return dnstest.Reply{A: []string{"192.0.2.7"}} |
| 46 | + } |
| 47 | + return dnstest.Reply{} |
| 48 | + }) |
| 49 | + if srv.Addr[0] != '[' { |
| 50 | + t.Fatalf("server address %q is not an IPv6 host:port", srv.Addr) |
| 51 | + } |
| 52 | + recs, outcome := ResolveDomainWithRetry(context.Background(), NewResolver(time.Second, srv.Addr), "www.example.com", time.Second, nil, 1, []string{"A"}) |
| 53 | + if outcome != OutcomeFound || len(recs) != 1 { |
| 54 | + t.Fatalf("lookup via %s: records %v, outcome %v", srv.Addr, recs, outcome) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// TestWireQueriesPerType (#67) pins the exact queries each record type puts on |
| 59 | +// the wire, which -rate's up-front reservation (queriesPerType) relies on. |
| 60 | +func TestWireQueriesPerType(t *testing.T) { |
| 61 | + srv := dnstest.Start(t, func(dnstest.Query) dnstest.Reply { |
| 62 | + return dnstest.Reply{A: []string{"192.0.2.1"}, AAAA: []string{"2001:db8::1"}} |
| 63 | + }) |
| 64 | + r := NewResolver(time.Second, srv.Addr) |
| 65 | + for _, typ := range []string{"A", "AAAA", "CNAME"} { |
| 66 | + name := "q-" + typ + ".example.com" |
| 67 | + if _, _, err := ResolveTypes(context.Background(), r, name, time.Second, []string{typ}); err != nil { |
| 68 | + t.Fatalf("%s: %v", typ, err) |
| 69 | + } |
| 70 | + got := map[string]int64{ |
| 71 | + "A": srv.QueriesFor(name, dnsmessage.TypeA), |
| 72 | + "AAAA": srv.QueriesFor(name, dnsmessage.TypeAAAA), |
| 73 | + "CNAME": srv.QueriesFor(name, dnsmessage.TypeCNAME), |
| 74 | + } |
| 75 | + total := got["A"] + got["AAAA"] + got["CNAME"] |
| 76 | + if int32(total) != queriesPerType[typ] { |
| 77 | + t.Errorf("-type %s sent %v (%d queries); queriesPerType reserves %d", typ, got, total, queriesPerType[typ]) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// TestTruncationFallsBackToTCP: a truncated UDP answer is retried over TCP, |
| 83 | +// and each transport's query is counted. |
| 84 | +func TestTruncationFallsBackToTCP(t *testing.T) { |
| 85 | + var sawTCP atomic.Bool |
| 86 | + srv := dnstest.Start(t, func(q dnstest.Query) dnstest.Reply { |
| 87 | + if q.TCP { |
| 88 | + sawTCP.Store(true) |
| 89 | + } |
| 90 | + return dnstest.Reply{A: []string{"192.0.2.9"}, Truncate: true} |
| 91 | + }) |
| 92 | + recs, _, err := ResolveTypes(context.Background(), NewResolver(time.Second, srv.Addr), "big.example.com", time.Second, []string{"A"}) |
| 93 | + if err != nil || len(recs) != 1 || !sawTCP.Load() { |
| 94 | + t.Fatalf("records %v, err %v, TCP retry %v", recs, err, sawTCP.Load()) |
| 95 | + } |
| 96 | + if n := srv.QueriesFor("big.example.com", dnsmessage.TypeA); n != 2 { |
| 97 | + t.Fatalf("A queries = %d, want 2 (UDP, then TCP)", n) |
| 98 | + } |
| 99 | +} |
0 commit comments