Search in sources :

Example 21 with ProtonClient

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");
}
Also used : ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.jupiter.api.Test)

Example 22 with ProtonClient

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");
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) Handler(io.vertx.core.Handler) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonClient(io.vertx.proton.ProtonClient) AsyncResult(io.vertx.core.AsyncResult) Test(org.junit.jupiter.api.Test)

Example 23 with ProtonClient

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();
    });
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue) AbstractVerticle(io.vertx.core.AbstractVerticle) ProtonClient(io.vertx.proton.ProtonClient) Runner(io.vertx.example.util.Runner) ProtonConnection(io.vertx.proton.ProtonConnection) ProtonClient(io.vertx.proton.ProtonClient) AmqpValue(org.apache.qpid.proton.amqp.messaging.AmqpValue)

Example 24 with ProtonClient

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());
}
Also used : Handler(io.vertx.core.Handler) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test)

Example 25 with ProtonClient

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"));
}
Also used : Handler(io.vertx.core.Handler) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) ProtonClient(io.vertx.proton.ProtonClient) Test(org.junit.Test)

Aggregations

ProtonClient (io.vertx.proton.ProtonClient)55 ProtonConnection (io.vertx.proton.ProtonConnection)42 Handler (io.vertx.core.Handler)27 Message (org.apache.qpid.proton.message.Message)27 AmqpValue (org.apache.qpid.proton.amqp.messaging.AmqpValue)24 AsyncResult (io.vertx.core.AsyncResult)23 Test (org.junit.Test)22 Arrays (java.util.Arrays)21 ProtonHelper.message (io.vertx.proton.ProtonHelper.message)20 ProtonStreams (io.vertx.proton.streams.ProtonStreams)20 Section (org.apache.qpid.proton.amqp.messaging.Section)20 Async (io.vertx.ext.unit.Async)19 ProtonClientOptions (io.vertx.proton.ProtonClientOptions)19 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 Logger (io.vertx.core.impl.logging.Logger)18 LoggerFactory (io.vertx.core.impl.logging.LoggerFactory)18 TestContext (io.vertx.ext.unit.TestContext)18 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)18 FutureHandler (io.vertx.proton.FutureHandler)18 MockServerTestBase (io.vertx.proton.MockServerTestBase)18