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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
// Code created by gotmpl. DO NOT MODIFY.
// source: internal/shared/semconvutil/netconv_test.go.tmpl
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package semconvutil
import (
"testing"
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/otel/attribute"
)
const (
addr = "127.0.0.1"
port = 1834
)
func TestNetTransport(t *testing.T) {
transports := map[string]attribute.KeyValue{
"tcp": attribute.String("net.transport", "ip_tcp"),
"tcp4": attribute.String("net.transport", "ip_tcp"),
"tcp6": attribute.String("net.transport", "ip_tcp"),
"udp": attribute.String("net.transport", "ip_udp"),
"udp4": attribute.String("net.transport", "ip_udp"),
"udp6": attribute.String("net.transport", "ip_udp"),
"unix": attribute.String("net.transport", "inproc"),
"unixgram": attribute.String("net.transport", "inproc"),
"unixpacket": attribute.String("net.transport", "inproc"),
"ip:1": attribute.String("net.transport", "other"),
"ip:icmp": attribute.String("net.transport", "other"),
"ip4:proto": attribute.String("net.transport", "other"),
"ip6:proto": attribute.String("net.transport", "other"),
}
for network, want := range transports {
assert.Equal(t, want, NetTransport(network))
}
}
func TestNetHost(t *testing.T) {
testAddrs(t, []addrTest{
{address: "", expected: nil},
{address: "192.0.0.1", expected: []attribute.KeyValue{
nc.HostName("192.0.0.1"),
}},
{address: "192.0.0.1:9090", expected: []attribute.KeyValue{
nc.HostName("192.0.0.1"),
nc.HostPort(9090),
}},
}, nc.Host)
}
func TestNetHostName(t *testing.T) {
expected := attribute.Key("net.host.name").String(addr)
assert.Equal(t, expected, nc.HostName(addr))
}
func TestNetHostPort(t *testing.T) {
expected := attribute.Key("net.host.port").Int(port)
assert.Equal(t, expected, nc.HostPort(port))
}
func TestNetPeer(t *testing.T) {
testAddrs(t, []addrTest{
{address: "", expected: nil},
{address: "example.com", expected: []attribute.KeyValue{
nc.PeerName("example.com"),
}},
{address: "/tmp/file", expected: []attribute.KeyValue{
nc.PeerName("/tmp/file"),
}},
{address: "192.0.0.1", expected: []attribute.KeyValue{
nc.PeerName("192.0.0.1"),
}},
{address: ":9090", expected: nil},
{address: "192.0.0.1:9090", expected: []attribute.KeyValue{
nc.PeerName("192.0.0.1"),
nc.PeerPort(9090),
}},
}, nc.Peer)
}
func TestNetPeerName(t *testing.T) {
expected := attribute.Key("net.peer.name").String(addr)
assert.Equal(t, expected, nc.PeerName(addr))
}
func TestNetPeerPort(t *testing.T) {
expected := attribute.Key("net.peer.port").Int(port)
assert.Equal(t, expected, nc.PeerPort(port))
}
func TestNetSockPeerName(t *testing.T) {
expected := attribute.Key("net.sock.peer.addr").String(addr)
assert.Equal(t, expected, nc.SockPeerAddr(addr))
}
func TestNetSockPeerPort(t *testing.T) {
expected := attribute.Key("net.sock.peer.port").Int(port)
assert.Equal(t, expected, nc.SockPeerPort(port))
}
func TestNetFamily(t *testing.T) {
tests := []struct {
network string
address string
expect string
}{
{"", "", ""},
{"unix", "", "unix"},
{"unix", "gibberish", "unix"},
{"unixgram", "", "unix"},
{"unixgram", "gibberish", "unix"},
{"unixpacket", "gibberish", "unix"},
{"tcp", "123.0.2.8", "inet"},
{"tcp", "gibberish", ""},
{"", "123.0.2.8", "inet"},
{"", "gibberish", ""},
{"tcp", "fe80::1", "inet6"},
{"", "fe80::1", "inet6"},
}
for _, test := range tests {
got := family(test.network, test.address)
assert.Equal(t, test.expect, got, test.network+"/"+test.address)
}
}
func TestSplitHostPort(t *testing.T) {
tests := []struct {
hostport string
host string
port int
}{
{"", "", -1},
{":8080", "", 8080},
{"127.0.0.1", "127.0.0.1", -1},
{"www.example.com", "www.example.com", -1},
{"127.0.0.1%25en0", "127.0.0.1%25en0", -1},
{"[]", "", -1}, // Ensure this doesn't panic.
{"[fe80::1", "", -1},
{"[fe80::1]", "fe80::1", -1},
{"[fe80::1%25en0]", "fe80::1%25en0", -1},
{"[fe80::1]:8080", "fe80::1", 8080},
{"[fe80::1]::", "", -1}, // Too many colons.
{"127.0.0.1:", "127.0.0.1", -1},
{"127.0.0.1:port", "127.0.0.1", -1},
{"127.0.0.1:8080", "127.0.0.1", 8080},
{"www.example.com:8080", "www.example.com", 8080},
{"127.0.0.1%25en0:8080", "127.0.0.1%25en0", 8080},
}
for _, test := range tests {
h, p := splitHostPort(test.hostport)
assert.Equal(t, test.host, h, test.hostport)
assert.Equal(t, test.port, p, test.hostport)
}
}
type addrTest struct {
address string
expected []attribute.KeyValue
}
func testAddrs(t *testing.T, tests []addrTest, f func(string) []attribute.KeyValue) {
t.Helper()
for _, test := range tests {
got := f(test.address)
assert.Equal(t, cap(test.expected), cap(got), "slice capacity")
assert.ElementsMatch(t, test.expected, got, test.address)
}
}
func TestNetProtocol(t *testing.T) {
type testCase struct {
name, version string
}
tests := map[string]testCase{
"HTTP/1.0": {name: "http", version: "1.0"},
"HTTP/1.1": {name: "http", version: "1.1"},
"HTTP/2": {name: "http", version: "2"},
"HTTP/3": {name: "http", version: "3"},
"SPDY": {name: "spdy"},
"SPDY/2": {name: "spdy", version: "2"},
"QUIC": {name: "quic"},
"unknown/proto/2": {name: "unknown", version: "proto/2"},
"other": {name: "other"},
}
for proto, want := range tests {
name, version := netProtocol(proto)
assert.Equal(t, want.name, name)
assert.Equal(t, want.version, version)
}
}
|