io.moquette.parser.netty.SubscribeDecoderTest.java Source code

Java tutorial

Introduction

Here is the source code for io.moquette.parser.netty.SubscribeDecoderTest.java

Source

/*
 * Copyright (c) 2012-2015 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 io.moquette.parser.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.CorruptedFrameException;
import java.util.ArrayList;
import java.util.List;
import io.moquette.parser.proto.messages.AbstractMessage;
import io.moquette.parser.proto.messages.SubscribeMessage;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;

/**
 *
 * @author andrea
 */
public class SubscribeDecoderTest {
    ByteBuf m_buff;
    SubscribeDecoder m_msgdec;
    List<Object> m_results;

    @Before
    public void setUp() {
        m_msgdec = new SubscribeDecoder();
        m_results = new ArrayList<>();
    }

    @Test(expected = CorruptedFrameException.class)
    public void testBadQos() throws Exception {
        m_buff = Unpooled.buffer(2);
        initHeaderBadQos(m_buff);

        //Exercise
        m_msgdec.decode(null, m_buff, m_results);
    }

    @Test
    public void testMultiTopic() throws Exception {
        m_buff = Unpooled.buffer(4);
        SubscribeMessage.Couple c1 = new SubscribeMessage.Couple((byte) 2, "a/b");
        SubscribeMessage.Couple c2 = new SubscribeMessage.Couple((byte) 1, "c/d/e");
        initMultiTopic(m_buff, 123, c1, c2);

        //Exercise
        m_msgdec.decode(null, m_buff, m_results);

        //Verify
        assertFalse(m_results.isEmpty());
        SubscribeMessage message = (SubscribeMessage) m_results.get(0);
        assertEquals(2, message.subscriptions().size());
        assertEquals(AbstractMessage.SUBSCRIBE, message.getMessageType());
    }

    @Test(expected = CorruptedFrameException.class)
    public void testFailOnEmptyTopic() throws Exception {
        m_buff = Unpooled.buffer(4);
        initMultiTopic(m_buff, 123);

        //Exercise
        m_msgdec.decode(null, m_buff, m_results);

        //Verify
        assertFalse(m_results.isEmpty());
        SubscribeMessage message = (SubscribeMessage) m_results.get(0);
        assertEquals(2, message.subscriptions().size());
        assertEquals(AbstractMessage.SUBSCRIBE, message.getMessageType());
    }

    @Test(expected = CorruptedFrameException.class)
    public void testFailOnSubscriptionWithReservedQosPolluted() throws Exception {
        m_buff = Unpooled.buffer(4);
        SubscribeMessage.Couple c1 = new SubscribeMessage.Couple((byte) 2, "a/b");
        initPollutedTopic(m_buff, 123, c1);

        //Exercise
        m_msgdec.decode(null, m_buff, m_results);

        //Verify
        assertFalse(m_results.isEmpty());
        SubscribeMessage message = (SubscribeMessage) m_results.get(0);
        assertEquals(2, message.subscriptions().size());
        assertEquals(AbstractMessage.SUBSCRIBE, message.getMessageType());
    }

    /*
     * Check topic is at least one char [MQTT-4.7.3-1]
     * */
    @Test(expected = CorruptedFrameException.class)
    public void testMinimumTopicLength() throws Exception {
        m_buff = Unpooled.buffer(4);
        SubscribeMessage.Couple c1 = new SubscribeMessage.Couple((byte) 2, "");
        initMultiTopic(m_buff, 123, c1);

        //Exercise
        m_msgdec.decode(null, m_buff, m_results);
    }

    private void initHeaderBadQos(ByteBuf buff) {
        buff.clear().writeByte(AbstractMessage.SUBSCRIBE << 4).writeByte(0);
    }

    private void initMultiTopic(ByteBuf buff, int messageID, SubscribeMessage.Couple... topics)
            throws IllegalAccessException {
        ByteBuf topicBuffer = Unpooled.buffer(4);
        topicBuffer.writeShort(messageID);
        for (SubscribeMessage.Couple couple : topics) {
            topicBuffer.writeBytes(Utils.encodeString(couple.topicFilter));
            topicBuffer.writeByte(couple.qos);
        }

        buff.clear().writeByte(AbstractMessage.SUBSCRIBE << 4 | (byte) 0x02)
                .writeBytes(Utils.encodeRemainingLength(topicBuffer.readableBytes()));
        buff.writeBytes(topicBuffer);
    }

    private void initPollutedTopic(ByteBuf buff, int messageID, SubscribeMessage.Couple topic)
            throws IllegalAccessException {
        ByteBuf topicBuffer = Unpooled.buffer(4);
        topicBuffer.writeShort(messageID);
        topicBuffer.writeBytes(Utils.encodeString(topic.topicFilter));
        topicBuffer.writeByte(0xF0 | topic.qos);

        buff.clear().writeByte(AbstractMessage.SUBSCRIBE << 4 | (byte) 0x02)
                .writeBytes(Utils.encodeRemainingLength(topicBuffer.readableBytes()));
        buff.writeBytes(topicBuffer);
    }
}