-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver_test.go
67 lines (59 loc) · 1.91 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
package main
import (
"net"
"testing"
)
func assertLookupEquals(t *testing.T, got []net.IP, want []net.IP) {
if len(got) != len(want) {
t.Errorf("len(Lookup(dest)) != %d; want %d", len(got), len(want))
}
for _, wantResult := range want {
assertIpInResult(t, got, wantResult)
}
}
func assertIpInResult(t *testing.T, got []net.IP, want net.IP) bool {
for _, gotResult := range got {
if gotResult.String() == want.String() {
return true
}
}
t.Errorf("Lookup(dest) = %s; missing %s", got, want)
return false
}
func assertLenOfResultsInRange(t *testing.T, got []net.IP, wantMin int, wantMax int) {
if wantMin > wantMax {
t.Errorf("wantMin (%d) must be less than or equal to wantMax (%d)", wantMin, wantMax)
} else if len(got) < wantMin {
t.Errorf("len(Lookup(dest)) = %s; want >= %d", got, wantMin)
} else if len(got) > wantMax {
t.Errorf("len(Lookup(dest)) = %s; want <= %d", got, wantMax)
}
}
func TestLookupLoopbackIp(t *testing.T) {
dest, err := NewDestination(Url{Label: "localhost", Url: "http://127.0.0.1"})
assertNoError(t, dest, err)
got, err := Lookup(dest)
assertNoError(t, dest, err)
assertLookupEquals(t, got, []net.IP{net.ParseIP("127.0.0.1")})
}
func TestLookupPublicIp(t *testing.T) {
dest, err := NewDestination(Url{Label: "google_dns", Url: "http://8.8.8.8"})
assertNoError(t, dest, err)
got, err := Lookup(dest)
assertNoError(t, dest, err)
assertLookupEquals(t, got, []net.IP{net.ParseIP("8.8.8.8")})
}
func TestLookupExample(t *testing.T) {
dest, err := NewDestination(Url{Label: "example", Url: "https://example.com"})
assertNoError(t, dest, err)
got, err := Lookup(dest)
assertNoError(t, dest, err)
assertLenOfResultsInRange(t, got, 1, 10)
}
func TestLookupInvalidHostname(t *testing.T) {
dest, err := NewDestination(Url{Label: "invalid", Url: "https://a.b.c"})
assertNoError(t, dest, err)
got, err := Lookup(dest)
assertError(t, dest, err)
assertLenOfResultsInRange(t, got, 0, 0)
}