|
| 1 | +package dnsserver |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/miekg/dns" |
| 10 | +) |
| 11 | + |
| 12 | +func TestIterativeDSUsesParentAuthority(t *testing.T) { |
| 13 | + t.Parallel() |
| 14 | + for _, cached := range []bool{false, true} { |
| 15 | + for _, name := range []string{"com.", "example.com."} { |
| 16 | + t.Run(fmt.Sprintf("%s/cached=%t", name, cached), func(t *testing.T) { |
| 17 | + runtime := recursiveTestRuntime(t) |
| 18 | + if cached { |
| 19 | + runtime.delegations.set("com", []string{"192.0.2.2:53"}, 300, time.Now()) |
| 20 | + runtime.delegations.set("example.com", []string{"192.0.2.3:53"}, 300, time.Now()) |
| 21 | + } |
| 22 | + handler := NewHandler(runtime) |
| 23 | + parent := "udp://192.0.2.1:53" |
| 24 | + if name == "example.com." { |
| 25 | + parent = "udp://192.0.2.2:53" |
| 26 | + } |
| 27 | + handler.upstreamExchange = func(_ context.Context, request *dns.Msg, endpoint string, _ time.Duration) (*dns.Msg, error) { |
| 28 | + question := request.Question[0] |
| 29 | + if question.Qtype == dns.TypeNS && question.Name == "com." && endpoint == "udp://192.0.2.1:53" { |
| 30 | + return referralResponse(request, "com.", "a.gtld-servers.net.", "192.0.2.2"), nil |
| 31 | + } |
| 32 | + if question.Qtype != dns.TypeDS || question.Name != name || endpoint != parent { |
| 33 | + return nil, fmt.Errorf("unexpected %s/%s at %s; DS must use %s", question.Name, dns.TypeToString[question.Qtype], endpoint, parent) |
| 34 | + } |
| 35 | + response := new(dns.Msg) |
| 36 | + response.SetReply(request) |
| 37 | + response.Authoritative = true |
| 38 | + record, err := dns.NewRR(name + " 300 IN DS 12345 13 2 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF") |
| 39 | + if err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + response.Answer = []dns.RR{record} |
| 43 | + return response, nil |
| 44 | + } |
| 45 | + request := new(dns.Msg) |
| 46 | + request.SetQuestion(name, dns.TypeDS) |
| 47 | + response, err := handler.resolveNetwork(request, runtime, nil) |
| 48 | + if err != nil { |
| 49 | + t.Fatal(err) |
| 50 | + } |
| 51 | + if len(response.Answer) != 1 || response.Answer[0].Header().Rrtype != dns.TypeDS { |
| 52 | + t.Fatalf("DS answer = %v", response) |
| 53 | + } |
| 54 | + }) |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestIterativeFailoverPreservesQueryBudget(t *testing.T) { |
| 60 | + t.Parallel() |
| 61 | + runtime := recursiveTestRuntime(t) |
| 62 | + runtime.timeout = 2 * time.Second |
| 63 | + runtime.retryTimeout = 1500 * time.Millisecond |
| 64 | + runtime.retries = 2 |
| 65 | + handler := NewHandler(runtime) |
| 66 | + healthyTried := false |
| 67 | + handler.upstreamExchange = func(ctx context.Context, request *dns.Msg, endpoint string, _ time.Duration) (*dns.Msg, error) { |
| 68 | + if endpoint == "udp://192.0.2.1:53" { |
| 69 | + <-ctx.Done() |
| 70 | + return nil, ctx.Err() |
| 71 | + } |
| 72 | + healthyTried = true |
| 73 | + return addressResponse(request, "192.0.2.44"), nil |
| 74 | + } |
| 75 | + ctx, cancel := context.WithTimeout(context.Background(), runtime.timeout) |
| 76 | + defer cancel() |
| 77 | + response, err := handler.exchangeIterative(ctx, iterativeQuery("www.example.com.", dns.TypeA), []string{"192.0.2.1:53", "192.0.2.2:53"}, runtime, &iterativeBudget{remaining: maximumIterativeQueries}) |
| 78 | + if err != nil || !healthyTried { |
| 79 | + t.Fatalf("healthy server tried=%t; resolution error=%v", healthyTried, err) |
| 80 | + } |
| 81 | + if len(response.Answer) != 1 { |
| 82 | + t.Fatalf("answer = %v", response) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestIterativeDNSSECValidatesWithCachedChildDelegations(t *testing.T) { |
| 87 | + t.Parallel() |
| 88 | + now := time.Date(2026, 8, 10, 12, 0, 0, 0, time.UTC) |
| 89 | + root := newValidatorTestKey(t, ".") |
| 90 | + parent := newValidatorTestKey(t, "demo.") |
| 91 | + child := newValidatorTestKey(t, "secure.demo.") |
| 92 | + runtime := recursiveTestRuntime(t) |
| 93 | + runtime.dnssec = validatorWithAnchor(t, root, now) |
| 94 | + runtime.delegations.set("demo", []string{"192.0.2.2:53"}, 300, time.Now()) |
| 95 | + runtime.delegations.set("secure.demo", []string{"192.0.2.3:53"}, 300, time.Now()) |
| 96 | + responses := validatorChainQueries(t, now, root, parent, child) |
| 97 | + responses[validatorQueryKey("www.secure.demo.", dns.TypeA)] = validatorSignedResponse(t, now, child, "www.secure.demo.", dns.TypeA, "192.0.2.44") |
| 98 | + handler := NewHandler(runtime) |
| 99 | + handler.upstreamExchange = func(_ context.Context, request *dns.Msg, endpoint string, _ time.Duration) (*dns.Msg, error) { |
| 100 | + question := request.Question[0] |
| 101 | + authority := question.Name |
| 102 | + if question.Qtype == dns.TypeDS { |
| 103 | + authority = parentFQDN(authority) |
| 104 | + } |
| 105 | + expected := "udp://192.0.2.1:53" |
| 106 | + if dns.IsSubDomain("secure.demo.", authority) { |
| 107 | + expected = "udp://192.0.2.3:53" |
| 108 | + } else if dns.IsSubDomain("demo.", authority) { |
| 109 | + expected = "udp://192.0.2.2:53" |
| 110 | + } |
| 111 | + if endpoint != expected { |
| 112 | + return nil, fmt.Errorf("%s/%s sent to %s, want %s", question.Name, dns.TypeToString[question.Qtype], endpoint, expected) |
| 113 | + } |
| 114 | + response := responses[validatorQueryKey(question.Name, question.Qtype)] |
| 115 | + if response == nil { |
| 116 | + return nil, fmt.Errorf("unexpected query %v", question) |
| 117 | + } |
| 118 | + return response.Copy(), nil |
| 119 | + } |
| 120 | + request := new(dns.Msg) |
| 121 | + request.SetQuestion("www.secure.demo.", dns.TypeA) |
| 122 | + response, state, err := handler.resolveUpstream(request, runtime, nil) |
| 123 | + if err != nil || state != validationSecure { |
| 124 | + t.Fatalf("resolution state=%v error=%v", state, err) |
| 125 | + } |
| 126 | + if len(response.Answer) == 0 { |
| 127 | + t.Fatal("missing answer") |
| 128 | + } |
| 129 | +} |
0 commit comments