-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathCoapServerTestsBase.cs
443 lines (365 loc) · 14.4 KB
/
CoapServerTestsBase.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Waher.Networking.CoAP.ContentFormats;
using Waher.Networking.CoAP.Options;
using Waher.Networking.Sniffers;
using Waher.Runtime.Console;
using Waher.Security.DTLS;
namespace Waher.Networking.CoAP.Test
{
public abstract class CoapServerTestsBase
{
protected CoapEndpoint server;
protected CoapEndpoint client;
protected IDtlsCredentials clientCredentials;
protected const string ResponseTest = "Hello world.";
protected const string ResponseRoot =
"************************************************************\r\n" +
"CoAP RFC 7252\r\n" +
"************************************************************\r\n" +
"This server is using the Waher.Networking.CoAP framework\r\n" +
"published under the following license:\r\n" +
"https://github.com/PeterWaher/IoTGateway#license\r\n" +
"\r\n" +
"(c) 2017 Waher Data AB\r\n" +
"************************************************************";
protected const string ResponseLarge =
"/-------------------------------------------------------------\\\r\n" +
"| RESOURCE BLOCK NO. 1 OF 5 |\r\n" +
"| [each line contains 64 bytes] |\r\n" +
"\\-------------------------------------------------------------/\r\n" +
"/-------------------------------------------------------------\\\r\n" +
"| RESOURCE BLOCK NO. 2 OF 5 |\r\n" +
"| [each line contains 64 bytes] |\r\n" +
"\\-------------------------------------------------------------/\r\n" +
"/-------------------------------------------------------------\\\r\n" +
"| RESOURCE BLOCK NO. 3 OF 5 |\r\n" +
"| [each line contains 64 bytes] |\r\n" +
"\\-------------------------------------------------------------/\r\n" +
"/-------------------------------------------------------------\\\r\n" +
"| RESOURCE BLOCK NO. 4 OF 5 |\r\n" +
"| [each line contains 64 bytes] |\r\n" +
"\\-------------------------------------------------------------/\r\n" +
"/-------------------------------------------------------------\\\r\n" +
"| RESOURCE BLOCK NO. 5 OF 5 |\r\n" +
"| [each line contains 64 bytes] |\r\n" +
"\\-------------------------------------------------------------/";
protected const string ResponseHierarchical =
"</path/sub2>;title=\"Hierarchical link description sub-resource\"," +
"</path/sub3>;title=\"Hierarchical link description sub-resource\"," +
"</path/sub1>;title=\"Hierarchical link description sub-resource\"";
protected virtual void SetupClientServer()
{
this.server = new CoapEndpoint(new int[] { CoapEndpoint.DefaultCoapPort }, null, null, null, false, true,
new ConsoleOutSniffer(BinaryPresentationMethod.Hexadecimal, LineEnding.NewLine));
this.client = new CoapEndpoint(new int[] { CoapEndpoint.DefaultCoapPort + 2 }, null, null, null, true, false);
this.clientCredentials = null;
}
[TestInitialize]
public void TestInitialize()
{
this.SetupClientServer();
this.server.Register("/test", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, ResponseTest, 64);
}, Notifications.None, "Default test resource");
this.server.Register("/", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, ResponseRoot, 64);
});
this.server.Register("/separate", (req, resp) =>
{
Task _ = Task.Run(async () =>
{
await Task.Delay(2000);
await resp.RespondAsync(CoapCode.Content, ResponseTest, 64);
});
return Task.CompletedTask;
}, Notifications.None, "Resource which cannot be served immediately and which cannot be acknowledged in a piggy-backed way.");
this.server.Register("/seg1", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, ResponseTest, 64);
}, Notifications.None, "Long path resource");
this.server.Register("/seg1/seg2", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, ResponseTest, 64);
}, Notifications.None, "Long path resource");
this.server.Register("/seg1/seg2/seg3", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, ResponseTest, 64);
}, Notifications.None, "Long path resource");
this.server.Register("/large", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, ResponseLarge, 64);
}, Notifications.None, "Large resource", new string[] { "block" }, null, null, 1280);
this.server.Register("/large-separate", (req, resp) =>
{
Task _ = Task.Run(async () =>
{
await Task.Delay(2000);
await resp.RespondAsync(CoapCode.Content, ResponseLarge, 64);
});
return Task.CompletedTask;
}, Notifications.None, "Large resource", new string[] { "block" }, null, null, 1280);
this.server.Register("/multi-format", async (req, resp) =>
{
if (req.IsAcceptable(PlainText.ContentFormatCode))
await resp.RespondAsync(CoapCode.Content, ResponseTest, 64);
else if (req.IsAcceptable(Xml.ContentFormatCode))
{
await resp.RespondAsync(CoapCode.Content, "<text>" + ResponseTest + "</text>", 64,
new CoapOptionContentFormat(Xml.ContentFormatCode));
}
else
throw new CoapException(CoapCode.NotAcceptable);
}, Notifications.None, "Resource that exists in different content formats (text/plain utf8 and application/xml)",
null, null, new int[] { PlainText.ContentFormatCode, Xml.ContentFormatCode });
this.server.Register("/path", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, ResponseHierarchical, 64,
new CoapOptionContentFormat(CoreLinkFormat.ContentFormatCode));
}, Notifications.None, "Hierarchical link description entry", null, null,
new int[] { CoreLinkFormat.ContentFormatCode });
this.server.Register("/path/sub1", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, "/path/sub1", 64);
}, Notifications.None, "Hierarchical link description sub-resource");
this.server.Register("/path/sub2", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, "/path/sub2", 64);
}, Notifications.None, "Hierarchical link description sub-resource");
this.server.Register("/path/sub3", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, "/path/sub3", 64);
}, Notifications.None, "Hierarchical link description sub-resource");
this.server.Register("/query", async (req, resp) =>
{
StringBuilder sb = new();
bool First = true;
foreach (CoapOption Option in req.Options)
{
if (Option is CoapOptionUriQuery Query)
{
if (First)
{
First = false;
sb.Append('?');
}
else
sb.Append('&');
sb.Append(Query.Key);
sb.Append('=');
sb.Append(Query.KeyValue);
}
}
await resp.RespondAsync(CoapCode.Content, sb.ToString(), 64);
}, Notifications.None, "Resource accepting query parameters");
CoapResource Obs = this.server.Register("/obs", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, DateTime.Now.ToString("T"), 64);
}, Notifications.Acknowledged, "Observable resource which changes every 5 seconds",
new string[] { "observe" });
Obs.TriggerAll(new TimeSpan(0, 0, 5));
Obs = this.server.Register("/obs-large", async (req, resp) =>
{
string s = DateTime.Now.ToString("T");
int i = 61 - s.Length;
s = '|' + new string(' ', i / 2) + s + new string(' ', i - (i / 2)) + "|\r\n";
string Response =
"/-------------------------------------------------------------\\\r\n" + s +
"\\-------------------------------------------------------------/\r\n";
await resp.RespondAsync(CoapCode.Content, Response, 64);
}, Notifications.Acknowledged, "Observable resource which changes every 5 seconds",
new string[] { "observe" });
Obs.TriggerAll(new TimeSpan(0, 0, 5));
Obs = this.server.Register("/obs-non", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, DateTime.Now.ToString("T"), 64);
}, Notifications.Unacknowledged, "Observable resource which changes every 5 seconds",
new string[] { "observe" });
Obs.TriggerAll(new TimeSpan(0, 0, 5));
Obs = this.server.Register("/obs-pumping", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, DateTime.Now.ToString("T"), 64);
}, Notifications.Acknowledged, "Observable resource which changes every 5 seconds",
new string[] { "observe" });
Obs.TriggerAll(new TimeSpan(0, 0, 5));
Obs = this.server.Register("/obs-pumping-non", async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, DateTime.Now.ToString("T"), 64);
}, Notifications.Unacknowledged, "Observable resource which changes every 5 seconds",
new string[] { "observe" });
Obs.TriggerAll(new TimeSpan(0, 0, 5));
this.server.Register("/location-query", null, async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, req.Payload, 64);
}, Notifications.None, "Perform POST transaction with responses containing several Location-Query options (CON mode)");
this.server.Register("/large-create", null, async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Created, req.Payload, 64);
}, Notifications.None, "Large resource that can be created using POST method",
new string[] { "block" }, null, new int[] { 0 }, 2000);
this.server.Register("/large-post", null, async (req, resp) =>
{
await resp.RespondAsync(CoapCode.Content, req.Payload, 64);
}, Notifications.None, "Handle POST with two-way blockwise transfer",
new string[] { "block" });
this.server.Register(new LargeUpdate());
}
private class LargeUpdate : CoapResource, ICoapPutMethod
{
public LargeUpdate()
: base("/large-update")
{
}
public bool AllowsPUT => true;
public Task PUT(CoapMessage Request, CoapResponse Response)
{
Response.Respond(CoapCode.Changed, Request.Payload, 64);
return Task.CompletedTask;
}
public override Notifications Notifications => Notifications.None;
public override string Title => "Large resource that can be updated using PUT method";
public override string[] ResourceTypes => new string[] { "block" };
public override int[] ContentFormats => new int[] { 0 };
public override int? MaximumSizeEstimate => 2000;
}
[TestCleanup]
public async Task TestCleanup()
{
try
{
this.client = await Cleanup(this.client);
}
finally
{
this.server = await Cleanup(this.server);
}
}
private static async Task<CoapEndpoint> Cleanup(CoapEndpoint Client)
{
if (Client is not null)
{
ulong[] Tokens = Client.GetActiveTokens();
ushort[] MessageIDs = Client.GetActiveMessageIDs();
await Client.DisposeAsync();
Client = null;
Assert.AreEqual(0, Tokens.Length, "There are tokens that have not been unregistered properly.");
Assert.AreEqual(0, MessageIDs.Length, "There are message IDs that have not been unregistered properly.");
}
return Client;
}
protected async Task<object> Get(string Uri, params CoapOption[] Options)
{
ManualResetEvent Done = new(false);
ManualResetEvent Error = new(false);
object Result = null;
await this.client.GET(Uri, true, this.clientCredentials, async (Sender, e) =>
{
if (e.Ok)
{
Result = await e.Message.DecodeAsync();
Done.Set();
}
else
Error.Set();
}, null, Options);
Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 30000));
Assert.IsNotNull(Result);
ConsoleOut.WriteLine(Result.ToString());
return Result;
}
protected async Task<object> Observe(string Uri, params CoapOption[] Options)
{
ManualResetEvent Done = new(false);
ManualResetEvent Error = new(false);
object Result = null;
ulong Token = 0;
int Count = 0;
await this.client.Observe(Uri, true, this.clientCredentials, async (Sender, e) =>
{
if (e.Ok)
{
Token = e.Message.Token;
Result = await e.Message.DecodeAsync();
ConsoleOut.WriteLine(Result.ToString());
Count++;
if (Count == 3)
Done.Set();
}
else
Error.Set();
}, null, Options);
Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 30000));
Assert.IsNotNull(Result);
Done.Reset();
await this.client.UnregisterObservation(Uri, true, Token, this.clientCredentials, (Sender, e) =>
{
if (e.Ok)
Done.Set();
else
Error.Set();
return Task.CompletedTask;
}, null, Options);
Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 5000));
return Result;
}
protected async Task Post(string Uri, byte[] Payload, int BlockSize, params CoapOption[] Options)
{
ManualResetEvent Done = new(false);
ManualResetEvent Error = new(false);
await this.client.POST(Uri, true, Payload, BlockSize, this.clientCredentials, async (Sender, e) =>
{
if (e.Ok)
{
object Result = await e.Message.DecodeAsync();
if (Result is not null)
ConsoleOut.WriteLine(Result.ToString());
Done.Set();
}
else
Error.Set();
}, null, Options);
Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 30000));
}
protected async Task Put(string Uri, byte[] Payload, int BlockSize, params CoapOption[] Options)
{
ManualResetEvent Done = new(false);
ManualResetEvent Error = new(false);
await this.client.PUT(Uri, true, Payload, BlockSize, this.clientCredentials, async (Sender, e) =>
{
if (e.Ok)
{
object Result = await e.Message.DecodeAsync();
if (Result is not null)
ConsoleOut.WriteLine(Result.ToString());
Done.Set();
}
else
Error.Set();
}, null, Options);
Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 30000));
}
protected async Task Delete(string Uri, params CoapOption[] Options)
{
ManualResetEvent Done = new(false);
ManualResetEvent Error = new(false);
await this.client.DELETE(Uri, true, this.clientCredentials, async (Sender, e) =>
{
if (e.Ok)
{
object Result = await e.Message.DecodeAsync();
if (Result is not null)
ConsoleOut.WriteLine(Result.ToString());
Done.Set();
}
else
Error.Set();
}, null, Options);
Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 30000));
}
}
}