Skip to content

Commit 031f0a1

Browse files
committed
Fix name of argument in ArgumentOutOfRangeException thrown in OperationTimeout.
Added tests for OperationTimeout.
1 parent 660b075 commit 031f0a1

File tree

4 files changed

+263
-24
lines changed

4 files changed

+263
-24
lines changed

src/Renci.SshNet.Tests/Classes/NetConfClientTest.cs

Lines changed: 103 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,111 @@
55

66
namespace Renci.SshNet.Tests.Classes
77
{
8-
// TODO: Please help with documentation here, as I don't know the details, specially for the methods not documented.
9-
/// <summary>
10-
///
11-
/// </summary>
128
[TestClass]
13-
public partial class NetConfClientTest : TestBase
9+
public class NetConfClientTest : TestBase
1410
{
11+
private Random _random;
12+
13+
[TestInitialize]
14+
public void SetUp()
15+
{
16+
_random = new Random();
17+
}
18+
19+
[TestMethod]
20+
public void OperationTimeout_Default()
21+
{
22+
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
23+
var target = new NetConfClient(connectionInfo);
24+
25+
var actual = target.OperationTimeout;
26+
27+
Assert.AreEqual(TimeSpan.FromMilliseconds(-1), actual);
28+
}
29+
30+
[TestMethod]
31+
public void OperationTimeout_InsideLimits()
32+
{
33+
var operationTimeout = TimeSpan.FromMilliseconds(_random.Next(0, int.MaxValue - 1));
34+
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
35+
var target = new NetConfClient(connectionInfo)
36+
{
37+
OperationTimeout = operationTimeout
38+
};
39+
40+
var actual = target.OperationTimeout;
41+
42+
Assert.AreEqual(operationTimeout, actual);
43+
}
44+
45+
[TestMethod]
46+
public void OperationTimeout_LowerLimit()
47+
{
48+
var operationTimeout = TimeSpan.FromMilliseconds(-1);
49+
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
50+
var target = new NetConfClient(connectionInfo)
51+
{
52+
OperationTimeout = operationTimeout
53+
};
54+
55+
var actual = target.OperationTimeout;
56+
57+
Assert.AreEqual(operationTimeout, actual);
58+
}
59+
60+
[TestMethod]
61+
public void OperationTimeout_UpperLimit()
62+
{
63+
var operationTimeout = TimeSpan.FromMilliseconds(int.MaxValue);
64+
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
65+
var target = new NetConfClient(connectionInfo)
66+
{
67+
OperationTimeout = operationTimeout
68+
};
69+
70+
var actual = target.OperationTimeout;
71+
72+
Assert.AreEqual(operationTimeout, actual);
73+
}
74+
75+
[TestMethod]
76+
public void OperationTimeout_LessThanLowerLimit()
77+
{
78+
var operationTimeout = TimeSpan.FromMilliseconds(-2);
79+
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
80+
var target = new NetConfClient(connectionInfo);
81+
82+
try
83+
{
84+
target.OperationTimeout = operationTimeout;
85+
}
86+
catch (ArgumentOutOfRangeException ex)
87+
{
88+
Assert.IsNull(ex.InnerException);
89+
Assert.AreEqual("The timeout must represent a value between -1 and Int32.MaxValue, inclusive." + Environment.NewLine + "Parameter name: " + ex.ParamName, ex.Message);
90+
Assert.AreEqual("value", ex.ParamName);
91+
}
92+
}
93+
94+
[TestMethod]
95+
public void OperationTimeout_GreaterThanLowerLimit()
96+
{
97+
var operationTimeout = TimeSpan.FromMilliseconds(int.MaxValue).Add(TimeSpan.FromMilliseconds(1));
98+
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
99+
var target = new NetConfClient(connectionInfo);
100+
101+
try
102+
{
103+
target.OperationTimeout = operationTimeout;
104+
}
105+
catch (ArgumentOutOfRangeException ex)
106+
{
107+
Assert.IsNull(ex.InnerException);
108+
Assert.AreEqual("The timeout must represent a value between -1 and Int32.MaxValue, inclusive." + Environment.NewLine + "Parameter name: " + ex.ParamName, ex.Message);
109+
Assert.AreEqual("value", ex.ParamName);
110+
}
111+
}
112+
15113
/// <summary>
16114
///A test for NetConfClient Constructor
17115
///</summary>
@@ -146,23 +244,6 @@ public void ServerCapabilitiesTest()
146244
Assert.Inconclusive("Verify the correctness of this test method.");
147245
}
148246

149-
/// <summary>
150-
///A test for OperationTimeout
151-
///</summary>
152-
[TestMethod]
153-
[Ignore] // placeholder for actual test
154-
public void OperationTimeoutTest()
155-
{
156-
ConnectionInfo connectionInfo = null; // TODO: Initialize to an appropriate value
157-
NetConfClient target = new NetConfClient(connectionInfo); // TODO: Initialize to an appropriate value
158-
TimeSpan expected = new TimeSpan(); // TODO: Initialize to an appropriate value
159-
TimeSpan actual;
160-
target.OperationTimeout = expected;
161-
actual = target.OperationTimeout;
162-
Assert.AreEqual(expected, actual);
163-
Assert.Inconclusive("Verify the correctness of this test method.");
164-
}
165-
166247
/// <summary>
167248
///A test for ClientCapabilities
168249
///</summary>

src/Renci.SshNet.Tests/Classes/SftpClientTest.cs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,140 @@ namespace Renci.SshNet.Tests.Classes
1616
[TestClass]
1717
public partial class SftpClientTest : TestBase
1818
{
19+
private Random _random;
20+
21+
[TestInitialize]
22+
public void SetUp()
23+
{
24+
_random = new Random();
25+
}
26+
27+
[TestMethod]
28+
public void OperationTimeout_Default()
29+
{
30+
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
31+
var target = new SftpClient(connectionInfo);
32+
33+
var actual = target.OperationTimeout;
34+
35+
Assert.AreEqual(TimeSpan.FromMilliseconds(-1), actual);
36+
}
37+
38+
[TestMethod]
39+
public void OperationTimeout_InsideLimits()
40+
{
41+
var operationTimeout = TimeSpan.FromMilliseconds(_random.Next(0, int.MaxValue - 1));
42+
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
43+
var target = new SftpClient(connectionInfo)
44+
{
45+
OperationTimeout = operationTimeout
46+
};
47+
48+
var actual = target.OperationTimeout;
49+
50+
Assert.AreEqual(operationTimeout, actual);
51+
}
52+
53+
[TestMethod]
54+
public void OperationTimeout_LowerLimit()
55+
{
56+
var operationTimeout = TimeSpan.FromMilliseconds(-1);
57+
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
58+
var target = new SftpClient(connectionInfo)
59+
{
60+
OperationTimeout = operationTimeout
61+
};
62+
63+
var actual = target.OperationTimeout;
64+
65+
Assert.AreEqual(operationTimeout, actual);
66+
}
67+
68+
[TestMethod]
69+
public void OperationTimeout_UpperLimit()
70+
{
71+
var operationTimeout = TimeSpan.FromMilliseconds(int.MaxValue);
72+
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
73+
var target = new SftpClient(connectionInfo)
74+
{
75+
OperationTimeout = operationTimeout
76+
};
77+
78+
var actual = target.OperationTimeout;
79+
80+
Assert.AreEqual(operationTimeout, actual);
81+
}
82+
83+
[TestMethod]
84+
public void OperationTimeout_LessThanLowerLimit()
85+
{
86+
var operationTimeout = TimeSpan.FromMilliseconds(-2);
87+
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
88+
var target = new SftpClient(connectionInfo);
89+
90+
try
91+
{
92+
target.OperationTimeout = operationTimeout;
93+
}
94+
catch (ArgumentOutOfRangeException ex)
95+
{
96+
Assert.IsNull(ex.InnerException);
97+
Assert.AreEqual("The timeout must represent a value between -1 and Int32.MaxValue, inclusive." + Environment.NewLine + "Parameter name: " + ex.ParamName, ex.Message);
98+
Assert.AreEqual("value", ex.ParamName);
99+
}
100+
}
101+
102+
[TestMethod]
103+
public void OperationTimeout_GreaterThanLowerLimit()
104+
{
105+
var operationTimeout = TimeSpan.FromMilliseconds(int.MaxValue).Add(TimeSpan.FromMilliseconds(1));
106+
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
107+
var target = new SftpClient(connectionInfo);
108+
109+
try
110+
{
111+
target.OperationTimeout = operationTimeout;
112+
}
113+
catch (ArgumentOutOfRangeException ex)
114+
{
115+
Assert.IsNull(ex.InnerException);
116+
Assert.AreEqual("The timeout must represent a value between -1 and Int32.MaxValue, inclusive." + Environment.NewLine + "Parameter name: " + ex.ParamName, ex.Message);
117+
Assert.AreEqual("value", ex.ParamName);
118+
}
119+
}
120+
121+
[TestMethod]
122+
public void OperationTimeout_Disposed()
123+
{
124+
var connectionInfo = new PasswordConnectionInfo("host", 22, "admin", "pwd");
125+
var target = new SftpClient(connectionInfo);
126+
target.Dispose();
127+
128+
// getter
129+
try
130+
{
131+
var actual = target.OperationTimeout;
132+
Assert.Fail("Should have failed, but returned: " + actual);
133+
}
134+
catch (ObjectDisposedException ex)
135+
{
136+
Assert.IsNull(ex.InnerException);
137+
Assert.AreEqual(typeof(SftpClient).FullName, ex.ObjectName);
138+
}
139+
140+
// setter
141+
try
142+
{
143+
target.OperationTimeout = TimeSpan.FromMilliseconds(5);
144+
Assert.Fail();
145+
}
146+
catch (ObjectDisposedException ex)
147+
{
148+
Assert.IsNull(ex.InnerException);
149+
Assert.AreEqual(typeof(SftpClient).FullName, ex.ObjectName);
150+
}
151+
}
152+
19153
/// <summary>
20154
///A test for SftpClient Constructor
21155
///</summary>

src/Renci.SshNet/NetConfClient.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,30 @@ public class NetConfClient : BaseClient
2727
/// The timeout to wait until an operation completes. The default value is negative
2828
/// one (-1) milliseconds, which indicates an infinite time-out period.
2929
/// </value>
30+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> represents a value that is less than -1 or greater than <see cref="Int32.MaxValue"/> milliseconds.</exception>
3031
public TimeSpan OperationTimeout {
3132
get { return TimeSpan.FromMilliseconds(_operationTimeout); }
3233
set
3334
{
3435
var timeoutInMilliseconds = value.TotalMilliseconds;
3536
if (timeoutInMilliseconds < -1d || timeoutInMilliseconds > int.MaxValue)
36-
throw new ArgumentOutOfRangeException("timeout", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.");
37+
throw new ArgumentOutOfRangeException("value", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.");
3738

3839
_operationTimeout = (int) timeoutInMilliseconds;
3940
}
4041
}
4142

43+
/// <summary>
44+
/// Gets the current NetConf session.
45+
/// </summary>
46+
/// <value>
47+
/// The current NetConf session.
48+
/// </value>
49+
internal INetConfSession NetConfSession
50+
{
51+
get { return _netConfSession; }
52+
}
53+
4254
#region Constructors
4355

4456
/// <summary>

src/Renci.SshNet/SftpClient.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class SftpClient : BaseClient
4444
/// one (-1) milliseconds, which indicates an infinite timeout period.
4545
/// </value>
4646
/// <exception cref="ObjectDisposedException">The method was called after the client was disposed.</exception>
47+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value"/> represents a value that is less than -1 or greater than <see cref="Int32.MaxValue"/> milliseconds.</exception>
4748
public TimeSpan OperationTimeout
4849
{
4950
get
@@ -58,7 +59,7 @@ public TimeSpan OperationTimeout
5859

5960
var timeoutInMilliseconds = value.TotalMilliseconds;
6061
if (timeoutInMilliseconds < -1d || timeoutInMilliseconds > int.MaxValue)
61-
throw new ArgumentOutOfRangeException("timeout", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.");
62+
throw new ArgumentOutOfRangeException("value", "The timeout must represent a value between -1 and Int32.MaxValue, inclusive.");
6263

6364
_operationTimeout = (int) timeoutInMilliseconds;
6465
}
@@ -141,6 +142,17 @@ public int ProtocolVersion
141142
}
142143
}
143144

145+
/// <summary>
146+
/// Gets the current SFTP session.
147+
/// </summary>
148+
/// <value>
149+
/// The current SFTP session.
150+
/// </value>
151+
internal ISftpSession SftpSession
152+
{
153+
get { return _sftpSession; }
154+
}
155+
144156
#region Constructors
145157

146158
/// <summary>

0 commit comments

Comments
 (0)