use of io.vertx.proton.ProtonClient in project hono by eclipse.
the class AmqpAdapterTestBase method connectToAdapter.
/**
* Connects to the AMQP protocol adapter using a username and password.
*
* @param tlsVersion The TLS protocol version to use for connecting to the adapter.
* @param cipherSuite The TLS cipher suite to use for connecting to the adapter or {@code null} if the
* cipher suite should not be restricted.
* @param username The username to use for authentication.
* @param password The password to use for authentication.
* @return A succeeded future containing the established connection.
*/
protected Future<ProtonConnection> connectToAdapter(final String tlsVersion, final String cipherSuite, final String username, final String password) {
final Promise<ProtonConnection> result = Promise.promise();
final ProtonClient client = ProtonClient.create(vertx);
final ProtonClientOptions options = new ProtonClientOptions(defaultOptions);
options.addEnabledSaslMechanism(ProtonSaslPlainImpl.MECH_NAME);
options.setEnabledSecureTransportProtocols(Set.of(tlsVersion));
Optional.ofNullable(cipherSuite).ifPresent(options::addEnabledCipherSuite);
client.connect(options, IntegrationTestSupport.AMQP_HOST, IntegrationTestSupport.AMQPS_PORT, username, password, result);
return result.future().compose(con -> handleConnectAttempt(con, IntegrationTestSupport.AMQP_HOST));
}
use of io.vertx.proton.ProtonClient in project hono by eclipse.
the class AmqpAdapterTestBase method connectToAdapter.
/**
* Connects to the AMQP protocol adapter using a client certificate.
*
* @param hostname The name of the host to connect to.
* @param clientCertificate The certificate to use for authentication.
* @param tlsProtocolVersion The TLS protocol version to use for connecting to the host.
* @return A succeeded future containing the established connection.
* @throws NullPointerException if any of the parameters are {@code null}.
*/
protected Future<ProtonConnection> connectToAdapter(final String hostname, final SelfSignedCertificate clientCertificate, final String tlsProtocolVersion) {
Objects.requireNonNull(hostname);
Objects.requireNonNull(clientCertificate);
Objects.requireNonNull(tlsProtocolVersion);
final Promise<ProtonConnection> result = Promise.promise();
final ProtonClient client = ProtonClient.create(vertx);
final ProtonClientOptions secureOptions = new ProtonClientOptions(defaultOptions);
secureOptions.setKeyCertOptions(clientCertificate.keyCertOptions());
secureOptions.addEnabledSaslMechanism(ProtonSaslExternalImpl.MECH_NAME);
secureOptions.setEnabledSecureTransportProtocols(Set.of(tlsProtocolVersion));
client.connect(secureOptions, hostname, IntegrationTestSupport.AMQPS_PORT, result);
return result.future().compose(con -> handleConnectAttempt(con, hostname));
}
use of io.vertx.proton.ProtonClient in project hono by eclipse.
the class ConnectionFactoryImplTest method testConnectInvokesHandlerOnConnectTimeout.
/**
* Verifies that the given result handler is invoked if a connection attempt times out.
*
* @param ctx The vert.x test context.
*/
@Test
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
public void testConnectInvokesHandlerOnConnectTimeout(final VertxTestContext ctx) {
final long connectTimeout = 200L;
// GIVEN a factory configured to connect to a server with a mocked ProtonClient that won't actually try to connect
props.setConnectTimeout((int) connectTimeout);
final AtomicReference<Handler<Long>> timeoutHandlerRef = new AtomicReference<>();
when(vertx.setTimer(eq(connectTimeout), VertxMockSupport.anyHandler())).thenAnswer(invocation -> {
timeoutHandlerRef.set(invocation.getArgument(1));
return 1L;
});
final ConnectionFactoryImpl factory = new ConnectionFactoryImpl(vertx, props);
final ProtonClient protonClientMock = mock(ProtonClient.class);
factory.setProtonClient(protonClientMock);
// WHEN trying to connect to the server
factory.connect(null, null, null, ctx.failing(t -> {
// THEN the connection attempt fails with a TimeoutException and the given handler is invoked
ctx.verify(() -> assertTrue(t instanceof ConnectTimeoutException));
ctx.completeNow();
}));
timeoutHandlerRef.get().handle(1L);
}
use of io.vertx.proton.ProtonClient in project hono by eclipse.
the class ConnectionFactoryImplTest method testConnectAddsSaslPlainForNonEmptyUsernameAndPassword.
/**
* Verifies that the factory enables SASL_PLAIN if the username and password are non-empty
* strings.
*/
@Test
public void testConnectAddsSaslPlainForNonEmptyUsernameAndPassword() {
// 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 non-empty strings for username and password
factory.connect(options, "user", "pw", null, null, c -> {
});
// THEN the factory uses SASL_PLAIN when establishing the connection
final ArgumentCaptor<ProtonClientOptions> optionsCaptor = ArgumentCaptor.forClass(ProtonClientOptions.class);
verify(client).connect(optionsCaptor.capture(), anyString(), anyInt(), eq("user"), eq("pw"), VertxMockSupport.anyHandler());
assertTrue(optionsCaptor.getValue().getEnabledSaslMechanisms().contains("PLAIN"));
}
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.
*/
@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(PREFIX_KEY_PATH + "trusted-certs.pem");
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());
}
Aggregations