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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations