forked from quic-go/quic-go
qlog: use PathEndpointInfo in connection_started (#5368)
* qlog: use PathEndpointInfo in connection_started * correctly deal with dual-stack addresses
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package quic
|
||||
|
||||
import (
|
||||
"net"
|
||||
"net/netip"
|
||||
"testing"
|
||||
|
||||
"github.com/quic-go/quic-go/internal/wire"
|
||||
@@ -70,3 +72,72 @@ func TestConnectionLoggingOtherFrames(t *testing.T) {
|
||||
f := toQlogFrame(&wire.MaxDataFrame{MaximumData: 1234})
|
||||
require.Equal(t, &qlog.MaxDataFrame{MaximumData: 1234}, f.Frame)
|
||||
}
|
||||
|
||||
func TestConnectionLoggingStartedConnectionEvent(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
local *net.UDPAddr
|
||||
remote *net.UDPAddr
|
||||
wantLocalIP string
|
||||
wantLocalPort uint16
|
||||
wantRemote netip.AddrPort
|
||||
}{
|
||||
{
|
||||
name: "unspecified local, remote IPv4 -> 0.0.0.0",
|
||||
local: &net.UDPAddr{Port: 58451},
|
||||
remote: &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 6121},
|
||||
wantLocalIP: "0.0.0.0",
|
||||
wantLocalPort: 58451,
|
||||
wantRemote: netip.AddrPortFrom(netip.AddrFrom4([4]byte{127, 0, 0, 1}), 6121),
|
||||
},
|
||||
{
|
||||
name: "unspecified local, remote IPv6 -> ::",
|
||||
local: &net.UDPAddr{Port: 4242},
|
||||
remote: &net.UDPAddr{IP: net.ParseIP("2001:db8::1"), Port: 6121},
|
||||
wantLocalIP: "::",
|
||||
wantLocalPort: 4242,
|
||||
wantRemote: func() netip.AddrPort { a, _ := netip.ParseAddr("2001:db8::1"); return netip.AddrPortFrom(a, 6121) }(),
|
||||
},
|
||||
{
|
||||
name: "specified local IPv4",
|
||||
local: &net.UDPAddr{IP: net.IPv4(192, 168, 1, 10), Port: 9999},
|
||||
remote: &net.UDPAddr{IP: net.IPv4(10, 0, 0, 1), Port: 1234},
|
||||
wantLocalIP: "192.168.1.10",
|
||||
wantLocalPort: 9999,
|
||||
wantRemote: netip.AddrPortFrom(netip.AddrFrom4([4]byte{10, 0, 0, 1}), 1234),
|
||||
},
|
||||
{
|
||||
name: "specified local IPv6",
|
||||
local: &net.UDPAddr{IP: net.ParseIP("fe80::1"), Port: 999},
|
||||
remote: &net.UDPAddr{IP: net.ParseIP("2001:db8::1"), Port: 6121},
|
||||
wantLocalIP: "fe80::1",
|
||||
wantLocalPort: 999,
|
||||
wantRemote: func() netip.AddrPort { a, _ := netip.ParseAddr("2001:db8::1"); return netip.AddrPortFrom(a, 6121) }(),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ev := startedConnectionEvent(tc.local, tc.remote)
|
||||
var gotIP string
|
||||
var gotPort uint16
|
||||
if ev.Local.IPv4.IsValid() {
|
||||
gotIP = ev.Local.IPv4.Addr().String()
|
||||
gotPort = ev.Local.IPv4.Port()
|
||||
} else if ev.Local.IPv6.IsValid() {
|
||||
gotIP = ev.Local.IPv6.Addr().String()
|
||||
gotPort = ev.Local.IPv6.Port()
|
||||
}
|
||||
require.Equal(t, tc.wantLocalIP, gotIP)
|
||||
require.Equal(t, tc.wantLocalPort, gotPort)
|
||||
|
||||
var gotRemote netip.AddrPort
|
||||
if ev.Remote.IPv4.IsValid() {
|
||||
gotRemote = ev.Remote.IPv4
|
||||
} else if ev.Remote.IPv6.IsValid() {
|
||||
gotRemote = ev.Remote.IPv6
|
||||
}
|
||||
require.Equal(t, tc.wantRemote, gotRemote)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user