ForwardedPortLocalTest.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. namespace Renci.SshNet.Tests.Classes
  9. {
  10. /// <summary>
  11. /// Provides functionality for local port forwarding
  12. /// </summary>
  13. [TestClass]
  14. public partial class ForwardedPortLocalTest : TestBase
  15. {
  16. [TestMethod]
  17. [WorkItem(713)]
  18. [Owner("kenneth_aa")]
  19. [TestCategory("PortForwarding")]
  20. [Description("Test if calling Stop on ForwardedPortLocal instance causes wait.")]
  21. public void Test_PortForwarding_Local_Stop_Hangs_On_Wait()
  22. {
  23. using (var client = new SshClient(Resources.HOST, Int32.Parse(Resources.PORT), Resources.USERNAME, Resources.PASSWORD))
  24. {
  25. client.Connect();
  26. var port1 = new ForwardedPortLocal("localhost", 8084, "www.google.com", 80);
  27. client.AddForwardedPort(port1);
  28. port1.Exception += delegate(object sender, ExceptionEventArgs e)
  29. {
  30. Assert.Fail(e.Exception.ToString());
  31. };
  32. port1.Start();
  33. bool hasTestedTunnel = false;
  34. System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state)
  35. {
  36. try
  37. {
  38. var url = "http://www.google.com/";
  39. Debug.WriteLine("Starting web request to \"" + url + "\"");
  40. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  41. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  42. Assert.IsNotNull(response);
  43. Debug.WriteLine("Http Response status code: " + response.StatusCode.ToString());
  44. response.Close();
  45. hasTestedTunnel = true;
  46. }
  47. catch (Exception ex)
  48. {
  49. Assert.Fail(ex.ToString());
  50. }
  51. });
  52. // Wait for the web request to complete.
  53. while (!hasTestedTunnel)
  54. {
  55. System.Threading.Thread.Sleep(1000);
  56. }
  57. try
  58. {
  59. // Try stop the port forwarding, wait 3 seconds and fail if it is still started.
  60. System.Threading.ThreadPool.QueueUserWorkItem(delegate(object state)
  61. {
  62. Debug.WriteLine("Trying to stop port forward.");
  63. port1.Stop();
  64. Debug.WriteLine("Port forwarding stopped.");
  65. });
  66. System.Threading.Thread.Sleep(3000);
  67. if (port1.IsStarted)
  68. {
  69. Assert.Fail("Port forwarding not stopped.");
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. Assert.Fail(ex.ToString());
  75. }
  76. client.Disconnect();
  77. Debug.WriteLine("Success.");
  78. }
  79. }
  80. [TestMethod]
  81. [Description("Test passing null to AddForwardedPort hosts (local).")]
  82. [ExpectedException(typeof(ArgumentNullException))]
  83. public void Test_AddForwardedPort_Local_Hosts_Are_Null()
  84. {
  85. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  86. {
  87. client.Connect();
  88. var port1 = new ForwardedPortLocal(null, 8080, null, 80);
  89. client.AddForwardedPort(port1);
  90. client.Disconnect();
  91. }
  92. }
  93. }
  94. }