private void fetchEncoderOutput(List<Object> out) { for (;;) { ByteBuf buf = encoder.readOutbound(); if (buf == null) { break; } if (!buf.isReadable()) { buf.release(); continue; } out.add(new DefaultHttpContent(buf)); } }
public ByteBuf readNextOutboundBuffer(int expectedWrittenMessages) { assertThat("Unexpected outbound messages size.", channel.outboundMessages(), hasSize(expectedWrittenMessages)); Object writtenMsg = channel.readOutbound(); assertThat("Unexpected message type written on the channel.", writtenMsg, is(instanceOf(ByteBuf.class))); return (ByteBuf) writtenMsg; }
private void verifyNoPublishIsReceived(EmbeddedChannel channel) { final Object messageReceived = channel.readOutbound(); assertNull("Received an out message from processor while not expected", messageReceived); } }
static void assertConnectAccepted(EmbeddedChannel channel) { MqttConnAckMessage connAck = channel.readOutbound(); final MqttConnectReturnCode connAckReturnCode = connAck.variableHeader().connectReturnCode(); assertEquals("Connect must be accepted", CONNECTION_ACCEPTED, connAckReturnCode); }
protected void unsubscribeAndVerifyAck(String topic) { final int messageId = 1; sut.unsubscribe(Collections.singletonList(topic), connection, messageId); MqttUnsubAckMessage unsubAckMessageAck = channel.readOutbound(); assertEquals("Unsubscribe must be accepted", messageId, unsubAckMessageAck.variableHeader().messageId()); }
@Test public void shouldFallbackToNoProtocolIfNoMatch() { // Given BoltProtocol protocol = newBoltProtocol( 1 ); BoltProtocolFactory handlerFactory = newProtocolFactory( 1, protocol ); EmbeddedChannel channel = new EmbeddedChannel( new ProtocolHandshaker( handlerFactory, boltChannel, logProvider, false, true ) ); // When ByteBuf input = Unpooled.wrappedBuffer( // create handshake data new byte[]{(byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17}, // preamble new byte[]{0, 0, 0, 0}, // first choice - no protocol new byte[]{0, 0, 0, 2}, // second choise - protocol 1 new byte[]{0, 0, 0, 3}, // third choice - no protocol new byte[]{0, 0, 0, 4} ); // fourth choice - no protocol channel.writeInbound( input ); // Then assertEquals( 1, channel.outboundMessages().size() ); assertByteBufEquals( Unpooled.buffer().writeInt( 0 ), channel.readOutbound() ); assertFalse( channel.isActive() ); verify( protocol, never() ).install(); }
static void verifyReceiveRetainedPublish(EmbeddedChannel embeddedChannel, String expectedTopic, String expectedContent) { MqttPublishMessage receivedPublish = embeddedChannel.readOutbound(); assertPublishIsCorrect(expectedTopic, expectedContent, receivedPublish); assertTrue("MUST be retained publish", receivedPublish.fixedHeader().isRetain()); }
private void subscribe(MqttQoS topic, String newsTopic, MQTTConnection connection) { MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(topic, newsTopic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, this.connection); MqttSubAckMessage subAck = ((EmbeddedChannel) this.connection.channel).readOutbound(); assertEquals(topic.value(), (int) subAck.payload().grantedQoSLevels().get(0)); }
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()); }
static void verifyPublishIsReceived(EmbeddedChannel embCh, MqttQoS expectedQos, String expectedPayload) { final MqttPublishMessage publishReceived = embCh.flushOutbound().readOutbound(); final String payloadMessage = DebugUtils.payload2Str(publishReceived.payload()); assertEquals("Sent and received payload must be identical", expectedPayload, payloadMessage); assertEquals("Expected QoS don't match", expectedQos, publishReceived.fixedHeader().qosLevel()); }
@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()); }
private void subscribe(MqttQoS topic, String newsTopic, MQTTConnection connection) { MqttSubscribeMessage subscribe = MqttMessageBuilders.subscribe() .addSubscription(topic, newsTopic) .messageId(1) .build(); sut.subscribeClientToTopics(subscribe, connection.getClientId(), null, this.connection); MqttSubAckMessage subAck = ((EmbeddedChannel) this.connection.channel).readOutbound(); assertEquals(topic.value(), (int) subAck.payload().grantedQoSLevels().get(0)); }
@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()); }
static void verifyReceiveRetainedPublish(EmbeddedChannel embeddedChannel, String expectedTopic, String expectedContent, MqttQoS expectedQos) { MqttPublishMessage receivedPublish = embeddedChannel.flushOutbound().readOutbound(); assertEquals(receivedPublish.fixedHeader().qosLevel(), expectedQos); assertPublishIsCorrect(expectedTopic, expectedContent, receivedPublish); assertTrue("MUST be retained publish", receivedPublish.fixedHeader().isRetain()); }
@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 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()); }