forked from chronoxor/NetCoreServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWssTests.cs
386 lines (327 loc) · 14.5 KB
/
WssTests.cs
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using NetCoreServer;
using Xunit;
namespace tests
{
class EchoWssClient : WssClient
{
public bool IsWsConnected { get; set; }
public bool Connected { get; set; }
public bool Disconnected { get; set; }
public int Received { get; set; }
public bool Errors { get; set; }
public EchoWssClient(SslContext context, string address, int port) : base(context, address, port) {}
public static SslContext CreateContext()
{
return new SslContext(SslProtocols.Tls13, new X509Certificate2("client.pfx", "qwerty"), (sender, certificate, chain, sslPolicyErrors) => true);
}
public override void OnWsConnecting(HttpRequest request)
{
request.SetBegin("GET", "/");
request.SetHeader("Host", "localhost");
request.SetHeader("Origin", "http://localhost");
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(WsNonce));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
request.SetBody();
}
public override void OnWsConnected(HttpResponse response) { IsWsConnected = true; Connected = true; }
public override void OnWsDisconnected() { IsWsConnected = false; Disconnected = true; }
public override void OnWsReceived(byte[] buffer, long offset, long size) { Received += (int)size; }
protected override void OnError(SocketError error) { Errors = true; }
}
class EchoWssSession : WssSession
{
public bool Connected { get; set; }
public bool Disconnected { get; set; }
public bool Errors { get; set; }
public EchoWssSession(WssServer server) : base(server) {}
public override void OnWsConnected(HttpResponse response) { Connected = true; }
public override void OnWsDisconnected() { Disconnected = true; }
public override void OnWsReceived(byte[] buffer, long offset, long size) { SendBinaryAsync(buffer, offset, size); }
protected override void OnError(SocketError error) { Errors = true; }
}
class EchoWssServer : WssServer
{
public bool Started { get; set; }
public bool Stopped { get; set; }
public bool Connected { get; set; }
public bool Disconnected { get; set; }
public int Clients { get; set; }
public bool Errors { get; set; }
public EchoWssServer(SslContext context, IPAddress address, int port) : base(context, address, port) {}
public static SslContext CreateContext()
{
return new SslContext(SslProtocols.Tls13, new X509Certificate2("server.pfx", "qwerty"), (sender, certificate, chain, sslPolicyErrors) => true);
}
protected override SslSession CreateSession() { return new EchoWssSession(this); }
protected override void OnStarted() { Started = true; }
protected override void OnStopped() { Stopped = true; }
protected override void OnConnected(SslSession session) { Connected = true; Clients++; }
protected override void OnDisconnected(SslSession session) { Disconnected = true; Clients = Math.Max(Clients - 1, 0); }
protected override void OnError(SocketError error) { Errors = true; }
}
public class WssTests
{
[Fact(DisplayName = "WebSocket secure server test")]
public void WssServerTest()
{
string address = "127.0.0.1";
int port = 8444;
// Create and prepare a new SSL server context
var serverContext = EchoWssServer.CreateContext();
// Create and start Echo server
var server = new EchoWssServer(serverContext, IPAddress.Any, port);
Assert.True(server.Start());
while (!server.IsStarted)
Thread.Yield();
// Create and prepare a new SSL client context
var clientContext = EchoWssClient.CreateContext();
// Create and connect Echo client
var client = new EchoWssClient(clientContext, address, port);
Assert.True(client.ConnectAsync());
while (!client.IsWsConnected || (server.Clients != 1))
Thread.Yield();
// Send a message to the Echo server
client.SendTextAsync("test");
// Wait for all data processed...
while (client.Received != 4)
Thread.Yield();
// Disconnect the Echo client
Assert.True(client.CloseAsync(1000));
while (client.IsWsConnected || (server.Clients != 0))
Thread.Yield();
// Stop the Echo server
Assert.True(server.Stop());
while (server.IsStarted)
Thread.Yield();
// Check the Echo server state
Assert.True(server.Started);
Assert.True(server.Stopped);
Assert.True(server.Connected);
Assert.True(server.Disconnected);
Assert.True(server.BytesSent > 0);
Assert.True(server.BytesReceived > 0);
Assert.True(!server.Errors);
// Check the Echo client state
Assert.True(client.Connected);
Assert.True(client.Disconnected);
Assert.True(client.BytesSent > 0);
Assert.True(client.BytesReceived > 0);
Assert.True(!client.Errors);
}
[Fact(DisplayName = "WebSocket secure server multicast test")]
public void WssServerMulticastTest()
{
string address = "127.0.0.1";
int port = 8445;
// Create and prepare a new SSL server context
var serverContext = EchoWssServer.CreateContext();
// Create and start Echo server
var server = new EchoWssServer(serverContext, IPAddress.Any, port);
Assert.True(server.Start());
while (!server.IsStarted)
Thread.Yield();
// Create and prepare a new SSL client context
var clientContext = EchoWssClient.CreateContext();
// Create and connect Echo client
var client1 = new EchoWssClient(clientContext, address, port);
Assert.True(client1.ConnectAsync());
while (!client1.IsWsConnected || (server.Clients != 1))
Thread.Yield();
// Multicast some data to all clients
server.MulticastText("test");
// Wait for all data processed...
while (client1.Received != 4)
Thread.Yield();
// Create and connect Echo client
var client2 = new EchoWssClient(clientContext, address, port);
Assert.True(client2.ConnectAsync());
while (!client2.IsWsConnected || (server.Clients != 2))
Thread.Yield();
// Multicast some data to all clients
server.MulticastText("test");
// Wait for all data processed...
while ((client1.Received != 8) || (client2.Received != 4))
Thread.Yield();
// Create and connect Echo client
var client3 = new EchoWssClient(clientContext, address, port);
Assert.True(client3.ConnectAsync());
while (!client3.IsWsConnected || (server.Clients != 3))
Thread.Yield();
// Multicast some data to all clients
server.MulticastText("test");
// Wait for all data processed...
while ((client1.Received != 12) || (client2.Received != 8) || (client3.Received != 4))
Thread.Yield();
// Disconnect the Echo client
Assert.True(client1.CloseAsync(1000));
while (client1.IsWsConnected || (server.Clients != 2))
Thread.Yield();
// Multicast some data to all clients
server.MulticastText("test");
// Wait for all data processed...
while ((client1.Received != 12) || (client2.Received != 12) || (client3.Received != 8))
Thread.Yield();
// Disconnect the Echo client
Assert.True(client2.CloseAsync(1000));
while (client2.IsWsConnected || (server.Clients != 1))
Thread.Yield();
// Multicast some data to all clients
server.MulticastText("test");
// Wait for all data processed...
while ((client1.Received != 12) || (client2.Received != 12) || (client3.Received != 12))
Thread.Yield();
// Disconnect the Echo client
Assert.True(client3.CloseAsync(1000));
while (client3.IsWsConnected || (server.Clients != 0))
Thread.Yield();
// Stop the Echo server
Assert.True(server.Stop());
while (server.IsStarted)
Thread.Yield();
// Check the Echo server state
Assert.True(server.Started);
Assert.True(server.Stopped);
Assert.True(server.Connected);
Assert.True(server.Disconnected);
Assert.True(server.BytesSent > 0);
Assert.True(server.BytesReceived > 0);
Assert.True(!server.Errors);
// Check the Echo client state
Assert.True(client1.BytesSent > 0);
Assert.True(client2.BytesSent > 0);
Assert.True(client3.BytesSent > 0);
Assert.True(client1.BytesReceived > 0);
Assert.True(client2.BytesReceived > 0);
Assert.True(client3.BytesReceived > 0);
Assert.True(!client1.Errors);
Assert.True(!client2.Errors);
Assert.True(!client3.Errors);
}
[Fact(DisplayName = "WebSocket secure server random test")]
public void WssServerRandomTest()
{
string address = "127.0.0.1";
int port = 8446;
// Create and prepare a new SSL server context
var serverContext = EchoWssServer.CreateContext();
// Create and start Echo server
var server = new EchoWssServer(serverContext, IPAddress.Any, port);
Assert.True(server.Start());
while (!server.IsStarted)
Thread.Yield();
// Test duration in seconds
int duration = 10;
// Create and prepare a new SSL client context
var clientContext = EchoWssClient.CreateContext();
// Clients collection
var clients = new List<EchoWssClient>();
// Start random test
var rand = new Random();
var start = DateTime.UtcNow;
while ((DateTime.UtcNow - start).TotalSeconds < duration)
{
// Disconnect all clients
if ((rand.Next() % 1000) == 0)
{
server.CloseAll(1000);
}
// Create a new client and connect
else if ((rand.Next() % 100) == 0)
{
if (clients.Count < 100)
{
// Create and connect Echo client
var client = new EchoWssClient(clientContext, address, port);
clients.Add(client);
client.ConnectAsync();
while (!client.IsWsConnected)
Thread.Yield();
}
}
// Connect/Disconnect the random client
else if ((rand.Next() % 100) == 0)
{
if (clients.Count > 0)
{
int index = rand.Next() % clients.Count;
var client = clients[index];
if (client.IsWsConnected)
{
client.CloseAsync(1000);
while (client.IsWsConnected)
Thread.Yield();
}
else
{
client.ConnectAsync();
while (!client.IsWsConnected)
Thread.Yield();
}
}
}
// Reconnect the random client
else if ((rand.Next() % 100) == 0)
{
if (clients.Count > 0)
{
int index = rand.Next() % clients.Count;
var client = clients[index];
if (client.IsWsConnected)
{
client.ReconnectAsync();
while (!client.IsWsConnected)
Thread.Yield();
}
}
}
// Multicast a message to all clients
else if ((rand.Next() % 10) == 0)
{
server.MulticastText("test");
}
// Send a message from the random client
else if ((rand.Next() % 1) == 0)
{
if (clients.Count > 0)
{
int index = rand.Next() % clients.Count;
var client = clients[index];
if (client.IsWsConnected)
client.SendTextAsync("test");
}
}
// Sleep for a while...
Thread.Sleep(1);
}
// Disconnect clients
foreach (var client in clients)
{
client.CloseAsync(1000);
while (client.IsWsConnected)
Thread.Yield();
}
// Stop the Echo server
Assert.True(server.Stop());
while (server.IsStarted)
Thread.Yield();
// Check the Echo server state
Assert.True(server.Started);
Assert.True(server.Stopped);
Assert.True(server.Connected);
Assert.True(server.Disconnected);
Assert.True(server.BytesSent > 0);
Assert.True(server.BytesReceived > 0);
Assert.True(!server.Errors);
}
}
}