2
0

ConnectionTest.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using System.IO;
  7. using Renci.SshClient.Tests.Properties;
  8. using System.Security.Authentication;
  9. using Renci.SshClient.Common;
  10. using System.Net;
  11. namespace Renci.SshClient.Tests
  12. {
  13. [TestClass]
  14. public class ConnectionTest
  15. {
  16. [TestMethod]
  17. public void Test_Connect_Using_Correct_Password()
  18. {
  19. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  20. {
  21. client.Connect();
  22. client.Disconnect();
  23. }
  24. }
  25. [TestMethod]
  26. [ExpectedException(typeof(SshAuthenticationException))]
  27. public void Test_Connect_Using_Invalid_Password()
  28. {
  29. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, "invalid password"))
  30. {
  31. client.Connect();
  32. client.Disconnect();
  33. }
  34. }
  35. [TestMethod]
  36. public void Test_Connect_Using_Rsa_Key_Without_PassPhrase()
  37. {
  38. MemoryStream keyFileStream = new MemoryStream(Encoding.ASCII.GetBytes(Resources.RSA_KEY_WITHOUT_PASS));
  39. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, new PrivateKeyFile(keyFileStream)))
  40. {
  41. client.Connect();
  42. client.Disconnect();
  43. }
  44. }
  45. [TestMethod]
  46. public void Test_Connect_Using_RsaKey_With_PassPhrase()
  47. {
  48. MemoryStream keyFileStream = new MemoryStream(Encoding.ASCII.GetBytes(Resources.RSA_KEY_WITH_PASS));
  49. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, new PrivateKeyFile(keyFileStream, Resources.PASSWORD)))
  50. {
  51. client.Connect();
  52. client.Disconnect();
  53. }
  54. }
  55. [TestMethod]
  56. [ExpectedException(typeof(SshPassPhraseNullOrEmptyException))]
  57. public void Test_Connect_Using_Key_With_Empty_PassPhrase()
  58. {
  59. MemoryStream keyFileStream = new MemoryStream(Encoding.ASCII.GetBytes(Resources.RSA_KEY_WITH_PASS));
  60. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, new PrivateKeyFile(keyFileStream, null)))
  61. {
  62. client.Connect();
  63. client.Disconnect();
  64. }
  65. }
  66. [TestMethod]
  67. public void Test_Connect_Using_DsaKey_Without_PassPhrase()
  68. {
  69. MemoryStream keyFileStream = new MemoryStream(Encoding.ASCII.GetBytes(Resources.DSA_KEY_WITHOUT_PASS));
  70. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, new PrivateKeyFile(keyFileStream)))
  71. {
  72. client.Connect();
  73. client.Disconnect();
  74. }
  75. }
  76. [TestMethod]
  77. public void Test_Connect_Using_DsaKey_With_PassPhrase()
  78. {
  79. MemoryStream keyFileStream = new MemoryStream(Encoding.ASCII.GetBytes(Resources.DSA_KEY_WITH_PASS));
  80. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, new PrivateKeyFile(keyFileStream, Resources.PASSWORD)))
  81. {
  82. client.Connect();
  83. client.Disconnect();
  84. }
  85. }
  86. [TestMethod]
  87. [ExpectedException(typeof(SshAuthenticationException))]
  88. public void Test_Connect_Using_Invalid_PrivateKey()
  89. {
  90. MemoryStream keyFileStream = new MemoryStream(Encoding.ASCII.GetBytes(Resources.INVALID_KEY));
  91. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, new PrivateKeyFile(keyFileStream)))
  92. {
  93. client.Connect();
  94. client.Disconnect();
  95. }
  96. }
  97. [TestMethod]
  98. public void Test_Connect_Using_Multiple_PrivateKeys()
  99. {
  100. using (var client = new SshClient(Resources.HOST, Resources.USERNAME,
  101. new PrivateKeyFile(new MemoryStream(Encoding.ASCII.GetBytes(Resources.INVALID_KEY))),
  102. new PrivateKeyFile(new MemoryStream(Encoding.ASCII.GetBytes(Resources.DSA_KEY_WITH_PASS)), Resources.PASSWORD),
  103. new PrivateKeyFile(new MemoryStream(Encoding.ASCII.GetBytes(Resources.RSA_KEY_WITH_PASS)), Resources.PASSWORD),
  104. new PrivateKeyFile(new MemoryStream(Encoding.ASCII.GetBytes(Resources.RSA_KEY_WITHOUT_PASS))),
  105. new PrivateKeyFile(new MemoryStream(Encoding.ASCII.GetBytes(Resources.DSA_KEY_WITHOUT_PASS)))
  106. ))
  107. {
  108. client.Connect();
  109. client.Disconnect();
  110. }
  111. }
  112. [TestMethod]
  113. public void Test_Connect_Then_Reconnect()
  114. {
  115. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  116. {
  117. client.Connect();
  118. client.Disconnect();
  119. client.Connect();
  120. client.Disconnect();
  121. }
  122. }
  123. [TestMethod]
  124. [ExpectedException(typeof(ArgumentNullException))]
  125. public void Test_ConnectionInfo_NullHost()
  126. {
  127. var connectionInfo = new PasswordConnectionInfo(null, null, null);
  128. }
  129. [TestMethod]
  130. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  131. public void Test_ConnectionInfo_SmallPortNumber()
  132. {
  133. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, IPEndPoint.MinPort - 1, null, null);
  134. }
  135. [TestMethod]
  136. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  137. public void Test_ConnectionInfo_BigPortNumber()
  138. {
  139. var connectionInfo = new PasswordConnectionInfo(Resources.HOST, IPEndPoint.MaxPort + 1, null, null);
  140. }
  141. }
  142. }