blob: a9297daf09d269844ce8a7a167c0bb4708933c99 [file] [log] [blame]
Jan Schär75ea9f42024-07-29 17:01:41 +02001package proxy
2
3// Taken and modified from CoreDNS, under Apache 2.0.
4
5import (
6 "errors"
7 "fmt"
8 "math"
9 "testing"
10 "time"
11
12 "github.com/miekg/dns"
13
14 "source.monogon.dev/osbase/net/dns/test"
15)
16
17func TestProxy(t *testing.T) {
18 s := test.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
19 ret := new(dns.Msg)
20 ret.SetReply(r)
21 ret.Answer = append(ret.Answer, test.RR("example.org. IN A 127.0.0.1"))
22 w.WriteMsg(ret)
23 })
24 defer s.Close()
25
26 p := NewProxy(s.Addr)
27 p.readTimeout = 10 * time.Millisecond
28 p.Start(5 * time.Second)
29 m := new(dns.Msg)
30
31 m.SetQuestion("example.org.", dns.TypeA)
32
33 resp, err := p.Connect(m, false)
34 if err != nil {
35 t.Errorf("Failed to connect to testdnsserver: %s", err)
36 }
37
38 if x := resp.Answer[0].Header().Name; x != "example.org." {
39 t.Errorf("Expected %s, got %s", "example.org.", x)
40 }
41}
42
43func TestProtocolSelection(t *testing.T) {
44 p := NewProxy("bad_address")
45 p.readTimeout = 10 * time.Millisecond
46
47 go func() {
48 p.Connect(new(dns.Msg), false)
49 p.Connect(new(dns.Msg), true)
50 }()
51
52 for i, exp := range []string{"udp", "tcp"} {
53 proto := <-p.transport.dial
54 p.transport.ret <- nil
55 if proto != exp {
56 t.Errorf("Unexpected protocol in case %d, expected %q, actual %q", i, exp, proto)
57 }
58 }
59}
60
61func TestProxyIncrementFails(t *testing.T) {
62 var testCases = []struct {
63 name string
64 fails uint32
65 expectFails uint32
66 }{
67 {
68 name: "increment fails counter overflows",
69 fails: math.MaxUint32,
70 expectFails: math.MaxUint32,
71 },
72 {
73 name: "increment fails counter",
74 fails: 0,
75 expectFails: 1,
76 },
77 }
78
79 for _, tc := range testCases {
80 t.Run(tc.name, func(t *testing.T) {
81 p := NewProxy("bad_address")
82 p.fails = tc.fails
83 p.incrementFails()
84 if p.fails != tc.expectFails {
85 t.Errorf("Expected fails to be %d, got %d", tc.expectFails, p.fails)
86 }
87 })
88 }
89}
90
91func TestCoreDNSOverflow(t *testing.T) {
92 s := test.NewServer(func(w dns.ResponseWriter, r *dns.Msg) {
93 ret := new(dns.Msg)
94 ret.SetReply(r)
95
96 var answers []dns.RR
97 for i := range 50 {
98 answers = append(answers, test.RR(fmt.Sprintf("example.org. IN A 127.0.0.%v", i)))
99 }
100 ret.Answer = answers
101 w.WriteMsg(ret)
102 })
103 defer s.Close()
104
105 p := NewProxy(s.Addr)
106 p.readTimeout = 10 * time.Millisecond
107 p.Start(5 * time.Second)
108 defer p.Stop()
109
110 // Test different connection modes
111 testConnection := func(proto string, useTCP bool, expectTruncated bool) {
112 t.Helper()
113
114 queryMsg := new(dns.Msg)
115 queryMsg.SetQuestion("example.org.", dns.TypeA)
116
117 response, err := p.Connect(queryMsg, useTCP)
118 if err != nil {
119 t.Errorf("Failed to connect to testdnsserver: %s", err)
120 }
121
122 if response.Truncated != expectTruncated {
123 t.Errorf("Expected truncated response for %s, but got TC flag %v", proto, response.Truncated)
124 }
125 }
126
127 // Test udp, expect truncated response
128 testConnection("udp", false, true)
129
130 // Test tcp, expect no truncated response
131 testConnection("tcp", true, false)
132}
133
134func TestShouldTruncateResponse(t *testing.T) {
135 testCases := []struct {
136 testname string
137 err error
138 expected bool
139 }{
140 {"BadAlgorithm", dns.ErrAlg, false},
141 {"BufferSizeTooSmall", dns.ErrBuf, true},
142 {"OverflowUnpackingA", errors.New("overflow unpacking a"), true},
143 {"OverflowingHeaderSize", errors.New("overflowing header size"), true},
144 {"OverflowpackingA", errors.New("overflow packing a"), true},
145 {"ErrSig", dns.ErrSig, false},
146 }
147
148 for _, tc := range testCases {
149 t.Run(tc.testname, func(t *testing.T) {
150 result := shouldTruncateResponse(tc.err)
151 if result != tc.expected {
152 t.Errorf("For testname '%v', expected %v but got %v", tc.testname, tc.expected, result)
153 }
154 })
155 }
156}