ForwardedPortLocalTest.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. [TestCategory("integration")]
  22. [Description("Test if calling Stop on ForwardedPortLocal instance causes wait.")]
  23. public void Test_PortForwarding_Local_Stop_Hangs_On_Wait()
  24. {
  25. using (var client = new SshClient(Resources.HOST, Int32.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD))
  26. {
  27. client.Connect();
  28. var port1 = new ForwardedPortLocal("localhost", 8084, "www.google.com", 80);
  29. client.AddForwardedPort(port1);
  30. port1.Exception += delegate(object sender, ExceptionEventArgs e)
  31. {
  32. Assert.Fail(e.Exception.ToString());
  33. };
  34. port1.Start();
  35. bool hasTestedTunnel = false;
  36. System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state)
  37. {
  38. try
  39. {
  40. var url = "http://www.google.com/";
  41. Debug.WriteLine("Starting web request to \"" + url + "\"");
  42. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  43. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  44. Assert.IsNotNull(response);
  45. Debug.WriteLine("Http Response status code: " + response.StatusCode.ToString());
  46. response.Close();
  47. hasTestedTunnel = true;
  48. }
  49. catch (Exception ex)
  50. {
  51. Assert.Fail(ex.ToString());
  52. }
  53. });
  54. // Wait for the web request to complete.
  55. while (!hasTestedTunnel)
  56. {
  57. System.Threading.Thread.Sleep(1000);
  58. }
  59. try
  60. {
  61. // Try stop the port forwarding, wait 3 seconds and fail if it is still started.
  62. System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state)
  63. {
  64. Debug.WriteLine("Trying to stop port forward.");
  65. port1.Stop();
  66. Debug.WriteLine("Port forwarding stopped.");
  67. });
  68. System.Threading.Thread.Sleep(3000);
  69. if (port1.IsStarted)
  70. {
  71. Assert.Fail("Port forwarding not stopped.");
  72. }
  73. }
  74. catch (Exception ex)
  75. {
  76. Assert.Fail(ex.ToString());
  77. }
  78. client.Disconnect();
  79. Debug.WriteLine("Success.");
  80. }
  81. }
  82. [TestMethod]
  83. public void ConstructorShouldThrowArgumentNullExceptionWhenBoundHostIsNull()
  84. {
  85. try
  86. {
  87. new ForwardedPortLocal(null, 8080, Resources.HOST, 80);
  88. Assert.Fail();
  89. }
  90. catch (ArgumentNullException ex)
  91. {
  92. Assert.IsNull(ex.InnerException);
  93. Assert.AreEqual("boundHost", ex.ParamName);
  94. }
  95. }
  96. [TestMethod]
  97. public void ConstructorShouldNotThrowExceptionWhenBoundHostIsEmpty()
  98. {
  99. var boundHost = string.Empty;
  100. var forwardedPort = new ForwardedPortLocal(boundHost, 8080, Resources.HOST, 80);
  101. Assert.AreSame(boundHost, forwardedPort.BoundHost);
  102. }
  103. [TestMethod]
  104. public void ConstructorShouldNotThrowExceptionWhenBoundHostIsInvalidDnsName()
  105. {
  106. const string boundHost = "in_valid_host.";
  107. var forwardedPort = new ForwardedPortLocal(boundHost, 8080, Resources.HOST, 80);
  108. Assert.AreSame(boundHost, forwardedPort.BoundHost);
  109. }
  110. [TestMethod]
  111. public void ConstructorShouldThrowArgumentNullExceptionWhenHostIsNull()
  112. {
  113. try
  114. {
  115. new ForwardedPortLocal(Resources.HOST, 8080, null, 80);
  116. Assert.Fail();
  117. }
  118. catch (ArgumentNullException ex)
  119. {
  120. Assert.IsNull(ex.InnerException);
  121. Assert.AreEqual("host", ex.ParamName);
  122. }
  123. }
  124. [TestMethod]
  125. public void ConstructorShouldNotThrowExceptionWhenHostIsEmpty()
  126. {
  127. var host = string.Empty;
  128. var forwardedPort = new ForwardedPortLocal(Resources.HOST, 8080, string.Empty, 80);
  129. Assert.AreSame(host, forwardedPort.Host);
  130. }
  131. [TestMethod]
  132. public void ConstructorShouldNotThrowExceptionWhenHostIsInvalidDnsName()
  133. {
  134. const string host = "in_valid_host.";
  135. var forwardedPort = new ForwardedPortLocal(Resources.HOST, 8080, host, 80);
  136. Assert.AreSame(host, forwardedPort.Host);
  137. }
  138. /// <summary>
  139. ///A test for ForwardedPortRemote Constructor
  140. ///</summary>
  141. [TestMethod]
  142. [TestCategory("integration")]
  143. public void Test_ForwardedPortRemote()
  144. {
  145. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  146. {
  147. #region Example SshClient AddForwardedPort Start Stop ForwardedPortLocal
  148. client.Connect();
  149. var port = new ForwardedPortLocal(8082, "www.cnn.com", 80);
  150. client.AddForwardedPort(port);
  151. port.Exception += delegate(object sender, ExceptionEventArgs e)
  152. {
  153. Console.WriteLine(e.Exception.ToString());
  154. };
  155. port.Start();
  156. Thread.Sleep(1000 * 60 * 20); // Wait 20 minutes for port to be forwarded
  157. port.Stop();
  158. #endregion
  159. }
  160. Assert.Inconclusive("TODO: Implement code to verify target");
  161. }
  162. }
  163. }