Search in sources :

Example 91 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class SenderTest method testSenderOpenRejectedByRemote.

@Test
public void testSenderOpenRejectedByRemote() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().respond().withNullTarget();
        peer.remoteDetach().withErrorCondition(AmqpError.UNAUTHORIZED_ACCESS.toString(), "Cannot read from this address").queue();
        peer.expectDetach();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Sender test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        connection.openFuture().get(10, TimeUnit.SECONDS);
        Session session = connection.openSession();
        session.openFuture().get(10, TimeUnit.SECONDS);
        Sender sender = session.openSender("test-queue");
        try {
            sender.openFuture().get(10, TimeUnit.SECONDS);
            fail("Open of sender should fail due to remote indicating pending close.");
        } catch (ExecutionException exe) {
            assertNotNull(exe.getCause());
            assertTrue(exe.getCause() instanceof ClientLinkRemotelyClosedException);
            ClientLinkRemotelyClosedException linkClosed = (ClientLinkRemotelyClosedException) exe.getCause();
            assertNotNull(linkClosed.getErrorCondition());
            assertEquals(AmqpError.UNAUTHORIZED_ACCESS.toString(), linkClosed.getErrorCondition().condition());
        }
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        // Should not result in any close being sent now, already closed.
        sender.closeAsync().get(10, TimeUnit.SECONDS);
        peer.expectClose().respond();
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(1, TimeUnit.SECONDS);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) ClientLinkRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientLinkRemotelyClosedException) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.apache.qpid.protonj2.client.Session) Test(org.junit.jupiter.api.Test)

Example 92 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class SenderTest method testConcurrentSendBlocksBehindSendWaitingForCredit.

@Test
void testConcurrentSendBlocksBehindSendWaitingForCredit() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        Sender sender = connection.openSender("test-queue").openFuture().get();
        final byte[] payload = new byte[1024];
        Arrays.fill(payload, (byte) 1);
        final CountDownLatch send1Started = new CountDownLatch(1);
        final CountDownLatch send2Completed = new CountDownLatch(1);
        final AtomicBoolean sendFailed = new AtomicBoolean();
        ForkJoinPool.commonPool().execute(() -> {
            try {
                LOG.info("Test send 1 is preparing to fire:");
                ForkJoinPool.commonPool().execute(() -> send1Started.countDown());
                sender.send(Message.create(payload));
            } catch (Exception e) {
                LOG.info("Test send 1 failed with error: ", e);
                sendFailed.set(true);
            }
        });
        ForkJoinPool.commonPool().execute(() -> {
            try {
                assertTrue(send1Started.await(10, TimeUnit.SECONDS));
                LOG.info("Test send 2 is preparing to fire:");
                Tracker tracker = sender.send(Message.create(payload));
                tracker.awaitSettlement(10, TimeUnit.SECONDS);
                send2Completed.countDown();
            } catch (Exception e) {
                LOG.info("Test send 2 failed with error: ", e);
                sendFailed.set(true);
            }
        });
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.remoteFlow().withIncomingWindow(1).withDeliveryCount(0).withNextIncomingId(1).withLinkCredit(1).now();
        peer.expectTransfer().withNonNullPayload().withMore(false).respond().withSettled(true).withState().accepted();
        peer.remoteFlow().withIncomingWindow(1).withDeliveryCount(1).withNextIncomingId(2).withLinkCredit(1).queue();
        peer.expectTransfer().withNonNullPayload().withMore(false).respond().withSettled(true).withState().accepted();
        assertTrue(send2Completed.await(10, TimeUnit.SECONDS));
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectDetach().respond();
        peer.expectClose().respond();
        assertFalse(sendFailed.get());
        sender.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tracker(org.apache.qpid.protonj2.client.Tracker) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) Client(org.apache.qpid.protonj2.client.Client) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientDeliveryStateException(org.apache.qpid.protonj2.client.exceptions.ClientDeliveryStateException) ClientResourceRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientResourceRemotelyClosedException) ClientLinkRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientLinkRemotelyClosedException) ClientOperationTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientOperationTimedOutException) ClientUnsupportedOperationException(org.apache.qpid.protonj2.client.exceptions.ClientUnsupportedOperationException) ClientException(org.apache.qpid.protonj2.client.exceptions.ClientException) ClientConnectionRemotelyClosedException(org.apache.qpid.protonj2.client.exceptions.ClientConnectionRemotelyClosedException) ClientSendTimedOutException(org.apache.qpid.protonj2.client.exceptions.ClientSendTimedOutException) ClientLinkRedirectedException(org.apache.qpid.protonj2.client.exceptions.ClientLinkRedirectedException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Example 93 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class SenderTest method testWaitForAcceptanceFailsIfRemoteSendsRejected.

@Test
void testWaitForAcceptanceFailsIfRemoteSendsRejected() throws Exception {
    try (ProtonTestServer peer = new ProtonTestServer()) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectBegin().respond();
        peer.expectAttach().ofSender().respond();
        peer.remoteFlow().withLinkCredit(2).queue();
        peer.expectTransfer().withNonNullPayload().withMore(false).respond().withSettled(true).withState().rejected();
        peer.start();
        URI remoteURI = peer.getServerURI();
        LOG.info("Test started, peer listening on: {}", remoteURI);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort());
        Sender sender = connection.openSender("test-queue").openFuture().get();
        Tracker tracker = sender.send(Message.create("Hello World"));
        try {
            tracker.awaitAccepted(10, TimeUnit.SECONDS);
            fail("Should not succeed since remote sent something other than Accepted");
        } catch (ClientDeliveryStateException dlvEx) {
        // Expected
        }
        assertTrue(tracker.remoteSettled());
        assertFalse(tracker.remoteState().isAccepted());
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
        peer.expectDetach().respond();
        peer.expectClose().respond();
        sender.closeAsync().get();
        connection.closeAsync().get();
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : Sender(org.apache.qpid.protonj2.client.Sender) Tracker(org.apache.qpid.protonj2.client.Tracker) ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) Connection(org.apache.qpid.protonj2.client.Connection) ClientDeliveryStateException(org.apache.qpid.protonj2.client.exceptions.ClientDeliveryStateException) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) Test(org.junit.jupiter.api.Test)

Example 94 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class SslConnectionTest method doConnectionWithAliasTestImpl.

private void doConnectionWithAliasTestImpl(String alias, String expectedDN, boolean requestOpenSSL) throws Exception, SSLPeerUnverifiedException, IOException {
    ProtonTestServerOptions serverOptions = serverOptions();
    serverOptions.setSecure(true);
    serverOptions.setKeyStoreLocation(BROKER_JKS_KEYSTORE);
    serverOptions.setTrustStoreLocation(BROKER_JKS_TRUSTSTORE);
    serverOptions.setKeyStorePassword(PASSWORD);
    serverOptions.setTrustStorePassword(PASSWORD);
    serverOptions.setVerifyHost(false);
    serverOptions.setNeedClientAuth(true);
    try (ProtonTestServer peer = new ProtonTestServer(serverOptions)) {
        peer.expectSASLAnonymousConnect();
        peer.expectOpen().respond();
        peer.expectClose().respond();
        peer.start();
        URI remoteURI = peer.getServerURI();
        ConnectionOptions clientOptions = connectionOptions();
        clientOptions.sslOptions().keyStoreLocation(CLIENT_MULTI_KEYSTORE).keyStorePassword(PASSWORD).trustStoreLocation(CLIENT_JKS_TRUSTSTORE).trustStorePassword(PASSWORD).keyAlias(alias).allowNativeSSL(requestOpenSSL);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), clientOptions);
        connection.openFuture().get(10, TimeUnit.SECONDS);
        assertTrue(peer.hasSecureConnection());
        assertTrue(peer.isConnectionVerified());
        SSLSession session = peer.getConnectionSSLEngine().getSession();
        Certificate[] peerCertificates = session.getPeerCertificates();
        assertNotNull(peerCertificates);
        Certificate cert = peerCertificates[0];
        assertTrue(cert instanceof X509Certificate);
        String dn = ((X509Certificate) cert).getSubjectX500Principal().getName();
        assertEquals(expectedDN, dn, "Unexpected certificate DN");
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ProtonTestServerOptions(org.apache.qpid.protonj2.test.driver.ProtonTestServerOptions) Connection(org.apache.qpid.protonj2.client.Connection) SSLSession(javax.net.ssl.SSLSession) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Client(org.apache.qpid.protonj2.client.Client) URI(java.net.URI) X509Certificate(java.security.cert.X509Certificate) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Example 95 with Connection

use of com.swiftmq.amqp.v100.client.Connection in project qpid-protonj2 by apache.

the class SslConnectionTest method doConnectionWithSslContextOverride.

private void doConnectionWithSslContextOverride(String clientKeyStorePath, String expectedDN) throws Exception {
    ProtonTestServerOptions serverOptions = serverOptions();
    serverOptions.setSecure(true);
    serverOptions.setKeyStoreLocation(BROKER_JKS_KEYSTORE);
    serverOptions.setTrustStoreLocation(BROKER_JKS_TRUSTSTORE);
    serverOptions.setKeyStorePassword(PASSWORD);
    serverOptions.setTrustStorePassword(PASSWORD);
    serverOptions.setNeedClientAuth(true);
    serverOptions.setVerifyHost(false);
    SslOptions clientSslOptions = new SslOptions();
    clientSslOptions.sslEnabled(true).keyStoreLocation(clientKeyStorePath).keyStorePassword(PASSWORD).trustStoreLocation(CLIENT_JKS_TRUSTSTORE).trustStorePassword(PASSWORD);
    try (ProtonTestServer peer = new ProtonTestServer(serverOptions)) {
        peer.expectSASLPlainConnect("guest", "guest");
        peer.expectOpen().respond();
        peer.expectClose().respond();
        peer.start();
        URI remoteURI = peer.getServerURI();
        SSLContext sslContext = SslSupport.createJdkSslContext(clientSslOptions);
        ConnectionOptions clientOptions = connectionOptions();
        clientOptions.user("guest").password("guest").sslOptions().sslContextOverride(sslContext);
        Client container = Client.create();
        Connection connection = container.connect(remoteURI.getHost(), remoteURI.getPort(), clientOptions);
        connection.openFuture().get(10, TimeUnit.SECONDS);
        assertTrue(peer.hasSecureConnection());
        assertTrue(peer.isConnectionVerified());
        SSLSession session = peer.getConnectionSSLEngine().getSession();
        Certificate[] peerCertificates = session.getPeerCertificates();
        assertNotNull(peerCertificates);
        Certificate cert = peerCertificates[0];
        assertTrue(cert instanceof X509Certificate);
        String dn = ((X509Certificate) cert).getSubjectX500Principal().getName();
        assertEquals(expectedDN, dn, "Unexpected certificate DN");
        connection.closeAsync().get(10, TimeUnit.SECONDS);
        peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
    }
}
Also used : ProtonTestServer(org.apache.qpid.protonj2.test.driver.ProtonTestServer) ProtonTestServerOptions(org.apache.qpid.protonj2.test.driver.ProtonTestServerOptions) Connection(org.apache.qpid.protonj2.client.Connection) SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) URI(java.net.URI) X509Certificate(java.security.cert.X509Certificate) SslOptions(org.apache.qpid.protonj2.client.SslOptions) ConnectionOptions(org.apache.qpid.protonj2.client.ConnectionOptions) Client(org.apache.qpid.protonj2.client.Client) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate)

Aggregations

Connection (org.apache.qpid.protonj2.client.Connection)368 Client (org.apache.qpid.protonj2.client.Client)367 URI (java.net.URI)354 ProtonTestServer (org.apache.qpid.protonj2.test.driver.ProtonTestServer)352 Test (org.junit.jupiter.api.Test)250 Session (org.apache.qpid.protonj2.client.Session)166 ConnectionOptions (org.apache.qpid.protonj2.client.ConnectionOptions)112 Sender (org.apache.qpid.protonj2.client.Sender)89 Receiver (org.apache.qpid.protonj2.client.Receiver)79 ExecutionException (java.util.concurrent.ExecutionException)72 StreamReceiver (org.apache.qpid.protonj2.client.StreamReceiver)65 ClientException (org.apache.qpid.protonj2.client.exceptions.ClientException)60 StreamSender (org.apache.qpid.protonj2.client.StreamSender)49 StreamDelivery (org.apache.qpid.protonj2.client.StreamDelivery)46 TransferPayloadCompositeMatcher (org.apache.qpid.protonj2.test.driver.matchers.transport.TransferPayloadCompositeMatcher)44 Tracker (org.apache.qpid.protonj2.client.Tracker)41 StreamSenderMessage (org.apache.qpid.protonj2.client.StreamSenderMessage)38 ReceiverOptions (org.apache.qpid.protonj2.client.ReceiverOptions)34 SenderOptions (org.apache.qpid.protonj2.client.SenderOptions)33 OutputStream (java.io.OutputStream)31