public void DecodeMessage_MultipleEscapeCharactersEndingIn_f_x5() { byte controllerID = 0x01; byte msgID = 0x90; Command cmd = Command.DebugString; byte[] commandData = Encoding.UTF8.GetBytes("fffff"); byte[] expectedMsg = new byte[] { 0x00, controllerID, msgID, (byte)cmd, 0x66, 0x00, 0x66, 0x00, 0x66, 0x00, 0x66, 0x00, 0x66, 0x00, 0x00, 0x00 }; byte[] expectedMsgNotEscaped = new byte[] { 0x00, controllerID, msgID, (byte)cmd, 0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x00 }; expectedMsg[expectedMsg.Length - 2] = MessageCodec.CalculateCRC(expectedMsgNotEscaped); expectedMsg[expectedMsg.Length - 1] = ETX; expectedMsg[0] = STX; byte[] msg = MessageCodec.EncodeMessage(new ProtocolMessage(controllerID, msgID, cmd, commandData)); MessageCodec.DecodeMessages(msg, out List <ProtocolMessage> msgs, out byte[] outputRemainder); ProtocolMessage myMessage = msgs.First(); Assert.AreEqual(expectedMsg.Length, msg.Length); CollectionAssert.AreEqual(expectedMsg, msg); CollectionAssert.AreEqual(commandData, myMessage.CommandData); }
public void DecodeMessage_OneMessageDifferentStream() { byte controllerID = 0x01; byte msgID = 0x34; byte cmd = (byte)Command.GetVersion; byte[] commandData = new byte[] { 0x49, 0x03, 0x92, 0x96, 0x00 }; byte[] inputData = new byte[] { 0x00, controllerID, msgID, cmd, 0x00, 0x00 }; List <byte> dataList = inputData.ToList(); dataList.InsertRange(4, commandData); inputData = dataList.ToArray(); inputData[inputData.Length - 2] = MessageCodec.CalculateCRC(inputData); inputData[0] = STX; inputData[inputData.Length - 1] = ETX; List <ProtocolMessage> myMsgs = new List <ProtocolMessage>(); MessageCodec.DecodeMessages(inputData.Take(4).ToArray(), out List <ProtocolMessage> msgs, out byte[] outputRemainder); myMsgs.AddRange(msgs); MessageCodec.DecodeMessages(inputData.Skip(4).ToArray(), out List <ProtocolMessage> msgs2, out byte[] outputRemainder2, outputRemainder); myMsgs.AddRange(msgs2); Assert.IsTrue(myMsgs.Count == 1); foreach (ProtocolMessage msg in myMsgs) { Assert.AreEqual(msg.ControllerID, controllerID); Assert.AreEqual(msg.MsgID, msgID); Assert.AreEqual((byte)msg.Command, cmd); CollectionAssert.AreEqual(commandData, msg.CommandData); } CollectionAssert.AreEqual(inputData.Take(4).ToArray(), outputRemainder); CollectionAssert.AreEqual(new byte[0], outputRemainder2); }
public void DecodeMessage_TwoMessagesSameStream_GarbageInBetween() { byte controllerID = 0x01; byte msgID = 0x34; byte cmd = (byte)Command.GetVersion; byte[] commandData = new byte[] { 0x49, 0x03, 0x92, 0x96, 0x00 }; byte[] inputData = new byte[] { 0x00, controllerID, msgID, cmd, 0x00, 0x00 }; List <byte> dataList = inputData.ToList(); dataList.InsertRange(4, commandData); inputData = dataList.ToArray(); inputData[inputData.Length - 2] = MessageCodec.CalculateCRC(inputData); inputData[0] = STX; inputData[inputData.Length - 1] = ETX; dataList = inputData.ToList(); dataList.AddRange(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }); dataList.AddRange(inputData); inputData = dataList.ToArray(); MessageCodec.DecodeMessages(inputData, out List <ProtocolMessage> msgs, out byte[] outputRemainder); Assert.IsTrue(msgs.Count == 2); foreach (ProtocolMessage msg in msgs) { Assert.AreEqual(msg.ControllerID, controllerID); Assert.AreEqual(msg.MsgID, msgID); Assert.AreEqual((byte)msg.Command, cmd); CollectionAssert.AreEqual(commandData, msg.CommandData); } CollectionAssert.AreEqual(new byte[0], outputRemainder); }
public void DecodeMessage_OneMessageCommandNoData_Remainder() { byte controllerID = 0x01; byte msgID = 0x34; byte cmd = (byte)Command.GetVersion; byte[] commandData = new byte[0]; byte[] inputData = new byte[] { 0x00, controllerID, msgID, cmd, 0x00, 0x00 }; List <byte> dataList = inputData.ToList(); dataList.InsertRange(4, commandData); inputData = dataList.ToArray(); inputData[inputData.Length - 2] = MessageCodec.CalculateCRC(inputData); inputData[0] = STX; inputData[inputData.Length - 1] = ETX; dataList = inputData.ToList(); dataList.AddRange(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }); inputData = dataList.ToArray(); MessageCodec.DecodeMessages(inputData, out List <ProtocolMessage> msgs, out byte[] outputRemainder); Assert.IsTrue(msgs.Count == 1); ProtocolMessage msg = msgs.First(); Assert.AreEqual(msg.ControllerID, controllerID); Assert.AreEqual(msg.MsgID, msgID); Assert.AreEqual((byte)msg.Command, cmd); CollectionAssert.AreEqual(commandData, msg.CommandData); CollectionAssert.AreEqual(new byte[] { 0xFF, 0xFF, 0xFF, 0xFF }, outputRemainder); }
public void DecodeMessage_OneMessage_UnexpectedSTX() { byte controllerID = 0x01; byte msgID = 0x34; byte cmd = (byte)Command.GetVersion; byte[] commandData = new byte[] { 0x49, STX, 0x67, 0x96, 0x00 }; byte[] inputData = new byte[] { 0x00, controllerID, msgID, cmd, 0x00, 0x00 }; List <byte> dataList = inputData.ToList(); dataList.InsertRange(4, commandData); inputData = dataList.ToArray(); inputData[inputData.Length - 2] = MessageCodec.CalculateCRC(inputData); inputData[0] = STX; inputData[inputData.Length - 1] = ETX; MessageCodec.DecodeMessages(inputData, out List <ProtocolMessage> msgs, out byte[] outputRemainder); Assert.IsFalse(msgs.First().Valid); }
public void EncodeMessage_NoCommandDataValidFalse_ByteArray() { ProtocolMessage msg = new ProtocolMessage(0x01, 0x84, Command.QueryRegister); byte[] output = MessageCodec.EncodeMessage(msg); byte crc = output[output.Length - 2]; Assert.AreEqual(output[0], STX); Assert.AreEqual(output[1], msg.ControllerID); Assert.AreEqual(output[2], msg.MsgID); Assert.AreEqual(output[3], (byte)msg.Command); Assert.AreEqual(output[output.Length - 1], ETX); Assert.IsTrue(string.IsNullOrEmpty(MessageCodec.ValidateMessageBytes(output))); output[0] = 0x00; output[output.Length - 2] = 0x00; output[output.Length - 1] = 0x00; Assert.AreEqual(crc, MessageCodec.CalculateCRC(output)); }
public void DecodeMessage_OneMessageNoCommandData_NoRemainder() { byte controllerID = 0x01; byte msgID = 0x34; byte cmd = (byte)Command.GetVersion; byte[] inputData = new byte[] { 0x00, controllerID, msgID, cmd, 0x00, 0x00 }; inputData[inputData.Length - 2] = MessageCodec.CalculateCRC(inputData); inputData[0] = STX; inputData[inputData.Length - 1] = ETX; MessageCodec.DecodeMessages(inputData, out List <ProtocolMessage> msgs, out byte[] outputRemainder); Assert.IsTrue(msgs.Count == 1); ProtocolMessage msg = msgs.First(); Assert.AreEqual(msg.ControllerID, controllerID); Assert.AreEqual(msg.MsgID, msgID); Assert.AreEqual((byte)msg.Command, cmd); CollectionAssert.AreEqual(new byte[0], outputRemainder); }
public void EncodeMessage_EncodeDecodedMessage_NormalData() { byte controllerID = 0x01; byte msgID = 0x34; byte cmd = (byte)Command.GetVersion; byte[] commandData = new byte[] { 0x49, 0x05, 0x67, 0x96, 0x00 }; byte[] inputData = new byte[] { 0x00, controllerID, msgID, cmd, 0x00, 0x00 }; List <byte> dataList = inputData.ToList(); dataList.InsertRange(4, commandData); inputData = dataList.ToArray(); inputData[inputData.Length - 2] = MessageCodec.CalculateCRC(inputData); inputData[0] = STX; inputData[inputData.Length - 1] = ETX; MessageCodec.DecodeMessages(inputData, out List <ProtocolMessage> msgs, out byte[] remainder); byte[] msg = MessageCodec.EncodeMessage(msgs.First()); Assert.IsTrue(msgs.Count == 1); Assert.IsTrue(remainder.Length == 0); CollectionAssert.AreEqual(inputData, msg); }