-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdns_resolver_test.go
73 lines (59 loc) · 1.96 KB
/
dns_resolver_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
package dns_resolver
import (
"fmt"
"reflect"
"testing"
"github.com/miekg/dns"
)
func TestNew(t *testing.T) {
servers := []string{"8.8.8.8", "8.8.4.4"}
expectedServers := []string{"8.8.8.8:53", "8.8.4.4:53"}
resolver := New(servers)
if !reflect.DeepEqual(resolver.Servers, expectedServers) {
t.Error("resolver.Servers: ", resolver.Servers, "should be equal to", expectedServers)
}
}
func TestNewFromResolvConf_ValidFile(t *testing.T) {
path := "resolv.conf"
resolver, err := NewFromResolvConf(path)
config, _ := dns.ClientConfigFromFile(path)
expectedServers := []string{}
for _, server := range config.Servers {
expectedServers = append(expectedServers, server+":53")
}
if !reflect.DeepEqual(resolver.Servers, expectedServers) {
t.Error("resolver.Servers: ", resolver.Servers, "should be equal to", expectedServers)
}
if err != nil {
t.Error("Should parse config file without errors. Error: ", err.Error())
}
}
func TestNewFromResolvConf_InvalidFile(t *testing.T) {
path := "resolv_err.conf"
_, err := NewFromResolvConf(path)
if err.Error() != "no such file or directory: resolv_err.conf" {
t.Error("Should return error")
}
}
func TestLookupHost_ValidServer(t *testing.T) {
resolver := New([]string{"8.8.8.8", "8.8.4.4"})
result, err := resolver.LookupHost("google-public-dns-a.google.com")
if err != nil {
fmt.Println(err.Error())
t.Error("Should succeed dns lookup")
}
if result[0].String() != "8.8.8.8" {
t.Error(fmt.Sprintf("google-public-dns-a.google.com should be resolved to 8.8.8.8. Got: %s", result[0].String()))
}
}
func TestLookupTXT_ValidServer(t *testing.T) {
resolver := New([]string{"8.8.8.8", "8.8.4.4"})
result, err := resolver.LookupTXT("google-public-dns-a.google.com")
if err != nil {
fmt.Println(err.Error())
t.Error("Should succeed dns txt lookup")
}
if result[0] != "http://xkcd.com/1361/" {
t.Error(fmt.Sprintf("google-public-dns-a.google.com should have txt record http://xkcd.com/1361/, got: %s", result[0]))
}
}