2
0

ForwardedPortLocalTest.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Diagnostics;
  2. #if !NET
  3. using System.Net;
  4. #endif
  5. using Renci.SshNet.Common;
  6. namespace Renci.SshNet.IntegrationTests.OldIntegrationTests
  7. {
  8. /// <summary>
  9. /// Provides functionality for local port forwarding
  10. /// </summary>
  11. [TestClass]
  12. public class ForwardedPortLocalTest : IntegrationTestBase
  13. {
  14. [TestMethod]
  15. [WorkItem(713)]
  16. [Owner("Kenneth_aa")]
  17. [TestCategory("PortForwarding")]
  18. [Description("Test if calling Stop on ForwardedPortLocal instance causes wait.")]
  19. public void Test_PortForwarding_Local_Stop_Hangs_On_Wait()
  20. {
  21. using (var client = new SshClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  22. {
  23. client.Connect();
  24. using var port1 = new ForwardedPortLocal("localhost", 8085, "www.google.com", 80);
  25. client.AddForwardedPort(port1);
  26. port1.Exception += delegate (object sender, ExceptionEventArgs e)
  27. {
  28. Assert.Fail(e.Exception.ToString());
  29. };
  30. port1.Start();
  31. var hasTestedTunnel = false;
  32. _ = ThreadPool.QueueUserWorkItem(delegate (object state)
  33. {
  34. try
  35. {
  36. var url = "http://www.google.com/";
  37. Debug.WriteLine("Starting web request to \"" + url + "\"");
  38. #if NET
  39. var httpClient = new HttpClient();
  40. var response = httpClient.GetAsync(url)
  41. .ConfigureAwait(false)
  42. .GetAwaiter()
  43. .GetResult();
  44. #else
  45. var request = (HttpWebRequest)WebRequest.Create(url);
  46. var response = (HttpWebResponse)request.GetResponse();
  47. #endif // NET
  48. Assert.IsNotNull(response);
  49. Debug.WriteLine("Http Response status code: " + response.StatusCode.ToString());
  50. response.Dispose();
  51. hasTestedTunnel = true;
  52. }
  53. catch (Exception ex)
  54. {
  55. Assert.Fail(ex.ToString());
  56. }
  57. });
  58. // Wait for the web request to complete.
  59. while (!hasTestedTunnel)
  60. {
  61. Thread.Sleep(1000);
  62. }
  63. try
  64. {
  65. // Try stop the port forwarding, wait 3 seconds and fail if it is still started.
  66. _ = ThreadPool.QueueUserWorkItem(delegate (object state)
  67. {
  68. Debug.WriteLine("Trying to stop port forward.");
  69. port1.Stop();
  70. Debug.WriteLine("Port forwarding stopped.");
  71. });
  72. Thread.Sleep(3000);
  73. if (port1.IsStarted)
  74. {
  75. Assert.Fail("Port forwarding not stopped.");
  76. }
  77. }
  78. catch (Exception ex)
  79. {
  80. Assert.Fail(ex.ToString());
  81. }
  82. client.RemoveForwardedPort(port1);
  83. client.Disconnect();
  84. Debug.WriteLine("Success.");
  85. }
  86. }
  87. [TestMethod]
  88. public void Test_PortForwarding_Local_Without_Connecting()
  89. {
  90. using (var client = new SshClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
  91. {
  92. using var port1 = new ForwardedPortLocal("localhost", 8084, "www.renci.org", 80);
  93. Assert.ThrowsException<SshConnectionException>(() => client.AddForwardedPort(port1));
  94. }
  95. }
  96. }
  97. }