Exemplo n.º 1
0
  @Test
  public void testBugBadRemainingCalculation() throws Exception {
    byte[] overallMessage =
        new byte[] {
          (byte) 0x90,
          0x03, // fixed header
          0x00,
          0x0A, // MSG ID
          0x01
        }; // QoS array
    m_buff = Unpooled.buffer(overallMessage.length);
    m_buff.writeBytes(overallMessage);

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

    assertFalse(m_results.isEmpty());
    SubAckMessage message = (SubAckMessage) m_results.get(0);
    assertNotNull(message);
    assertEquals(0x0A, message.getMessageID().intValue());
    assertEquals(1, message.types().size());
    assertEquals(AbstractMessage.QOSType.LEAST_ONE, message.types().get(0));
  }
Exemplo n.º 2
0
  @Test
  public void testBadQos() throws Exception {
    initHeaderQos(
        m_buff,
        0xAABB,
        AbstractMessage.QOSType.LEAST_ONE,
        AbstractMessage.QOSType.MOST_ONE,
        AbstractMessage.QOSType.MOST_ONE);

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

    // Verify
    assertFalse(m_results.isEmpty());
    SubAckMessage message = (SubAckMessage) m_results.get(0);
    assertNotNull(message);
    assertEquals(0xAABB, message.getMessageID().intValue());
    List<AbstractMessage.QOSType> qoses = message.types();
    assertEquals(3, qoses.size());
    assertEquals(AbstractMessage.QOSType.LEAST_ONE, qoses.get(0));
    assertEquals(AbstractMessage.QOSType.MOST_ONE, qoses.get(1));
    assertEquals(AbstractMessage.QOSType.MOST_ONE, qoses.get(2));
    assertEquals(AbstractMessage.SUBACK, message.getMessageType());
  }
  private void onMessageFromClient(AbstractMessage msg) throws Exception {
    Container.logger().debug("<<< " + msg);
    switch (msg.getMessageType()) {
      case CONNECT:
        ConnectMessage connect = (ConnectMessage) msg;
        ConnAckMessage connAck = new ConnAckMessage();
        if (session == null) {
          session = new MQTTSession(vertx, config);
          session.setPublishMessageHandler(this::sendMessageToClient);
          session.setKeepaliveErrorHandler(
              clientID -> {
                String cinfo = clientID;
                if (session != null) {
                  cinfo = session.getClientInfo();
                }
                Container.logger()
                    .info("keep alive exausted! closing connection for client[" + cinfo + "] ...");
                closeConnection();
              });
          connAck.setSessionPresent(false);
        } else {
          Container.logger().warn("Session alredy allocated ...");
          /*
          The Server MUST process a second CONNECT Packet sent from a Client as a protocol violation and disconnect the Client
           */
          //                    connAck.setSessionPresent(true);// TODO implement cleanSession=false
          closeConnection();
          break;
        }
        session.handleConnectMessage(
            connect,
            authenticated -> {
              if (authenticated) {
                connAck.setReturnCode(ConnAckMessage.CONNECTION_ACCEPTED);
                sendMessageToClient(connAck);
              } else {
                Container.logger()
                    .error(
                        "Authentication failed! clientID= "
                            + connect.getClientID()
                            + " username="******"type of message not known: " + msg.getClass().getSimpleName());
        break;
    }

    // TODO: forward mqtt message to backup server

  }