forked from sshnet/SSH.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForwardedPortLocalTest.cs
More file actions
158 lines (139 loc) · 5.88 KB
/
ForwardedPortLocalTest.cs
File metadata and controls
158 lines (139 loc) · 5.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
using System.Diagnostics;
using Renci.SshNet.Common;
namespace Renci.SshNet.IntegrationTests.OldIntegrationTests
{
/// <summary>
/// Provides functionality for local port forwarding
/// </summary>
[TestClass]
public class ForwardedPortLocalTest : IntegrationTestBase
{
[TestMethod]
[WorkItem(713)]
[Owner("Kenneth_aa")]
[TestCategory("PortForwarding")]
[Description("Test if calling Stop on ForwardedPortLocal instance causes wait.")]
public void Test_PortForwarding_Local_Stop_Hangs_On_Wait()
{
using (var client = new SshClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
{
client.Connect();
using var port1 = new ForwardedPortLocal("localhost", 8085, "www.google.com", 80);
client.AddForwardedPort(port1);
port1.Exception += delegate (object sender, ExceptionEventArgs e)
{
Assert.Fail(e.Exception.ToString());
};
port1.Start();
var hasTestedTunnel = false;
_ = ThreadPool.QueueUserWorkItem(delegate (object state)
{
try
{
var url = "http://www.google.com/";
Debug.WriteLine("Starting web request to \"" + url + "\"");
#if NET6_0_OR_GREATER
var httpClient = new HttpClient();
var response = httpClient.GetAsync(url)
.ConfigureAwait(false)
.GetAwaiter()
.GetResult();
#else
var request = (HttpWebRequest) WebRequest.Create(url);
var response = (HttpWebResponse) request.GetResponse();
#endif // NET6_0_OR_GREATER
Assert.IsNotNull(response);
Debug.WriteLine("Http Response status code: " + response.StatusCode.ToString());
response.Dispose();
hasTestedTunnel = true;
}
catch (Exception ex)
{
Assert.Fail(ex.ToString());
}
});
// Wait for the web request to complete.
while (!hasTestedTunnel)
{
Thread.Sleep(1000);
}
try
{
// Try stop the port forwarding, wait 3 seconds and fail if it is still started.
_ = ThreadPool.QueueUserWorkItem(delegate (object state)
{
Debug.WriteLine("Trying to stop port forward.");
port1.Stop();
Debug.WriteLine("Port forwarding stopped.");
});
Thread.Sleep(3000);
if (port1.IsStarted)
{
Assert.Fail("Port forwarding not stopped.");
}
}
catch (Exception ex)
{
Assert.Fail(ex.ToString());
}
client.RemoveForwardedPort(port1);
client.Disconnect();
Debug.WriteLine("Success.");
}
}
[TestMethod]
[ExpectedException(typeof(SshConnectionException))]
public void Test_PortForwarding_Local_Without_Connecting()
{
using (var client = new SshClient(SshServerHostName, SshServerPort, User.UserName, User.Password))
{
using var port1 = new ForwardedPortLocal("localhost", 8084, "www.renci.org", 80);
client.AddForwardedPort(port1);
port1.Exception += delegate (object sender, ExceptionEventArgs e)
{
Assert.Fail(e.Exception.ToString());
};
port1.Start();
var test = Parallel.For(0,
100,
counter =>
{
var start = DateTime.Now;
#if NET6_0_OR_GREATER
var httpClient = new HttpClient();
using (var response = httpClient.GetAsync("http://localhost:8084").GetAwaiter().GetResult())
{
var data = ReadStream(response.Content.ReadAsStream());
#else
var request = (HttpWebRequest) WebRequest.Create("http://localhost:8084");
using (var response = (HttpWebResponse) request.GetResponse())
{
var data = ReadStream(response.GetResponseStream());
#endif // NET6_0_OR_GREATER
var end = DateTime.Now;
Debug.WriteLine(string.Format("Request# {2}: Lenght: {0} Time: {1}", data.Length, end - start, counter));
}
});
}
}
private static byte[] ReadStream(Stream stream)
{
var buffer = new byte[1024];
using (var ms = new MemoryStream())
{
while (true)
{
var read = stream.Read(buffer, 0, buffer.Length);
if (read > 0)
{
ms.Write(buffer, 0, read);
}
else
{
return ms.ToArray();
}
}
}
}
}
}