Debian Codegraph @

edns_test.go

raw symbols SCIP
1 package dns
2
3 import (
4 "bytes"
5 "net"
6 "testing"
7 )
8
9 func TestOPTTtl(t *testing.T) {
10 e := &OPT{}
11 e.Hdr.Name = "."
12 e.Hdr.Rrtype = TypeOPT
13
14 // verify the default setting of DO=0
15 if e.Do() {
16 t.Errorf("DO bit should be zero")
17 }
18
19 // There are 6 possible invocations of SetDo():
20 //
21 // 1. Starting with DO=0, using SetDo()
22 // 2. Starting with DO=0, using SetDo(true)
23 // 3. Starting with DO=0, using SetDo(false)
24 // 4. Starting with DO=1, using SetDo()
25 // 5. Starting with DO=1, using SetDo(true)
26 // 6. Starting with DO=1, using SetDo(false)
27
28 // verify that invoking SetDo() sets DO=1 (TEST #1)
29 e.SetDo()
30 if !e.Do() {
31 t.Errorf("DO bit should be non-zero")
32 }
33 // verify that using SetDo(true) works when DO=1 (TEST #5)
34 e.SetDo(true)
35 if !e.Do() {
36 t.Errorf("DO bit should still be non-zero")
37 }
38 // verify that we can use SetDo(false) to set DO=0 (TEST #6)
39 e.SetDo(false)
40 if e.Do() {
41 t.Errorf("DO bit should be zero")
42 }
43 // verify that if we call SetDo(false) when DO=0 that it is unchanged (TEST #3)
44 e.SetDo(false)
45 if e.Do() {
46 t.Errorf("DO bit should still be zero")
47 }
48 // verify that using SetDo(true) works for DO=0 (TEST #2)
49 e.SetDo(true)
50 if !e.Do() {
51 t.Errorf("DO bit should be non-zero")
52 }
53 // verify that using SetDo() works for DO=1 (TEST #4)
54 e.SetDo()
55 if !e.Do() {
56 t.Errorf("DO bit should be non-zero")
57 }
58
59 // CO (Compact ANswers OK) flag tests follow the same pattern as DO tests
60 // verify that invoking SetCo() sets CO=1
61 e.SetCo()
62 if !e.Co() {
63 t.Errorf("CO bit should be non-zero")
64 }
65
66 // verify that using SetCo(true) works when CO=1
67 e.SetCo(true)
68 if !e.Co() {
69 t.Errorf("CO bit should still be non-zero")
70 }
71 // verify that we can use SetCo(false) to set CO=0
72 e.SetCo(false)
73 if e.Co() {
74 t.Errorf("CO bit should be zero")
75 }
76 // verify that if we call SetCo(false) when CO=0 that it is unchanged
77 e.SetCo(false)
78 if e.Co() {
79 t.Errorf("CO bit should still be zero")
80 }
81 // verify that using SetCo(true) works for CO=0
82 e.SetCo(true)
83 if !e.Co() {
84 t.Errorf("CO bit should be non-zero")
85 }
86 // verify that using SetCo() works for CO=1
87 e.SetCo()
88 if !e.Co() {
89 t.Errorf("CO bit should be non-zero")
90 }
91
92 if e.Version() != 0 {
93 t.Errorf("version should be non-zero")
94 }
95
96 e.SetVersion(42)
97 if e.Version() != 42 {
98 t.Errorf("set 42, expected %d, got %d", 42, e.Version())
99 }
100
101 e.SetExtendedRcode(42)
102 // ExtendedRcode has the last 4 bits set to 0.
103 if e.ExtendedRcode() != 42&0xFFFFFFF0 {
104 t.Errorf("set 42, expected %d, got %d", 42&0xFFFFFFF0, e.ExtendedRcode())
105 }
106
107 // This will reset the 8 upper bits of the extended rcode
108 e.SetExtendedRcode(RcodeNotAuth)
109 if e.ExtendedRcode() != 0 {
110 t.Errorf("Setting a non-extended rcode is expected to set extended rcode to 0, got: %d", e.ExtendedRcode())
111 }
112 }
113
114 func TestEDNS0_SUBNETUnpack(t *testing.T) {
115 for _, ip := range []net.IP{
116 net.IPv4(0xde, 0xad, 0xbe, 0xef),
117 net.ParseIP("192.0.2.1"),
118 net.ParseIP("2001:db8::68"),
119 } {
120 var s1 EDNS0_SUBNET
121 s1.Address = ip
122
123 if ip.To4() == nil {
124 s1.Family = 2
125 s1.SourceNetmask = net.IPv6len * 8
126 } else {
127 s1.Family = 1
128 s1.SourceNetmask = net.IPv4len * 8
129 }
130
131 b, err := s1.pack()
132 if err != nil {
133 t.Fatalf("failed to pack: %v", err)
134 }
135
136 var s2 EDNS0_SUBNET
137 if err := s2.unpack(b); err != nil {
138 t.Fatalf("failed to unpack: %v", err)
139 }
140
141 if !ip.Equal(s2.Address) {
142 t.Errorf("address different after unpacking; expected %s, got %s", ip, s2.Address)
143 }
144 }
145 }
146
147 func TestEDNS0_UL(t *testing.T) {
148 cases := []struct {
149 l uint32
150 kl uint32
151 }{
152 {0x01234567, 0},
153 {0x76543210, 0xFEDCBA98},
154 }
155 for _, c := range cases {
156 expect := EDNS0_UL{EDNS0UL, c.l, c.kl}
157 b, err := expect.pack()
158 if err != nil {
159 t.Fatalf("failed to pack: %v", err)
160 }
161 actual := EDNS0_UL{EDNS0UL, ^uint32(0), ^uint32(0)}
162 if err := actual.unpack(b); err != nil {
163 t.Fatalf("failed to unpack: %v", err)
164 }
165 if expect != actual {
166 t.Errorf("unpacked option is different; expected %v, got %v", expect, actual)
167 }
168 }
169 }
170
171 func TestZ(t *testing.T) {
172 e := &OPT{}
173 e.Hdr.Name = "."
174 e.Hdr.Rrtype = TypeOPT
175 e.SetVersion(8)
176 e.SetDo()
177 e.SetCo()
178 if e.Z() != 0 {
179 t.Errorf("expected Z of 0, got %d", e.Z())
180 }
181 e.SetZ(5)
182 if e.Z() != 5 {
183 t.Errorf("expected Z of 5, got %d", e.Z())
184 }
185 e.SetZ(0xFFFF)
186 if e.Z() != 0x3FFF {
187 t.Errorf("expected Z of 0x3FFFF, got %d", e.Z())
188 }
189 if e.Version() != 8 {
190 t.Errorf("expected version to still be 8, got %d", e.Version())
191 }
192 if !e.Do() {
193 t.Error("expected DO to be set")
194 }
195 }
196
197 func TestEDNS0_ESU(t *testing.T) {
198 p := []byte{
199 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
200 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x29, 0x04,
201 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x28, 0x00,
202 0x04, 0x00, 0x24, 0x73, 0x69, 0x70, 0x3A, 0x2B,
203 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
204 0x39, 0x40, 0x74, 0x65, 0x73, 0x74, 0x2E, 0x63,
205 0x6F, 0x6D, 0x3B, 0x75, 0x73, 0x65, 0x72, 0x3D,
206 0x63, 0x67, 0x72, 0x61, 0x74, 0x65, 0x73,
207 }
208
209 m := new(Msg)
210 if err := m.Unpack(p); err != nil {
211 t.Fatalf("failed to unpack: %v", err)
212 }
213 opt := m.IsEdns0()
214 if opt == nil {
215 t.Fatalf("expected edns0 option")
216 }
217 if len(opt.Option) != 1 {
218 t.Fatalf("expected only one option: %v", opt.Option)
219 }
220 edns0 := opt.Option[0]
221 esu, ok := edns0.(*EDNS0_ESU)
222 if !ok {
223 t.Fatalf("expected option of type EDNS0_ESU, got %t", edns0)
224 }
225 expect := "sip:[email protected];user=cgrates"
226 if esu.Uri != expect {
227 t.Errorf("unpacked option is different; expected %v, got %v", expect, esu.Uri)
228 }
229 }
230
231 func TestEDNS0_TCP_KEEPALIVE_unpack(t *testing.T) {
232 cases := []struct {
233 name string
234 b []byte
235 expected uint16
236 expectedErr bool
237 }{
238 {
239 name: "empty",
240 b: []byte{},
241 expected: 0,
242 },
243 {
244 name: "timeout 1",
245 b: []byte{0, 1},
246 expected: 1,
247 },
248 {
249 name: "invalid",
250 b: []byte{0, 1, 3},
251 expectedErr: true,
252 },
253 }
254
255 for _, tc := range cases {
256 t.Run(tc.name, func(t *testing.T) {
257 e := &EDNS0_TCP_KEEPALIVE{}
258 err := e.unpack(tc.b)
259 if err != nil && !tc.expectedErr {
260 t.Error("failed to unpack, expected no error")
261 }
262 if err == nil && tc.expectedErr {
263 t.Error("unpacked, but expected an error")
264 }
265 if e.Timeout != tc.expected {
266 t.Errorf("invalid timeout, actual: %d, expected: %d", e.Timeout, tc.expected)
267 }
268 })
269 }
270 }
271
272 func TestEDNS0_TCP_KEEPALIVE_pack(t *testing.T) {
273 cases := []struct {
274 name string
275 edns *EDNS0_TCP_KEEPALIVE
276 expected []byte
277 }{
278 {
279 name: "empty",
280 edns: &EDNS0_TCP_KEEPALIVE{
281 Code: EDNS0TCPKEEPALIVE,
282 Timeout: 0,
283 },
284 expected: nil,
285 },
286 {
287 name: "timeout 1",
288 edns: &EDNS0_TCP_KEEPALIVE{
289 Code: EDNS0TCPKEEPALIVE,
290 Timeout: 1,
291 },
292 expected: []byte{0, 1},
293 },
294 }
295
296 for _, tc := range cases {
297 t.Run(tc.name, func(t *testing.T) {
298 b, err := tc.edns.pack()
299 if err != nil {
300 t.Error("expected no error")
301 }
302
303 if tc.expected == nil && b != nil {
304 t.Errorf("invalid result, expected nil")
305 }
306
307 res := bytes.Compare(b, tc.expected)
308 if res != 0 {
309 t.Errorf("invalid result, expected: %v, actual: %v", tc.expected, b)
310 }
311 })
312 }
313 }