@Test public void testBadFlagUserPwd() throws UnsupportedEncodingException, Exception { m_buff = IoBuffer.allocate(14); m_buff.clear().put((byte)(AbstractMessage.CONNECT << 4)).put((byte)12); //Proto name encodeString(m_buff, "MQIsdp"); //version m_buff.put((byte)3); //conn flags m_buff.put((byte)0x4E); //sets user to false and password to true //keepAlive m_buff.put((byte)0).put((byte) 0x0A); m_buff.flip(); //Excercise MessageDecoderResult res = m_msgdec.decode(null, m_buff, m_mockProtoDecoder); assertNull(m_mockProtoDecoder.getMessage()); assertEquals(MessageDecoder.NOT_OK, res); }
@Test public void testBugBadRemainingCalculation() throws Exception { byte[] overallMessage = new byte[] {(byte)0x90, 0x03, //fixed header 0x00, 0x0A, //MSG ID 0x01}; //QoS array 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); SubAckMessage message = (SubAckMessage) m_mockProtoDecoder.getMessage(); assertEquals(0x0A, message.getMessageID().intValue()); assertEquals(1, message.types().size()); assertEquals(AbstractMessage.QOSType.LEAST_ONE, message.types().get(0)); }
@Test public void testHeaderWithMessageID_Payload() throws Exception { m_buff = IoBuffer.allocate(14).setAutoExpand(true); int messageID = 123; byte[] payload = new byte[]{0x0A, 0x0B, 0x0C}; initHeaderWithMessageID_Payload(m_buff, messageID, payload); m_buff.flip(); //Exercise MessageDecoderResult res = m_msgdec.decode(null, m_buff, m_mockProtoDecoder); assertNotNull(m_mockProtoDecoder.getMessage()); assertEquals(MessageDecoder.OK, res); assertEquals("Fake Topic", m_mockProtoDecoder.getMessage().getTopicName()); assertEquals(messageID, (int) m_mockProtoDecoder.getMessage().getMessageID()); TestUtils.verifyEquals(payload, m_mockProtoDecoder.getMessage().getPayload()); }
@Test public void testBugOnRealCase() throws Exception { 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); }
@Test public void testDecodeBigContent() throws Exception { int size = 129; IoBuffer payload = TestUtils.generateRandomPayload(size); IoBuffer firstPublish = generatePublishQoS0(payload); IoBuffer secondPublish = generatePublishQoS0(TestUtils.generateRandomPayload(size)); IoBuffer doubleMessageBuf = IoBuffer.allocate(size * 2).setAutoExpand(true); doubleMessageBuf.put(firstPublish).put(secondPublish).flip(); //Exercise MessageDecoderResult res = m_msgdec.decode(null, doubleMessageBuf, m_mockProtoDecoder); assertEquals(MessageDecoder.OK, res); PublishMessage pubMsg = m_mockProtoDecoder.getMessage(); assertNotNull(pubMsg); res = m_msgdec.decode(null, doubleMessageBuf, m_mockProtoDecoder); assertNotNull(m_mockProtoDecoder.getMessage()); assertEquals(MessageDecoder.OK, res); m_buff.flip(); }
@Test public void testDecodable_OK() { m_buff.put((byte)(AbstractMessage.CONNECT << 4)) .put((byte)0) //0 length .flip(); assertEquals(MessageDecoder.OK , m_msgdec.decodable(null, m_buff)); }
@Test public void testDecodable_NOT_OK() { m_buff.put((byte)(AbstractMessage.CONNACK << 4)) .put((byte)0) //0 length .flip(); assertEquals(MessageDecoder.NOT_OK , m_msgdec.decodable(null, m_buff)); }
@Test public void testHeader() throws Exception { m_buff = IoBuffer.allocate(14); int messageId = 0xAABB; initHeader(m_buff, messageId); m_buff.flip(); //Exercise MessageDecoderResult res = m_msgdec.decode(null, m_buff, m_mockProtoDecoder); assertNotNull(m_mockProtoDecoder.getMessage()); assertEquals(MessageDecoder.OK, res); assertEquals(messageId, m_mockProtoDecoder.getMessage().getMessageID().intValue()); assertEquals(AbstractMessage.PUBACK, m_mockProtoDecoder.getMessage().getMessageType()); }
@Test public void testDecodable_OK() { m_buff.put((byte)(AbstractMessage.CONNACK << 4)) .put((byte)0) //0 length .flip(); assertEquals(MessageDecoder.OK , m_msgdec.decodable(null, m_buff)); }
@Test public void testDecodable_NOT_OK() { m_buff.put((byte)(AbstractMessage.CONNECT << 4)) .put((byte)0) //0 length .flip(); assertEquals(MessageDecoder.NOT_OK , m_msgdec.decodable(null, m_buff)); }
@Test public void testHeader() throws Exception { m_buff = IoBuffer.allocate(14); initHeader(m_buff); m_buff.flip(); //Exercise MessageDecoderResult res = m_msgdec.decode(null, m_buff, m_mockProtoDecoder); assertNotNull(m_mockProtoDecoder.getMessage()); assertEquals(MessageDecoder.OK, res); assertEquals(ConnAckMessage.CONNECTION_ACCEPTED, m_mockProtoDecoder.getMessage().getReturnCode()); assertEquals(AbstractMessage.CONNACK, m_mockProtoDecoder.getMessage().getMessageType()); }
@Test public void testDecodable_OK() { m_buff.put((byte) (AbstractMessage.PUBLISH << 4)) .put((byte) 0) //0 length .flip(); assertEquals(MessageDecoder.OK, m_msgdec.decodable(null, m_buff)); }
@Test public void testHeader() throws Exception { m_buff = IoBuffer.allocate(14); initHeader(m_buff); m_buff.flip(); //Exercise MessageDecoderResult res = m_msgdec.decode(null, m_buff, m_mockProtoDecoder); assertNotNull(m_mockProtoDecoder.getMessage()); assertEquals(MessageDecoder.OK, res); assertEquals("Fake Topic", m_mockProtoDecoder.getMessage().getTopicName()); assertNull(m_mockProtoDecoder.getMessage().getMessageID()); assertEquals(AbstractMessage.PUBLISH, m_mockProtoDecoder.getMessage().getMessageType()); }
@Test public void testHeaderWithMessageID() throws Exception { m_buff = IoBuffer.allocate(14).setAutoExpand(true); int messageID = 123; initHeaderWithMessageID(m_buff, messageID); m_buff.flip(); //Exercise MessageDecoderResult res = m_msgdec.decode(null, m_buff, m_mockProtoDecoder); assertNotNull(m_mockProtoDecoder.getMessage()); assertEquals(MessageDecoder.OK, res); assertEquals("Fake Topic", m_mockProtoDecoder.getMessage().getTopicName()); assertEquals(messageID, (int) m_mockProtoDecoder.getMessage().getMessageID()); }
public static void addDecoder(Class t, MessageDecoder encoder){ if (decoderMap == null ){ decoderMap = new HashMap<Class, MessageDecoder>(); } decoderMap.put(t, encoder); }
public static Map<Class, MessageDecoder> getDecoderMap(){ return decoderMap; }
@Test public void testDecodable_NEED_DATA() { m_buff = IoBuffer.allocate(0); assertFalse(m_buff.hasRemaining()); assertEquals(MessageDecoder.NEED_DATA , m_msgdec.decodable(null, m_buff)); }