You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ta...@apache.org on 2023/04/07 19:55:42 UTC

[qpid-protonj2] branch main updated: PROTON-2698 Simplify test client connect with SASL plain and anonymous

This is an automated email from the ASF dual-hosted git repository.

tabish pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-protonj2.git


The following commit(s) were added to refs/heads/main by this push:
     new 28b2c100 PROTON-2698 Simplify test client connect with SASL plain and anonymous
28b2c100 is described below

commit 28b2c100e1bb796d6b62d51391dd5568b39c6c73
Author: Timothy Bish <ta...@gmail.com>
AuthorDate: Fri Apr 7 15:50:11 2023 -0400

    PROTON-2698 Simplify test client connect with SASL plain and anonymous
    
    Provide some simple APIs in the script writer for scripting a normal
    client connect with SASL plain or anonymous.
---
 .../qpid/protonj2/test/driver/ScriptWriter.java    | 74 ++++++++++++++++++
 .../protonj2/test/driver/ProtonTestClientTest.java | 88 ++++++++++++++++++++++
 2 files changed, 162 insertions(+)

diff --git a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/ScriptWriter.java b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/ScriptWriter.java
index 85281ec1..ea52b321 100644
--- a/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/ScriptWriter.java
+++ b/protonj2-test-driver/src/main/java/org/apache/qpid/protonj2/test/driver/ScriptWriter.java
@@ -550,6 +550,80 @@ public abstract class ScriptWriter {
         remoteSaslOutcome().withCode(SaslCode.AUTH).queue();
     }
 
+    /**
+     * Used to queue the sequence of frames that would occur during a typical client
+     * connection to a remote peer with SASL anonymous. This should be called before a
+     * client connect attempt as the queued headers won't fire if queued after the
+     * connection has already been established.
+     */
+    public void queueClientSaslAnonymousConnect() {
+        remoteSASLHeader().queue();
+        expectSASLHeader();
+        expectSaslMechanisms().withSaslServerMechanism("ANONYMOUS");
+        remoteSaslInit().withMechanism("ANONYMOUS").queue();
+        expectSaslOutcome().withCode(SaslCode.OK);
+        remoteAMQPHeader().queue();
+    }
+
+    /**
+     * Used to trigger the sequence of frames that would occur during a typical client
+     * connection to a remote peer with SASL anonymous. This should be called after a
+     * client connects to the remote as the fired frames would fail until there is a
+     * connection in place.
+     */
+    public void triggerClientSaslAnonymousConnect() {
+        expectSASLHeader();
+        expectSaslMechanisms().withSaslServerMechanism("ANONYMOUS");
+        remoteSaslInit().withMechanism("ANONYMOUS").queue();
+        expectSaslOutcome().withCode(SaslCode.OK);
+        remoteAMQPHeader().queue();
+
+        // This trigger the exchange of frames.
+        remoteSASLHeader().now();
+    }
+
+    /**
+     * Used to queue the sequence of frames that would occur during a typical client
+     * connection to a remote peer with SASL plain. This should be called before a
+     * client connect attempt as the queued headers won't fire if queued after the
+     * connection has already been established.
+     *
+     * @param username
+     *      The user name that is expected in the SASL Plain initial response.
+     * @param password
+     *      The password that is expected in the SASL Plain initial response.
+     */
+    public void queueClientSaslPlainConnect(String username, String password) {
+        remoteSASLHeader().queue();
+        expectSASLHeader();
+        expectSaslMechanisms().withSaslServerMechanism("PLAIN");
+        remoteSaslInit().withMechanism("PLAIN").withInitialResponse(saslPlainInitialResponse(username, password)).queue();
+        expectSaslOutcome().withCode(SaslCode.OK);
+        remoteAMQPHeader().queue();
+    }
+
+    /**
+     * Used to trigger the sequence of frames that would occur during a typical client
+     * connection to a remote peer with SASL plain. This should be called after a
+     * client connects to the remote as the fired frames would fail until there is a
+     * connection in place.
+     *
+     * @param username
+     *      The user name that is expected in the SASL Plain initial response.
+     * @param password
+     *      The password that is expected in the SASL Plain initial response.
+     */
+    public void triggerClientSaslPlainConnect(String username, String password) {
+        expectSASLHeader();
+        expectSaslMechanisms().withSaslServerMechanism("PLAIN");
+        remoteSaslInit().withMechanism("PLAIN").withInitialResponse(saslPlainInitialResponse(username, password)).queue();
+        expectSaslOutcome().withCode(SaslCode.OK);
+        remoteAMQPHeader().queue();
+
+        // This trigger the exchange of frames.
+        remoteSASLHeader().now();
+    }
+
     //----- Utility methods for tests writing raw scripted SASL tests
 
     public byte[] saslPlainInitialResponse(String username, String password) {
diff --git a/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/ProtonTestClientTest.java b/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/ProtonTestClientTest.java
index d15e9396..2cc9caa2 100644
--- a/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/ProtonTestClientTest.java
+++ b/protonj2-test-driver/src/test/java/org/apache/qpid/protonj2/test/driver/ProtonTestClientTest.java
@@ -224,4 +224,92 @@ class ProtonTestClientTest extends TestPeerTestsBase {
             peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
         }
     }
+
+    @Test
+    public void testScriptedClientSASLAnonymousBeforeConnect() throws Exception {
+        try (ProtonTestServer peer = new ProtonTestServer()) {
+            peer.expectSASLAnonymousConnect("PLAIN", "ANONYMOUS");
+            peer.start();
+
+            URI remoteURI = peer.getServerURI();
+
+            ProtonTestClient client = new ProtonTestClient();
+
+            client.queueClientSaslAnonymousConnect();
+            client.connect(remoteURI.getHost(), remoteURI.getPort());
+
+            client.waitForScriptToComplete(5, TimeUnit.SECONDS);
+            client.close();
+
+            LOG.info("Test started, peer listening on: {}", remoteURI);
+
+            peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+        }
+    }
+
+    @Test
+    public void testScriptedClientSASLAnonymousAfterConnect() throws Exception {
+        try (ProtonTestServer peer = new ProtonTestServer()) {
+            peer.expectSASLAnonymousConnect("PLAIN", "ANONYMOUS");
+            peer.start();
+
+            URI remoteURI = peer.getServerURI();
+
+            ProtonTestClient client = new ProtonTestClient();
+
+            client.connect(remoteURI.getHost(), remoteURI.getPort());
+            client.triggerClientSaslAnonymousConnect();
+
+            client.waitForScriptToComplete(5, TimeUnit.SECONDS);
+            client.close();
+
+            LOG.info("Test started, peer listening on: {}", remoteURI);
+
+            peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+        }
+    }
+
+    @Test
+    public void testScriptedClientSASLPlainBeforeConnect() throws Exception {
+        try (ProtonTestServer peer = new ProtonTestServer()) {
+            peer.expectSASLPlainConnect("test", "test");
+            peer.start();
+
+            URI remoteURI = peer.getServerURI();
+
+            ProtonTestClient client = new ProtonTestClient();
+
+            client.queueClientSaslPlainConnect("test", "test");
+            client.connect(remoteURI.getHost(), remoteURI.getPort());
+
+            client.waitForScriptToComplete(5, TimeUnit.SECONDS);
+            client.close();
+
+            LOG.info("Test started, peer listening on: {}", remoteURI);
+
+            peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+        }
+    }
+
+    @Test
+    public void testScriptedClientSASLPlainAfterConnect() throws Exception {
+        try (ProtonTestServer peer = new ProtonTestServer()) {
+            peer.expectSASLPlainConnect("test", "test");
+            peer.start();
+
+            URI remoteURI = peer.getServerURI();
+
+            ProtonTestClient client = new ProtonTestClient();
+
+            client.connect(remoteURI.getHost(), remoteURI.getPort());
+            client.triggerClientSaslPlainConnect("test", "test");
+
+            client.waitForScriptToComplete(5, TimeUnit.SECONDS);
+            client.close();
+
+            LOG.info("Test started, peer listening on: {}", remoteURI);
+
+            peer.waitForScriptToComplete(5, TimeUnit.SECONDS);
+        }
+    }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org