< Summary

Information
Class: IceRpc.Transports.Tcp.TcpClientTransport
Assembly: IceRpc
File(s): /home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Transports/Tcp/TcpClientTransport.cs
Tag: 592_20856082467
Line coverage
100%
Covered lines: 54
Uncovered lines: 0
Coverable lines: 54
Total lines: 114
Line coverage: 100%
Branch coverage
97%
Covered branches: 35
Total branches: 36
Branch coverage: 97.2%
Method coverage
100%
Covered methods: 6
Total methods: 6
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_Name()100%11100%
.ctor()100%11100%
.ctor(...)100%11100%
CreateConnection(...)95.45%2222100%
CheckParams()100%88100%
IsValidTransportName()100%66100%

File(s)

/home/runner/work/icerpc-csharp/icerpc-csharp/src/IceRpc/Transports/Tcp/TcpClientTransport.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using IceRpc.Transports.Tcp.Internal;
 4using System.Net.Security;
 5
 6namespace IceRpc.Transports.Tcp;
 7
 8/// <summary>Implements <see cref="IDuplexClientTransport" /> for the tcp transport.</summary>
 9public class TcpClientTransport : IDuplexClientTransport
 10{
 11    /// <inheritdoc/>
 1512    public string Name => TcpName;
 13
 14    private const string SslName = "ssl";
 15    private const string TcpName = "tcp";
 16
 17    private readonly TcpClientTransportOptions _options;
 18
 19    /// <summary>Constructs a <see cref="TcpClientTransport" />.</summary>
 20    public TcpClientTransport()
 3221        : this(new TcpClientTransportOptions())
 3222    {
 3223    }
 24
 25    /// <summary>Constructs a <see cref="TcpClientTransport" />.</summary>
 26    /// <param name="options">The transport options.</param>
 25027    public TcpClientTransport(TcpClientTransportOptions options) => _options = options;
 28
 29    /// <inheritdoc/>
 30    public IDuplexConnection CreateConnection(
 31        ServerAddress serverAddress,
 32        DuplexConnectionOptions options,
 33        SslClientAuthenticationOptions? clientAuthenticationOptions)
 14134    {
 14135        if (serverAddress.Transport is string transport && !IsValidTransportName(transport, serverAddress.Protocol))
 336        {
 337            throw new NotSupportedException(
 338                $"The Tcp client transport does not support server addresses with transport '{transport}'.");
 39        }
 40
 13841        if (!CheckParams(serverAddress))
 542        {
 543            throw new ArgumentException(
 544                $"The server address '{serverAddress}' contains parameters that are not valid for the Tcp client transpo
 545                nameof(serverAddress));
 46        }
 47
 13348        if (serverAddress.Transport is null)
 749        {
 750            serverAddress = serverAddress with { Transport = Name };
 751        }
 52
 13353        SslClientAuthenticationOptions? authenticationOptions = clientAuthenticationOptions?.Clone() ??
 13354            (serverAddress.Transport == SslName ? new SslClientAuthenticationOptions() : null);
 55
 13356        if (authenticationOptions is not null)
 3157        {
 58            // We are establishing a secure TLS connection. It can rely on system certificates.
 59
 3160            authenticationOptions.TargetHost ??= serverAddress.Host;
 61
 62            // Set ApplicationProtocols to "ice" or "icerpc" in the common situation where the application does not
 63            // specify any application protocol. This way, a proxy server listening on a port shared by multiple
 64            // application protocols can use this ALPN protocol ID to forward all ice/icerpc traffic to an ice/icerpc
 65            // back-end server.
 66            // We do this only when the port is not the default port for ice or icerpc; when we use the IANA-registered
 67            // default port, the server can and should use this port number to identify the application protocol when no
 68            // ALPN protocol ID is provided.
 3169            if (authenticationOptions.ApplicationProtocols is null &&
 3170                serverAddress.Port != serverAddress.Protocol.DefaultPort)
 3171            {
 3172                authenticationOptions.ApplicationProtocols = new List<SslApplicationProtocol>
 3173                {
 3174                    new SslApplicationProtocol(serverAddress.Protocol.Name)
 3175                };
 3176            }
 3177        }
 78
 13379        return new TcpClientConnection(
 13380            serverAddress,
 13381            authenticationOptions,
 13382            options.Pool,
 13383            options.MinSegmentSize,
 13384            _options);
 85
 86        static bool CheckParams(ServerAddress serverAddress)
 13887        {
 13888            if (serverAddress.Protocol == Protocol.Ice)
 1389            {
 4890                foreach (string name in serverAddress.Params.Keys)
 591                {
 592                    switch (name)
 93                    {
 94                        case "t":
 95                        case "z":
 96                            // we don't check the value since we ignore it
 497                            break;
 98
 99                        default:
 1100                            return false;
 101                    }
 4102                }
 12103                return true;
 104            }
 105            else
 125106            {
 125107                return serverAddress.Params.Count == 0;
 108            }
 138109        }
 110
 111        static bool IsValidTransportName(string transportName, Protocol protocol) =>
 131112            protocol == Protocol.Ice ? transportName is TcpName or SslName : transportName is TcpName;
 133113    }
 114}