< Summary

Information
Class: Ice.Internal.IPEndpointI
Assembly: Ice
File(s): /home/runner/work/ice/ice/csharp/src/Ice/Internal/IPEndpointI.cs
Tag: 71_18251537082
Line coverage
89%
Covered lines: 138
Uncovered lines: 17
Coverable lines: 155
Total lines: 347
Line coverage: 89%
Branch coverage
80%
Covered branches: 68
Total branches: 84
Branch coverage: 80.9%
Method coverage
100%
Covered methods: 20
Total methods: 20
Method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
.ctor(...)100%11100%
.ctor(...)100%11100%
type()100%11100%
protocol()100%11100%
secure()100%11100%
connectionId()100%11100%
connectionId(...)100%22100%
connectors_async(...)100%11100%
expandHost()100%22100%
isLoopbackOrMulticast()100%44100%
equivalent(...)100%66100%
connectors(...)100%22100%
options()100%1414100%
GetHashCode()50%2.01287.5%
CompareTo(...)50%17.571473.68%
streamWriteImpl(...)100%11100%
initWithOptions(...)87.5%16.351688.89%
checkOption(...)66.67%28.441868.18%
normalizeHost(...)100%4.59466.67%

File(s)

/home/runner/work/ice/ice/csharp/src/Ice/Internal/IPEndpointI.cs

#LineLine coverage
 1// Copyright (c) ZeroC, Inc.
 2
 3using System.Globalization;
 4using System.Net;
 5
 6namespace Ice.Internal;
 7
 8public abstract class IPEndpointI : EndpointI
 9{
 110    protected IPEndpointI(ProtocolInstance instance, string host, int port, EndPoint sourceAddr, string connectionId)
 11    {
 112        instance_ = instance;
 113        host_ = host;
 114        _normalizedHost = normalizeHost(host_);
 115        port_ = port;
 116        sourceAddr_ = sourceAddr;
 117        connectionId_ = connectionId;
 118    }
 19
 120    protected IPEndpointI(ProtocolInstance instance)
 21    {
 122        instance_ = instance;
 123        host_ = null;
 124        _normalizedHost = null;
 125        port_ = 0;
 126        sourceAddr_ = null;
 127        connectionId_ = "";
 128    }
 29
 130    protected IPEndpointI(ProtocolInstance instance, Ice.InputStream s)
 31    {
 132        instance_ = instance;
 133        host_ = s.readString();
 134        _normalizedHost = normalizeHost(host_);
 135        port_ = s.readInt();
 136        sourceAddr_ = null;
 137        connectionId_ = "";
 138    }
 39
 140    public override short type() => instance_.type();
 41
 142    public override string protocol() => instance_.protocol();
 43
 144    public override bool secure() => instance_.secure();
 45
 146    public override string connectionId() => connectionId_;
 47
 48    public override EndpointI connectionId(string connectionId)
 49    {
 150        if (connectionId.Equals(connectionId_, StringComparison.Ordinal))
 51        {
 152            return this;
 53        }
 54        else
 55        {
 156            return createEndpoint(host_, port_, connectionId);
 57        }
 58    }
 59
 60    public override void connectors_async(EndpointI_connectors callback) =>
 161        instance_.resolve(host_, port_, this, callback);
 62
 63    public override List<EndpointI> expandHost()
 64    {
 65        // If this endpoint has an empty host (wildcard address), don't expand, just return this endpoint.
 166        if (host_.Length == 0)
 67        {
 168            return [this];
 69        }
 70
 171        List<EndPoint> addresses = Network.getAddresses(
 172            host_,
 173            port_,
 174            instance_.protocolSupport(),
 175            instance_.preferIPv6(),
 176            true);
 77
 178        return addresses.Select(
 179            addr => createEndpoint(
 180                        Network.endpointAddressToString(addr),
 181                        Network.endpointPort(addr),
 182                        connectionId_) as EndpointI).ToList();
 83    }
 84
 85    // Empty host_ means the endpoint is a wildcard address. This method must be called only on an endpoint with an
 86    // empty host or an IP address.
 87    public override bool isLoopbackOrMulticast()
 88    {
 189        if (host_.Length > 0)
 90        {
 191            var ipEndPoint = IPEndPoint.Parse(host_);
 192            return IPAddress.IsLoopback(ipEndPoint.Address) || Network.isMulticast(ipEndPoint);
 93        }
 94        else
 95        {
 196            return false;
 97        }
 98    }
 99
 100    public override bool equivalent(EndpointI endpoint)
 101    {
 1102        if (!(endpoint is IPEndpointI))
 103        {
 1104            return false;
 105        }
 1106        var ipEndpointI = (IPEndpointI)endpoint;
 1107        return ipEndpointI.type() == type() &&
 1108            ipEndpointI._normalizedHost == _normalizedHost &&
 1109            ipEndpointI.port_ == port_;
 110    }
 111
 112    public virtual List<Connector> connectors(List<EndPoint> addresses, NetworkProxy proxy)
 113    {
 1114        var connectors = new List<Connector>();
 1115        foreach (EndPoint p in addresses)
 116        {
 1117            connectors.Add(createConnector(p, proxy));
 118        }
 1119        return connectors;
 120    }
 121
 122    public override string options()
 123    {
 124        //
 125        // WARNING: Certain features, such as proxy validation in Glacier2,
 126        // depend on the format of proxy strings. Changes to toString() and
 127        // methods called to generate parts of the reference string could break
 128        // these features. Please review for all features that depend on the
 129        // format of proxyToString() before changing this and related code.
 130        //
 1131        string s = "";
 132
 1133        if (host_ != null && host_.Length > 0)
 134        {
 1135            s += " -h ";
 1136            bool addQuote = host_.Contains(':', StringComparison.Ordinal);
 1137            if (addQuote)
 138            {
 1139                s += "\"";
 140            }
 1141            s += host_;
 1142            if (addQuote)
 143            {
 1144                s += "\"";
 145            }
 146        }
 147
 1148        s += " -p " + port_;
 149
 1150        if (sourceAddr_ != null)
 151        {
 1152            string sourceAddr = Network.endpointAddressToString(sourceAddr_);
 1153            bool addQuote = sourceAddr.Contains(':', StringComparison.Ordinal);
 1154            s += " --sourceAddress ";
 1155            if (addQuote)
 156            {
 1157                s += "\"";
 158            }
 1159            s += sourceAddr;
 1160            if (addQuote)
 161            {
 1162                s += "\"";
 163            }
 164        }
 165
 1166        return s;
 167    }
 168
 169    public override int GetHashCode()
 170    {
 1171        var hash = new HashCode();
 1172        hash.Add(type());
 1173        hash.Add(host_);
 1174        hash.Add(port_);
 1175        if (sourceAddr_ is not null)
 176        {
 0177            hash.Add(sourceAddr_);
 178        }
 1179        hash.Add(connectionId_);
 1180        return hash.ToHashCode();
 181    }
 182
 183    public override int CompareTo(EndpointI other)
 184    {
 1185        if (!(other is IPEndpointI))
 186        {
 0187            return type() < other.type() ? -1 : 1;
 188        }
 189
 1190        var p = (IPEndpointI)other;
 1191        if (this == p)
 192        {
 0193            return 0;
 194        }
 195
 1196        int v = string.Compare(host_, p.host_, StringComparison.Ordinal);
 1197        if (v != 0)
 198        {
 0199            return v;
 200        }
 201
 1202        if (port_ < p.port_)
 203        {
 1204            return -1;
 205        }
 1206        else if (p.port_ < port_)
 207        {
 0208            return 1;
 209        }
 210
 1211        int rc = string.Compare(
 1212            Network.endpointAddressToString(sourceAddr_),
 1213            Network.endpointAddressToString(p.sourceAddr_),
 1214            StringComparison.Ordinal);
 1215        if (rc != 0)
 216        {
 0217            return rc;
 218        }
 219
 1220        return string.Compare(connectionId_, p.connectionId_, StringComparison.Ordinal);
 221    }
 222
 223    public override void streamWriteImpl(Ice.OutputStream s)
 224    {
 1225        s.writeString(host_);
 1226        s.writeInt(port_);
 1227    }
 228
 229    public virtual void initWithOptions(List<string> args, bool oaEndpoint)
 230    {
 1231        initWithOptions(args);
 232
 1233        if (host_ == null || host_.Length == 0)
 234        {
 1235            host_ = instance_.defaultHost();
 1236            _normalizedHost = normalizeHost(host_);
 237        }
 1238        else if (host_ == "*")
 239        {
 1240            if (oaEndpoint)
 241            {
 1242                host_ = "";
 1243                _normalizedHost = "";
 244            }
 245            else
 246            {
 0247                throw new ParseException($"'-h *' not valid for proxy endpoint '{ToString()}'");
 248            }
 249        }
 250
 1251        if (host_ == null)
 252        {
 1253            host_ = "";
 1254            _normalizedHost = normalizeHost(host_);
 255        }
 256
 1257        if (sourceAddr_ != null)
 258        {
 1259            if (oaEndpoint)
 260            {
 0261                throw new ParseException($"'--sourceAddress' not valid for object adapter endpoint '{ToString()}'");
 262            }
 263        }
 1264        else if (!oaEndpoint)
 265        {
 1266            sourceAddr_ = instance_.defaultSourceAddress();
 267        }
 1268    }
 269
 270    protected override bool checkOption(string option, string argument, string endpoint)
 271    {
 1272        if (option == "-h")
 273        {
 1274            host_ = argument ?? throw new ParseException(
 1275                $"no argument provided for -h option in endpoint '{endpoint}'");
 1276            _normalizedHost = normalizeHost(host_);
 277        }
 1278        else if (option == "-p")
 279        {
 1280            if (argument == null)
 281            {
 0282                throw new ParseException($"no argument provided for -p option in endpoint '{endpoint}'");
 283            }
 284
 285            try
 286            {
 1287                port_ = int.Parse(argument, CultureInfo.InvariantCulture);
 1288            }
 0289            catch (FormatException ex)
 290            {
 0291                throw new ParseException($"invalid port value '{argument}' in endpoint '{endpoint}'", ex);
 292            }
 293
 1294            if (port_ < 0 || port_ > 65535)
 295            {
 0296                throw new ParseException($"port value '{argument}' out of range in endpoint '{endpoint}'");
 297            }
 298        }
 1299        else if (option == "--sourceAddress")
 300        {
 1301            if (argument == null)
 302            {
 0303                throw new ParseException($"no argument provided for --sourceAddress option in endpoint '{endpoint}'");
 304            }
 1305            sourceAddr_ = Network.getNumericAddress(argument);
 1306            if (sourceAddr_ == null)
 307            {
 0308                throw new ParseException(
 0309                    $"invalid IP address provided for --sourceAddress option in endpoint '{endpoint}'");
 310            }
 311        }
 312        else
 313        {
 1314            return false;
 315        }
 1316        return true;
 317    }
 318
 319    private static string normalizeHost(string host)
 320    {
 1321        if (host is not null && host.Contains(':', StringComparison.Ordinal))
 322        {
 323            // Could be an IPv6 address that we need to normalize.
 324            try
 325            {
 1326                return IPAddress.Parse(host).ToString(); // normalized host
 327            }
 0328            catch
 329            {
 330                // Ignore - don't normalize host.
 0331            }
 332        }
 1333        return host;
 1334    }
 335
 336    protected abstract Connector createConnector(EndPoint addr, NetworkProxy proxy);
 337
 338    protected abstract IPEndpointI createEndpoint(string host, int port, string connectionId);
 339
 340    protected readonly ProtocolInstance instance_;
 341    protected string host_;
 342    protected int port_;
 343    protected EndPoint sourceAddr_;
 344    protected string connectionId_;
 345    // Set when we set _host; used by the implementation of equivalent.
 346    private string _normalizedHost;
 347}