ScpClientTest.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using System;
  2. using System.Text;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Renci.SshNet.Common;
  5. using Renci.SshNet.Tests.Common;
  6. namespace Renci.SshNet.Tests.Classes
  7. {
  8. /// <summary>
  9. /// Provides SCP client functionality.
  10. /// </summary>
  11. [TestClass]
  12. public partial class ScpClientTest : TestBase
  13. {
  14. private Random _random;
  15. [TestInitialize]
  16. public void SetUp()
  17. {
  18. _random = new Random();
  19. }
  20. [TestMethod]
  21. public void Ctor_ConnectionInfo_Null()
  22. {
  23. const ConnectionInfo connectionInfo = null;
  24. try
  25. {
  26. _ = new ScpClient(connectionInfo);
  27. Assert.Fail();
  28. }
  29. catch (ArgumentNullException ex)
  30. {
  31. Assert.IsNull(ex.InnerException);
  32. Assert.AreEqual("connectionInfo", ex.ParamName);
  33. }
  34. }
  35. [TestMethod]
  36. public void Ctor_ConnectionInfo_NotNull()
  37. {
  38. var connectionInfo = new ConnectionInfo("HOST", "USER", new PasswordAuthenticationMethod("USER", "PWD"));
  39. var client = new ScpClient(connectionInfo);
  40. Assert.AreEqual(16 * 1024U, client.BufferSize);
  41. Assert.AreSame(connectionInfo, client.ConnectionInfo);
  42. Assert.IsFalse(client.IsConnected);
  43. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.KeepAliveInterval);
  44. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.OperationTimeout);
  45. Assert.AreSame(RemotePathTransformation.DoubleQuote, client.RemotePathTransformation);
  46. Assert.IsNull(client.Session);
  47. }
  48. [TestMethod]
  49. public void Ctor_HostAndPortAndUsernameAndPassword()
  50. {
  51. var host = _random.Next().ToString();
  52. var port = _random.Next(1, 100);
  53. var userName = _random.Next().ToString();
  54. var password = _random.Next().ToString();
  55. var client = new ScpClient(host, port, userName, password);
  56. Assert.AreEqual(16 * 1024U, client.BufferSize);
  57. Assert.IsNotNull(client.ConnectionInfo);
  58. Assert.IsFalse(client.IsConnected);
  59. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.KeepAliveInterval);
  60. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.OperationTimeout);
  61. Assert.AreSame(RemotePathTransformation.DoubleQuote, client.RemotePathTransformation);
  62. Assert.IsNull(client.Session);
  63. var passwordConnectionInfo = client.ConnectionInfo as PasswordConnectionInfo;
  64. Assert.IsNotNull(passwordConnectionInfo);
  65. Assert.AreEqual(host, passwordConnectionInfo.Host);
  66. Assert.AreEqual(port, passwordConnectionInfo.Port);
  67. Assert.AreSame(userName, passwordConnectionInfo.Username);
  68. Assert.IsNotNull(passwordConnectionInfo.AuthenticationMethods);
  69. Assert.AreEqual(1, passwordConnectionInfo.AuthenticationMethods.Count);
  70. var passwordAuthentication = passwordConnectionInfo.AuthenticationMethods[0] as PasswordAuthenticationMethod;
  71. Assert.IsNotNull(passwordAuthentication);
  72. Assert.AreEqual(userName, passwordAuthentication.Username);
  73. Assert.IsTrue(Encoding.UTF8.GetBytes(password).IsEqualTo(passwordAuthentication.Password));
  74. }
  75. [TestMethod]
  76. public void Ctor_HostAndUsernameAndPassword()
  77. {
  78. var host = _random.Next().ToString();
  79. var userName = _random.Next().ToString();
  80. var password = _random.Next().ToString();
  81. var client = new ScpClient(host, userName, password);
  82. Assert.AreEqual(16 * 1024U, client.BufferSize);
  83. Assert.IsNotNull(client.ConnectionInfo);
  84. Assert.IsFalse(client.IsConnected);
  85. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.KeepAliveInterval);
  86. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.OperationTimeout);
  87. Assert.AreSame(RemotePathTransformation.DoubleQuote, client.RemotePathTransformation);
  88. Assert.IsNull(client.Session);
  89. var passwordConnectionInfo = client.ConnectionInfo as PasswordConnectionInfo;
  90. Assert.IsNotNull(passwordConnectionInfo);
  91. Assert.AreEqual(host, passwordConnectionInfo.Host);
  92. Assert.AreEqual(22, passwordConnectionInfo.Port);
  93. Assert.AreSame(userName, passwordConnectionInfo.Username);
  94. Assert.IsNotNull(passwordConnectionInfo.AuthenticationMethods);
  95. Assert.AreEqual(1, passwordConnectionInfo.AuthenticationMethods.Count);
  96. var passwordAuthentication = passwordConnectionInfo.AuthenticationMethods[0] as PasswordAuthenticationMethod;
  97. Assert.IsNotNull(passwordAuthentication);
  98. Assert.AreEqual(userName, passwordAuthentication.Username);
  99. Assert.IsTrue(Encoding.UTF8.GetBytes(password).IsEqualTo(passwordAuthentication.Password));
  100. }
  101. [TestMethod]
  102. public void Ctor_HostAndPortAndUsernameAndPrivateKeys()
  103. {
  104. var host = _random.Next().ToString();
  105. var port = _random.Next(1, 100);
  106. var userName = _random.Next().ToString();
  107. var privateKeys = new[] {GetRsaKey(), GetDsaKey()};
  108. var client = new ScpClient(host, port, userName, privateKeys);
  109. Assert.AreEqual(16 * 1024U, client.BufferSize);
  110. Assert.IsNotNull(client.ConnectionInfo);
  111. Assert.IsFalse(client.IsConnected);
  112. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.KeepAliveInterval);
  113. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.OperationTimeout);
  114. Assert.AreSame(RemotePathTransformation.DoubleQuote, client.RemotePathTransformation);
  115. Assert.IsNull(client.Session);
  116. var privateKeyConnectionInfo = client.ConnectionInfo as PrivateKeyConnectionInfo;
  117. Assert.IsNotNull(privateKeyConnectionInfo);
  118. Assert.AreEqual(host, privateKeyConnectionInfo.Host);
  119. Assert.AreEqual(port, privateKeyConnectionInfo.Port);
  120. Assert.AreSame(userName, privateKeyConnectionInfo.Username);
  121. Assert.IsNotNull(privateKeyConnectionInfo.AuthenticationMethods);
  122. Assert.AreEqual(1, privateKeyConnectionInfo.AuthenticationMethods.Count);
  123. var privateKeyAuthentication = privateKeyConnectionInfo.AuthenticationMethods[0] as PrivateKeyAuthenticationMethod;
  124. Assert.IsNotNull(privateKeyAuthentication);
  125. Assert.AreEqual(userName, privateKeyAuthentication.Username);
  126. Assert.IsNotNull(privateKeyAuthentication.KeyFiles);
  127. Assert.AreEqual(privateKeys.Length, privateKeyAuthentication.KeyFiles.Count);
  128. Assert.IsTrue(privateKeyAuthentication.KeyFiles.Contains(privateKeys[0]));
  129. Assert.IsTrue(privateKeyAuthentication.KeyFiles.Contains(privateKeys[1]));
  130. }
  131. [TestMethod]
  132. public void Ctor_HostAndUsernameAndPrivateKeys()
  133. {
  134. var host = _random.Next().ToString();
  135. var userName = _random.Next().ToString();
  136. var privateKeys = new[] { GetRsaKey(), GetDsaKey() };
  137. var client = new ScpClient(host, userName, privateKeys);
  138. Assert.AreEqual(16 * 1024U, client.BufferSize);
  139. Assert.IsNotNull(client.ConnectionInfo);
  140. Assert.IsFalse(client.IsConnected);
  141. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.KeepAliveInterval);
  142. Assert.AreEqual(new TimeSpan(0, 0, 0, 0, -1), client.OperationTimeout);
  143. Assert.AreSame(RemotePathTransformation.DoubleQuote, client.RemotePathTransformation);
  144. Assert.IsNull(client.Session);
  145. var privateKeyConnectionInfo = client.ConnectionInfo as PrivateKeyConnectionInfo;
  146. Assert.IsNotNull(privateKeyConnectionInfo);
  147. Assert.AreEqual(host, privateKeyConnectionInfo.Host);
  148. Assert.AreEqual(22, privateKeyConnectionInfo.Port);
  149. Assert.AreSame(userName, privateKeyConnectionInfo.Username);
  150. Assert.IsNotNull(privateKeyConnectionInfo.AuthenticationMethods);
  151. Assert.AreEqual(1, privateKeyConnectionInfo.AuthenticationMethods.Count);
  152. var privateKeyAuthentication = privateKeyConnectionInfo.AuthenticationMethods[0] as PrivateKeyAuthenticationMethod;
  153. Assert.IsNotNull(privateKeyAuthentication);
  154. Assert.AreEqual(userName, privateKeyAuthentication.Username);
  155. Assert.IsNotNull(privateKeyAuthentication.KeyFiles);
  156. Assert.AreEqual(privateKeys.Length, privateKeyAuthentication.KeyFiles.Count);
  157. Assert.IsTrue(privateKeyAuthentication.KeyFiles.Contains(privateKeys[0]));
  158. Assert.IsTrue(privateKeyAuthentication.KeyFiles.Contains(privateKeys[1]));
  159. }
  160. [TestMethod]
  161. public void RemotePathTransformation_Value_NotNull()
  162. {
  163. var client = new ScpClient("HOST", 22, "USER", "PWD");
  164. Assert.AreSame(RemotePathTransformation.DoubleQuote, client.RemotePathTransformation);
  165. client.RemotePathTransformation = RemotePathTransformation.ShellQuote;
  166. Assert.AreSame(RemotePathTransformation.ShellQuote, client.RemotePathTransformation);
  167. }
  168. [TestMethod]
  169. public void RemotePathTransformation_Value_Null()
  170. {
  171. var client = new ScpClient("HOST", 22, "USER", "PWD")
  172. {
  173. RemotePathTransformation = RemotePathTransformation.ShellQuote
  174. };
  175. try
  176. {
  177. client.RemotePathTransformation = null;
  178. Assert.Fail();
  179. }
  180. catch (ArgumentNullException ex)
  181. {
  182. Assert.IsNull(ex.InnerException);
  183. Assert.AreEqual("value", ex.ParamName);
  184. }
  185. Assert.AreSame(RemotePathTransformation.ShellQuote, client.RemotePathTransformation);
  186. }
  187. private PrivateKeyFile GetRsaKey()
  188. {
  189. using (var stream = GetData("Key.RSA.txt"))
  190. {
  191. return new PrivateKeyFile(stream);
  192. }
  193. }
  194. private PrivateKeyFile GetDsaKey()
  195. {
  196. using (var stream = GetData("Key.SSH2.DSA.txt"))
  197. {
  198. return new PrivateKeyFile(stream);
  199. }
  200. }
  201. }
  202. }