1
2
3
4
5
6
7
8
9
10
11
12
13
14 package internal
15
16 import (
17 "net"
18 "strconv"
19 "testing"
20
21 "github.com/stretchr/testify/assert"
22 "github.com/stretchr/testify/require"
23
24 "go.opentelemetry.io/otel/attribute"
25 )
26
27 const (
28 addr = "127.0.0.1"
29 port = 1834
30 )
31
32 var nc = &NetConv{
33 NetHostNameKey: attribute.Key("net.host.name"),
34 NetHostPortKey: attribute.Key("net.host.port"),
35 NetPeerNameKey: attribute.Key("net.peer.name"),
36 NetPeerPortKey: attribute.Key("net.peer.port"),
37 NetSockPeerAddrKey: attribute.Key("net.sock.peer.addr"),
38 NetSockPeerPortKey: attribute.Key("net.sock.peer.port"),
39 NetTransportOther: attribute.String("net.transport", "other"),
40 NetTransportTCP: attribute.String("net.transport", "ip_tcp"),
41 NetTransportUDP: attribute.String("net.transport", "ip_udp"),
42 NetTransportInProc: attribute.String("net.transport", "inproc"),
43 }
44
45 func TestNetTransport(t *testing.T) {
46 transports := map[string]attribute.KeyValue{
47 "tcp": attribute.String("net.transport", "ip_tcp"),
48 "tcp4": attribute.String("net.transport", "ip_tcp"),
49 "tcp6": attribute.String("net.transport", "ip_tcp"),
50 "udp": attribute.String("net.transport", "ip_udp"),
51 "udp4": attribute.String("net.transport", "ip_udp"),
52 "udp6": attribute.String("net.transport", "ip_udp"),
53 "unix": attribute.String("net.transport", "inproc"),
54 "unixgram": attribute.String("net.transport", "inproc"),
55 "unixpacket": attribute.String("net.transport", "inproc"),
56 "ip:1": attribute.String("net.transport", "other"),
57 "ip:icmp": attribute.String("net.transport", "other"),
58 "ip4:proto": attribute.String("net.transport", "other"),
59 "ip6:proto": attribute.String("net.transport", "other"),
60 }
61
62 for network, want := range transports {
63 assert.Equal(t, want, nc.Transport(network))
64 }
65 }
66
67 func TestNetServerNilListener(t *testing.T) {
68 const addr = "127.0.0.1:8080"
69 got := nc.Server(addr, nil)
70 expected := nc.Host(addr)
71 assert.Equal(t, cap(expected), cap(got), "slice capacity")
72 assert.ElementsMatch(t, expected, got)
73 }
74
75 type listener struct{ net.Listener }
76
77 func (listener) Addr() net.Addr { return nil }
78
79 func TestNetServerNilAddr(t *testing.T) {
80 const addr = "127.0.0.1:8080"
81 got := nc.Server(addr, listener{})
82 expected := nc.Host(addr)
83 assert.Equal(t, cap(expected), cap(got), "slice capacity")
84 assert.ElementsMatch(t, expected, got)
85 }
86
87 func newTCPListener() (net.Listener, error) {
88 return net.Listen("tcp4", "127.0.0.1:0")
89 }
90
91 func TestNetServerTCP(t *testing.T) {
92 ln, err := newTCPListener()
93 require.NoError(t, err)
94 defer func() { require.NoError(t, ln.Close()) }()
95
96 host, pStr, err := net.SplitHostPort(ln.Addr().String())
97 require.NoError(t, err)
98 port, err := strconv.Atoi(pStr)
99 require.NoError(t, err)
100
101 got := nc.Server("example.com:8080", ln)
102 expected := []attribute.KeyValue{
103 nc.HostName("example.com"),
104 nc.HostPort(8080),
105 nc.NetTransportTCP,
106 nc.NetSockFamilyKey.String("inet"),
107 nc.NetSockHostAddrKey.String(host),
108 nc.NetSockHostPortKey.Int(port),
109 }
110 assert.Equal(t, cap(expected), cap(got), "slice capacity")
111 assert.ElementsMatch(t, expected, got)
112 }
113
114 func TestNetHost(t *testing.T) {
115 testAddrs(t, []addrTest{
116 {address: "", expected: nil},
117 {address: "192.0.0.1", expected: []attribute.KeyValue{
118 nc.HostName("192.0.0.1"),
119 }},
120 {address: "192.0.0.1:9090", expected: []attribute.KeyValue{
121 nc.HostName("192.0.0.1"),
122 nc.HostPort(9090),
123 }},
124 }, nc.Host)
125 }
126
127 func TestNetHostName(t *testing.T) {
128 expected := attribute.Key("net.host.name").String(addr)
129 assert.Equal(t, expected, nc.HostName(addr))
130 }
131
132 func TestNetHostPort(t *testing.T) {
133 expected := attribute.Key("net.host.port").Int(port)
134 assert.Equal(t, expected, nc.HostPort(port))
135 }
136
137 func TestNetClientNilConn(t *testing.T) {
138 const addr = "127.0.0.1:8080"
139 got := nc.Client(addr, nil)
140 expected := nc.Peer(addr)
141 assert.Equal(t, cap(expected), cap(got), "slice capacity")
142 assert.ElementsMatch(t, expected, got)
143 }
144
145 type conn struct{ net.Conn }
146
147 func (conn) LocalAddr() net.Addr { return nil }
148 func (conn) RemoteAddr() net.Addr { return nil }
149
150 func TestNetClientNilAddr(t *testing.T) {
151 const addr = "127.0.0.1:8080"
152 got := nc.Client(addr, conn{})
153 expected := nc.Peer(addr)
154 assert.Equal(t, cap(expected), cap(got), "slice capacity")
155 assert.ElementsMatch(t, expected, got)
156 }
157
158 func newTCPConn() (net.Conn, net.Listener, error) {
159 ln, err := newTCPListener()
160 if err != nil {
161 return nil, nil, err
162 }
163
164 conn, err := net.Dial("tcp4", ln.Addr().String())
165 if err != nil {
166 _ = ln.Close()
167 return nil, nil, err
168 }
169
170 return conn, ln, nil
171 }
172
173 func TestNetClientTCP(t *testing.T) {
174 conn, ln, err := newTCPConn()
175 require.NoError(t, err)
176 defer func() { require.NoError(t, ln.Close()) }()
177 defer func() { require.NoError(t, conn.Close()) }()
178
179 lHost, pStr, err := net.SplitHostPort(conn.LocalAddr().String())
180 require.NoError(t, err)
181 lPort, err := strconv.Atoi(pStr)
182 require.NoError(t, err)
183
184 rHost, pStr, err := net.SplitHostPort(conn.RemoteAddr().String())
185 require.NoError(t, err)
186 rPort, err := strconv.Atoi(pStr)
187 require.NoError(t, err)
188
189 got := nc.Client("example.com:8080", conn)
190 expected := []attribute.KeyValue{
191 nc.PeerName("example.com"),
192 nc.PeerPort(8080),
193 nc.NetTransportTCP,
194 nc.NetSockFamilyKey.String("inet"),
195 nc.NetSockPeerAddrKey.String(rHost),
196 nc.NetSockPeerPortKey.Int(rPort),
197 nc.NetSockHostAddrKey.String(lHost),
198 nc.NetSockHostPortKey.Int(lPort),
199 }
200 assert.Equal(t, cap(expected), cap(got), "slice capacity")
201 assert.ElementsMatch(t, expected, got)
202 }
203
204 type remoteOnlyConn struct{ net.Conn }
205
206 func (remoteOnlyConn) LocalAddr() net.Addr { return nil }
207
208 func TestNetClientTCPNilLocal(t *testing.T) {
209 conn, ln, err := newTCPConn()
210 require.NoError(t, err)
211 defer func() { require.NoError(t, ln.Close()) }()
212 defer func() { require.NoError(t, conn.Close()) }()
213
214 conn = remoteOnlyConn{conn}
215
216 rHost, pStr, err := net.SplitHostPort(conn.RemoteAddr().String())
217 require.NoError(t, err)
218 rPort, err := strconv.Atoi(pStr)
219 require.NoError(t, err)
220
221 got := nc.Client("example.com:8080", conn)
222 expected := []attribute.KeyValue{
223 nc.PeerName("example.com"),
224 nc.PeerPort(8080),
225 nc.NetTransportTCP,
226 nc.NetSockFamilyKey.String("inet"),
227 nc.NetSockPeerAddrKey.String(rHost),
228 nc.NetSockPeerPortKey.Int(rPort),
229 }
230 assert.Equal(t, cap(expected), cap(got), "slice capacity")
231 assert.ElementsMatch(t, expected, got)
232 }
233
234 func TestNetPeer(t *testing.T) {
235 testAddrs(t, []addrTest{
236 {address: "", expected: nil},
237 {address: "example.com", expected: []attribute.KeyValue{
238 nc.PeerName("example.com"),
239 }},
240 {address: "/tmp/file", expected: []attribute.KeyValue{
241 nc.PeerName("/tmp/file"),
242 }},
243 {address: "192.0.0.1", expected: []attribute.KeyValue{
244 nc.PeerName("192.0.0.1"),
245 }},
246 {address: ":9090", expected: nil},
247 {address: "192.0.0.1:9090", expected: []attribute.KeyValue{
248 nc.PeerName("192.0.0.1"),
249 nc.PeerPort(9090),
250 }},
251 }, nc.Peer)
252 }
253
254 func TestNetPeerName(t *testing.T) {
255 expected := attribute.Key("net.peer.name").String(addr)
256 assert.Equal(t, expected, nc.PeerName(addr))
257 }
258
259 func TestNetPeerPort(t *testing.T) {
260 expected := attribute.Key("net.peer.port").Int(port)
261 assert.Equal(t, expected, nc.PeerPort(port))
262 }
263
264 func TestNetSockPeerName(t *testing.T) {
265 expected := attribute.Key("net.sock.peer.addr").String(addr)
266 assert.Equal(t, expected, nc.SockPeerAddr(addr))
267 }
268
269 func TestNetSockPeerPort(t *testing.T) {
270 expected := attribute.Key("net.sock.peer.port").Int(port)
271 assert.Equal(t, expected, nc.SockPeerPort(port))
272 }
273
274 func TestFamily(t *testing.T) {
275 tests := []struct {
276 network string
277 address string
278 expect string
279 }{
280 {"", "", ""},
281 {"unix", "", "unix"},
282 {"unix", "gibberish", "unix"},
283 {"unixgram", "", "unix"},
284 {"unixgram", "gibberish", "unix"},
285 {"unixpacket", "gibberish", "unix"},
286 {"tcp", "123.0.2.8", "inet"},
287 {"tcp", "gibberish", ""},
288 {"", "123.0.2.8", "inet"},
289 {"", "gibberish", ""},
290 {"tcp", "fe80::1", "inet6"},
291 {"", "fe80::1", "inet6"},
292 }
293
294 for _, test := range tests {
295 got := family(test.network, test.address)
296 assert.Equal(t, test.expect, got, test.network+"/"+test.address)
297 }
298 }
299
300 func TestSplitHostPort(t *testing.T) {
301 tests := []struct {
302 hostport string
303 host string
304 port int
305 }{
306 {"", "", -1},
307 {":8080", "", 8080},
308 {"127.0.0.1", "127.0.0.1", -1},
309 {"www.example.com", "www.example.com", -1},
310 {"127.0.0.1%25en0", "127.0.0.1%25en0", -1},
311 {"[]", "", -1},
312 {"[fe80::1", "", -1},
313 {"[fe80::1]", "fe80::1", -1},
314 {"[fe80::1%25en0]", "fe80::1%25en0", -1},
315 {"[fe80::1]:8080", "fe80::1", 8080},
316 {"[fe80::1]::", "", -1},
317 {"127.0.0.1:", "127.0.0.1", -1},
318 {"127.0.0.1:port", "127.0.0.1", -1},
319 {"127.0.0.1:8080", "127.0.0.1", 8080},
320 {"www.example.com:8080", "www.example.com", 8080},
321 {"127.0.0.1%25en0:8080", "127.0.0.1%25en0", 8080},
322 }
323
324 for _, test := range tests {
325 h, p := splitHostPort(test.hostport)
326 assert.Equal(t, test.host, h, test.hostport)
327 assert.Equal(t, test.port, p, test.hostport)
328 }
329 }
330
331 type addrTest struct {
332 address string
333 expected []attribute.KeyValue
334 }
335
336 func testAddrs(t *testing.T, tests []addrTest, f func(string) []attribute.KeyValue) {
337 t.Helper()
338
339 for _, test := range tests {
340 got := f(test.address)
341 assert.Equal(t, cap(test.expected), cap(got), "slice capacity")
342 assert.ElementsMatch(t, test.expected, got, test.address)
343 }
344 }
345
View as plain text