public void BufferedMessageSetWriteToValidSequence() { byte[] messageBytes = new byte[] { 1, 2, 3, 4, 5 }; Message msg1 = new Message(messageBytes); Message msg2 = new Message(messageBytes); MessageSet messageSet = new BufferedMessageSet(new List <Message>() { msg1, msg2 }); MemoryStream ms = new MemoryStream(); messageSet.WriteTo(ms); ////first message byte[] messageLength = new byte[MessageLengthPartLength]; Array.Copy(ms.ToArray(), MessageLengthPartOffset, messageLength, 0, MessageLengthPartLength); if (BitConverter.IsLittleEndian) { Array.Reverse(messageLength); } Assert.AreEqual(MagicNumberPartLength + AttributesPartLength + ChecksumPartLength + messageBytes.Length, BitConverter.ToInt32(messageLength, 0)); Assert.AreEqual(1, ms.ToArray()[MagicNumberPartOffset]); // default magic number should be 1 byte[] checksumPart = new byte[ChecksumPartLength]; Array.Copy(ms.ToArray(), ChecksumPartOffset, checksumPart, 0, ChecksumPartLength); Assert.AreEqual(Crc32Hasher.Compute(messageBytes), checksumPart); byte[] dataPart = new byte[messageBytes.Length]; Array.Copy(ms.ToArray(), DataPartOffset, dataPart, 0, messageBytes.Length); Assert.AreEqual(messageBytes, dataPart); ////second message int secondMessageOffset = MessageLengthPartLength + MagicNumberPartLength + AttributesPartLength + ChecksumPartLength + messageBytes.Length; messageLength = new byte[MessageLengthPartLength]; Array.Copy(ms.ToArray(), secondMessageOffset + MessageLengthPartOffset, messageLength, 0, MessageLengthPartLength); if (BitConverter.IsLittleEndian) { Array.Reverse(messageLength); } Assert.AreEqual(MagicNumberPartLength + AttributesPartLength + ChecksumPartLength + messageBytes.Length, BitConverter.ToInt32(messageLength, 0)); Assert.AreEqual(1, ms.ToArray()[secondMessageOffset + MagicNumberPartOffset]); // default magic number should be 1 checksumPart = new byte[ChecksumPartLength]; Array.Copy(ms.ToArray(), secondMessageOffset + ChecksumPartOffset, checksumPart, 0, ChecksumPartLength); Assert.AreEqual(Crc32Hasher.Compute(messageBytes), checksumPart); dataPart = new byte[messageBytes.Length]; Array.Copy(ms.ToArray(), secondMessageOffset + DataPartOffset, dataPart, 0, messageBytes.Length); Assert.AreEqual(messageBytes, dataPart); }
public void WriteToValidSequenceForDefaultConstructor() { byte[] messageBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; Message message = new Message(messageBytes); MemoryStream ms = new MemoryStream(); message.WriteTo(ms); Assert.AreEqual(1, ms.ToArray()[MagicNumberPartOffset]); // default magic number should be 1 byte[] checksumPart = new byte[ChecksumPartLength]; Array.Copy(ms.ToArray(), ChecksumPartOffset, checksumPart, 0, ChecksumPartLength); Assert.AreEqual(Crc32Hasher.Compute(messageBytes), checksumPart); message.ToString(); byte[] dataPart = new byte[messageBytes.Length]; Array.Copy(ms.ToArray(), DataPartOffset, dataPart, 0, messageBytes.Length); Assert.AreEqual(messageBytes, dataPart); }
/// <summary> /// Initializes a new instance of the Message class. /// </summary> /// <remarks> /// Initializes the checksum as null. It will be automatically computed. /// </remarks> /// <param name="payload">The data for the payload.</param> /// <param name="magic">The magic identifier.</param> public Message(byte[] payload, CompressionCodecs compressionCodec) : this(payload, Crc32Hasher.Compute(payload), compressionCodec) { Guard.NotNull(payload, "payload"); }