Fixed bug in Publish Encoder/Decoder

This commit is contained in:
selva.andre
2012-07-26 23:29:01 +02:00
parent 275b6fc1e1
commit 530704d31e
3 changed files with 48 additions and 6 deletions
@@ -82,6 +82,24 @@ public class PublishDecoderTest {
assertEquals(messageID, (int) m_mockProtoDecoder.getMessage().getMessageID());
TestUtils.verifyEquals(payload, m_mockProtoDecoder.getMessage().getPayload());
}
@Test
public void testBugOnRealCase() throws Exception {
//msg hex = 30:1b:00:06:2f:74:6f:70:69:63:54:65:73:74:20:6d:79:20:70:61:79:6c:6f:61:64
byte[] overallMessage = new byte[] {0x30, 0x17, //fixed header, 25 byte lenght
0x00, 0x06, 0x2f, 0x74, 0x6f, 0x70, 0x69, 0x63, //[/topic] string 2 len + 6 content
0x54, 0x65, 0x73, 0x74, 0x20, 0x6d, 0x79, // [Test my payload] encoding
0x20, 0x70, 0x61, 0x79, 0x6c, 0x6f, 0x61, 0x64};
m_buff = IoBuffer.allocate(overallMessage.length).setAutoExpand(true);
m_buff.put(overallMessage);
m_buff.flip();
//Exercise
MessageDecoderResult res = m_msgdec.decode(null, m_buff, m_mockProtoDecoder);
assertNotNull(m_mockProtoDecoder.getMessage());
assertEquals(MessageDecoder.OK, res);
}
private void initHeader(IoBuffer buff) throws IllegalAccessException {
IoBuffer tmp = IoBuffer.allocate(4).setAutoExpand(true).put(Utils.encodeString("Fake Topic")).flip();
@@ -25,7 +25,7 @@ public class PublishEncoderTest {
@Test
public void testEncodeWithQos_0_noMessageID() throws Exception {
String topic = "pictures/photos";
String topic = "/photos";
PublishMessage msg = new PublishMessage();
msg.setQos(QOSType.MOST_ONE);
msg.setTopicName(topic);
@@ -39,7 +39,8 @@ public class PublishEncoderTest {
//Verify
assertEquals(0x30, m_mockProtoEncoder.getBuffer().get()); //1 byte
assertEquals(24, m_mockProtoEncoder.getBuffer().get()); //remaining length
//(2+7) topic + 3 payload
assertEquals(12, m_mockProtoEncoder.getBuffer().get()); //remaining length
//Variable part
verifyString(topic, m_mockProtoEncoder.getBuffer());
@@ -48,7 +49,7 @@ public class PublishEncoderTest {
@Test
public void testEncodeWithQos_1_MessageID() throws Exception {
String topic = "pictures/photos";
String topic = "/photos";
PublishMessage msg = new PublishMessage();
msg.setQos(QOSType.LEAST_ONE);
msg.setMessageID(1);
@@ -63,7 +64,8 @@ public class PublishEncoderTest {
//Verify
assertEquals(0x32, m_mockProtoEncoder.getBuffer().get()); //1 byte
assertEquals(26, m_mockProtoEncoder.getBuffer().get()); //remaining length
//(2+7) topic + 2 messageID + 3 payload
assertEquals(14, m_mockProtoEncoder.getBuffer().get()); //remaining length
//Variable part
verifyString(topic, m_mockProtoEncoder.getBuffer());
@@ -118,4 +120,26 @@ public class PublishEncoderTest {
//Exercise
m_encoder.encode(null, msg, m_mockProtoEncoder);
}
@Test
public void testBugMissedMessageIDEncoding() throws Exception{
String topic = "/topic";
PublishMessage msg = new PublishMessage();
msg.setQos(QOSType.MOST_ONE);
msg.setTopicName(topic);
//variable part
byte[] payload = "Test my payload".getBytes();
msg.setPayload(payload);
//Exercise
m_encoder.encode(null, msg, m_mockProtoEncoder);
//Verify
//2 byte header + (2+6) topic name + [2 message ID if QoQ <> 0]+ payload
int size = m_mockProtoEncoder.getBuffer().remaining();
assertEquals(10+payload.length, size);
assertEquals(0x30, m_mockProtoEncoder.getBuffer().get()); //1 byte
assertEquals(23, m_mockProtoEncoder.getBuffer().get()); //1 byte the length
}
}