Introduced raw decoder for CONNECT messages

This commit is contained in:
andrea
2013-09-11 11:06:53 +02:00
parent 2886aa0bab
commit 2ee84797ba
5 changed files with 424 additions and 0 deletions
@@ -0,0 +1,80 @@
package org.dna.mqtt.moquette.parser.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
import org.dna.mqtt.moquette.proto.messages.ConnectMessage;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
public class MQTTDecoderTest {
ByteBuf m_buff;
MQTTDecoder m_msgdec;
@Before
public void setUp() {
m_msgdec = new MQTTDecoder();
}
@Test
public void testBaseHeader() throws Exception {
m_buff = Unpooled.buffer(14);
initBaseHeader(m_buff);
//m_buff.flip();
List<Object> results = new ArrayList<Object >();
//Excercise
m_msgdec.decode(null, m_buff, results);
//Verify
assertFalse(results.isEmpty());
verifyBaseHeader((ConnectMessage)results.get(0));
}
private void initBaseHeader(ByteBuf buff) throws UnsupportedEncodingException {
initHeader(buff, (byte)0x0C);
}
private void initHeader(ByteBuf buff, byte remaingLength) throws UnsupportedEncodingException {
buff.clear().writeByte((byte)(AbstractMessage.CONNECT << 4)).writeByte(remaingLength);
//Proto name
encodeString(buff, "MQIsdp");
//version
buff.writeByte(3);
//conn flags
buff.writeByte(0xCE);
//keepAlive
buff.writeBytes(new byte[]{(byte)0, (byte) 0x0A});
}
/**
* Encode and insert the given string into the given buff
*/
private ByteBuf encodeString(ByteBuf buff, String str) throws UnsupportedEncodingException {
buff.writeBytes(Utils.encodeString(str));
return buff;
}
private void verifyBaseHeader(ConnectMessage connMessage) {
assertNotNull(connMessage);
assertEquals("MQIsdp", connMessage.getProtocolName());
assertEquals(3, connMessage.getProcotolVersion());
assertTrue(connMessage.isUserFlag());
assertTrue(connMessage.isPasswordFlag());
assertTrue(connMessage.isCleanSession());
assertEquals(10, connMessage.getKeepAlive());
assertTrue(connMessage.isWillFlag());
assertFalse(connMessage.isWillRetain());
assertEquals(1, connMessage.getWillQos());
}
}
@@ -0,0 +1,50 @@
package org.dna.mqtt.moquette.parser.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
/**
*
* @author andrea
*/
/*@RunWith(Suite.class)
@Suite.SuiteClasses({})*/
public class UtilsTest {
ByteBuf m_buff;
@Before
public void setUp() {
m_buff = Unpooled.buffer(4);
}
@Test
public void testDecodeRemainingLength() {
//1 byte length
m_buff.writeByte(0x0);
assertEquals(0, Utils.decodeRemainingLenght(m_buff));
m_buff.clear().writeByte(0x007F);
assertEquals(127, Utils.decodeRemainingLenght(m_buff));
//2 byte length
m_buff.clear().writeBytes(new byte[]{(byte)0x80, (byte)0x01});
assertEquals(128, Utils.decodeRemainingLenght(m_buff));
m_buff.clear().writeBytes(new byte[]{(byte)0xFF, (byte)0x7F});
assertEquals(16383, Utils.decodeRemainingLenght(m_buff));
//3 byte length
m_buff.clear().writeBytes(new byte[]{(byte)0x80, (byte)0x80, (byte)0x01});
assertEquals(16384, Utils.decodeRemainingLenght(m_buff));
m_buff.clear().writeBytes(new byte[]{(byte)0xFF, (byte)0xFF, (byte)0x7F});
assertEquals(2097151, Utils.decodeRemainingLenght(m_buff));
//4 byte length
m_buff.clear().writeBytes(new byte[]{(byte)0x80, (byte)0x80, (byte)0x80, (byte)0x01});
assertEquals(2097152, Utils.decodeRemainingLenght(m_buff));
m_buff.clear().writeBytes(new byte[]{(byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0x7F});
assertEquals(268435455, Utils.decodeRemainingLenght(m_buff));
}
}