This repository was archived by the owner on Dec 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathCloudToDeviceMessageTests.cs
More file actions
986 lines (785 loc) · 45.3 KB
/
Copy pathCloudToDeviceMessageTests.cs
File metadata and controls
986 lines (785 loc) · 45.3 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace LoRaWan.Tests.Integration
{
using System;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using LoRaTools;
using LoRaTools.LoRaMessage;
using LoRaWan.NetworkServer;
using LoRaWan.Tests.Common;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
using Microsoft.Extensions.Caching.Memory;
using Moq;
using Xunit;
using Xunit.Abstractions;
using static LoRaWan.ReceiveWindowNumber;
using IoTHubDeviceInfo = NetworkServer.IoTHubDeviceInfo;
// End to end tests without external dependencies (IoT Hub, Service Facade Function)
// Cloud to device message processing tests (Join tests are handled in other class)
public class CloudToDeviceMessageTests : MessageProcessorTestBase
{
private const FramePort TestPort = FramePorts.App1;
public CloudToDeviceMessageTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper) { }
[Theory]
[InlineData(ServerGatewayID)]
[InlineData(null)]
public async Task When_Device_With_Downlink_Disabled_Received_Unconfirmed_Data_Should_Not_Check_For_C2D(string deviceGatewayID)
{
var simulatedDevice = new SimulatedDevice(TestDeviceInfo.CreateABPDevice(1, gatewayID: deviceGatewayID));
// message will be sent
LoRaDeviceClient.Setup(x => x.SendEventAsync(It.IsNotNull<LoRaDeviceTelemetry>(), null))
.ReturnsAsync(true);
if (string.IsNullOrEmpty(deviceGatewayID))
{
LoRaDeviceApi.Setup(x => x.ABPFcntCacheResetAsync(It.IsNotNull<DevEui>(), It.IsAny<uint>(), It.IsNotNull<string>()))
.ReturnsAsync(true);
}
using var memoryCache = new MemoryCache(new MemoryCacheOptions());
var cachedDevice = CreateLoRaDevice(simulatedDevice);
cachedDevice.DownlinkEnabled = false;
DeviceCache.Register(cachedDevice);
await using var deviceRegistry = new LoRaDeviceRegistry(ServerConfiguration, memoryCache, LoRaDeviceApi.Object, LoRaDeviceFactory, DeviceCache);
// Send to message processor
await using var messageProcessor = TestMessageDispatcher.Create(
memoryCache,
ServerConfiguration,
deviceRegistry,
FrameCounterUpdateStrategyProvider);
// sends unconfirmed message
var unconfirmedMessagePayload = simulatedDevice.CreateUnconfirmedDataUpMessage("1234");
using var request = CreateWaitableRequest(unconfirmedMessagePayload);
messageProcessor.DispatchRequest(request);
Assert.True(await request.WaitCompleteAsync());
Assert.Null(request.ResponseDownlink);
Assert.True(request.ProcessingSucceeded);
LoRaDeviceClient.Verify(x => x.ReceiveAsync(It.IsAny<TimeSpan>()), Times.Never());
LoRaDeviceClient.VerifyAll();
LoRaDeviceApi.VerifyAll();
}
[Theory]
[InlineData(ServerGatewayID)]
[InlineData(null)]
public async Task When_Device_With_Downlink_Disabled_Received_Confirmed_Data_Should_Not_Check_For_C2D(string deviceGatewayID)
{
const int payloadFcnt = 10;
var simulatedDevice = new SimulatedDevice(TestDeviceInfo.CreateABPDevice(1, gatewayID: deviceGatewayID));
var devEUI = simulatedDevice.LoRaDevice.DevEui;
// message will be sent
LoRaDeviceClient.Setup(x => x.SendEventAsync(It.IsNotNull<LoRaDeviceTelemetry>(), null))
.ReturnsAsync(true);
this.LoRaDeviceClient.Setup(x => x.UpdateReportedPropertiesAsync(It.IsAny<TwinCollection>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);
// multi gateway will ask for next fcnt down
var isMultigateway = string.IsNullOrEmpty(deviceGatewayID);
if (isMultigateway)
{
LoRaDeviceApi.Setup(x => x.NextFCntDownAsync(devEUI, simulatedDevice.FrmCntDown, payloadFcnt, ServerConfiguration.GatewayID))
.ReturnsAsync((ushort)(simulatedDevice.FrmCntDown + 1));
// if we run with ADR, we will combine the call with the bundler
LoRaDeviceApi
.Setup(x => x.ExecuteFunctionBundlerAsync(devEUI, It.IsAny<FunctionBundlerRequest>()))
.ReturnsAsync(() =>
new FunctionBundlerResult
{
AdrResult = new LoRaTools.ADR.LoRaADRResult
{
// Todo check
CanConfirmToDevice = false,
FCntDown = simulatedDevice.FrmCntDown + 1,
NbRepetition = 1,
TxPower = 0
},
NextFCntDown = simulatedDevice.FrmCntDown + 1
});
}
using var memoryCache = new MemoryCache(new MemoryCacheOptions());
var cachedDevice = CreateLoRaDevice(simulatedDevice);
cachedDevice.DownlinkEnabled = false;
DeviceCache.Register(cachedDevice);
await using var deviceRegistry = new LoRaDeviceRegistry(ServerConfiguration, memoryCache, LoRaDeviceApi.Object, LoRaDeviceFactory, DeviceCache);
// Send to message processor
await using var messageProcessor = TestMessageDispatcher.Create(
memoryCache,
ServerConfiguration,
deviceRegistry,
FrameCounterUpdateStrategyProvider);
// sends confirmed message
var payload = simulatedDevice.CreateConfirmedDataUpMessage("1234", fcnt: payloadFcnt);
using var request = CreateWaitableRequest(payload);
messageProcessor.DispatchRequest(request);
Assert.True(await request.WaitCompleteAsync());
Assert.NotNull(request.ResponseDownlink);
Assert.True(request.ProcessingSucceeded);
Assert.Single(DownstreamMessageSender.DownlinkMessages);
LoRaDeviceClient.Verify(x => x.ReceiveAsync(It.IsAny<TimeSpan>()), Times.Never());
LoRaDeviceClient.VerifyAll();
if (isMultigateway && ((LoRaPayloadData)request.Payload).IsDataRateNetworkControlled)
{
LoRaDeviceApi.Verify(x => x.ExecuteFunctionBundlerAsync(devEUI, It.IsAny<FunctionBundlerRequest>()));
}
}
[Theory]
[InlineData(9, 10)]
[InlineData(9, 19)]
public async Task OTAA_Unconfirmed_With_Cloud_To_Device_Message_Returns_Downstream_Message(uint initialDeviceFcntUp, uint payloadFcnt)
{
const uint InitialDeviceFcntDown = 20;
var needsToSaveFcnt = payloadFcnt - initialDeviceFcntUp >= NetworkServer.Constants.MaxFcntUnsavedDelta;
var simulatedDevice = new SimulatedDevice(
TestDeviceInfo.CreateABPDevice(1, gatewayID: ServerConfiguration.GatewayID),
frmCntUp: initialDeviceFcntUp,
frmCntDown: InitialDeviceFcntDown);
var loraDevice = CreateLoRaDevice(simulatedDevice);
LoRaDeviceClient.Setup(x => x.SendEventAsync(It.IsNotNull<LoRaDeviceTelemetry>(), null))
.ReturnsAsync(true);
if (needsToSaveFcnt)
{
LoRaDeviceClient.Setup(x => x.UpdateReportedPropertiesAsync(It.IsNotNull<TwinCollection>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
}
var cloudToDeviceMessageBody = new ReceivedLoRaCloudToDeviceMessage()
{
Fport = TestPort,
Payload = "c2d"
};
using var cloudToDeviceMessage = cloudToDeviceMessageBody.CreateMessage();
LoRaDeviceClient.SetupSequence(x => x.ReceiveAsync(It.IsAny<TimeSpan>()))
.ReturnsAsync(cloudToDeviceMessage)
.ReturnsAsync((Message)null); // 2nd cloud to device message does not return anything
LoRaDeviceClient.Setup(x => x.CompleteAsync(cloudToDeviceMessage))
.ReturnsAsync(true);
using var cache = EmptyMemoryCache();
await using var loraDeviceCache = CreateDeviceCache(loraDevice);
await using var deviceRegistry = new LoRaDeviceRegistry(ServerConfiguration, cache, LoRaDeviceApi.Object, LoRaDeviceFactory, loraDeviceCache);
// Send to message processor
await using var messageProcessor = TestMessageDispatcher.Create(
cache,
ServerConfiguration,
deviceRegistry,
FrameCounterUpdateStrategyProvider);
var payload = simulatedDevice.CreateUnconfirmedDataUpMessage("1234", fcnt: payloadFcnt);
using var request = CreateWaitableRequest(payload);
messageProcessor.DispatchRequest(request);
Assert.True(await request.WaitCompleteAsync());
// Expectations
// 1. Message was sent to IoT Hub
LoRaDeviceClient.VerifyAll();
LoRaDeviceApi.VerifyAll();
// 2. Return is downstream message
Assert.NotNull(request.ResponseDownlink);
Assert.True(request.ProcessingSucceeded);
Assert.Single(DownstreamMessageSender.DownlinkMessages);
var downlinkMessage = DownstreamMessageSender.DownlinkMessages[0];
var payloadDataDown = new LoRaPayloadData(downlinkMessage.Data);
payloadDataDown.Serialize(loraDevice.AppSKey.Value);
Assert.Equal(payloadDataDown.DevAddr, loraDevice.DevAddr);
Assert.False(payloadDataDown.IsConfirmed);
Assert.Equal(MacMessageType.UnconfirmedDataDown, payloadDataDown.MessageType);
// 4. Frame counter up was updated
Assert.Equal(payloadFcnt, loraDevice.FCntUp);
// 5. Frame counter down is updated
Assert.Equal(InitialDeviceFcntDown + 1, loraDevice.FCntDown);
Assert.Equal(InitialDeviceFcntDown + 1, payloadDataDown.Fcnt);
// 6. Frame count has pending changes?
if (needsToSaveFcnt)
Assert.False(loraDevice.HasFrameCountChanges);
else
Assert.True(loraDevice.HasFrameCountChanges);
}
[Theory]
[InlineData(9, 10)]
[InlineData(9, 19)]
public async Task OTAA_Confirmed_With_Cloud_To_Device_Message_Returns_Downstream_Message(uint initialDeviceFcntUp, uint payloadFcnt)
{
const uint InitialDeviceFcntDown = 20;
var needsToSaveFcnt = payloadFcnt - initialDeviceFcntUp >= NetworkServer.Constants.MaxFcntUnsavedDelta;
var simulatedDevice = new SimulatedDevice(
TestDeviceInfo.CreateABPDevice(1, gatewayID: ServerConfiguration.GatewayID),
frmCntUp: initialDeviceFcntUp,
frmCntDown: InitialDeviceFcntDown);
var loraDevice = CreateLoRaDevice(simulatedDevice);
LoRaDeviceClient.Setup(x => x.SendEventAsync(It.IsNotNull<LoRaDeviceTelemetry>(), null))
.ReturnsAsync(true);
if (needsToSaveFcnt)
{
LoRaDeviceClient.Setup(x => x.UpdateReportedPropertiesAsync(It.IsNotNull<TwinCollection>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
}
using var cloudToDeviceMessage = new ReceivedLoRaCloudToDeviceMessage() { Payload = "c2d", Fport = TestPort }
.CreateMessage();
LoRaDeviceClient.SetupSequence(x => x.ReceiveAsync(It.IsAny<TimeSpan>()))
.ReturnsAsync(cloudToDeviceMessage)
.ReturnsAsync((Message)null); // 2nd cloud to device message does not return anything
LoRaDeviceClient.Setup(x => x.CompleteAsync(cloudToDeviceMessage))
.ReturnsAsync(true);
using var cache = EmptyMemoryCache();
await using var loraDeviceCache = CreateDeviceCache(loraDevice);
await using var deviceRegistry = new LoRaDeviceRegistry(ServerConfiguration, cache, LoRaDeviceApi.Object, LoRaDeviceFactory, loraDeviceCache);
// Send to message processor
await using var messageProcessor = TestMessageDispatcher.Create(
cache,
ServerConfiguration,
deviceRegistry,
FrameCounterUpdateStrategyProvider);
var payload = simulatedDevice.CreateConfirmedDataUpMessage("1234", fcnt: payloadFcnt);
using var request = CreateWaitableRequest(payload);
messageProcessor.DispatchRequest(request);
Assert.True(await request.WaitCompleteAsync());
// Expectations
// 1. Message was sent to IoT Hub
LoRaDeviceClient.VerifyAll();
LoRaDeviceApi.VerifyAll();
// 2. Return is downstream message
Assert.NotNull(request.ResponseDownlink);
Assert.True(request.ProcessingSucceeded);
Assert.Single(DownstreamMessageSender.DownlinkMessages);
var downlinkMessage = DownstreamMessageSender.DownlinkMessages[0];
var payloadDataDown = new LoRaPayloadData(downlinkMessage.Data);
payloadDataDown.Serialize(loraDevice.AppSKey.Value);
Assert.Equal(payloadDataDown.DevAddr, loraDevice.DevAddr);
Assert.False(payloadDataDown.IsConfirmed);
Assert.Equal(MacMessageType.UnconfirmedDataDown, payloadDataDown.MessageType);
// 4. Frame counter up was updated
Assert.Equal(payloadFcnt, loraDevice.FCntUp);
// 5. Frame counter down is updated
Assert.Equal(InitialDeviceFcntDown + 1, loraDevice.FCntDown);
Assert.Equal(InitialDeviceFcntDown + 1, payloadDataDown.Fcnt);
// 6. Frame count has pending changes?
if (needsToSaveFcnt)
Assert.False(loraDevice.HasFrameCountChanges);
else
Assert.True(loraDevice.HasFrameCountChanges);
}
[Fact]
public async Task When_In_Time_For_First_Window_Should_Send_Downstream_In_First_Window()
{
const uint PayloadFcnt = 10;
const uint InitialDeviceFcntUp = 9;
const uint InitialDeviceFcntDown = 20;
var simulatedDevice = new SimulatedDevice(
TestDeviceInfo.CreateABPDevice(1, gatewayID: ServerConfiguration.GatewayID),
frmCntUp: InitialDeviceFcntUp,
frmCntDown: InitialDeviceFcntDown);
var devAddr = simulatedDevice.DevAddr.Value;
var devEUI = simulatedDevice.DevEUI;
// Will get twin to initialize LoRaDevice
LoRaDeviceClient.Setup(x => x.GetTwinAsync(CancellationToken.None)).ReturnsAsync(simulatedDevice.GetDefaultAbpTwin());
LoRaDeviceClient.Setup(x => x.SendEventAsync(It.IsNotNull<LoRaDeviceTelemetry>(), null))
.ReturnsAsync(true);
LoRaDeviceClient.Setup(x => x.UpdateReportedPropertiesAsync(It.IsNotNull<TwinCollection>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
using var cloudToDeviceMessage = new ReceivedLoRaCloudToDeviceMessage() { Payload = "c2d", Fport = TestPort }
.CreateMessage();
LoRaDeviceClient.SetupSequence(x => x.ReceiveAsync(It.IsAny<TimeSpan>()))
.ReturnsAsync(cloudToDeviceMessage)
.ReturnsAsync((Message)null); // 2nd cloud to device message does not return anything
LoRaDeviceClient.Setup(x => x.CompleteAsync(cloudToDeviceMessage))
.ReturnsAsync(true);
LoRaDeviceApi.Setup(x => x.SearchByDevAddrAsync(devAddr))
.ReturnsAsync(new SearchDevicesResult(new IoTHubDeviceInfo(devAddr, devEUI, "adad").AsList()));
using var cache = NewMemoryCache();
await using var loRaDeviceRegistry = new LoRaDeviceRegistry(ServerConfiguration, cache, LoRaDeviceApi.Object, LoRaDeviceFactory, DeviceCache);
// Send to message processor
await using var messageProcessor = TestMessageDispatcher.Create(
cache,
ServerConfiguration,
loRaDeviceRegistry,
FrameCounterUpdateStrategyProvider);
var payload = simulatedDevice.CreateUnconfirmedDataUpMessage("1234", fcnt: PayloadFcnt);
using var request = CreateWaitableRequest(payload);
messageProcessor.DispatchRequest(request);
Assert.True(await request.WaitCompleteAsync());
// Expectations
// 1. Message was sent to IoT Hub
LoRaDeviceClient.VerifyAll();
LoRaDeviceApi.VerifyAll();
// 2. Return is downstream message
Assert.NotNull(request.ResponseDownlink);
Assert.True(request.ProcessingSucceeded);
Assert.Single(DownstreamMessageSender.DownlinkMessages);
var downlinkMessage = DownstreamMessageSender.DownlinkMessages[0];
TestUtils.CheckDRAndFrequencies(request, downlinkMessage);
// Get the device from cache
Assert.True(DeviceCache.TryGetForPayload(request.Payload, out var loRaDevice));
var payloadDataDown = new LoRaPayloadData(downlinkMessage.Data);
payloadDataDown.Serialize(loRaDevice.AppSKey.Value);
Assert.Equal(payloadDataDown.DevAddr, loRaDevice.DevAddr);
Assert.False(payloadDataDown.IsConfirmed);
Assert.Equal(MacMessageType.UnconfirmedDataDown, payloadDataDown.MessageType);
// 4. Frame counter up was updated
Assert.Equal(PayloadFcnt, loRaDevice.FCntUp);
// 5. Frame counter down is updated
var expectedFcntDown = InitialDeviceFcntDown + NetworkServer.Constants.MaxFcntUnsavedDelta; // adding 10 as buffer when creating a new device instance
Assert.Equal(expectedFcntDown, loRaDevice.FCntDown);
Assert.Equal(expectedFcntDown, payloadDataDown.Fcnt);
// 6. Frame count has no pending changes
Assert.False(loRaDevice.HasFrameCountChanges);
}
[Fact]
public async Task When_Too_Late_For_First_Window_Should_Send_Downstream_In_Second_Window()
{
const uint PayloadFcnt = 10;
const uint InitialDeviceFcntUp = 9;
const uint InitialDeviceFcntDown = 20;
var simulatedDevice = new SimulatedDevice(
TestDeviceInfo.CreateABPDevice(1, gatewayID: ServerConfiguration.GatewayID),
frmCntUp: InitialDeviceFcntUp,
frmCntDown: InitialDeviceFcntDown);
var devAddr = simulatedDevice.DevAddr.Value;
var devEUI = simulatedDevice.DevEUI;
// Will get twin to initialize LoRaDevice
LoRaDeviceClient.Setup(x => x.GetTwinAsync(CancellationToken.None)).ReturnsAsync(simulatedDevice.GetDefaultAbpTwin());
LoRaDeviceClient.Setup(x => x.SendEventAsync(It.IsNotNull<LoRaDeviceTelemetry>(), null))
.ReturnsAsync(true);
using var cloudToDeviceMessage = new ReceivedLoRaCloudToDeviceMessage() { Payload = "c2d", Fport = TestPort }
.CreateMessage();
LoRaDeviceClient.Setup(x => x.UpdateReportedPropertiesAsync(It.IsNotNull<TwinCollection>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
LoRaDeviceClient.SetupSequence(x => x.ReceiveAsync(It.IsAny<TimeSpan>()))
.ReturnsAsync(cloudToDeviceMessage)
.Returns(EmptyAdditionalMessageReceiveAsync);
LoRaDeviceClient.Setup(x => x.CompleteAsync(cloudToDeviceMessage))
.ReturnsAsync(true);
LoRaDeviceApi.Setup(x => x.SearchByDevAddrAsync(devAddr))
.ReturnsAsync(new SearchDevicesResult(new IoTHubDeviceInfo(devAddr, devEUI, "adad").AsList()));
using var cache = NewMemoryCache();
await using var loRaDeviceRegistry = new LoRaDeviceRegistry(ServerConfiguration, cache, LoRaDeviceApi.Object, LoRaDeviceFactory, DeviceCache);
// Send to message processor
await using var messageProcessor = TestMessageDispatcher.Create(
cache,
ServerConfiguration,
loRaDeviceRegistry,
FrameCounterUpdateStrategyProvider);
var payload = simulatedDevice.CreateUnconfirmedDataUpMessage("1234", fcnt: PayloadFcnt);
using var request = CreateWaitableRequest(payload, constantElapsedTime: TestUtils.GetStartTimeOffsetForSecondWindow());
messageProcessor.DispatchRequest(request);
Assert.True(await request.WaitCompleteAsync());
// Expectations
// 1. Message was sent to IoT Hub
LoRaDeviceClient.VerifyAll();
LoRaDeviceApi.VerifyAll();
// 2. Return is downstream message
Assert.NotNull(request.ResponseDownlink);
Assert.True(request.ProcessingSucceeded);
Assert.Single(DownstreamMessageSender.DownlinkMessages);
var downlinkMessage = DownstreamMessageSender.DownlinkMessages.First();
TestUtils.CheckDRAndFrequencies(request, downlinkMessage, true);
// Get the device from cache
Assert.True(DeviceCache.TryGetForPayload(request.Payload, out var loRaDevice));
var payloadDataDown = new LoRaPayloadData(downlinkMessage.Data);
payloadDataDown.Serialize(loRaDevice.AppSKey.Value);
Assert.Equal(payloadDataDown.DevAddr, loRaDevice.DevAddr);
Assert.False(payloadDataDown.IsConfirmed);
Assert.Equal(MacMessageType.UnconfirmedDataDown, payloadDataDown.MessageType);
// 4. Frame counter up was updated
Assert.Equal(PayloadFcnt, loRaDevice.FCntUp);
// 5. Frame counter down is updated
var expectedFcntDown = InitialDeviceFcntDown + NetworkServer.Constants.MaxFcntUnsavedDelta; // adding 10 as buffer when creating a new device instance
Assert.Equal(expectedFcntDown, loRaDevice.FCntDown);
Assert.Equal(expectedFcntDown, payloadDataDown.Fcnt);
// 6. Frame count has no pending changes
Assert.False(loRaDevice.HasFrameCountChanges);
}
[Fact]
public async Task When_Device_Prefers_Second_Window_Should_Send_Downstream_In_Second_Window()
{
const uint PayloadFcnt = 10;
const uint InitialDeviceFcntUp = 9;
const uint InitialDeviceFcntDown = 20;
var simulatedDevice = new SimulatedDevice(
TestDeviceInfo.CreateABPDevice(1, gatewayID: ServerConfiguration.GatewayID),
frmCntUp: InitialDeviceFcntUp,
frmCntDown: InitialDeviceFcntDown);
var devAddr = simulatedDevice.DevAddr.Value;
var devEUI = simulatedDevice.DevEUI;
// Will get twin to initialize LoRaDevice
var twin = LoRaDeviceTwin.Create(simulatedDevice.LoRaDevice.GetAbpDesiredTwinProperties() with { PreferredWindow = ReceiveWindow2 },
simulatedDevice.GetAbpReportedTwinProperties());
LoRaDeviceClient.Setup(x => x.GetTwinAsync(CancellationToken.None)).ReturnsAsync(twin);
LoRaDeviceClient.Setup(x => x.UpdateReportedPropertiesAsync(It.IsNotNull<TwinCollection>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
LoRaDeviceClient.Setup(x => x.SendEventAsync(It.IsNotNull<LoRaDeviceTelemetry>(), null))
.ReturnsAsync(true);
using var cloudToDeviceMessage = new ReceivedLoRaCloudToDeviceMessage() { Payload = "c2d", Fport = TestPort }
.CreateMessage();
LoRaDeviceClient.SetupSequence(x => x.ReceiveAsync(It.IsAny<TimeSpan>()))
.ReturnsAsync(cloudToDeviceMessage)
.Returns(EmptyAdditionalMessageReceiveAsync); // 2nd cloud to device message does not return anything
LoRaDeviceClient.Setup(x => x.CompleteAsync(cloudToDeviceMessage))
.ReturnsAsync(true);
LoRaDeviceApi.Setup(x => x.SearchByDevAddrAsync(devAddr))
.ReturnsAsync(new SearchDevicesResult(new IoTHubDeviceInfo(devAddr, devEUI, "adad").AsList()));
using var cache = NewMemoryCache();
await using var loRaDeviceRegistry = new LoRaDeviceRegistry(ServerConfiguration, cache, LoRaDeviceApi.Object, LoRaDeviceFactory, DeviceCache);
// Send to message processor
await using var messageProcessor = TestMessageDispatcher.Create(
cache,
ServerConfiguration,
loRaDeviceRegistry,
FrameCounterUpdateStrategyProvider);
var payload = simulatedDevice.CreateUnconfirmedDataUpMessage("1234", fcnt: PayloadFcnt);
using var request = CreateWaitableRequest(payload);
messageProcessor.DispatchRequest(request);
Assert.True(await request.WaitCompleteAsync());
// Expectations
// 1. Message was sent to IoT Hub
LoRaDeviceClient.VerifyAll();
LoRaDeviceApi.VerifyAll();
// 3. Return is downstream message
Assert.True(request.ProcessingSucceeded);
Assert.NotNull(request.ResponseDownlink);
Assert.Single(DownstreamMessageSender.DownlinkMessages);
var downlinkMessage = DownstreamMessageSender.DownlinkMessages.First();
TestUtils.CheckDRAndFrequencies(request, downlinkMessage, true);
// Get the device from cache
Assert.True(DeviceCache.TryGetForPayload(request.Payload, out var loRaDevice));
var payloadDataDown = new LoRaPayloadData(downlinkMessage.Data);
payloadDataDown.Serialize(loRaDevice.AppSKey.Value);
Assert.Equal(payloadDataDown.DevAddr, loRaDevice.DevAddr);
Assert.False(payloadDataDown.IsConfirmed);
Assert.Equal(MacMessageType.UnconfirmedDataDown, payloadDataDown.MessageType);
// 4. Frame counter up was updated
Assert.Equal(PayloadFcnt, loRaDevice.FCntUp);
// 5. Frame counter down is updated
var expectedFcntDown = InitialDeviceFcntDown + NetworkServer.Constants.MaxFcntUnsavedDelta - 1 + 1; // adding 9 as buffer when creating a new device instance
Assert.Equal(expectedFcntDown, loRaDevice.FCntDown);
Assert.Equal(expectedFcntDown, payloadDataDown.Fcnt);
Assert.Equal(0U, loRaDevice.FCntDown - loRaDevice.LastSavedFCntDown);
// 6. Frame count has no pending changes
Assert.False(loRaDevice.HasFrameCountChanges);
}
[Theory]
// Preferred Window: 1
// - Aiming for RX1
[InlineData(ReceiveWindow1, 0, 400, 610, false)] // 1000 - (400 - noise)
[InlineData(ReceiveWindow1, 100, 300, 510, false)]
[InlineData(ReceiveWindow1, 200, 200, 410, false)]
// - Aiming for RX2
[InlineData(ReceiveWindow1, 750, 690, 999, true)]
[InlineData(ReceiveWindow1, 1000, 250, 610, true)]
// Preferred Window: 2
// - Aiming for RX2
[InlineData(ReceiveWindow2, 0, 1400, 1610, true)]
[InlineData(ReceiveWindow2, 100, 1300, 1510, true)]
public async Task When_Device_Checks_For_C2D_Message_Uses_Available_Time(
ReceiveWindowNumber preferredWindow,
int sendEventDurationInMs,
int checkMinDuration,
int checkMaxDuration,
bool expectingSecondWindow)
{
const int PayloadFcnt = 10;
const int InitialDeviceFcntUp = 9;
const int InitialDeviceFcntDown = 20;
var simulatedDevice = new SimulatedDevice(
TestDeviceInfo.CreateABPDevice(1, gatewayID: ServerConfiguration.GatewayID),
frmCntUp: InitialDeviceFcntUp,
frmCntDown: InitialDeviceFcntDown);
var devAddr = simulatedDevice.DevAddr;
var devEUI = simulatedDevice.DevEUI;
LoRaDeviceClient.Setup(x => x.SendEventAsync(It.IsNotNull<LoRaDeviceTelemetry>(), null))
.ReturnsAsync(true);
using var cloudToDeviceMessage = new ReceivedLoRaCloudToDeviceMessage() { Payload = "c2d", Fport = TestPort }
.CreateMessage();
LoRaDeviceClient.Setup(x => x.ReceiveAsync(It.IsInRange<TimeSpan>(TimeSpan.FromMilliseconds(checkMinDuration), TimeSpan.FromMilliseconds(checkMaxDuration), Moq.Range.Inclusive)))
.ReturnsAsync(cloudToDeviceMessage);
LoRaDeviceClient.Setup(x => x.ReceiveAsync(LoRaOperationTimeWatcher.MinimumAvailableTimeToCheckForCloudMessage))
.Returns(EmptyAdditionalMessageReceiveAsync); // 2nd cloud to device message does not return anything
LoRaDeviceClient.Setup(x => x.CompleteAsync(cloudToDeviceMessage))
.ReturnsAsync(true);
var loRaDevice = CreateLoRaDevice(simulatedDevice);
loRaDevice.PreferredWindow = preferredWindow;
using var cache = EmptyMemoryCache();
await using var loraDeviceCache = CreateDeviceCache(loRaDevice);
await using var deviceRegistry = new LoRaDeviceRegistry(ServerConfiguration, cache, LoRaDeviceApi.Object, LoRaDeviceFactory, loraDeviceCache);
// Send to message processor
await using var messageProcessor = TestMessageDispatcher.Create(
cache,
ServerConfiguration,
deviceRegistry,
FrameCounterUpdateStrategyProvider);
var payload = simulatedDevice.CreateUnconfirmedDataUpMessage("1234", fcnt: PayloadFcnt);
using var request = CreateWaitableRequest(
payload,
constantElapsedTime: sendEventDurationInMs > 0 ? TimeSpan.FromMilliseconds(sendEventDurationInMs) : TimeSpan.FromMilliseconds(100));
messageProcessor.DispatchRequest(request);
Assert.True(await request.WaitCompleteAsync());
Assert.NotNull(request.ResponseDownlink);
var downlinkMessage = Assert.Single(DownstreamMessageSender.DownlinkMessages);
Assert.Equal(LoRaDeviceClassType.A, downlinkMessage.DeviceClassType);
Assert.Equal(loRaDevice.ReportedRXDelay, downlinkMessage.LnsRxDelay);
if (expectingSecondWindow)
{
Assert.Null(downlinkMessage.Rx1);
}
else
{
Assert.NotNull(downlinkMessage.Rx1);
}
LoRaDeviceClient.VerifyAll();
LoRaDeviceApi.VerifyAll();
}
[Theory]
[InlineData("")]
[InlineData("test")]
public async Task OTAA_Unconfirmed_With_Cloud_To_Device_Mac_Command_Returns_Downstream_Message(string msg)
{
const uint PayloadFcnt = 20;
const uint InitialDeviceFcntUp = 9;
const uint InitialDeviceFcntDown = 20;
var simulatedDevice = new SimulatedDevice(
TestDeviceInfo.CreateABPDevice(1, gatewayID: ServerConfiguration.GatewayID),
frmCntUp: InitialDeviceFcntUp,
frmCntDown: InitialDeviceFcntDown);
var loraDevice = CreateLoRaDevice(simulatedDevice);
LoRaDeviceClient.Setup(x => x.SendEventAsync(It.IsNotNull<LoRaDeviceTelemetry>(), null))
.ReturnsAsync(true);
LoRaDeviceClient.Setup(x => x.UpdateReportedPropertiesAsync(It.IsNotNull<TwinCollection>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
var c2d = new ReceivedLoRaCloudToDeviceMessage()
{
Payload = msg,
MacCommands = { new DevStatusRequest() },
};
if (!string.IsNullOrEmpty(msg))
{
c2d.Fport = TestPort;
}
using var cloudToDeviceMessage = c2d.CreateMessage();
LoRaDeviceClient.SetupSequence(x => x.ReceiveAsync(It.IsAny<TimeSpan>()))
.ReturnsAsync(cloudToDeviceMessage)
.ReturnsAsync((Message)null); // 2nd cloud to device message does not return anything
LoRaDeviceClient.Setup(x => x.CompleteAsync(cloudToDeviceMessage))
.ReturnsAsync(true);
using var cache = EmptyMemoryCache();
await using var loraDeviceCache = CreateDeviceCache(loraDevice);
await using var deviceRegistry = new LoRaDeviceRegistry(ServerConfiguration, cache, LoRaDeviceApi.Object, LoRaDeviceFactory, loraDeviceCache);
// Send to message processor
await using var messageProcessor = TestMessageDispatcher.Create(
cache,
ServerConfiguration,
deviceRegistry,
FrameCounterUpdateStrategyProvider);
var payload = simulatedDevice.CreateUnconfirmedDataUpMessage("1234", fcnt: PayloadFcnt);
using var request = CreateWaitableRequest(payload);
messageProcessor.DispatchRequest(request);
Assert.True(await request.WaitCompleteAsync());
// Expectations
// 1. Message was sent to IoT Hub
LoRaDeviceClient.VerifyAll();
LoRaDeviceApi.VerifyAll();
// 2. Return is downstream message
Assert.NotNull(request.ResponseDownlink);
Assert.True(request.ProcessingSucceeded);
Assert.Single(DownstreamMessageSender.DownlinkMessages);
var downlinkMessage = DownstreamMessageSender.DownlinkMessages[0];
var payloadDataDown = new LoRaPayloadData(downlinkMessage.Data);
// in case no payload the mac is in the FRMPayload and is decrypted with NwkSKey
if (string.IsNullOrEmpty(msg))
{
payloadDataDown.Serialize(loraDevice.NwkSKey.Value);
}
else
{
payloadDataDown.Serialize(loraDevice.AppSKey.Value);
}
Assert.Equal(payloadDataDown.DevAddr, loraDevice.DevAddr);
Assert.False(payloadDataDown.IsConfirmed);
Assert.Equal(MacMessageType.UnconfirmedDataDown, payloadDataDown.MessageType);
// 4. Frame counter up was updated
Assert.Equal(PayloadFcnt, loraDevice.FCntUp);
// 5. Frame counter down is updated
Assert.Equal(InitialDeviceFcntDown + 1, loraDevice.FCntDown);
Assert.Equal(InitialDeviceFcntDown + 1, payloadDataDown.Fcnt);
// 6. Frame count has no pending changes
Assert.False(loraDevice.HasFrameCountChanges);
// 7. Mac commands should be present in reply
// mac command is in fopts if there is a c2d message
if (string.IsNullOrEmpty(msg))
{
Assert.Equal(FramePort.MacCommand, payloadDataDown.Fport);
Assert.NotNull(payloadDataDown.Frmpayload.Span.ToArray());
Assert.Single(payloadDataDown.Frmpayload.Span.ToArray());
Assert.Equal((byte)LoRaTools.Cid.DevStatusCmd, payloadDataDown.Frmpayload.Span[0]);
}
else
{
Assert.NotNull(payloadDataDown.Fopts.Span.ToArray());
Assert.Single(payloadDataDown.Fopts.Span.ToArray());
Assert.Equal((byte)LoRaTools.Cid.DevStatusCmd, payloadDataDown.Fopts.Span[0]);
}
LoRaDeviceClient.VerifyAll();
LoRaDeviceApi.VerifyAll();
}
[Theory]
[InlineData("test", 1)]
[InlineData("DevStatusCmd", null)]
[InlineData("DevStatusCmd", 0)]
public async Task OTAA_Unconfirmed_With_Cloud_To_Device_Mac_Command_Fails_Due_To_Wrong_Setup(string mac, int? fport)
{
const int PayloadFcnt = 20;
const int InitialDeviceFcntUp = 9;
const int InitialDeviceFcntDown = 20;
var simulatedDevice = new SimulatedDevice(
TestDeviceInfo.CreateABPDevice(1, gatewayID: ServerConfiguration.GatewayID),
frmCntUp: InitialDeviceFcntUp,
frmCntDown: InitialDeviceFcntDown);
var loraDevice = CreateLoRaDevice(simulatedDevice);
LoRaDeviceClient.Setup(x => x.SendEventAsync(It.IsNotNull<LoRaDeviceTelemetry>(), null))
.ReturnsAsync(true);
LoRaDeviceClient.Setup(x => x.UpdateReportedPropertiesAsync(It.IsNotNull<TwinCollection>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(true);
var c2dJson = $"{{\"fport\":{fport}, \"payload\":\"asd\", \"macCommands\": [ {{ \"cid\": \"{mac}\" }}] }}";
using var cloudToDeviceMessage = new Message(Encoding.UTF8.GetBytes(c2dJson));
LoRaDeviceClient.SetupSequence(x => x.ReceiveAsync(It.IsAny<TimeSpan>()))
.ReturnsAsync(cloudToDeviceMessage)
.ReturnsAsync((Message)null); // 2nd cloud to device message does not return anything
LoRaDeviceClient.Setup(x => x.RejectAsync(cloudToDeviceMessage))
.ReturnsAsync(true);
using var cache = EmptyMemoryCache();
await using var loraDeviceCache = CreateDeviceCache(loraDevice);
await using var deviceRegistry = new LoRaDeviceRegistry(ServerConfiguration, cache, LoRaDeviceApi.Object, LoRaDeviceFactory, loraDeviceCache);
// Send to message processor
await using var messageProcessor = TestMessageDispatcher.Create(
cache,
ServerConfiguration,
deviceRegistry,
FrameCounterUpdateStrategyProvider);
var payload = simulatedDevice.CreateUnconfirmedDataUpMessage("1234", fcnt: PayloadFcnt);
using var request = CreateWaitableRequest(payload, constantElapsedTime: TimeSpan.Zero);
messageProcessor.DispatchRequest(request);
Assert.True(await request.WaitCompleteAsync());
// Expectations
// 1. Message was sent to IoT Hub
LoRaDeviceClient.VerifyAll();
LoRaDeviceApi.VerifyAll();
// 2. DownStream Message should be null as the processing should fail
Assert.Null(request.ResponseDownlink);
}
[Theory]
[InlineData(null)]
[InlineData("0000000000000001")]
public async Task Unconfirmed_Cloud_To_Device_From_Decoder_Should_Send_Downstream_Message(string c2dDevEUI)
{
const uint PayloadFcnt = 10;
const uint InitialDeviceFcntUp = 9;
const uint InitialDeviceFcntDown = 20;
var simulatedDevice = new SimulatedDevice(
TestDeviceInfo.CreateABPDevice(1, gatewayID: ServerConfiguration.GatewayID),
frmCntUp: InitialDeviceFcntUp,
frmCntDown: InitialDeviceFcntDown);
var loraDevice = CreateLoRaDevice(simulatedDevice);
LoRaDeviceClient.Setup(x => x.SendEventAsync(It.IsNotNull<LoRaDeviceTelemetry>(), null))
.ReturnsAsync(true);
var decoderResult = new DecodePayloadResult("1")
{
CloudToDeviceMessage = new ReceivedLoRaCloudToDeviceMessage()
{
Fport = TestPort,
MessageId = "123",
Payload = "12",
DevEUI = c2dDevEUI is { } someDevEui ? DevEui.Parse(someDevEui) : null
},
};
var payloadDecoder = new Mock<ILoRaPayloadDecoder>(MockBehavior.Strict);
payloadDecoder.Setup(x => x.DecodeMessageAsync(simulatedDevice.DevEUI, It.IsNotNull<byte[]>(), TestPort, It.IsAny<string>()))
.ReturnsAsync(decoderResult);
PayloadDecoder.SetDecoder(payloadDecoder.Object);
using var cache = EmptyMemoryCache();
await using var loraDeviceCache = CreateDeviceCache(loraDevice);
await using var deviceRegistry = new LoRaDeviceRegistry(ServerConfiguration, cache, LoRaDeviceApi.Object, LoRaDeviceFactory, loraDeviceCache);
// Send to message processor
await using var messageProcessor = TestMessageDispatcher.Create(
cache,
ServerConfiguration,
deviceRegistry,
FrameCounterUpdateStrategyProvider);
var payload = simulatedDevice.CreateUnconfirmedDataUpMessage("1234", fcnt: PayloadFcnt);
using var request = CreateWaitableRequest(payload);
messageProcessor.DispatchRequest(request);
Assert.True(await request.WaitCompleteAsync());
// Expectations
// 1. Message was sent to IoT Hub
LoRaDeviceClient.VerifyAll();
LoRaDeviceApi.VerifyAll();
// 2. Return is downstream message
Assert.NotNull(request.ResponseDownlink);
Assert.True(request.ProcessingSucceeded);
Assert.Single(DownstreamMessageSender.DownlinkMessages);
var downlinkMessage = DownstreamMessageSender.DownlinkMessages[0];
var payloadDataDown = new LoRaPayloadData(downlinkMessage.Data);
payloadDataDown.Serialize(loraDevice.AppSKey.Value);
Assert.Equal(payloadDataDown.DevAddr, loraDevice.DevAddr);
Assert.False(payloadDataDown.IsConfirmed);
Assert.Equal(MacMessageType.UnconfirmedDataDown, payloadDataDown.MessageType);
// 4. Frame counter up was updated
Assert.Equal(PayloadFcnt, loraDevice.FCntUp);
// 5. Frame counter down is updated
Assert.Equal(InitialDeviceFcntDown + 1, loraDevice.FCntDown);
Assert.Equal(InitialDeviceFcntDown + 1, payloadDataDown.Fcnt);
// 6. Frame count has pending changes
Assert.True(loraDevice.HasFrameCountChanges);
LoRaDeviceApi.VerifyAll();
LoRaDeviceClient.VerifyAll();
payloadDecoder.VerifyAll();
}
[Fact]
public async Task When_Takes_Too_Long_Processing_C2D_Should_Abandon_Message()
{
const uint PayloadFcnt = 10;
const uint InitialDeviceFcntUp = 9;
const uint InitialDeviceFcntDown = 20;
var simulatedDevice = new SimulatedDevice(
TestDeviceInfo.CreateABPDevice(1, gatewayID: ServerConfiguration.GatewayID),
frmCntUp: InitialDeviceFcntUp,
frmCntDown: InitialDeviceFcntDown);
var devAddr = simulatedDevice.DevAddr.Value;
var devEUI = simulatedDevice.DevEUI;
// Will get twin to initialize LoRaDevice
LoRaDeviceClient.Setup(x => x.GetTwinAsync(CancellationToken.None)).ReturnsAsync(simulatedDevice.GetDefaultAbpTwin());
LoRaDeviceClient.Setup(x => x.UpdateReportedPropertiesAsync(It.IsAny<TwinCollection>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);
LoRaDeviceClient.Setup(x => x.SendEventAsync(It.IsNotNull<LoRaDeviceTelemetry>(), null))
.ReturnsAsync(true);
using var cloudToDeviceMessage = new ReceivedLoRaCloudToDeviceMessage() { Payload = "c2d", Fport = TestPort }
.CreateMessage();
LoRaDeviceClient.Setup(x => x.ReceiveAsync(It.IsAny<TimeSpan>()))
.ReturnsAsync(cloudToDeviceMessage);
LoRaDeviceClient.Setup(x => x.AbandonAsync(cloudToDeviceMessage))
.ReturnsAsync(true);
LoRaDeviceApi.Setup(x => x.SearchByDevAddrAsync(devAddr))
.ReturnsAsync(new SearchDevicesResult(new IoTHubDeviceInfo(devAddr, devEUI, "adad").AsList()));
using var cache = NewMemoryCache();
await using var loRaDeviceRegistry = new LoRaDeviceRegistry(ServerConfiguration, cache, LoRaDeviceApi.Object, LoRaDeviceFactory, DeviceCache);
// Send to message processor
await using var messageProcessor = TestMessageDispatcher.Create(
cache,
ServerConfiguration,
loRaDeviceRegistry,
FrameCounterUpdateStrategyProvider);
var payload = simulatedDevice.CreateUnconfirmedDataUpMessage("1234", fcnt: PayloadFcnt);
using var request = WaitableLoRaRequest.Create(TestUtils.GenerateTestRadioMetadata(), DownstreamMessageSender,
inTimeForC2DMessageCheck: true,
inTimeForAdditionalMessageCheck: false,
inTimeForDownlinkDelivery: false,
payload);
request.SetRegion(this.DefaultRegion);
messageProcessor.DispatchRequest(request);
Assert.True(await request.WaitCompleteAsync());
// Expectations
// 1. Message was sent to IoT Hub
LoRaDeviceClient.VerifyAll();
LoRaDeviceApi.VerifyAll();
// 2. No downstream message
Assert.Null(request.ResponseDownlink);
Assert.True(request.ProcessingSucceeded);
Assert.Empty(DownstreamMessageSender.DownlinkMessages);
// 3. Device FcntDown did change
Assert.True(DeviceCache.TryGetForPayload(request.Payload, out var loRaDevice));
Assert.Equal(InitialDeviceFcntDown + NetworkServer.Constants.MaxFcntUnsavedDelta, loRaDevice.FCntDown);
}
}
}