-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
msgs_test.go
151 lines (143 loc) · 4.55 KB
/
msgs_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright © by Jeff Foley 2021-2024. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0
package resolve
import (
"net"
"strings"
"testing"
"github.com/miekg/dns"
)
func TestAnswersByType(t *testing.T) {
data := []string{"192.168.1.1", "192.168.1.1", "2001:db8:0:1:1:1:1:1"}
m := new(dns.Msg)
m.SetReply(QueryMsg("test.caffix.net", dns.TypeA))
m.Answer = make([]dns.RR, 3)
m.Answer[0] = &dns.A{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassINET}, A: net.ParseIP(data[0])}
m.Answer[1] = &dns.A{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeA, Class: dns.ClassINET}, A: net.ParseIP(data[1])}
m.Answer[2] = &dns.AAAA{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: dns.TypeAAAA, Class: dns.ClassINET}, AAAA: net.ParseIP(data[2])}
ans := AnswersByType(ExtractAnswers(m), dns.TypeA)
if l := len(ans); l != 2 {
t.Errorf("Returned %d answers and expected %d", l, 2)
return
}
if ans[0].Data != data[0] || ans[1].Data != data[1] {
t.Errorf("First answer is %s and second was %s: expected %s and %s", ans[0].Data, ans[1].Data, data[0], data[1])
}
}
func TestExtractAnswers(t *testing.T) {
cases := []struct {
label string
name string
qtype uint16
txt []string
badMsg bool
want string
}{
{
label: "valid TypeA query",
name: "test.caffix.net",
qtype: dns.TypeA,
want: "192.168.1.1",
},
{
label: "valid TypeAAAA query",
name: "test.caffix.net",
qtype: dns.TypeAAAA,
want: "2001:db8:0:1:1:1:1:1",
},
{
label: "valid TypeCNAME query",
name: "test.caffix.net",
qtype: dns.TypeCNAME,
want: "mail.google.com",
},
{
label: "valid TypePTR query",
name: "192.168.1.1",
qtype: dns.TypePTR,
want: "test.caffix.net",
},
{
label: "invalid TypePTR query",
name: "bad.address",
qtype: dns.TypePTR,
badMsg: true,
},
{
label: "valid TypeNS query",
name: "caffix.net",
qtype: dns.TypeNS,
want: "ns.caffix.net",
},
{
label: "valid TypeMX query",
name: "caffix.net",
qtype: dns.TypeMX,
want: "mail.caffix.net",
},
{
label: "valid TypeTXT query",
name: "caffix.net",
qtype: dns.TypeTXT,
txt: []string{"Hello", "World"},
want: "Hello World",
},
{
label: "valid TypeSOA query",
name: "caffix.net",
qtype: dns.TypeSOA,
want: "ns.caffix.net,mail.caffix.net",
},
{
label: "valid TypeSRV query",
name: "caffix.net",
qtype: dns.TypeSRV,
want: "srv.google.com",
},
}
for _, c := range cases {
m := new(dns.Msg)
if c.qtype == dns.TypePTR {
resp := ReverseMsg(c.name)
if c.badMsg {
if resp != nil {
t.Errorf("ReverseMsg should have returned nil when provided a bad address: %s", c.name)
}
continue
}
m.SetReply(resp)
} else {
m.SetReply(QueryMsg(c.name, c.qtype))
}
m.Answer = make([]dns.RR, 1)
switch c.qtype {
case dns.TypeA:
m.Answer[0] = &dns.A{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: c.qtype, Class: dns.ClassINET}, A: net.ParseIP(c.want)}
case dns.TypeAAAA:
m.Answer[0] = &dns.AAAA{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: c.qtype, Class: dns.ClassINET}, AAAA: net.ParseIP(c.want)}
case dns.TypeCNAME:
m.Answer[0] = &dns.CNAME{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: c.qtype, Class: dns.ClassINET}, Target: c.want}
case dns.TypePTR:
m.Answer[0] = &dns.PTR{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: c.qtype, Class: dns.ClassINET}, Ptr: c.want}
case dns.TypeNS:
m.Answer[0] = &dns.NS{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: c.qtype, Class: dns.ClassINET}, Ns: c.want}
case dns.TypeMX:
m.Answer[0] = &dns.MX{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: c.qtype, Class: dns.ClassINET}, Mx: c.want}
case dns.TypeTXT:
m.Answer[0] = &dns.TXT{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: c.qtype, Class: dns.ClassINET}, Txt: c.txt}
case dns.TypeSOA:
parts := strings.Split(c.want, ",")
m.Answer[0] = &dns.SOA{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: c.qtype, Class: dns.ClassINET}, Ns: parts[0], Mbox: parts[1]}
case dns.TypeSRV:
m.Answer[0] = &dns.SRV{Hdr: dns.RR_Header{Name: m.Question[0].Name, Rrtype: c.qtype, Class: dns.ClassINET}, Target: c.want}
}
if ans := ExtractAnswers(m); len(ans) > 0 {
if d := ans[0].Data; d != c.want {
t.Errorf("Extracted answer for %d was %s and %s was expected", c.qtype, d, c.want)
}
} else {
t.Errorf("Extracted answer for %d was empty", c.qtype)
}
}
}