-
Notifications
You must be signed in to change notification settings - Fork 115
/
target_test.go
152 lines (140 loc) · 2.78 KB
/
target_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// SPDX-License-Identifier: MIT
package main
import (
"context"
"net"
"os"
"sync"
"testing"
log "github.com/sirupsen/logrus"
)
var (
ipv4Addr, ipv6Addr, ipv4AddrGoogle, ipv6AddrGoogle []net.IPAddr
)
func TestMain(m *testing.M) {
// --- set up test addresses ---- //
var err error
ipv4Addr, err = net.DefaultResolver.LookupIPAddr(context.TODO(), "127.0.0.1")
if err != nil || len(ipv4Addr) < 1 {
log.Fatal("skipping test, cannot resolve 127.0.0.1 to net.IPAddr")
return
}
ipv6Addr, err = net.DefaultResolver.LookupIPAddr(context.TODO(), "::1")
if err != nil || len(ipv6Addr) < 1 {
log.Fatal("skipping test, cannot resolve ::1 to net.IPAddr")
return
}
ipv4AddrGoogle, err = net.DefaultResolver.LookupIPAddr(context.TODO(), "142.250.72.206")
if err != nil || len(ipv4Addr) < 1 {
log.Fatal("skipping test, cannot resolve 142.250.72.206 to net.IPAddr")
return
}
ipv6AddrGoogle, err = net.DefaultResolver.LookupIPAddr(context.TODO(), "2607:f8b0:4005:810::200e")
if err != nil || len(ipv6Addr) < 1 {
log.Fatal("skipping test, cannot resolve 2607:f8b0:4005:810::200e to net.IPAddr")
return
}
os.Exit(m.Run())
}
func Test_ipVersion_String(t *testing.T) {
tests := []struct {
name string
ipv ipVersion
want string
}{
{
"ipv6",
ipv6,
"6",
},
{
"ipv4",
ipv4,
"4",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.ipv.String(); got != tt.want {
t.Errorf("IPVersion.String() = %v, want %v", got, tt.want)
}
})
}
}
func Test_getIPVersion(t *testing.T) {
tests := []struct {
name string
addr net.IPAddr
want ipVersion
}{
{
"ipv4",
ipv4Addr[0],
ipv4,
},
{
"ipv6",
ipv6Addr[0],
ipv6,
},
{
"ipv4-google",
ipv4AddrGoogle[0],
ipv4,
},
{
"ipv6-google",
ipv6AddrGoogle[0],
ipv6,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getIPVersion(tt.addr); got != tt.want {
t.Errorf("getIPVersion() = %v, want %v", got, tt.want)
}
})
}
}
func Test_target_nameForIP(t *testing.T) {
tests := []struct {
name string
addr net.IPAddr
want string
}{
{
"ipv4-localhost",
ipv4Addr[0],
"testhost.com 127.0.0.1 4",
},
{
"ipv6-localhost",
ipv6Addr[0],
"testhost.com ::1 6",
},
{
"ipv4-google",
ipv4AddrGoogle[0],
"testhost.com 142.250.72.206 4",
},
{
"ipv6-google",
ipv6AddrGoogle[0],
"testhost.com 2607:f8b0:4005:810::200e 6",
},
}
for _, tt := range tests {
tr := &target{
host: "testhost.com",
addresses: []net.IPAddr{},
delay: 0,
resolver: &net.Resolver{},
mutex: sync.Mutex{},
}
t.Run(tt.name, func(t *testing.T) {
if got := tr.nameForIP(tt.addr); got != tt.want {
t.Errorf("target.nameForIP() = %v, want %v", got, tt.want)
}
})
}
}