/** * Returns {@code true} if the {@link Channel} is open and records optionally * an {@link Exception} if it isn't. */ private boolean checkOpen(boolean recordException) { if (!isOpen()) { if (recordException) { recordException(new ClosedChannelException()); } return false; } return true; }
/** * Returns {@code true} if the {@link Channel} is open and records optionally * an {@link Exception} if it isn't. */ private boolean checkOpen(boolean recordException) { if (!isOpen()) { if (recordException) { recordException(new ClosedChannelException()); } return false; } return true; }
/** * Returns {@code true} if the {@link Channel} is open and records optionally * an {@link Exception} if it isn't. */ private boolean checkOpen(boolean recordException) { if (!isOpen()) { if (recordException) { recordException(new ClosedChannelException()); } return false; } return true; }
@Test(timeout = 60000) public void testCloseWithFlush() throws Exception { TestSubscriber<Void> subscriber = new TestSubscriber<>(); channelOpRule.channelOperations.close().subscribe(subscriber); subscriber.assertTerminalEvent(); subscriber.assertNoErrors(); assertThat("Channel not closed.", channelOpRule.channel.isOpen(), is(false)); }
@Test public void testUnsubscribeWithBadFormattedTopic() { connect(this.connection, FAKE_CLIENT_ID); // Exercise sut.unsubscribe(singletonList(BAD_FORMATTED_TOPIC), connection, 1); // Verify assertFalse("Unsubscribe with bad topic MUST close drop the connection, (issue 68)", channel.isOpen()); }
@Test(timeout = 60000) public void testCloseWithoutFlush() throws Exception { TestSubscriber<Void> subscriber = new TestSubscriber<>(); channelOpRule.channel.write("Hello"); channelOpRule.channelOperations.close(false).subscribe(subscriber); subscriber.assertTerminalEvent(); subscriber.assertNoErrors(); channelOpRule.verifyOutboundMessages(); assertThat("Channel not closed.", channelOpRule.channel.isOpen(), is(false)); }
@Test public void dropConnectionOnPublishWithInvalidTopicFormat() { // Connect message with clean session set to true and client id is null. MqttPublishMessage publish = MqttMessageBuilders.publish() .topicName("") .retained(false) .qos(MqttQoS.AT_MOST_ONCE) .payload(Unpooled.copiedBuffer("Hello MQTT world!".getBytes(UTF_8))).build(); sut.processPublish(publish); // Verify assertFalse("Connection should be closed by the broker", channel.isOpen()); }
@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 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 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 testForceClientDisconnection_issue116() { MqttConnectMessage msg = connMsg.clientId(FAKE_CLIENT_ID) .username(TEST_USER) .password(TEST_PWD) .build(); sut.processConnect(msg); assertEqualsConnAck(CONNECTION_ACCEPTED, channel.readOutbound()); // now create another connection and check the new one closes the older MQTTConnection anotherConnection = createMQTTConnection(CONFIG); anotherConnection.processConnect(msg); EmbeddedChannel anotherChannel = (EmbeddedChannel) anotherConnection.channel; assertEqualsConnAck(CONNECTION_ACCEPTED, anotherChannel.readOutbound()); // Verify assertFalse("First 'FAKE_CLIENT_ID' channel MUST be closed by the broker", channel.isOpen()); assertTrue("Second 'FAKE_CLIENT_ID' channel MUST be still open", anotherChannel.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 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 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 testConnAckContainsSessionPresentFlag() { MqttConnectMessage msg = connMsg.clientId(FAKE_CLIENT_ID) .protocolVersion(MqttVersion.MQTT_3_1_1) .build(); NettyUtils.clientID(channel, FAKE_CLIENT_ID); NettyUtils.cleanSession(channel, false); // Connect a first time sut.bindToSession(connection, msg, FAKE_CLIENT_ID); // disconnect sut.disconnect(FAKE_CLIENT_ID); // Exercise, reconnect EmbeddedChannel anotherChannel = new EmbeddedChannel(); MQTTConnection anotherConnection = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZEROBYTE_CLIENT_ID, anotherChannel); sut.bindToSession(anotherConnection, msg, FAKE_CLIENT_ID); // Verify assertEqualsConnAck(CONNECTION_ACCEPTED, anotherChannel.readOutbound()); assertTrue("Connection is accepted and therefore should remain open", anotherChannel.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()); }
@Test public void connectWithCleanSessionUpdateClientSession() { // first connect with clean session true MqttConnectMessage msg = connMsg.clientId(FAKE_CLIENT_ID).cleanSession(true).build(); connection.processConnect(msg); assertEqualsConnAck(CONNECTION_ACCEPTED, channel.readOutbound()); connection.processDisconnect(null); assertFalse(channel.isOpen()); // second connect with clean session false EmbeddedChannel anotherChannel = new EmbeddedChannel(); MQTTConnection anotherConnection = createMQTTConnection(ALLOW_ANONYMOUS_AND_ZEROBYTE_CLIENT_ID, anotherChannel); MqttConnectMessage secondConnMsg = MqttMessageBuilders.connect() .clientId(FAKE_CLIENT_ID) .protocolVersion(MqttVersion.MQTT_3_1) .build(); anotherConnection.processConnect(secondConnMsg); assertEqualsConnAck(CONNECTION_ACCEPTED, anotherChannel.readOutbound()); // Verify client session is clean false Session session = sut.retrieve(FAKE_CLIENT_ID); assertFalse(session.isClean()); } }