MQTT3.1.3-6 stated that the client can connect without giving up an client identifier. However, the broker should generate one for the client id as of the client gave the identifier to broker. The client will now disconnect if the client hasn't given up an identifier with clean session set to 0. Done this regarding issue #190
NettyChannelAssertions class has been modified so you could add a message to the assertion. Unit tests have been added to test the modified processConnect() method. The server can allow zero byte client identifiers through moquette configuration.
This commit is contained in:
@@ -30,9 +30,17 @@ import static org.junit.Assert.assertTrue;
|
||||
class NettyChannelAssertions {
|
||||
|
||||
static void assertEqualsConnAck(byte expectedCode, Object connAck) {
|
||||
assertTrue(connAck instanceof ConnAckMessage);
|
||||
assertEqualsConnAck(null, expectedCode, connAck);
|
||||
}
|
||||
|
||||
static void assertEqualsConnAck(String msg, byte expectedCode, Object connAck) {
|
||||
assertTrue("connAck is not an instance of ConnAckMessage", connAck instanceof ConnAckMessage);
|
||||
ConnAckMessage connAckMsg = (ConnAckMessage) connAck;
|
||||
assertEquals(expectedCode, connAckMsg.getReturnCode());
|
||||
|
||||
if(msg == null)
|
||||
assertEquals(expectedCode, connAckMsg.getReturnCode());
|
||||
else
|
||||
assertEquals(msg, expectedCode, connAckMsg.getReturnCode());
|
||||
}
|
||||
|
||||
static void assertConnAckAccepted(EmbeddedChannel channel) {
|
||||
|
||||
@@ -35,9 +35,7 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static io.moquette.parser.netty.Utils.VERSION_3_1_1;
|
||||
import static io.moquette.parser.proto.messages.ConnAckMessage.BAD_USERNAME_OR_PASSWORD;
|
||||
import static io.moquette.parser.proto.messages.ConnAckMessage.CONNECTION_ACCEPTED;
|
||||
import static io.moquette.parser.proto.messages.ConnAckMessage.UNNACEPTABLE_PROTOCOL_VERSION;
|
||||
import static io.moquette.parser.proto.messages.ConnAckMessage.*;
|
||||
import static io.moquette.spi.impl.NettyChannelAssertions.assertEqualsConnAck;
|
||||
import static io.moquette.spi.impl.NettyChannelAssertions.assertEqualsSubAck;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -331,4 +329,59 @@ public class ProtocolProcessor_CONNECT_Test {
|
||||
//verify that the first subscription is still preserved
|
||||
assertTrue(subscriptions.contains(expectedSubscription));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZeroByteClientIdWithoutCleanSession() {
|
||||
// Allow zero byte client ids
|
||||
m_processor = new ProtocolProcessor();
|
||||
m_processor.init(subscriptions, m_messagesStore, m_sessionStore, m_mockAuthenticator, true, true,
|
||||
new PermitAllAuthorizator(), ProtocolProcessorTest.NO_OBSERVERS_INTERCEPTOR);
|
||||
|
||||
// Connect message without clean session set to true but client id is still null
|
||||
connMsg = new ConnectMessage();
|
||||
connMsg.setProtocolVersion(VERSION_3_1_1);
|
||||
connMsg.setClientID(null);
|
||||
connMsg.setCleanSession(false);
|
||||
|
||||
m_processor.processConnect(m_session, connMsg);
|
||||
assertEqualsConnAck("Identifier should be rejected due to having clean session set to false.",
|
||||
IDENTIFIER_REJECTED, m_session.readOutbound());
|
||||
|
||||
assertFalse("Connection should be closed by the broker.", m_session.isOpen());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZeroByteClientIdWithCleanSession() {
|
||||
// Allow zero byte client ids
|
||||
m_processor = new ProtocolProcessor();
|
||||
m_processor.init(subscriptions, m_messagesStore, m_sessionStore, m_mockAuthenticator, true, true,
|
||||
new PermitAllAuthorizator(), ProtocolProcessorTest.NO_OBSERVERS_INTERCEPTOR);
|
||||
|
||||
// Connect message with clean session set to true and client id is null.
|
||||
connMsg = new ConnectMessage();
|
||||
connMsg.setProtocolVersion(VERSION_3_1_1);
|
||||
connMsg.setClientID(null);
|
||||
connMsg.setCleanSession(true);
|
||||
|
||||
m_processor.processConnect(m_session, connMsg);
|
||||
assertEqualsConnAck("Connection should be accepted. A unique clientid should be generated" +
|
||||
" and clean session set to true.", CONNECTION_ACCEPTED, m_session.readOutbound());
|
||||
|
||||
assertTrue("Connection should be valid and open,", m_session.isOpen());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZeroByteClientIdNotAllowed() {
|
||||
// Connect message with clean session set to true and client id is null.
|
||||
connMsg = new ConnectMessage();
|
||||
connMsg.setProtocolVersion(VERSION_3_1_1);
|
||||
connMsg.setClientID(null);
|
||||
connMsg.setCleanSession(true);
|
||||
|
||||
m_processor.processConnect(m_session, connMsg);
|
||||
assertEqualsConnAck("Zero byte client identifiers are not allowed.",
|
||||
IDENTIFIER_REJECTED, m_session.readOutbound());
|
||||
|
||||
assertFalse("Connection should closed.", m_session.isOpen());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user