Added check of reserved flags for PUBREL message

This commit is contained in:
selva.andre
2014-08-02 11:37:31 +02:00
parent b0ef8114b6
commit 53f87a6e4f
2 changed files with 107 additions and 4 deletions
@@ -15,6 +15,13 @@
*/
package org.dna.mqtt.moquette.parser.netty;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.util.AttributeMap;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
import org.dna.mqtt.moquette.proto.messages.MessageIDMessage;
import org.dna.mqtt.moquette.proto.messages.PubRelMessage;
@@ -22,11 +29,31 @@ import org.dna.mqtt.moquette.proto.messages.PubRelMessage;
*
* @author andrea
*/
class PubRelDecoder extends MessageIDDecoder {
class PubRelDecoder extends DemuxDecoder {
@Override
protected MessageIDMessage createMessage() {
return new PubRelMessage();
void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws UnsupportedEncodingException {
decode((AttributeMap)ctx, in, out);
}
// @Override
void decode(AttributeMap ctx, ByteBuf in, List<Object> out) throws UnsupportedEncodingException {
in.resetReaderIndex();
//Common decoding part
MessageIDMessage message = new PubRelMessage();
if (!decodeCommonHeader(message, in)) {
in.resetReaderIndex();
return;
}
//if 3.1.1, check the flags (dup, retain and qos == 0)
if (message.isDupFlag() || message.isRetainFlag() || message.getQos() != AbstractMessage.QOSType.LEAST_ONE) {
throw new CorruptedFrameException("Received a PURREL with fixed header flags != b0010");
}
//read messageIDs
message.setMessageID(in.readUnsignedShort());
out.add(message);
}
}
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2012-2014 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/
package org.dna.mqtt.moquette.parser.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.CorruptedFrameException;
import io.netty.util.AttributeMap;
import io.netty.util.DefaultAttributeMap;
import java.util.ArrayList;
import java.util.List;
import org.dna.mqtt.moquette.proto.messages.AbstractMessage;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
/**
*
* @author andrea
*/
public class PubRelDecoderTest {
private static final int MESSAGE_ID = 123;
ByteBuf m_buff;
PubRelDecoder m_msgdec;
AttributeMap attrMap;
@Before
public void setUp() {
m_msgdec = new PubRelDecoder();
attrMap = new DefaultAttributeMap();
}
@Test
public void testValidPurRel() throws Exception {
m_buff = Unpooled.buffer(4);
m_buff.clear().writeByte(AbstractMessage.PUBREL << 4 | 0x02).writeByte(4);
m_buff.writeShort(MESSAGE_ID); //fake message_id
List<Object> results = new ArrayList<Object>();
//Excercise
m_msgdec.decode(this.attrMap, m_buff, results);
//Verify
assertFalse(results.isEmpty());
}
@Test(expected = CorruptedFrameException.class)
public void testInvalidPurRel_badReservedFlags() throws Exception {
m_buff = Unpooled.buffer(4);
m_buff.clear().writeByte(AbstractMessage.PUBREL << 4 | 0x03).writeByte(4);
m_buff.writeShort(MESSAGE_ID); //fake message_id
List<Object> results = new ArrayList<Object>();
//Excercise
m_msgdec.decode(this.attrMap, m_buff, results);
}
}