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