use of io.vertx.proton.ProtonClient in project hono by eclipse.
the class ConnectionFactoryImplTest method testConnectUsesConfiguredCipherSuitesOnly.
/**
* Verifies that the factory sets the configured cipher suites on the AMQP connection.
*/
@Test
public void testConnectUsesConfiguredCipherSuitesOnly() {
// GIVEN a factory configured to use a set of cipher suites only
final ClientConfigProperties config = new ClientConfigProperties();
config.setHost("remote.host");
config.setTrustStorePath(PREFIX_KEY_PATH + "trusted-certs.pem");
config.setSupportedCipherSuites(Arrays.asList("TLS_PSK_WITH_AES_256_CCM_8", "TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8"));
final ProtonClient client = mock(ProtonClient.class);
final ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, config);
factory.setProtonClient(client);
// WHEN connecting to the server
factory.connect(null, null, null, c -> {
});
// THEN the factory uses TLS when establishing the connection
final ArgumentCaptor<ProtonClientOptions> optionsCaptor = ArgumentCaptor.forClass(ProtonClientOptions.class);
verify(client).connect(optionsCaptor.capture(), eq("remote.host"), anyInt(), any(), any(), VertxMockSupport.anyHandler());
assertTrue(optionsCaptor.getValue().isSsl());
assertThat(optionsCaptor.getValue().getEnabledCipherSuites()).containsExactly("TLS_PSK_WITH_AES_256_CCM_8", "TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8");
}
use of io.vertx.proton.ProtonClient in project hono by eclipse.
the class ConnectionFactoryImplTest method testConnectInvokesHandlerOnDisconnectAfterSendingOpenFrame.
/**
* Verifies that the given result handler is invoked if a connection gets closed after SASL auth was successful and
* AMQP open frame was sent by client, but no AMQP open frame from server was received yet.
*/
@Test
public void testConnectInvokesHandlerOnDisconnectAfterSendingOpenFrame() {
// GIVEN a factory configured to connect to a server (with mocked connection)
final ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, props);
final ProtonClient protonClientMock = mock(ProtonClient.class);
final ProtonConnection protonConnectionMock = mock(ProtonConnection.class, Mockito.RETURNS_SELF);
doAnswer(invocation -> {
final Handler<AsyncResult<ProtonConnection>> resultHandler = invocation.getArgument(5);
resultHandler.handle(Future.succeededFuture(protonConnectionMock));
return null;
}).when(protonClientMock).connect(any(ProtonClientOptions.class), any(), anyInt(), any(), any(), VertxMockSupport.anyHandler());
factory.setProtonClient(protonClientMock);
// WHEN trying to connect to the server
final Promise<ProtonConnection> resultHandler = Promise.promise();
factory.connect(new ProtonClientOptions(), null, null, resultHandler);
// THEN the disconnect handler gets called which calls the given result handler with a failure
final ArgumentCaptor<Handler<ProtonConnection>> disconnectHandlerCaptor = VertxMockSupport.argumentCaptorHandler();
verify(protonConnectionMock).disconnectHandler(disconnectHandlerCaptor.capture());
disconnectHandlerCaptor.getValue().handle(protonConnectionMock);
// as we call handler ourselves handling is synchronous here
assertTrue(resultHandler.future().failed(), "Connection result handler was not failed");
}
use of io.vertx.proton.ProtonClient in project vertx-examples by vert-x3.
the class Receiver method start.
@Override
public void start() throws Exception {
ProtonClient client = ProtonClient.create(vertx);
client.connect("localhost", 5672, res -> {
if (!res.succeeded()) {
System.out.println("Connect failed: " + res.cause());
return;
}
ProtonConnection connection = res.result();
connection.open();
connection.createReceiver(address).handler((delivery, msg) -> {
String content = (String) ((AmqpValue) msg.getBody()).getValue();
System.out.println("Received message with content: " + content);
// By default, receivers automatically accept (and settle) the delivery
// when the handler returns, if no other disposition has been applied.
// To change this and always manage dispositions yourself, use the
// setAutoAccept method on the receiver.
}).open();
});
}
use of io.vertx.proton.ProtonClient in project hono by eclipse.
the class ConnectionFactoryImplTest method testConnectEnablesSslIfTrustStoreIsConfigured.
/**
* Verifies that the factory uses TLS when connecting to the peer if a trust store
* is configured but TLS has not been enabled explicitly.
*/
@SuppressWarnings("unchecked")
@Test
public void testConnectEnablesSslIfTrustStoreIsConfigured() {
// GIVEN a factory configured to use a specific trust store
final ClientConfigProperties config = new ClientConfigProperties();
config.setHost("remote.host");
config.setTrustStorePath("/tmp/trusted-ca.p12");
final ProtonClient client = mock(ProtonClient.class);
final ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, config);
factory.setProtonClient(client);
// WHEN connecting to the server
factory.connect(null, null, null, c -> {
});
// THEN the factory uses TLS when establishing the connection
final ArgumentCaptor<ProtonClientOptions> optionsCaptor = ArgumentCaptor.forClass(ProtonClientOptions.class);
verify(client).connect(optionsCaptor.capture(), eq("remote.host"), anyInt(), any(), any(), any(Handler.class));
assertTrue(optionsCaptor.getValue().isSsl());
}
use of io.vertx.proton.ProtonClient in project hono by eclipse.
the class ConnectionFactoryImplTest method testConnectDoesNotUseSaslPlainForEmptyUsernameAndPassword.
/**
* Verifies that the factory does not enable SASL_PLAIN if the username and password are empty
* strings.
*
* @param ctx The vert.x test context.
*/
@SuppressWarnings("unchecked")
@Test
public void testConnectDoesNotUseSaslPlainForEmptyUsernameAndPassword(final TestContext ctx) {
// GIVEN a factory configured to connect to a server
final ProtonClientOptions options = new ProtonClientOptions();
final ProtonClient client = mock(ProtonClient.class);
final ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, props);
factory.setProtonClient(client);
// WHEN connecting to the server using empty strings for username and password
factory.connect(options, "", "", null, null, c -> {
});
// THEN the factory does not enable the SASL_PLAIN mechanism when establishing
// the connection
ArgumentCaptor<ProtonClientOptions> optionsCaptor = ArgumentCaptor.forClass(ProtonClientOptions.class);
verify(client).connect(optionsCaptor.capture(), anyString(), anyInt(), eq(""), eq(""), any(Handler.class));
assertFalse(optionsCaptor.getValue().getEnabledSaslMechanisms().contains("PLAIN"));
}
Aggregations