-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathCoapClientTests.cs
More file actions
422 lines (354 loc) · 11.5 KB
/
CoapClientTests.cs
File metadata and controls
422 lines (354 loc) · 11.5 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
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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml;
using Waher.Content;
using Waher.Events;
using Waher.Events.Console;
using Waher.Networking.CoAP.CoRE;
using Waher.Networking.CoAP.Options;
using Waher.Networking.LWM2M;
using Waher.Networking.Sniffers;
using Waher.Persistence;
using Waher.Persistence.Files;
using Waher.Persistence.Serialization;
using Waher.Runtime.Console;
using Waher.Runtime.Inventory;
using Waher.Security.DTLS;
namespace Waher.Networking.CoAP.Test
{
[TestClass]
public class CoapClientTests
{
private static ConsoleEventSink consoleEventSink = null;
private static FilesProvider filesProvider = null;
private CoapEndpoint client;
[AssemblyInitialize]
public static async Task AssemblyInitialize(TestContext _)
{
Types.Initialize(
typeof(IContentDecoder).Assembly,
typeof(Content.Xml.Text.XmlCodec).Assembly,
typeof(CoapEndpoint).Assembly,
typeof(Lwm2mClient).Assembly,
typeof(Database).Assembly,
typeof(FilesProvider).Assembly,
typeof(ObjectSerializer).Assembly,
typeof(ICipher).Assembly);
Log.Register(consoleEventSink = new ConsoleEventSink());
filesProvider = await FilesProvider.CreateAsync("Data", "Default", 8192, 10000, 8192, Encoding.UTF8, 10000);
Database.Register(filesProvider);
}
[AssemblyCleanup]
public static async Task AssemblyCleanup()
{
if (filesProvider is not null)
{
await filesProvider.DisposeAsync();
filesProvider = null;
}
if (consoleEventSink is not null)
{
Log.Unregister(consoleEventSink);
consoleEventSink = null;
}
}
[TestInitialize]
public void TestInitialize()
{
this.client = new CoapEndpoint([CoapEndpoint.DefaultCoapPort],
[CoapEndpoint.DefaultCoapsPort], null, null, false, false,
new ConsoleOutSniffer(BinaryPresentationMethod.Hexadecimal, LineEnding.NewLine));
}
[TestCleanup]
public async Task TestCleanup()
{
if (this.client is not null)
{
ulong[] Tokens = this.client.GetActiveTokens();
ushort[] MessageIDs = this.client.GetActiveMessageIDs();
await this.client.DisposeAsync();
this.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.");
}
}
private Task<object> Get(string Uri, params CoapOption[] Options)
{
return this.Get(Uri, null, Options);
}
private async Task<object> Get(string Uri, IDtlsCredentials Credentials, params CoapOption[] Options)
{
ManualResetEvent Done = new(false);
ManualResetEvent Error = new(false);
object Result = null;
await this.client.GET(Uri, true, Credentials, async (Sender, e) =>
{
if (e.Ok)
{
Result = await e.Message.DecodeAsync();
Done.Set();
}
else
Error.Set();
}, null, Options);
Assert.AreEqual(0, WaitHandle.WaitAny([Done, Error], 30000));
Assert.IsNotNull(Result);
ConsoleOut.WriteLine(Result.ToString());
return Result;
}
private 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, null, 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([Done, Error], 30000));
Assert.IsNotNull(Result);
Done.Reset();
await this.client.UnregisterObservation(Uri, true, Token, null, (Sender, e) =>
{
if (e.Ok)
Done.Set();
else
Error.Set();
return Task.CompletedTask;
}, null, Options);
Assert.AreEqual(0, WaitHandle.WaitAny([Done, Error], 5000));
return Result;
}
private 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, null, 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([Done, Error], 30000));
}
private 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, null, 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([Done, Error], 30000));
}
// TODO: Test DELETE.
//
//private async Task Delete(string Uri, params CoapOption[] Options)
//{
// ManualResetEvent Done = new ManualResetEvent(false);
// ManualResetEvent Error = new ManualResetEvent(false);
//
// await this.client.DELETE(Uri, true, null, (Sender, e) =>
// {
// if (e.Ok)
// {
// object Result = e.Message.Decode();
// if (!(Result is null))
// ConsoleOut.WriteLine(Result.ToString());
//
// Done.Set();
// }
// else
// Error.Set();
// }, null, Options);
//
// Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done, Error }, 30000));
//}
[TestMethod]
public async Task CoAP_Client_Test_01_GET()
{
// Default test resource
await this.Get("coap://californium.eclipseprojects.io/test");
}
[TestMethod]
public async Task CoAP_Client_Test_02_Root()
{
await this.Get("coap://californium.eclipseprojects.io/");
}
[TestMethod]
public async Task CoAP_Client_Test_03_Discover()
{
LinkDocument Doc = await this.Get("coap://californium.eclipseprojects.io/.well-known/core") as LinkDocument;
Assert.IsNotNull(Doc);
}
[TestMethod]
public async Task CoAP_Client_Test_04_Separate()
{
// Resource which cannot be served immediately and which cannot be acknowledged in a piggy-backed way
await this.Get("coap://californium.eclipseprojects.io/separate");
}
[TestMethod]
public async Task CoAP_Client_Test_05_LongPath()
{
// Long path resource
await this.Get("coap://californium.eclipseprojects.io/seg1");
}
[TestMethod]
public async Task CoAP_Client_Test_06_LongPath()
{
// Long path resource
await this.Get("coap://californium.eclipseprojects.io/seg1/seg2");
}
[TestMethod]
public async Task CoAP_Client_Test_07_LongPath()
{
// Long path resource
await this.Get("coap://californium.eclipseprojects.io/seg1/seg2/seg3");
}
[TestMethod]
public async Task CoAP_Client_Test_08_Large()
{
// Large resource
await this.Get("coap://californium.eclipseprojects.io/large");
}
[TestMethod]
public async Task CoAP_Client_Test_09_LargeSeparate()
{
// Large resource
await this.Get("coap://californium.eclipseprojects.io/large-separate");
}
[TestMethod]
[Ignore]
public async Task CoAP_Client_Test_10_MultiFormat()
{
// Resource that exists in different content formats (text/plain utf8 and application/xml)
string s = await this.Get("coap://californium.eclipseprojects.io/multi-format", new CoapOptionAccept(0)) as string;
AssertNotNull(s);
XmlDocument Xml = await this.Get("coap://californium.eclipseprojects.io/multi-format", new CoapOptionAccept(41)) as XmlDocument;
AssertNotNull(Xml);
}
private static void AssertNotNull(object Obj)
{
Assert.IsTrue(Obj is not null);
}
[TestMethod]
public async Task CoAP_Client_Test_11_Hierarchical()
{
// Hierarchical link description entry
AssertNotNull(await this.Get("coap://californium.eclipseprojects.io/path") as LinkDocument);
AssertNotNull(await this.Get("coap://californium.eclipseprojects.io/path/sub1") as string);
AssertNotNull(await this.Get("coap://californium.eclipseprojects.io/path/sub2") as string);
AssertNotNull(await this.Get("coap://californium.eclipseprojects.io/path/sub3") as string);
}
[TestMethod]
public async Task CoAP_Client_Test_12_Query()
{
// Hierarchical link description entry
AssertNotNull(await this.Get("coap://californium.eclipseprojects.io/query?A=1&B=2") as string);
}
[TestMethod]
public async Task CoAP_Client_Test_13_Observable()
{
// Observable resource which changes every 5 seconds
AssertNotNull(await this.Observe("coap://californium.eclipseprojects.io/obs") as string);
}
[TestMethod]
public async Task CoAP_Client_Test_14_Observable_Large()
{
// Observable resource which changes every 5 seconds
AssertNotNull(await this.Observe("coap://californium.eclipseprojects.io/obs-large") as string);
}
[TestMethod]
public async Task CoAP_Client_Test_15_Observable_NON()
{
// Observable resource which changes every 5 seconds
AssertNotNull(await this.Observe("coap://californium.eclipseprojects.io/obs-non") as string);
}
[TestMethod]
public async Task CoAP_Client_Test_16_Observable_Pumping()
{
// Observable resource which changes every 5 seconds
AssertNotNull(await this.Observe("coap://californium.eclipseprojects.io/obs-pumping") as string);
}
[TestMethod]
public async Task CoAP_Client_Test_17_Observable_Pumping_NON()
{
// Observable resource which changes every 5 seconds
AssertNotNull(await this.Observe("coap://californium.eclipseprojects.io/obs-pumping-non") as string);
}
[TestMethod]
public async Task CoAP_Client_Test_18_POST()
{
// Perform POST transaction with responses containing several Location-Query options (CON mode)
await this.Post("coap://californium.eclipseprojects.io/location-query", Encoding.UTF8.GetBytes("Hello"), 64, new CoapOptionContentFormat(0));
}
[TestMethod]
public async Task CoAP_Client_Test_19_POST_Large()
{
// Large resource that can be created using POST method
string s = "0123456789";
s = s + s + s + s + s + s + s + s + s + s;
s = s + s + s + s + s + s + s + s + s + s;
s += s;
await this.Post("coap://californium.eclipseprojects.io/large-create", Encoding.UTF8.GetBytes(s), 64, new CoapOptionContentFormat(0));
}
[TestMethod]
public async Task CoAP_Client_Test_20_POST_Large_Response()
{
// Handle POST with two-way blockwise transfer
string s = "0123456789";
s = s + s + s + s + s + s + s + s + s + s;
s = s + s + s + s + s + s + s + s + s + s;
s += s;
await this.Post("coap://californium.eclipseprojects.io/large-post", Encoding.UTF8.GetBytes(s), 64, new CoapOptionContentFormat(0));
}
[TestMethod]
public async Task CoAP_Client_Test_21_PUT()
{
// Large resource that can be updated using PUT method
await this.Put("coap://californium.eclipseprojects.io/large-update", Encoding.UTF8.GetBytes("Hello"), 64, new CoapOptionContentFormat(0));
}
[TestMethod]
public async Task CoAP_Client_Test_22_PUT_Large()
{
// Large resource that can be updated using PUT method
string s = "0123456789";
s = s + s + s + s + s + s + s + s + s + s;
s = s + s + s + s + s + s + s + s + s + s;
s += s;
await this.Put("coap://californium.eclipseprojects.io/large-update", Encoding.UTF8.GetBytes(s), 64, new CoapOptionContentFormat(0));
}
/*
</obs-reset>,
</validate>;title="Resource which varies", (Test with ETag)
*/
}
}