TestPortForwarding.cs 6.3 KB

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