2
0

ForwardedPortLocalTest.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. public void ConstructorShouldThrowArgumentNullExceptionWhenBoundHostIsNull()
  83. {
  84. try
  85. {
  86. new ForwardedPortLocal(null, 8080, Resources.HOST, 80);
  87. Assert.Fail();
  88. }
  89. catch (ArgumentNullException ex)
  90. {
  91. Assert.IsNull(ex.InnerException);
  92. Assert.AreEqual("boundHost", ex.ParamName);
  93. }
  94. }
  95. [TestMethod]
  96. public void ConstructorShouldNotThrowExceptionWhenBoundHostIsEmpty()
  97. {
  98. var boundHost = string.Empty;
  99. var forwardedPort = new ForwardedPortLocal(boundHost, 8080, Resources.HOST, 80);
  100. Assert.AreSame(boundHost, forwardedPort.BoundHost);
  101. }
  102. [TestMethod]
  103. public void ConstructorShouldNotThrowExceptionWhenBoundHostIsInvalidDnsName()
  104. {
  105. const string boundHost = "in_valid_host.";
  106. var forwardedPort = new ForwardedPortLocal(boundHost, 8080, Resources.HOST, 80);
  107. Assert.AreSame(boundHost, forwardedPort.BoundHost);
  108. }
  109. [TestMethod]
  110. public void ConstructorShouldThrowArgumentNullExceptionWhenHostIsNull()
  111. {
  112. try
  113. {
  114. new ForwardedPortLocal(Resources.HOST, 8080, null, 80);
  115. Assert.Fail();
  116. }
  117. catch (ArgumentNullException ex)
  118. {
  119. Assert.IsNull(ex.InnerException);
  120. Assert.AreEqual("host", ex.ParamName);
  121. }
  122. }
  123. [TestMethod]
  124. public void ConstructorShouldNotThrowExceptionWhenHostIsEmpty()
  125. {
  126. var host = string.Empty;
  127. var forwardedPort = new ForwardedPortLocal(Resources.HOST, 8080, string.Empty, 80);
  128. Assert.AreSame(host, forwardedPort.Host);
  129. }
  130. [TestMethod]
  131. public void ConstructorShouldNotThrowExceptionWhenHostIsInvalidDnsName()
  132. {
  133. const string host = "in_valid_host.";
  134. var forwardedPort = new ForwardedPortLocal(Resources.HOST, 8080, host, 80);
  135. Assert.AreSame(host, forwardedPort.Host);
  136. }
  137. /// <summary>
  138. ///A test for ForwardedPortRemote Constructor
  139. ///</summary>
  140. [TestMethod()]
  141. public void Test_ForwardedPortRemote()
  142. {
  143. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  144. {
  145. #region Example SshClient AddForwardedPort Start Stop ForwardedPortLocal
  146. client.Connect();
  147. var port = new ForwardedPortLocal(8082, "www.cnn.com", 80);
  148. client.AddForwardedPort(port);
  149. port.Exception += delegate(object sender, ExceptionEventArgs e)
  150. {
  151. Console.WriteLine(e.Exception.ToString());
  152. };
  153. port.Start();
  154. Thread.Sleep(1000 * 60 * 20); // Wait 20 minutes for port to be forwarded
  155. port.Stop();
  156. #endregion
  157. }
  158. Assert.Inconclusive("TODO: Implement code to verify target");
  159. }
  160. }
  161. }