public async Task When_Payload_Has_Invalid_Mic_Should_Not_Send_Messages(int searchDevicesDelayMs) { // Setup const string wrongSKey = "00000000000000000000000000EEDDFF"; var simulatedDevice = new SimulatedDevice(TestDeviceInfo.CreateABPDevice(1)); var searchResult = new SearchDevicesResult(new IoTHubDeviceInfo(simulatedDevice.DevAddr, simulatedDevice.DevEUI, "1321").AsList()); if (searchDevicesDelayMs > 0) { this.LoRaDeviceApi.Setup(x => x.SearchByDevAddrAsync(simulatedDevice.DevAddr)) .ReturnsAsync(searchResult, TimeSpan.FromMilliseconds(searchDevicesDelayMs)); } else { this.LoRaDeviceApi.Setup(x => x.SearchByDevAddrAsync(simulatedDevice.DevAddr)) .ReturnsAsync(searchResult); } this.LoRaDeviceClient.Setup(x => x.GetTwinAsync()) .ReturnsAsync(TestUtils.CreateABPTwin(simulatedDevice)); var deviceRegistry = new LoRaDeviceRegistry(this.ServerConfiguration, this.NewMemoryCache(), this.LoRaDeviceApi.Object, this.LoRaDeviceFactory); // Send to message processor var messageProcessor = new MessageDispatcher( this.ServerConfiguration, deviceRegistry, this.FrameCounterUpdateStrategyProvider); // Send request #1 var payload1 = simulatedDevice.CreateUnconfirmedDataUpMessage("1", fcnt: 2); var request1 = this.CreateWaitableRequest(payload1.SerializeUplink(simulatedDevice.AppSKey, wrongSKey).Rxpk[0]); messageProcessor.DispatchRequest(request1); Assert.True(await request1.WaitCompleteAsync()); Assert.Null(request1.ResponseDownlink); Assert.True(request1.ProcessingFailed); Assert.Equal(LoRaDeviceRequestFailedReason.NotMatchingDeviceByMicCheck, request1.ProcessingFailedReason); await Task.Delay(2000); Assert.Single(deviceRegistry.InternalGetCachedDevicesForDevAddr(simulatedDevice.DevAddr)); // Send request #2 var payload2 = simulatedDevice.CreateUnconfirmedDataUpMessage("2", fcnt: 3); var request2 = this.CreateWaitableRequest(payload2.SerializeUplink(simulatedDevice.AppSKey, wrongSKey).Rxpk[0]); messageProcessor.DispatchRequest(request2); Assert.True(await request2.WaitCompleteAsync()); Assert.Null(request2.ResponseDownlink); Assert.True(request2.ProcessingFailed); Assert.Equal(LoRaDeviceRequestFailedReason.NotMatchingDeviceByMicCheck, request2.ProcessingFailedReason); Assert.Single(deviceRegistry.InternalGetCachedDevicesForDevAddr(simulatedDevice.DevAddr)); this.LoRaDeviceApi.VerifyAll(); this.LoRaDeviceClient.VerifyAll(); this.LoRaDeviceApi.Verify(x => x.SearchByDevAddrAsync(simulatedDevice.DevAddr), Times.Exactly(1)); }
public void Index() { // arrange var sut = new SearchDevicesResult(GenerateIoTHubDeviceInfo(5)); // act + assert Assert.Equal(new DevEui(2), sut[1].DevEUI); }
public void When_Initialized_With_Null_Falls_Back_To_Empty_Array() { // arrange var searchDevicesResult = new SearchDevicesResult(null); // act + assert Assert.Empty(searchDevicesResult); }
public void Count(int numberOfElements) { // arrange var searchDevicesResult = new SearchDevicesResult(GenerateIoTHubDeviceInfo(numberOfElements)); // act + assert Assert.Equal(numberOfElements, searchDevicesResult.Count); }
public void Iterate() { // arrange var sut = new SearchDevicesResult(GenerateIoTHubDeviceInfo(5)); // act + assert foreach (var(el, n) in sut.Select((el, i) => (el, checked ((ulong)i + 1)))) { Assert.Equal(new DevEui(n), el.DevEUI); } }
public async Task When_Payload_Has_Invalid_Mic_Should_Not_Send_Messages(int searchDevicesDelayMs) { // Setup var wrongSKey = TestKeys.CreateNetworkSessionKey(0xEEDDFF); var simulatedDevice = new SimulatedDevice(TestDeviceInfo.CreateABPDevice(1)); var searchResult = new SearchDevicesResult(new IoTHubDeviceInfo(simulatedDevice.DevAddr, simulatedDevice.DevEUI, "1321").AsList()); if (searchDevicesDelayMs > 0) { LoRaDeviceApi.Setup(x => x.SearchByDevAddrAsync(simulatedDevice.DevAddr.Value)) .ReturnsAsync(searchResult, TimeSpan.FromMilliseconds(searchDevicesDelayMs)); } else { LoRaDeviceApi.Setup(x => x.SearchByDevAddrAsync(simulatedDevice.DevAddr.Value)) .ReturnsAsync(searchResult); } LoRaDeviceClient.Setup(x => x.GetTwinAsync(CancellationToken.None)) .ReturnsAsync(TestUtils.CreateABPTwin(simulatedDevice)); using var cache = NewMemoryCache(); using var deviceRegistry = new LoRaDeviceRegistry(ServerConfiguration, cache, LoRaDeviceApi.Object, LoRaDeviceFactory, DeviceCache); // Send to message processor using var messageProcessor = new MessageDispatcher( ServerConfiguration, deviceRegistry, FrameCounterUpdateStrategyProvider); // Send request #1 var payload1 = simulatedDevice.CreateUnconfirmedDataUpMessage("1", fcnt: 2, nwkSKey: wrongSKey); using var request1 = CreateWaitableRequest(TestUtils.GenerateTestRadioMetadata(), payload1); messageProcessor.DispatchRequest(request1); Assert.True(await request1.WaitCompleteAsync()); Assert.Null(request1.ResponseDownlink); Assert.True(request1.ProcessingFailed); Assert.Equal(LoRaDeviceRequestFailedReason.NotMatchingDeviceByMicCheck, request1.ProcessingFailedReason); await Task.Delay(2000); Assert.Equal(1, DeviceCache.RegistrationCount(simulatedDevice.DevAddr.Value)); // Send request #2 var payload2 = simulatedDevice.CreateUnconfirmedDataUpMessage("2", fcnt: 3, nwkSKey: wrongSKey); using var request2 = CreateWaitableRequest(TestUtils.GenerateTestRadioMetadata(), payload2); messageProcessor.DispatchRequest(request2); Assert.True(await request2.WaitCompleteAsync()); Assert.Null(request2.ResponseDownlink); Assert.True(request2.ProcessingFailed); Assert.Equal(LoRaDeviceRequestFailedReason.NotMatchingDeviceByMicCheck, request2.ProcessingFailedReason); Assert.Equal(1, DeviceCache.RegistrationCount(simulatedDevice.DevAddr.Value)); LoRaDeviceApi.VerifyAll(); LoRaDeviceClient.VerifyAll(); LoRaDeviceApi.Verify(x => x.SearchByDevAddrAsync(simulatedDevice.DevAddr.Value), Times.Exactly(1)); }