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