< Summary

Information
Class: Renci.SshNet.NetConfClient
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\NetConfClient.cs
Line coverage
65%
Covered lines: 52
Uncovered lines: 27
Coverable lines: 79
Total lines: 288
Line coverage: 65.8%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_OperationTimeout()100%1100%
set_OperationTimeout(...)100%6100%
get_NetConfSession()100%1100%
.ctor(...)100%1100%
.ctor(...)100%10%
.ctor(...)100%10%
.ctor(...)100%10%
.ctor(...)100%10%
.ctor(...)100%1100%
.ctor(...)100%1100%
get_ServerCapabilities()100%10%
get_ClientCapabilities()100%10%
get_AutomaticMessageIdHandling()100%1100%
SendReceiveRpc(...)100%10%
SendReceiveRpc(...)100%10%
SendCloseRpc()100%10%
OnConnected()100%1100%
OnDisconnecting()100%1100%
Dispose(...)100%4100%
CreateAndConnectNetConfSession()100%1100%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\NetConfClient.cs

#LineLine coverage
 1using System;
 2using System.Diagnostics.CodeAnalysis;
 3using System.Net;
 4using System.Xml;
 5
 6using Renci.SshNet.Common;
 7using Renci.SshNet.NetConf;
 8
 9namespace Renci.SshNet
 10{
 11    /// <summary>
 12    /// Contains operation for working with NetConf server.
 13    /// </summary>
 14    public class NetConfClient : BaseClient
 15    {
 16        private int _operationTimeout;
 17
 18        /// <summary>
 19        /// Holds <see cref="INetConfSession"/> instance that used to communicate to the server.
 20        /// </summary>
 21        private INetConfSession _netConfSession;
 22
 23        /// <summary>
 24        /// Gets or sets the operation timeout.
 25        /// </summary>
 26        /// <value>
 27        /// The timeout to wait until an operation completes. The default value is negative
 28        /// one (-1) milliseconds, which indicates an infinite time-out period.
 29        /// </value>
 30        /// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> represents a value that is less than 
 31        public TimeSpan OperationTimeout
 32        {
 33            get
 1234            {
 1235                return TimeSpan.FromMilliseconds(_operationTimeout);
 1236            }
 37            set
 12038            {
 12039                var timeoutInMilliseconds = value.TotalMilliseconds;
 12040                if (timeoutInMilliseconds is < -1d or > int.MaxValue)
 641                {
 642                    throw new ArgumentOutOfRangeException(nameof(value), "The timeout must represent a value between -1 
 43                }
 44
 11445                _operationTimeout = (int) timeoutInMilliseconds;
 11446            }
 47        }
 48
 49        /// <summary>
 50        /// Gets the current NetConf session.
 51        /// </summary>
 52        /// <value>
 53        /// The current NetConf session.
 54        /// </value>
 55        internal INetConfSession NetConfSession
 56        {
 957            get { return _netConfSession; }
 58        }
 59
 60        /// <summary>
 61        /// Initializes a new instance of the <see cref="NetConfClient"/> class.
 62        /// </summary>
 63        /// <param name="connectionInfo">The connection info.</param>
 64        /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</except
 65        public NetConfClient(ConnectionInfo connectionInfo)
 1866            : this(connectionInfo, ownsConnectionInfo: false)
 1867        {
 1868        }
 69
 70        /// <summary>
 71        /// Initializes a new instance of the <see cref="NetConfClient"/> class.
 72        /// </summary>
 73        /// <param name="host">Connection host.</param>
 74        /// <param name="port">Connection port.</param>
 75        /// <param name="username">Authentication username.</param>
 76        /// <param name="password">Authentication password.</param>
 77        /// <exception cref="ArgumentNullException"><paramref name="password"/> is <see langword="null"/>.</exception>
 78        /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is <s
 79        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.Mi
 80        [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in
 81        public NetConfClient(string host, int port, string username, string password)
 082            : this(new PasswordConnectionInfo(host, port, username, password), ownsConnectionInfo: true)
 083        {
 084        }
 85
 86        /// <summary>
 87        /// Initializes a new instance of the <see cref="NetConfClient"/> class.
 88        /// </summary>
 89        /// <param name="host">Connection host.</param>
 90        /// <param name="username">Authentication username.</param>
 91        /// <param name="password">Authentication password.</param>
 92        /// <exception cref="ArgumentNullException"><paramref name="password"/> is <see langword="null"/>.</exception>
 93        /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, or <paramref name="username"/> is <s
 94        public NetConfClient(string host, string username, string password)
 095            : this(host, ConnectionInfo.DefaultPort, username, password)
 096        {
 097        }
 98
 99        /// <summary>
 100        /// Initializes a new instance of the <see cref="NetConfClient"/> class.
 101        /// </summary>
 102        /// <param name="host">Connection host.</param>
 103        /// <param name="port">Connection port.</param>
 104        /// <param name="username">Authentication username.</param>
 105        /// <param name="keyFiles">Authentication private key file(s) .</param>
 106        /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <see langword="null"/>.</exception>
 107        /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is 
 108        /// <exception cref="ArgumentOutOfRangeException"><paramref name="port"/> is not within <see cref="IPEndPoint.Mi
 109        [SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope", Justification = "Disposed in
 110        public NetConfClient(string host, int port, string username, params IPrivateKeySource[] keyFiles)
 0111            : this(new PrivateKeyConnectionInfo(host, port, username, keyFiles), ownsConnectionInfo: true)
 0112        {
 0113        }
 114
 115        /// <summary>
 116        /// Initializes a new instance of the <see cref="NetConfClient"/> class.
 117        /// </summary>
 118        /// <param name="host">Connection host.</param>
 119        /// <param name="username">Authentication username.</param>
 120        /// <param name="keyFiles">Authentication private key file(s) .</param>
 121        /// <exception cref="ArgumentNullException"><paramref name="keyFiles"/> is <see langword="null"/>.</exception>
 122        /// <exception cref="ArgumentException"><paramref name="host"/> is invalid, -or- <paramref name="username"/> is 
 123        public NetConfClient(string host, string username, params IPrivateKeySource[] keyFiles)
 0124            : this(host, ConnectionInfo.DefaultPort, username, keyFiles)
 0125        {
 0126        }
 127
 128        /// <summary>
 129        /// Initializes a new instance of the <see cref="NetConfClient"/> class.
 130        /// </summary>
 131        /// <param name="connectionInfo">The connection info.</param>
 132        /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>
 133        /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</except
 134        /// <remarks>
 135        /// If <paramref name="ownsConnectionInfo"/> is <see langword="true"/>, then the
 136        /// connection info will be disposed when this instance is disposed.
 137        /// </remarks>
 138        private NetConfClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo)
 18139            : this(connectionInfo, ownsConnectionInfo, new ServiceFactory())
 18140        {
 18141        }
 142
 143        /// <summary>
 144        /// Initializes a new instance of the <see cref="NetConfClient"/> class.
 145        /// </summary>
 146        /// <param name="connectionInfo">The connection info.</param>
 147        /// <param name="ownsConnectionInfo">Specified whether this instance owns the connection info.</param>
 148        /// <param name="serviceFactory">The factory to use for creating new services.</param>
 149        /// <exception cref="ArgumentNullException"><paramref name="connectionInfo"/> is <see langword="null"/>.</except
 150        /// <exception cref="ArgumentNullException"><paramref name="serviceFactory"/> is <see langword="null"/>.</except
 151        /// <remarks>
 152        /// If <paramref name="ownsConnectionInfo"/> is <see langword="true"/>, then the
 153        /// connection info will be disposed when this instance is disposed.
 154        /// </remarks>
 155        internal NetConfClient(ConnectionInfo connectionInfo, bool ownsConnectionInfo, IServiceFactory serviceFactory)
 138156            : base(connectionInfo, ownsConnectionInfo, serviceFactory)
 138157        {
 138158            _operationTimeout = SshNet.Session.Infinite;
 138159            AutomaticMessageIdHandling = true;
 138160        }
 161
 162        /// <summary>
 163        /// Gets the NetConf server capabilities.
 164        /// </summary>
 165        /// <value>
 166        /// The NetConf server capabilities.
 167        /// </value>
 168        public XmlDocument ServerCapabilities
 169        {
 0170            get { return _netConfSession.ServerCapabilities; }
 171        }
 172
 173        /// <summary>
 174        /// Gets the NetConf client capabilities.
 175        /// </summary>
 176        /// <value>
 177        /// The NetConf client capabilities.
 178        /// </value>
 179        public XmlDocument ClientCapabilities
 180        {
 0181            get { return _netConfSession.ClientCapabilities; }
 182        }
 183
 184        /// <summary>
 185        /// Gets or sets a value indicating whether automatic message id handling is
 186        /// enabled.
 187        /// </summary>
 188        /// <value>
 189        /// <see langword="true"/> if automatic message id handling is enabled; otherwise, <see langword="false"/>.
 190        /// The default value is <see langword="true"/>.
 191        /// </value>
 138192        public bool AutomaticMessageIdHandling { get; set; }
 193
 194        /// <summary>
 195        /// Sends the receive RPC.
 196        /// </summary>
 197        /// <param name="rpc">The RPC.</param>
 198        /// <returns>
 199        /// Reply message to RPC request.
 200        /// </returns>
 201        /// <exception cref="SshConnectionException">Client is not connected.</exception>
 202        public XmlDocument SendReceiveRpc(XmlDocument rpc)
 0203        {
 0204            return _netConfSession.SendReceiveRpc(rpc, AutomaticMessageIdHandling);
 0205        }
 206
 207        /// <summary>
 208        /// Sends the receive RPC.
 209        /// </summary>
 210        /// <param name="xml">The XML.</param>
 211        /// <returns>
 212        /// Reply message to RPC request.
 213        /// </returns>
 214        public XmlDocument SendReceiveRpc(string xml)
 0215        {
 0216            var rpc = new XmlDocument();
 0217            rpc.LoadXml(xml);
 0218            return SendReceiveRpc(rpc);
 0219        }
 220
 221        /// <summary>
 222        /// Sends the close RPC.
 223        /// </summary>
 224        /// <returns>
 225        /// Reply message to closing RPC request.
 226        /// </returns>
 227        /// <exception cref="SshConnectionException">Client is not connected.</exception>
 228        public XmlDocument SendCloseRpc()
 0229        {
 0230            var rpc = new XmlDocument();
 0231            rpc.LoadXml("<?xml version=\"1.0\" encoding=\"UTF-8\"?><rpc message-id=\"6666\" xmlns=\"urn:ietf:params:xml:
 0232            return _netConfSession.SendReceiveRpc(rpc, AutomaticMessageIdHandling);
 0233        }
 234
 235        /// <summary>
 236        /// Called when client is connected to the server.
 237        /// </summary>
 238        protected override void OnConnected()
 120239        {
 120240            base.OnConnected();
 241
 120242            _netConfSession = CreateAndConnectNetConfSession();
 105243        }
 244
 245        /// <summary>
 246        /// Called when client is disconnecting from the server.
 247        /// </summary>
 248        protected override void OnDisconnecting()
 144249        {
 144250            base.OnDisconnecting();
 251
 144252            _netConfSession.Disconnect();
 144253        }
 254
 255        /// <summary>
 256        /// Releases unmanaged and - optionally - managed resources.
 257        /// </summary>
 258        /// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langwor
 259        protected override void Dispose(bool disposing)
 162260        {
 162261            base.Dispose(disposing);
 262
 162263            if (disposing)
 120264            {
 120265                if (_netConfSession != null)
 96266                {
 96267                    _netConfSession.Dispose();
 96268                    _netConfSession = null;
 96269                }
 120270            }
 162271        }
 272
 273        private INetConfSession CreateAndConnectNetConfSession()
 120274        {
 120275            var netConfSession = ServiceFactory.CreateNetConfSession(Session, _operationTimeout);
 276            try
 120277            {
 120278                netConfSession.Connect();
 105279                return netConfSession;
 280            }
 15281            catch
 15282            {
 15283                netConfSession.Dispose();
 15284                throw;
 285            }
 105286        }
 287    }
 288}