public async Task HandleUpdateInfoAsync_Invokes_FetchFirmware_WhenUpdateAvailable() { // setup using var memoryStream = new MemoryStream(); var strictifiedInput = JsonUtil.Strictify(CupsRequestJson); var(httpContext, httpRequest, _) = SetupHttpContextWithRequest(strictifiedInput, memoryStream); _ = httpRequest.Setup(r => r.ContentLength).Returns(Encoding.UTF8.GetByteCount(strictifiedInput)); var signatureBase64 = "ABCD"; // setting up the twin in such a way that there is a fw update but no matching checksum var cupsTwinInfo = new CupsTwinInfo(new Uri(CupsUri), new Uri(TcUri), CredentialsChecksum, CredentialsChecksum, string.Empty, string.Empty, "anotherVersion", KeyChecksum, signatureBase64, new Uri(FwUrl)); _ = this.basicsStationConfigurationService.Setup(m => m.GetCupsConfigAsync(It.IsAny <StationEui>(), It.IsAny <CancellationToken>())) .Returns(Task.FromResult(cupsTwinInfo)); var firmwareBytes = new byte[] { 1, 2, 3 }; using var httpContent = new ByteArrayContent(firmwareBytes); _ = this.deviceAPIServiceBase.Setup(m => m.FetchStationFirmwareAsync(It.IsAny <StationEui>(), It.IsAny <CancellationToken>())) .ReturnsAsync(httpContent); // act await this.processor.HandleUpdateInfoAsync(httpContext.Object, default); // assert this.deviceAPIServiceBase.Verify(m => m.FetchStationFirmwareAsync(StationEui.Parse(StationEuiString), It.IsAny <CancellationToken>()), Times.Once); var expectedHeader = new CupsUpdateInfoResponseHeader { UpdateSignature = Convert.FromBase64String(signatureBase64), SignatureKeyCrc = KeyChecksum, UpdateDataLength = (uint)firmwareBytes.Length, }; var expectedHeaderBytes = expectedHeader.Serialize(new byte[256].AsMemory()).ToArray(); var response = memoryStream.ToArray(); Assert.Equal(expectedHeaderBytes, response[..expectedHeaderBytes.Length]);
public void Serialize_WithNoUpdates() { var updateResponseHeader = new CupsUpdateInfoResponseHeader { SignatureKeyCrc = 0, UpdateDataLength = 0, }; using var memoryRental = MemoryPool <byte> .Shared.Rent(2048); memoryRental.Memory.Span.Fill(0xbd); // Act var serializedResponse = updateResponseHeader.Serialize(memoryRental.Memory.Span); var serializedResponseSpanReader = serializedResponse.GetReader(); // Assert Assert.Equal(14, serializedResponse.Length); Assert.True(serializedResponseSpanReader.ReadAll().All(b => b == 0)); Assert.True(memoryRental.Memory.Span[serializedResponse.Length..].ToArray().All(b => b == 0xbd));