-
Notifications
You must be signed in to change notification settings - Fork 10
/
resolver_test.go
117 lines (107 loc) · 3.11 KB
/
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
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
package mockdns
import (
"context"
"net"
"reflect"
"sort"
"testing"
)
func TestResolver_LookupHost(t *testing.T) {
r := Resolver{Zones: map[string]Zone{
"example.org.": Zone{
A: []string{"1.2.3.4"},
AAAA: []string{"::1"},
},
"example.net.": Zone{},
"aaa.example.org.": Zone{
CNAME: "example.org.",
},
}}
// Existing zone with A and AAAA.
addrs, err := r.LookupHost(context.Background(), "example.org")
if err != nil {
t.Fatal(err)
}
sort.Strings(addrs)
want := []string{"1.2.3.4", "::1"}
if !reflect.DeepEqual(addrs, want) {
t.Errorf("Wrong result, want %v, got %v", want, addrs)
}
// Existing zone without A or AAAA.
addrs, err = r.LookupHost(context.Background(), "example.net")
if err == nil {
t.Fatal("Expected error, got nil")
}
dnsErr, ok := err.(*net.DNSError)
if !ok {
t.Fatalf("err is not *net.DNSError, but %T", err)
}
if !isNotFound(dnsErr) {
t.Fatalf("err.IsNotFound is false, should be true")
}
// Non-existing zone.
_, err = r.LookupHost(context.Background(), "example.com")
if err == nil {
t.Fatal("Expected error, got nil")
}
dnsErr, ok = err.(*net.DNSError)
if !ok {
t.Fatalf("err is not *net.DNSError, but %T", err)
}
if !isNotFound(dnsErr) {
t.Fatalf("err.IsNotFound is false, should be true")
}
// Existing zone CNAME pointing to a zone with with A and AAAA.
addrs, err = r.LookupHost(context.Background(), "aaa.example.org")
if err != nil {
t.Fatal(err)
}
sort.Strings(addrs)
if !reflect.DeepEqual(addrs, want) {
t.Errorf("Wrong result, want %v, got %v", want, addrs)
}
}
func TestResolver_LookupIP(t *testing.T) {
r := &Resolver{Zones: map[string]Zone{
"example.com.": {
A: []string{"192.168.0.1", "10.0.0.1"},
AAAA: []string{"2001:db8::1", "2001:db8::2"},
},
"example.org.": {
MX: []net.MX{
{Host: "example.com.", Pref: 10},
},
},
}}
cases := []struct {
name string
network string
host string
want []string
wantErr bool
}{
{name: "IPv4 Only", network: "ip4", host: "example.com", want: []string{"192.168.0.1", "10.0.0.1"}, wantErr: false},
{name: "IPv6 Only", network: "ip6", host: "example.com", want: []string{"2001:db8::1", "2001:db8::2"}, wantErr: false},
{name: "IPv4&IPv6", network: "ip", host: "example.com", want: []string{"192.168.0.1", "10.0.0.1", "2001:db8::1", "2001:db8::2"}, wantErr: false},
{name: "IPv4 Not Found", network: "ip4", host: "example.org", want: []string{}, wantErr: true},
{name: "IPv6 Not Found", network: "ip6", host: "example.org", want: []string{}, wantErr: true},
{name: "IPv4&IPv6 Not Found", network: "ip", host: "example.org", want: []string{}, wantErr: true},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
got, err := r.LookupIP(context.Background(), tt.network, tt.host)
if (err != nil) != tt.wantErr {
t.Errorf("Wrong error, want %v, got %v", tt.wantErr, err)
}
items := make([]string, len(got))
for i, ip := range got {
items[i] = ip.String()
}
sort.Strings(items)
sort.Strings(tt.want)
if !reflect.DeepEqual(items, tt.want) {
t.Errorf("Wrong result, want %v, got %v", tt.want, items)
}
})
}
}