-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver_ip_test.go
95 lines (87 loc) · 2.12 KB
/
resolver_ip_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
package resolver
import (
"context"
"net/netip"
"testing"
)
func TestLookupNetIP(t *testing.T) {
tests := []struct {
name string
network string
fail bool
}{
{name: "google.com", network: "ip"},
{name: "google.com", network: "ip4"},
{name: "google.com", network: "ip6"},
{name: "ipv6.google.com", network: "ip4", fail: true},
{name: "ipv6.google.com", network: "ip6"},
}
ctx := context.Background()
l, err := NewRootResolver("")
if err != nil {
t.Fatal(err)
}
for _, tc := range tests {
s, err := l.LookupNetIP(ctx, tc.network, tc.name)
switch {
case err != nil && tc.fail:
// failed as expected
t.Logf("%q %q: %s", tc.name, tc.network, err)
case err != nil && !tc.fail:
// not expected to fail
t.Errorf("%q %q failed: %s", tc.name, tc.network, err)
case err == nil && tc.fail:
// expected to fail
msg := "expected to fail"
t.Fatalf("%q %q %s: %q", tc.name, tc.network, msg, s)
case checkTestLookupNetIPResponse(t, tc.name, tc.network, s):
// good
t.Logf("%q %q: %q", tc.name, tc.network, s)
}
}
}
func checkTestLookupNetIPResponse(t *testing.T,
name, network string,
result []netip.Addr) bool {
//
var ip4, ip6, invalid bool
var msg string
for _, addr := range result {
switch {
case !addr.IsValid():
invalid = true
case addr.Is4():
ip4 = true
case addr.Is6():
ip6 = true
default:
invalid = true
}
}
msg, ok := validateTestLookupNetIPResult(network, invalid, ip4, ip6)
if !ok {
t.Errorf("%q %q: %s: %q", name, network, msg, result)
}
return ok
}
// revive:disable:cyclomatic
func validateTestLookupNetIPResult(network string,
invalid, ip4, ip6 bool) (string, bool) {
// revive:enable:cyclomatic
want4 := network == netIP4or6 || network == netIP4only
want6 := network == netIP4or6 || network == netIP6only
switch {
case invalid:
return "invalid address", false
case ip4 && !want4:
return "unexpected IPv4 address", false
case ip6 && !want6:
return "unexpected IPv6 address", false
case !ip4 && want4:
return "missing IPv4 address", false
case !ip6 && want6:
return "missing IPv6 address", false
default:
return "", true
}
}