-
Notifications
You must be signed in to change notification settings - Fork 6
/
dns_test.go
119 lines (104 loc) · 3.08 KB
/
dns_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
package spf
import "testing"
type spfTestpair struct {
query []string
expected bool
}
type SPFTestCase struct {
Host string
Txt []string
}
//TestSPFLookup ensures a TXT records are properly queried and reurned to the called. Function should also work with
// multiple TXT records for a given host.
/*
func TestSPFLookup(t *testing.T) {
testcases := []SPFTestCase{
SPFTestCase{"multi.spf.matching.com", []string{"v=spf1 ip6:2001:db8:a0b:12f0::1 -all", "v=spf1 mx -all"}},
SPFTestCase{"1.spf.matching.com", []string{"v=spf1 a mx -all"}},
SPFTestCase{"2.spf.matching.com", []string{"v=spf1 ip4:172.100.100.100 -all"}},
SPFTestCase{"3.spf.matching.com", []string{"v=spf1 ip4:172.100.100.1/24 ?all"}},
}
for _, testcase := range testcases {
lookup, err := LookupSPF(testcase.Host)
// There is no guarantee in which order TXT records will be returned for a given host, so we need to sort here
// in order to ensure the expected ordering will be provided (expected is sorted here)
sort.Strings(lookup)
if err != nil {
t.Error("Caught error: ", err)
} else if reflect.DeepEqual(testcase.Txt, lookup) == false {
t.Error("Host: ", testcase.Host, " expected: ", testcase.Txt, " got: ", lookup)
}
}
}
func TestSPFLookupNegative(t *testing.T) {
testcase := SPFTestCase{"incorrect.spf.matching.com", nil}
spfPrefix := "Invalid SPF record:"
_, err := LookupSPF(testcase.Host)
if strings.HasPrefix(err.Error(), spfPrefix) == false {
t.Error("Expected error to start with: ", spfPrefix, " got: ", err.Error(), " instead.")
}
}
func TestHandleNoSuchHostDNSError(t *testing.T) {
host := "idontexist.matching.com"
_, err := LookupSPF(host)
switch err.(type) {
case *net.DNSError:
break
default:
t.Errorf("Expected 'net.DNSError' error type, instead got: %T\n", err)
}
}
*/
// DNS domain name validation.
// This source code is copied from:
//https://github.com/golang/go/blob/master/src/net/dnsclient_test.go
type dnsNameTest struct {
name string
result bool
}
var dnsNameTests = []dnsNameTest{
// RFC 2181, section 11.
{"_xmpp-server._tcp.google.com", true},
{"foo.com", true},
{"1foo.com", true},
{"26.0.0.73.com", true},
{"fo-o.com", true},
{"fo1o.com", true},
{"foo1.com", true},
{"a.b..com", false},
{"a.b-.com", false},
{"a.b.com-", false},
{"a.b..", false},
{"b.com.", true},
}
func emitDNSNameTest(ch chan<- dnsNameTest) {
defer close(ch)
var char59, char63, char64 string
for i := 0; i < 59; i++ {
char59 += "a"
}
char63 = char59 + "aaaa"
char64 = char63 + "a"
for _, tc := range dnsNameTests {
ch <- tc
}
ch <- dnsNameTest{char63 + ".com", true}
ch <- dnsNameTest{char64 + ".com", false}
// 255 char name is fine:
ch <- dnsNameTest{char59 + "." + char63 + "." + char63 + "." +
char63 + ".com",
true}
// 256 char name is bad:
ch <- dnsNameTest{char59 + "a." + char63 + "." + char63 + "." +
char63 + ".com",
false}
}
func TestDNSName(t *testing.T) {
ch := make(chan dnsNameTest)
go emitDNSNameTest(ch)
for tc := range ch {
if isDomainName(tc.name) != tc.result {
t.Errorf("IsDomainName(%q) = %v; want %v", tc.name, !tc.result, tc.result)
}
}
}