|
| 1 | +package dns |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/miekg/dns" |
| 8 | +) |
| 9 | + |
| 10 | +// capture is a minimal dns.ResponseWriter that records the written message. |
| 11 | +type capture struct { |
| 12 | + msg *dns.Msg |
| 13 | +} |
| 14 | + |
| 15 | +func (c *capture) LocalAddr() net.Addr { return &net.TCPAddr{} } // nolint:exhaustruct |
| 16 | +func (c *capture) RemoteAddr() net.Addr { return &net.TCPAddr{} } // nolint:exhaustruct |
| 17 | +func (c *capture) WriteMsg(m *dns.Msg) error { c.msg = m; return nil } |
| 18 | +func (c *capture) Write(b []byte) (int, error) { |
| 19 | + return len(b), nil |
| 20 | +} |
| 21 | +func (c *capture) Close() error { return nil } |
| 22 | +func (c *capture) TsigStatus() error { return nil } |
| 23 | +func (c *capture) TsigTimersOnly(bool) {} |
| 24 | +func (c *capture) Hijack() {} |
| 25 | + |
| 26 | +// startUpstream runs a UDP DNS server answering every A query with answer and |
| 27 | +// returns its address. dns.Exchange forwards over UDP, so UDP is enough. |
| 28 | +func startUpstream(t *testing.T, answer string) string { |
| 29 | + t.Helper() |
| 30 | + conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)}) // nolint:exhaustruct |
| 31 | + if err != nil { |
| 32 | + t.Fatalf("listen udp: %v", err) |
| 33 | + } |
| 34 | + handler := dns.HandlerFunc(func(w dns.ResponseWriter, r *dns.Msg) { |
| 35 | + m := new(dns.Msg) |
| 36 | + m.SetReply(r) |
| 37 | + for _, q := range r.Question { |
| 38 | + if q.Qtype != dns.TypeA { |
| 39 | + continue |
| 40 | + } |
| 41 | + rr, err := dns.NewRR(q.Name + " 60 IN A " + answer) |
| 42 | + if err != nil { |
| 43 | + t.Errorf("build rr: %v", err) |
| 44 | + continue |
| 45 | + } |
| 46 | + m.Answer = append(m.Answer, rr) |
| 47 | + } |
| 48 | + _ = w.WriteMsg(m) |
| 49 | + }) |
| 50 | + server := &dns.Server{PacketConn: conn, Net: "udp", Handler: handler} // nolint:exhaustruct |
| 51 | + go func() { _ = server.ActivateAndServe() }() |
| 52 | + t.Cleanup(func() { _ = server.Shutdown() }) |
| 53 | + return conn.LocalAddr().String() |
| 54 | +} |
| 55 | + |
| 56 | +// deadAddr returns a UDP address with nothing listening on it. |
| 57 | +func deadAddr(t *testing.T) string { |
| 58 | + t.Helper() |
| 59 | + conn, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1)}) // nolint:exhaustruct |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("listen udp: %v", err) |
| 62 | + } |
| 63 | + addr := conn.LocalAddr().String() |
| 64 | + _ = conn.Close() |
| 65 | + return addr |
| 66 | +} |
| 67 | + |
| 68 | +func query(name string) *dns.Msg { |
| 69 | + m := new(dns.Msg) |
| 70 | + m.SetQuestion(dns.Fqdn(name), dns.TypeA) |
| 71 | + return m |
| 72 | +} |
| 73 | + |
| 74 | +func TestForwardSuccess(t *testing.T) { |
| 75 | + upstream := startUpstream(t, "1.2.3.4") |
| 76 | + w := &capture{} // nolint:exhaustruct |
| 77 | + newHandler([]string{upstream})(w, query("example.com")) |
| 78 | + |
| 79 | + if w.msg == nil { |
| 80 | + t.Fatal("no response written") |
| 81 | + } |
| 82 | + if w.msg.Rcode != dns.RcodeSuccess { |
| 83 | + t.Fatalf("rcode = %v, want success", w.msg.Rcode) |
| 84 | + } |
| 85 | + if len(w.msg.Answer) != 1 { |
| 86 | + t.Fatalf("answers = %d, want 1", len(w.msg.Answer)) |
| 87 | + } |
| 88 | + a, ok := w.msg.Answer[0].(*dns.A) |
| 89 | + if !ok || a.A.String() != "1.2.3.4" { |
| 90 | + t.Fatalf("answer = %v, want 1.2.3.4", w.msg.Answer[0]) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestForwardFailover(t *testing.T) { |
| 95 | + upstream := startUpstream(t, "5.6.7.8") |
| 96 | + w := &capture{} // nolint:exhaustruct |
| 97 | + newHandler([]string{deadAddr(t), upstream})(w, query("example.com")) |
| 98 | + |
| 99 | + if w.msg == nil || w.msg.Rcode != dns.RcodeSuccess { |
| 100 | + t.Fatalf("expected success via second upstream, got %v", w.msg) |
| 101 | + } |
| 102 | + a, ok := w.msg.Answer[0].(*dns.A) |
| 103 | + if !ok || a.A.String() != "5.6.7.8" { |
| 104 | + t.Fatalf("answer = %v, want 5.6.7.8", w.msg.Answer) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +func TestForwardAllDead(t *testing.T) { |
| 109 | + w := &capture{} // nolint:exhaustruct |
| 110 | + newHandler([]string{deadAddr(t), deadAddr(t)})(w, query("example.com")) |
| 111 | + |
| 112 | + if w.msg == nil { |
| 113 | + t.Fatal("no response written") |
| 114 | + } |
| 115 | + if w.msg.Rcode != dns.RcodeServerFailure { |
| 116 | + t.Fatalf("rcode = %v, want SERVFAIL", w.msg.Rcode) |
| 117 | + } |
| 118 | +} |
0 commit comments