forked from foxcpp/go-mockdns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver_test.go
71 lines (63 loc) · 1.55 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
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)
}
}