TestPortForwarding.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Net;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. using Renci.SshNet.Tests.Properties;
  7. using Renci.SshNet.Common;
  8. using System.Threading;
  9. namespace Renci.SshNet.Tests.SshClientTests
  10. {
  11. /// <summary>
  12. /// Summary description for UnitTest1
  13. /// </summary>
  14. [TestClass]
  15. public partial class TestPortForwarding
  16. {
  17. [TestMethod]
  18. [WorkItem(713)]
  19. [Owner("kenneth_aa")]
  20. [Description("Test if calling Stop on ForwardedPortLocal instance causes wait.")]
  21. public void Test_PortForwarding_Local_Stop_Hangs_On_Wait()
  22. {
  23. using (var client = new SshClient(Resources.HOST, Int32.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD))
  24. {
  25. client.Connect();
  26. var port1 = new ForwardedPortLocal("localhost", 8084, "www.google.com", 80);
  27. client.AddForwardedPort(port1);
  28. port1.Exception += delegate(object sender, ExceptionEventArgs e)
  29. {
  30. Assert.Fail(e.Exception.ToString());
  31. };
  32. port1.Start();
  33. bool hasTestedTunnel = false;
  34. System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state)
  35. {
  36. try
  37. {
  38. var url = "http://www.google.com/";
  39. Debug.WriteLine("Starting web request to \"" + url + "\"");
  40. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  41. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  42. Assert.IsNotNull(response);
  43. Debug.WriteLine("Http Response status code: " + response.StatusCode.ToString());
  44. response.Close();
  45. hasTestedTunnel = true;
  46. }
  47. catch (Exception ex)
  48. {
  49. Assert.Fail(ex.ToString());
  50. }
  51. });
  52. // Wait for the web request to complete.
  53. while(!hasTestedTunnel)
  54. {
  55. System.Threading.Thread.Sleep(1000);
  56. }
  57. try
  58. {
  59. // Try stop the port forwarding, wait 3 seconds and fail if it is still started.
  60. System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state)
  61. {
  62. Debug.WriteLine("Trying to stop port forward.");
  63. port1.Stop();
  64. Debug.WriteLine("Port forwarding stopped.");
  65. });
  66. System.Threading.Thread.Sleep(3000);
  67. if (port1.IsStarted)
  68. {
  69. Assert.Fail("Port forwarding not stopped.");
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. Assert.Fail(ex.ToString());
  75. }
  76. client.Disconnect();
  77. Debug.WriteLine("Success.");
  78. }
  79. }
  80. [TestMethod]
  81. [Description("Test passing null to AddForwardedPort hosts (local).")]
  82. [ExpectedException(typeof(ArgumentNullException))]
  83. public void Test_AddForwardedPort_Local_Hosts_Are_Null()
  84. {
  85. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  86. {
  87. client.Connect();
  88. var port1 = new ForwardedPortLocal(null, 8080, null, 80);
  89. client.AddForwardedPort(port1);
  90. client.Disconnect();
  91. }
  92. }
  93. [TestMethod]
  94. [Description("Test passing null to AddForwardedPort hosts (remote).")]
  95. [ExpectedException(typeof(ArgumentNullException))]
  96. public void Test_AddForwardedPort_Remote_Hosts_Are_Null()
  97. {
  98. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  99. {
  100. client.Connect();
  101. var port1 = new ForwardedPortRemote(null, 8080, null, 80);
  102. client.AddForwardedPort(port1);
  103. client.Disconnect();
  104. }
  105. }
  106. [TestMethod]
  107. [Description("Test passing string.Empty to AddForwardedPort host (remote).")]
  108. [ExpectedException(typeof(ArgumentException))]
  109. public void Test_AddForwardedPort_Remote_Hosts_Are_Empty()
  110. {
  111. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  112. {
  113. client.Connect();
  114. var port1 = new ForwardedPortRemote(string.Empty, 8080, string.Empty, 80);
  115. client.AddForwardedPort(port1);
  116. client.Disconnect();
  117. }
  118. }
  119. [TestMethod]
  120. [Description("Test passing string.Empty to AddForwardedPort host (local).")]
  121. [ExpectedException(typeof(ArgumentException))]
  122. public void Test_AddForwardedPort_Local_Hosts_Are_Empty()
  123. {
  124. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  125. {
  126. client.Connect();
  127. var port1 = new ForwardedPortLocal(string.Empty, 8080, string.Empty, 80);
  128. client.AddForwardedPort(port1);
  129. client.Disconnect();
  130. }
  131. }
  132. [TestMethod]
  133. [Description("Test passing invalid port numbers to AddForwardedPort.")]
  134. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  135. public void Test_AddForwardedPort_Invalid_PortNumber()
  136. {
  137. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  138. {
  139. client.Connect();
  140. var port1 = new ForwardedPortRemote("localhost", IPEndPoint.MaxPort + 1, "www.renci.org", IPEndPoint.MaxPort + 1);
  141. client.AddForwardedPort(port1);
  142. client.Disconnect();
  143. }
  144. }
  145. [TestMethod]
  146. [Description("Test passing null to constructor of PortForwardEventArgs.")]
  147. [ExpectedException(typeof(ArgumentNullException))]
  148. public void Test_PortForwardEventArgs_Host_Null()
  149. {
  150. var args = new PortForwardEventArgs(null, 80);
  151. }
  152. [TestMethod]
  153. [Description("Test passing string.Empty to constructor of PortForwardEventArgs.")]
  154. [ExpectedException(typeof(ArgumentException))]
  155. public void Test_PortForwardEventArgs_Host_Empty()
  156. {
  157. var args = new PortForwardEventArgs(string.Empty, 80);
  158. }
  159. [TestMethod]
  160. [Description("Test passing an invalid port to constructor of PortForwardEventArgs.")]
  161. [ExpectedException(typeof(ArgumentOutOfRangeException))]
  162. public void Test_PortForwardEventArgs_Port_Invalid()
  163. {
  164. var args = new PortForwardEventArgs("string", IPEndPoint.MaxPort + 1);
  165. }
  166. private static byte[] ReadStream(Stream stream)
  167. {
  168. byte[] buffer = new byte[1024];
  169. using (MemoryStream ms = new MemoryStream())
  170. {
  171. while (true)
  172. {
  173. int read = stream.Read(buffer, 0, buffer.Length);
  174. if (read > 0)
  175. ms.Write(buffer, 0, read);
  176. else
  177. return ms.ToArray();
  178. }
  179. }
  180. }
  181. }
  182. }