@Test(expected = CorruptedFrameException.class) public void testBadFlagUserPwd() throws UnsupportedEncodingException, Exception { m_buff = Unpooled.buffer(14); m_buff.writeByte((AbstractMessage.CONNECT << 4)).writeByte(12); encodeString(m_buff, "MQIsdp"); m_buff.writeByte(3); m_buff.writeByte(0x4E); m_buff.writeByte(0).writeByte(0x0A); List<Object> results = new ArrayList<Object >(); m_msgdec.decode(null, m_buff, results); }
@Override void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws UnsupportedEncodingException { in.resetReaderIndex(); ConnectMessage message = new ConnectMessage(); if (!decodeCommonHeader(message, in)) { in.resetReaderIndex(); return; } int remainingLength = message.getRemainingLength(); int start = in.readerIndex(); if (in.readableBytes() < 12) { in.resetReaderIndex(); return; } byte[] encProtoName = new byte[6]; in.skipBytes(2); in.readBytes(encProtoName); String protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid protoName: " + protoName); } message.setProtocolName(protoName); message.setProcotolVersion(in.readByte()); byte connFlags = in.readByte(); boolean cleanSession = ((connFlags & 0x02) >> 1) == 1 ? true : false; boolean willFlag = ((connFlags & 0x04) >> 2) == 1 ? true : false; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected will QoS in range 0..2 but found: " + willQos); } boolean willRetain = ((connFlags & 0x20) >> 5) == 1 ? true : false; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1 ? true : false; boolean userFlag = ((connFlags & 0x80) >> 7) == 1 ? true : false; if (!userFlag && passwordFlag) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected password flag to true if the user flag is true but was: " + passwordFlag); } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); int keepAlive = in.readUnsignedShort(); message.setKeepAlive(keepAlive); if (remainingLength == 12) { out.add(message); return; } String clientID = Utils.decodeString(in); if (clientID == null) { in.resetReaderIndex(); return; } message.setClientID(clientID); if (willFlag) { String willTopic = Utils.decodeString(in); if (willTopic == null) { in.resetReaderIndex(); return; } message.setWillTopic(willTopic); } if (willFlag) { String willMessage = Utils.decodeString(in); if (willMessage == null) { in.resetReaderIndex(); return; } message.setWillMessage(willMessage); } int readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } if (userFlag) { String userName = Utils.decodeString(in); if (userName == null) { in.resetReaderIndex(); return; } message.setUsername(userName); } readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } if (passwordFlag) { String password = Utils.decodeString(in); if (password == null) { in.resetReaderIndex(); return; } message.setPassword(password); } out.add(message); }
ConnectDecoder extends DemuxDecoder { @Override void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws UnsupportedEncodingException { in.resetReaderIndex(); ConnectMessage message = new ConnectMessage(); if (!decodeCommonHeader(message, in)) { in.resetReaderIndex(); return; } int remainingLength = message.getRemainingLength(); int start = in.readerIndex(); if (in.readableBytes() < 12) { in.resetReaderIndex(); return; } byte[] encProtoName = new byte[6]; in.skipBytes(2); in.readBytes(encProtoName); String protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid protoName: " + protoName); } message.setProtocolName(protoName); message.setProcotolVersion(in.readByte()); byte connFlags = in.readByte(); boolean cleanSession = ((connFlags & 0x02) >> 1) == 1 ? true : false; boolean willFlag = ((connFlags & 0x04) >> 2) == 1 ? true : false; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected will QoS in range 0..2 but found: " + willQos); } boolean willRetain = ((connFlags & 0x20) >> 5) == 1 ? true : false; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1 ? true : false; boolean userFlag = ((connFlags & 0x80) >> 7) == 1 ? true : false; if (!userFlag && passwordFlag) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected password flag to true if the user flag is true but was: " + passwordFlag); } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); int keepAlive = in.readUnsignedShort(); message.setKeepAlive(keepAlive); if (remainingLength == 12) { out.add(message); return; } String clientID = Utils.decodeString(in); if (clientID == null) { in.resetReaderIndex(); return; } message.setClientID(clientID); if (willFlag) { String willTopic = Utils.decodeString(in); if (willTopic == null) { in.resetReaderIndex(); return; } message.setWillTopic(willTopic); } if (willFlag) { String willMessage = Utils.decodeString(in); if (willMessage == null) { in.resetReaderIndex(); return; } message.setWillMessage(willMessage); } int readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } if (userFlag) { String userName = Utils.decodeString(in); if (userName == null) { in.resetReaderIndex(); return; } message.setUsername(userName); } readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } if (passwordFlag) { String password = Utils.decodeString(in); if (password == null) { in.resetReaderIndex(); return; } message.setPassword(password); } out.add(message); } }
ConnectDecoder extends DemuxDecoder { @Override void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws UnsupportedEncodingException { in.resetReaderIndex(); ConnectMessage message = new ConnectMessage(); if (!decodeCommonHeader(message, in)) { in.resetReaderIndex(); return; } int remainingLength = message.getRemainingLength(); int start = in.readerIndex(); if (in.readableBytes() < 12) { in.resetReaderIndex(); return; } byte[] encProtoName = new byte[6]; in.skipBytes(2); in.readBytes(encProtoName); String protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid protoName: " + protoName); } message.setProtocolName(protoName); message.setProcotolVersion(in.readByte()); byte connFlags = in.readByte(); boolean cleanSession = ((connFlags & 0x02) >> 1) == 1 ? true : false; boolean willFlag = ((connFlags & 0x04) >> 2) == 1 ? true : false; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected will QoS in range 0..2 but found: " + willQos); } boolean willRetain = ((connFlags & 0x20) >> 5) == 1 ? true : false; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1 ? true : false; boolean userFlag = ((connFlags & 0x80) >> 7) == 1 ? true : false; if (!userFlag && passwordFlag) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected password flag to true if the user flag is true but was: " + passwordFlag); } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); int keepAlive = in.readUnsignedShort(); message.setKeepAlive(keepAlive); if (remainingLength == 12) { out.add(message); return; } String clientID = Utils.decodeString(in); if (clientID == null) { in.resetReaderIndex(); return; } message.setClientID(clientID); if (willFlag) { String willTopic = Utils.decodeString(in); if (willTopic == null) { in.resetReaderIndex(); return; } message.setWillTopic(willTopic); } if (willFlag) { String willMessage = Utils.decodeString(in); if (willMessage == null) { in.resetReaderIndex(); return; } message.setWillMessage(willMessage); } int readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } if (userFlag) { String userName = Utils.decodeString(in); if (userName == null) { in.resetReaderIndex(); return; } message.setUsername(userName); } readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } if (passwordFlag) { String password = Utils.decodeString(in); if (password == null) { in.resetReaderIndex(); return; } message.setPassword(password); } out.add(message); }  }
ConnectDecoder extends DemuxDecoder { @Override void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws UnsupportedEncodingException { in.resetReaderIndex(); ConnectMessage message = new ConnectMessage(); if (!decodeCommonHeader(message, in)) { in.resetReaderIndex(); return; } int remainingLength = message.getRemainingLength(); int start = in.readerIndex(); if (in.readableBytes() < 12) { in.resetReaderIndex(); return; } byte[] encProtoName = new byte[6]; in.skipBytes(2); in.readBytes(encProtoName); String protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid protoName: " + protoName); } message.setProtocolName(protoName); message.setProcotolVersion(in.readByte()); byte connFlags = in.readByte(); boolean cleanSession = ((connFlags & 0x02) >> 1) == 1 ? true : false; boolean willFlag = ((connFlags & 0x04) >> 2) == 1 ? true : false; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected will QoS in range 0..2 but found: " + willQos); } boolean willRetain = ((connFlags & 0x20) >> 5) == 1 ? true : false; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1 ? true : false; boolean userFlag = ((connFlags & 0x80) >> 7) == 1 ? true : false; if (!userFlag && passwordFlag) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected password flag to true if the user flag is true but was: " + passwordFlag); } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); int keepAlive = in.readUnsignedShort(); message.setKeepAlive(keepAlive); if (remainingLength == 12) { out.add(message); return; } String clientID = Utils.decodeString(in); if (clientID == null) { in.resetReaderIndex(); return; } message.setClientID(clientID); if (willFlag) { String willTopic = Utils.decodeString(in); if (willTopic == null) { in.resetReaderIndex(); return; } message.setWillTopic(willTopic); } if (willFlag) { String willMessage = Utils.decodeString(in); if (willMessage == null) { in.resetReaderIndex(); return; } message.setWillMessage(willMessage); } int readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } if (userFlag) { String userName = Utils.decodeString(in); if (userName == null) { in.resetReaderIndex(); return; } message.setUsername(userName); } readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } if (passwordFlag) { String password = Utils.decodeString(in); if (password == null) { in.resetReaderIndex(); return; } message.setPassword(password); } out.add(message); }   }
ConnectDecoder extends DemuxDecoder { @Override void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws UnsupportedEncodingException { in.resetReaderIndex(); ConnectMessage message = new ConnectMessage(); if (!decodeCommonHeader(message, in)) { in.resetReaderIndex(); return; } int remainingLength = message.getRemainingLength(); int start = in.readerIndex(); if (in.readableBytes() < 12) { in.resetReaderIndex(); return; } byte[] encProtoName = new byte[6]; in.skipBytes(2); in.readBytes(encProtoName); String protoName = new String(encProtoName, "UTF-8"); if (!"MQIsdp".equals(protoName)) { in.resetReaderIndex(); throw new CorruptedFrameException("Invalid protoName: " + protoName); } message.setProtocolName(protoName); message.setProcotolVersion(in.readByte()); byte connFlags = in.readByte(); boolean cleanSession = ((connFlags & 0x02) >> 1) == 1 ? true : false; boolean willFlag = ((connFlags & 0x04) >> 2) == 1 ? true : false; byte willQos = (byte) ((connFlags & 0x18) >> 3); if (willQos > 2) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected will QoS in range 0..2 but found: " + willQos); } boolean willRetain = ((connFlags & 0x20) >> 5) == 1 ? true : false; boolean passwordFlag = ((connFlags & 0x40) >> 6) == 1 ? true : false; boolean userFlag = ((connFlags & 0x80) >> 7) == 1 ? true : false; if (!userFlag && passwordFlag) { in.resetReaderIndex(); throw new CorruptedFrameException("Expected password flag to true if the user flag is true but was: " + passwordFlag); } message.setCleanSession(cleanSession); message.setWillFlag(willFlag); message.setWillQos(willQos); message.setWillRetain(willRetain); message.setPasswordFlag(passwordFlag); message.setUserFlag(userFlag); int keepAlive = in.readUnsignedShort(); message.setKeepAlive(keepAlive); if (remainingLength == 12) { out.add(message); return; } String clientID = Utils.decodeString(in); if (clientID == null) { in.resetReaderIndex(); return; } message.setClientID(clientID); if (willFlag) { String willTopic = Utils.decodeString(in); if (willTopic == null) { in.resetReaderIndex(); return; } message.setWillTopic(willTopic); } if (willFlag) { String willMessage = Utils.decodeString(in); if (willMessage == null) { in.resetReaderIndex(); return; } message.setWillMessage(willMessage); } int readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } if (userFlag) { String userName = Utils.decodeString(in); if (userName == null) { in.resetReaderIndex(); return; } message.setUsername(userName); } readed = in.readerIndex() - start; if (readed == remainingLength) { out.add(message); return; } if (passwordFlag) { String password = Utils.decodeString(in); if (password == null) { in.resetReaderIndex(); return; } message.setPassword(password); } out.add(message); }    }