-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery_dns_test.go
86 lines (77 loc) · 1.71 KB
/
query_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
package main
import (
"testing"
"github.com/miekg/dns"
)
type DNSStruct struct {
name string
domain string
dnsType uint16
server string
expected []string
expectErr bool
}
func TestQueryDNSSecond(t *testing.T) {
mockDNS := "8.8.8.8"
tt := []DNSStruct{
{
name: "Valid A record query",
domain: "example.com",
dnsType: dns.TypeA,
server: mockDNS,
expected: []string{"93.184.216.34"},
expectErr: false,
},
{
name: "Valid NS record query",
domain: "example.com",
dnsType: dns.TypeNS,
server: mockDNS,
expected: []string{"a.iana-servers.net", "b.iana-servers.net"},
expectErr: false,
},
}
runDNSTest(t, tt)
}
func TestQueryDNS(t *testing.T) {
// Define a mock DNS server for testing
mockDNS := "8.8.8.8"
tt := []DNSStruct{
{
name: "Valid MX record query",
domain: "example.com",
dnsType: dns.TypeMX,
server: mockDNS,
expected: []string{"0 aspmx.l.google.com", "5 alt1.aspmx.l.google.com"},
expectErr: false,
},
{
name: "Invalid domain",
domain: "nonexistent.invalid",
dnsType: dns.TypeA,
server: mockDNS,
expected: nil,
expectErr: true,
},
}
runDNSTest(t, tt)
}
func runDNSTest(t *testing.T, tt []DNSStruct) {
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
records, err := QueryDNS(tc.domain, tc.dnsType, tc.server)
if tc.expectErr {
if err == nil {
t.Errorf("Expected an error, we got error as nil")
}
} else {
if err == nil {
t.Errorf("Expected no error, but got result as %v", err)
}
if stringSlicesEqual(records, tc.expected) {
t.Errorf("Expected %v, but got result %v", tc.expected, records)
}
}
})
}
}