public void LuigiMetadataBlock_InflateUnknownBlock_ProducesBaseMetadataBlock() { using (var rawMetadataBlock = new System.IO.MemoryStream()) { rawMetadataBlock.WriteByte((byte)LuigiDataBlockType.Metadata); rawMetadataBlock.WriteByte(2); rawMetadataBlock.WriteByte(0); // two byte payload length var headerCrc = Crc8.OfBlock(rawMetadataBlock.ToArray()); rawMetadataBlock.WriteByte(headerCrc); var payload = new byte[] { 0xED, 2 }; var payloadCrc = Crc32.OfBlock(payload, Crc32Polynomial.Castagnoli); var payloadCrcBytes = BitConverter.GetBytes(payloadCrc); rawMetadataBlock.Write(payloadCrcBytes, 0, payloadCrcBytes.Length); rawMetadataBlock.Write(payload, 0, payload.Length); rawMetadataBlock.Seek(0, System.IO.SeekOrigin.Begin); var data = rawMetadataBlock.ToArray(); using (var reader = new BinaryReader(rawMetadataBlock)) { var metadataBlock = LuigiMetadataBlock.Inflate(reader); Assert.NotNull(metadataBlock); Assert.Equal(headerCrc, metadataBlock.HeaderCrc); Assert.Equal(payloadCrc, metadataBlock.PayloadCrc); Assert.True(metadataBlock is LuigiMetadataBlock); } } }
public void LuigiMetadataBlock_InflateStringWithBadCharacters_ProducesExpectedStringMetadata() { using (var metadataStream = new System.IO.MemoryStream()) { var stringPayload = new byte[255]; // put the values 1..255 as a "string" in a name metadata for (var i = 0; i < 255; ++i) { stringPayload[i] = (byte)(256 - i); } var metadataPayload = new System.IO.MemoryStream(); metadataPayload.WriteByte((byte)LuigiMetadataIdTag.Name); metadataPayload.WriteByte((byte)stringPayload.Length); metadataPayload.Write(stringPayload, 0, stringPayload.Length); var payload = metadataPayload.ToArray(); metadataStream.WriteByte((byte)LuigiDataBlockType.Metadata); var payloadLengthBytes = BitConverter.GetBytes((ushort)payload.Length); metadataStream.Write(payloadLengthBytes, 0, payloadLengthBytes.Length); var headerCrc = Crc8.OfBlock(metadataStream.ToArray()); metadataStream.WriteByte(headerCrc); var payloadCrc = Crc32.OfBlock(payload, Crc32Polynomial.Castagnoli); var payloadCrcBytes = BitConverter.GetBytes(payloadCrc); metadataStream.Write(payloadCrcBytes, 0, payloadCrcBytes.Length); metadataStream.Write(payload, 0, payload.Length); metadataStream.Seek(0, System.IO.SeekOrigin.Begin); var metadataBlock = LuigiMetadataBlock.Inflate(metadataStream) as LuigiMetadataBlock; var expectedString = System.Text.Encoding.UTF8.GetString(stringPayload).Trim('\0'); Assert.Equal(expectedString, metadataBlock.LongNames.First()); } }