MCPcopy
hub / github.com/cloudflare/cloudflared / TestDialQuicWithSkipPortReuse

Function TestDialQuicWithSkipPortReuse

connection/quic_connection_test.go:717–780  ·  view source on GitHub ↗

TestDialQuicWithSkipPortReuse tests that DialQuic works correctly with the WithSkipPortReuse option.

(t *testing.T)

Source from the content-addressed store, hash-verified

715
716// TestDialQuicWithSkipPortReuse tests that DialQuic works correctly with the WithSkipPortReuse option.
717func TestDialQuicWithSkipPortReuse(t *testing.T) {
718 t.Parallel()
719
720 ctx, cancel := context.WithCancel(t.Context())
721 defer cancel()
722
723 // Start a mock QUIC server (similar to TestQUICServer)
724 udpListener, err := net.ListenUDP("udp", &net.UDPAddr{IP: net.IPv4(127, 0, 0, 1), Port: 0})
725 require.NoError(t, err)
726 defer func() { _ = udpListener.Close() }()
727
728 serverAddr := netip.MustParseAddrPort(udpListener.LocalAddr().String())
729
730 quicTransport := &quic.Transport{Conn: udpListener, ConnectionIDLength: 16}
731 quicListener, err := quicTransport.Listen(testTLSServerConfig, testQUICConfig)
732 require.NoError(t, err)
733
734 serverDone := make(chan struct{})
735 go func() {
736 // Accept one connection
737 session, err := quicListener.Accept(ctx)
738 if err != nil {
739 close(serverDone)
740 return
741 }
742 // Keep session open until context is cancelled
743 <-ctx.Done()
744 _ = session.CloseWithError(0, "test done")
745 close(serverDone)
746 }()
747
748 // Test DialQuic with WithSkipPortReuse option
749 tlsClientConfig := &tls.Config{
750 // nolint: gosec
751 InsecureSkipVerify: true,
752 NextProtos: []string{"argotunnel"},
753 }
754
755 log := zerolog.New(io.Discard)
756 dialCtx, dialCancel := context.WithTimeout(t.Context(), 5*time.Second)
757 defer dialCancel()
758
759 // Dial with skipPortReuse option - should use a random ephemeral port
760 conn, err := DialQuic(
761 dialCtx,
762 testQUICConfig,
763 tlsClientConfig,
764 serverAddr,
765 nil, // connect on a random port
766 0,
767 &log,
768 dialopts.DialOpts{SkipPortReuse: true},
769 )
770 require.NoError(t, err)
771 require.NotNil(t, conn)
772
773 // Verify we can get connection state
774 _ = conn.ConnectionState()

Callers

nothing calls this directly

Calls 8

DialQuicFunction · 0.85
ContextMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.65
LocalAddrMethod · 0.65
ListenMethod · 0.65
CloseWithErrorMethod · 0.65
ConnectionStateMethod · 0.65

Tested by

no test coverage detected