ForwardedPortLocalTest.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Renci.SshNet.Common;
  3. using Renci.SshNet.Tests.Common;
  4. using Renci.SshNet.Tests.Properties;
  5. using System;
  6. using System.Diagnostics;
  7. using System.Net;
  8. using System.Threading;
  9. namespace Renci.SshNet.Tests.Classes
  10. {
  11. /// <summary>
  12. /// Provides functionality for local port forwarding
  13. /// </summary>
  14. [TestClass]
  15. public partial class ForwardedPortLocalTest : TestBase
  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. /// <summary>
  95. ///A test for ForwardedPortRemote Constructor
  96. ///</summary>
  97. [TestMethod()]
  98. public void Test_ForwardedPortRemote()
  99. {
  100. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  101. {
  102. #region Example SshClient AddForwardedPort Start Stop ForwardedPortLocal
  103. client.Connect();
  104. var port = new ForwardedPortLocal(8082, "www.cnn.com", 80);
  105. client.AddForwardedPort(port);
  106. port.Exception += delegate(object sender, ExceptionEventArgs e)
  107. {
  108. Console.WriteLine(e.Exception.ToString());
  109. };
  110. port.Start();
  111. Thread.Sleep(1000 * 60 * 20); // Wait 20 minutes for port to be forwarded
  112. port.Stop();
  113. #endregion
  114. }
  115. Assert.Inconclusive("TODO: Implement code to verify target");
  116. }
  117. /// <summary>
  118. ///A test for ForwardedPortLocal Constructor
  119. ///</summary>
  120. [TestMethod()]
  121. public void ForwardedPortLocalConstructorTest()
  122. {
  123. string boundHost = string.Empty; // TODO: Initialize to an appropriate value
  124. uint boundPort = 0; // TODO: Initialize to an appropriate value
  125. string host = string.Empty; // TODO: Initialize to an appropriate value
  126. uint port = 0; // TODO: Initialize to an appropriate value
  127. ForwardedPortLocal target = new ForwardedPortLocal(boundHost, boundPort, host, port);
  128. Assert.Inconclusive("TODO: Implement code to verify target");
  129. }
  130. /// <summary>
  131. ///A test for ForwardedPortLocal Constructor
  132. ///</summary>
  133. [TestMethod()]
  134. public void ForwardedPortLocalConstructorTest1()
  135. {
  136. uint boundPort = 0; // TODO: Initialize to an appropriate value
  137. string host = string.Empty; // TODO: Initialize to an appropriate value
  138. uint port = 0; // TODO: Initialize to an appropriate value
  139. ForwardedPortLocal target = new ForwardedPortLocal(boundPort, host, port);
  140. Assert.Inconclusive("TODO: Implement code to verify target");
  141. }
  142. /// <summary>
  143. ///A test for Stop
  144. ///</summary>
  145. [TestMethod()]
  146. public void StopTest()
  147. {
  148. uint boundPort = 0; // TODO: Initialize to an appropriate value
  149. string host = string.Empty; // TODO: Initialize to an appropriate value
  150. uint port = 0; // TODO: Initialize to an appropriate value
  151. ForwardedPortLocal target = new ForwardedPortLocal(boundPort, host, port); // TODO: Initialize to an appropriate value
  152. target.Stop();
  153. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  154. }
  155. /// <summary>
  156. ///A test for Start
  157. ///</summary>
  158. [TestMethod()]
  159. public void StartTest()
  160. {
  161. uint boundPort = 0; // TODO: Initialize to an appropriate value
  162. string host = string.Empty; // TODO: Initialize to an appropriate value
  163. uint port = 0; // TODO: Initialize to an appropriate value
  164. ForwardedPortLocal target = new ForwardedPortLocal(boundPort, host, port); // TODO: Initialize to an appropriate value
  165. target.Start();
  166. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  167. }
  168. /// <summary>
  169. ///A test for Dispose
  170. ///</summary>
  171. [TestMethod()]
  172. public void DisposeTest()
  173. {
  174. uint boundPort = 0; // TODO: Initialize to an appropriate value
  175. string host = string.Empty; // TODO: Initialize to an appropriate value
  176. uint port = 0; // TODO: Initialize to an appropriate value
  177. ForwardedPortLocal target = new ForwardedPortLocal(boundPort, host, port); // TODO: Initialize to an appropriate value
  178. target.Dispose();
  179. Assert.Inconclusive("A method that does not return a value cannot be verified.");
  180. }
  181. }
  182. }