protected void connect(MQTTConnection connection, MqttConnectMessage connectMessage) { connection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted((EmbeddedChannel) connection.channel); }
@Test public void testSubscribe() { connection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted(channel); // Exercise & verify subscribe(channel, NEWS_TOPIC, AT_MOST_ONCE); }
@Before public void setUp() { sessionRegistry = initPostOfficeAndSubsystems(); mockAuthenticator = new MockAuthenticator(singleton(FAKE_CLIENT_ID), singletonMap(TEST_USER, TEST_PWD)); connection = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZERO_BYTES_CLID); connectMessage = ConnectionTestUtils.buildConnect(FAKE_CLIENT_ID); connection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted(channel); }
private MQTTConnection connectNotCleanAs(String clientId) { EmbeddedChannel channel = new EmbeddedChannel(); MQTTConnection connection = createMQTTConnection(CONFIG, channel); connection.processConnect(ConnectionTestUtils.buildConnectNotClean(clientId)); ConnectionTestUtils.assertConnectAccepted(channel); return connection; }
private MQTTConnection connectAs(String clientId) { EmbeddedChannel channel = new EmbeddedChannel(); MQTTConnection connection = createMQTTConnection(CONFIG, channel); connection.processConnect(ConnectionTestUtils.buildConnect(clientId)); ConnectionTestUtils.assertConnectAccepted(channel); return connection; }
private MQTTConnection connectAs(String clientId) { EmbeddedChannel channel = new EmbeddedChannel(); MQTTConnection connection = createMQTTConnection(CONFIG, channel); connection.processConnect(ConnectionTestUtils.buildConnect(clientId)); ConnectionTestUtils.assertConnectAccepted(channel); return connection; }
@Test public void testWillIsFired() { final PostOffice postOfficeMock = mock(PostOffice.class); sut = createMQTTConnectionWithPostOffice(CONFIG, postOfficeMock); channel = (EmbeddedChannel) sut.channel; MqttConnectMessage msg = connMsg.clientId(FAKE_CLIENT_ID).willFlag(true) .willTopic("topic").willMessage("Topic message").build(); sut.processConnect(msg); // Exercise sut.handleConnectionLost(); // Verify verify(postOfficeMock).fireWill(any(Session.Will.class)); assertFalse("Connection MUST be disconnected", sut.isConnected()); }
protected void connect() { MqttConnectMessage connectMessage = MqttMessageBuilders.connect() .clientId(FAKE_CLIENT_ID) .build(); connection.processConnect(connectMessage); MqttConnAckMessage connAck = channel.readOutbound(); assertEquals("Connect must be accepted", CONNECTION_ACCEPTED, connAck.variableHeader().connectReturnCode()); }
@Test public void testConnect_badClientID() { connMsg.clientId("extremely_long_clientID_greater_than_23").build(); // Exercise sut.processConnect(connMsg.clientId("extremely_long_clientID_greater_than_23").build()); // Verify assertEqualsConnAck(CONNECTION_ACCEPTED, channel.readOutbound()); }
@Test public void acceptAnonymousClient() { MqttConnectMessage msg = connMsg.clientId(FAKE_CLIENT_ID).build(); // Exercise sut.processConnect(msg); // Verify assertEqualsConnAck(CONNECTION_ACCEPTED, channel.readOutbound()); assertTrue("Connection is accepted and therefore must remain open", channel.isOpen()); }
@Test public void testDoubleSubscribe() { connection.processConnect(connectMessage); ConnectionTestUtils.assertConnectAccepted(channel); assertEquals("After CONNECT subscription MUST be empty", 0, subscriptions.size()); subscribe(channel, NEWS_TOPIC, AT_MOST_ONCE); assertEquals("After /news subscribe, subscription MUST contain it", 1, subscriptions.size()); //Exercise & verify subscribe(channel, NEWS_TOPIC, AT_MOST_ONCE); }
@Test public void noPasswdAuthentication() { MqttConnectMessage msg = connMsg.clientId(FAKE_CLIENT_ID) .username(TEST_USER) .build(); // Exercise sut.processConnect(msg); // Verify assertEqualsConnAck(CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD, channel.readOutbound()); assertFalse("Connection must be closed by the broker", channel.isOpen()); }
@Test public void testZeroByteClientIdWithoutCleanSession() { // Allow zero byte client ids // Connect message without clean session set to true but client id is still null MqttConnectMessage msg = MqttMessageBuilders.connect().clientId(null).protocolVersion(MqttVersion.MQTT_3_1_1) .build(); sut.processConnect(msg); assertEqualsConnAck("Identifier must be rejected due to having clean session set to false", CONNECTION_REFUSED_IDENTIFIER_REJECTED, channel.readOutbound()); assertFalse("Connection must be closed by the broker", channel.isOpen()); }
@Test public void invalidAuthentication() { MqttConnectMessage msg = connMsg.clientId(FAKE_CLIENT_ID) .username(TEST_USER + "_fake") .password(TEST_PWD) .build(); // Exercise sut.processConnect(msg); // Verify assertEqualsConnAck(CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD, channel.readOutbound()); assertFalse("Connection should be closed by the broker.", channel.isOpen()); }
@Test public void validAuthentication() { MqttConnectMessage msg = connMsg.clientId(FAKE_CLIENT_ID) .username(TEST_USER).password(TEST_PWD).build(); // Exercise sut.processConnect(msg); // Verify assertEqualsConnAck(CONNECTION_ACCEPTED, channel.readOutbound()); assertTrue("Connection is accepted and therefore must remain open", channel.isOpen()); }
@Test public void prohibitAnonymousClient() { MqttConnectMessage msg = connMsg.clientId(FAKE_CLIENT_ID).build(); BrokerConfiguration config = new BrokerConfiguration(false, true, false); sut = createMQTTConnection(config); channel = (EmbeddedChannel) sut.channel; // Exercise sut.processConnect(msg); // Verify assertEqualsConnAck(CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD, channel.readOutbound()); assertFalse("Connection must be closed by the broker", channel.isOpen()); }
@Test public void testWillIsAccepted() { MqttConnectMessage msg = connMsg.clientId(FAKE_CLIENT_ID).willFlag(true) .willTopic("topic").willMessage("Topic message").build(); // Exercise // m_handler.setMessaging(mockedMessaging); sut.processConnect(msg); // Verify assertEqualsConnAck(CONNECTION_ACCEPTED, channel.readOutbound()); assertTrue("Connection is accepted and therefore should remain open", channel.isOpen()); }
@Test public void prohibitAnonymousClient_providingUsername() { MqttConnectMessage msg = connMsg.clientId(FAKE_CLIENT_ID) .username(TEST_USER + "_fake") .build(); BrokerConfiguration config = new BrokerConfiguration(false, true, false); createMQTTConnection(config); // Exercise sut.processConnect(msg); // Verify assertEqualsConnAck(CONNECTION_REFUSED_BAD_USER_NAME_OR_PASSWORD, channel.readOutbound()); assertFalse("Connection should be closed by the broker.", channel.isOpen()); }
@Test public void testZeroByteClientIdNotAllowed() { BrokerConfiguration config = new BrokerConfiguration(false, false, false); sut = createMQTTConnection(config); channel = (EmbeddedChannel) sut.channel; // Connect message with clean session set to true and client id is null. MqttConnectMessage msg = connMsg.clientId(null) .protocolVersion(MqttVersion.MQTT_3_1_1) .cleanSession(true) .build(); sut.processConnect(msg); assertEqualsConnAck("Zero byte client identifiers are not allowed", CONNECTION_REFUSED_IDENTIFIER_REJECTED, channel.readOutbound()); assertFalse("Connection must closed", channel.isOpen()); }
@Test public void testZeroByteClientIdWithCleanSession() { // Connect message with clean session set to true and client id is null. MqttConnectMessage msg = MqttMessageBuilders.connect() .protocolVersion(MqttVersion.MQTT_3_1_1) .clientId(null) .cleanSession(true) .build(); sut.processConnect(msg); assertEqualsConnAck("Connection must be accepted", CONNECTION_ACCEPTED, channel.readOutbound()); assertNotNull("unique clientid must be generated", sut.getClientId()); assertTrue("clean session flag must be true", sessionRegistry.retrieve(sut.getClientId()).isClean()); assertTrue("Connection must be open", channel.isOpen()); }