2
0

ForwardedPortLocalTest.NET40.cs 3.8 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.IO;
  8. using System.Net;
  9. namespace Renci.SshNet.Tests.SshClientTests
  10. {
  11. /// <summary>
  12. /// Summary description for UnitTest1
  13. /// </summary>
  14. [TestClass]
  15. public partial class ForwardedPortLocalTest : TestBase
  16. {
  17. [TestMethod]
  18. [ExpectedException(typeof(SshConnectionException))]
  19. public void Test_PortForwarding_Local_Without_Connecting()
  20. {
  21. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  22. {
  23. var port1 = new ForwardedPortLocal("localhost", 8084, "www.renci.org", 80);
  24. client.AddForwardedPort(port1);
  25. port1.Exception += delegate(object sender, ExceptionEventArgs e)
  26. {
  27. Assert.Fail(e.Exception.ToString());
  28. };
  29. port1.Start();
  30. System.Threading.Tasks.Parallel.For(0, 100,
  31. //new ParallelOptions
  32. //{
  33. // MaxDegreeOfParallelism = 20,
  34. //},
  35. (counter) =>
  36. {
  37. var start = DateTime.Now;
  38. var req = HttpWebRequest.Create("http://localhost:8084");
  39. using (var response = req.GetResponse())
  40. {
  41. var data = ReadStream(response.GetResponseStream());
  42. var end = DateTime.Now;
  43. Debug.WriteLine(string.Format("Request# {2}: Lenght: {0} Time: {1}", data.Length, (end - start), counter));
  44. }
  45. }
  46. );
  47. }
  48. }
  49. [TestMethod]
  50. public void Test_PortForwarding_Local()
  51. {
  52. using (var client = new SshClient(Resources.HOST, Resources.USERNAME, Resources.PASSWORD))
  53. {
  54. client.Connect();
  55. var port1 = new ForwardedPortLocal("localhost", 8084, "www.renci.org", 80);
  56. client.AddForwardedPort(port1);
  57. port1.Exception += delegate(object sender, ExceptionEventArgs e)
  58. {
  59. Assert.Fail(e.Exception.ToString());
  60. };
  61. port1.Start();
  62. System.Threading.Tasks.Parallel.For(0, 100,
  63. //new ParallelOptions
  64. //{
  65. // MaxDegreeOfParallelism = 20,
  66. //},
  67. (counter) =>
  68. {
  69. var start = DateTime.Now;
  70. var req = HttpWebRequest.Create("http://localhost:8084");
  71. using (var response = req.GetResponse())
  72. {
  73. var data = ReadStream(response.GetResponseStream());
  74. var end = DateTime.Now;
  75. Debug.WriteLine(string.Format("Request# {2}: Length: {0} Time: {1}", data.Length, (end - start), counter));
  76. }
  77. }
  78. );
  79. }
  80. }
  81. private static byte[] ReadStream(Stream stream)
  82. {
  83. byte[] buffer = new byte[1024];
  84. using (MemoryStream ms = new MemoryStream())
  85. {
  86. while (true)
  87. {
  88. int read = stream.Read(buffer, 0, buffer.Length);
  89. if (read > 0)
  90. ms.Write(buffer, 0, read);
  91. else
  92. return ms.ToArray();
  93. }
  94. }
  95. }
  96. }
  97. }