You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ratis.apache.org by lj...@apache.org on 2022/03/04 11:01:07 UTC

[ratis-thirdparty] branch master updated: RATIS-1544. Refactor the GrpcSslTest in Thirdparty. (#29)

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

ljain pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ratis-thirdparty.git


The following commit(s) were added to refs/heads/master by this push:
     new 2fa0f8e  RATIS-1544. Refactor the GrpcSslTest in Thirdparty. (#29)
2fa0f8e is described below

commit 2fa0f8e2b0e3315a87334299b6fb197196fa9b25
Author: Tsz-Wo Nicholas Sze <sz...@apache.org>
AuthorDate: Fri Mar 4 19:01:03 2022 +0800

    RATIS-1544. Refactor the GrpcSslTest in Thirdparty. (#29)
---
 .../SslClientConfig.java}                          | 23 ++++----
 .../{GrpcSslConfig.java => common/SslConfig.java}  | 23 ++++----
 .../SslServerConfig.java}                          | 23 +++-----
 .../thirdparty/demo/{ => grpc}/GreeterImpl.java    |  5 +-
 .../thirdparty/demo/{ => grpc}/GrpcClient.java     | 10 ++--
 .../thirdparty/demo/{ => grpc}/GrpcServer.java     |  5 +-
 .../thirdparty/demo/{ => grpc}/GrpcSslClient.java  | 19 +++---
 .../thirdparty/demo/{ => grpc}/GrpcSslServer.java  | 21 +++----
 test/src/main/proto/hello.proto                    |  2 +-
 .../ratis/thirdparty/demo/common/TestUtils.java    | 67 ++++++++++++++++++++++
 .../thirdparty/demo/{ => grpc}/GrpcSslTest.java    | 41 +++----------
 .../ratis/thirdparty/demo/{ => grpc}/GrpcTest.java | 17 ++----
 12 files changed, 140 insertions(+), 116 deletions(-)

diff --git a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslClientConfig.java b/test/src/main/java/org/apache/ratis/thirdparty/demo/common/SslClientConfig.java
similarity index 71%
rename from test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslClientConfig.java
rename to test/src/main/java/org/apache/ratis/thirdparty/demo/common/SslClientConfig.java
index 95a3096..2ceeb54 100644
--- a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslClientConfig.java
+++ b/test/src/main/java/org/apache/ratis/thirdparty/demo/common/SslClientConfig.java
@@ -15,27 +15,24 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.ratis.thirdparty.demo;
+package org.apache.ratis.thirdparty.demo.common;
 
 import java.io.File;
 
 /**
- * gRPC SSL client configurations.
+ * SSL client configurations.
  */
-public class GrpcSslClientConfig extends GrpcSslConfig{
-  private File trustCertCollection;
+public class SslClientConfig extends SslConfig {
+  private final File trustCertCollection;
 
   // Only needed for mTLS
-  private Boolean mutualAuthn;
-  private File privateKey;
-  private File certChain;
+  private final boolean mutualAuthn;
+  private final File privateKey;
+  private final File certChain;
 
 
-  public GrpcSslClientConfig(File privateKey,
-                             File trustCertCollection,
-                             File certChain,
-                             Boolean mutualAuthn,
-                             Boolean encryption) {
+  public SslClientConfig(File privateKey, File trustCertCollection, File certChain,
+      boolean mutualAuthn, boolean encryption) {
     super(encryption);
     this.privateKey = privateKey;
     this.trustCertCollection = trustCertCollection;
@@ -55,7 +52,7 @@ public class GrpcSslClientConfig extends GrpcSslConfig{
     return certChain;
   }
 
-  public Boolean isMutualAuthn() {
+  public boolean isMutualAuthn() {
     return mutualAuthn && privateKey!= null && certChain!= null;
   }
 
diff --git a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslConfig.java b/test/src/main/java/org/apache/ratis/thirdparty/demo/common/SslConfig.java
similarity index 71%
rename from test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslConfig.java
rename to test/src/main/java/org/apache/ratis/thirdparty/demo/common/SslConfig.java
index 68f9d13..2dddc65 100644
--- a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslConfig.java
+++ b/test/src/main/java/org/apache/ratis/thirdparty/demo/common/SslConfig.java
@@ -15,33 +15,32 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.ratis.thirdparty.demo;
-
-import org.apache.ratis.thirdparty.com.google.common.collect.Lists;
+package org.apache.ratis.thirdparty.demo.common;
 
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 
-public class GrpcSslConfig {
+public class SslConfig {
   // TODO: allow configure cipher suites
-  private final List<String> tlsCipherSuitesWithEncryption = Lists.newArrayList(
+  private final List<String> tlsCipherSuitesWithEncryption = Collections.unmodifiableList(Arrays.asList(
       "TLS_RSA_WITH_AES_128_GCM_SHA256",
       "TLS_RSA_WITH_AES_128_CBC_SHA",
-      "SSL_RSA_WITH_3DES_EDE_CBC_SHA");
+      "SSL_RSA_WITH_3DES_EDE_CBC_SHA"));
 
   // "RSA" in this case refers to the key exchange algorithm,
   // "SHA" refers to the message digest algorithm to provide integrity
   // "NULL" is the encryption algorithm, to disable encryption.
-  // TODO: suppot NULL cipher from tcnative
-  private final List<String> tlsCipherSuitesNoEncryption = Lists
-      .newArrayList("TLS_RSA_WITH_AES_128_GCM_SHA256");
+  // TODO: support NULL cipher from tcnative
+  private final List<String> tlsCipherSuitesNoEncryption = Collections.singletonList("TLS_RSA_WITH_AES_128_GCM_SHA256");
 
-  private Boolean encryption;
+  private final boolean encryption;
 
-  protected GrpcSslConfig(Boolean encryption) {
+  protected SslConfig(boolean encryption) {
     this.encryption = encryption;
   }
 
-  public Boolean encryptionEnabled() {
+  public boolean encryptionEnabled() {
     return encryption;
   }
 
diff --git a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslServerConfig.java b/test/src/main/java/org/apache/ratis/thirdparty/demo/common/SslServerConfig.java
similarity index 70%
rename from test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslServerConfig.java
rename to test/src/main/java/org/apache/ratis/thirdparty/demo/common/SslServerConfig.java
index ea512cb..acea4a1 100644
--- a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslServerConfig.java
+++ b/test/src/main/java/org/apache/ratis/thirdparty/demo/common/SslServerConfig.java
@@ -15,24 +15,19 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.ratis.thirdparty.demo;
+package org.apache.ratis.thirdparty.demo.common;
 
 import java.io.File;
 
-public class GrpcSslServerConfig extends GrpcSslConfig {
-  private File privateKey;
-  private File serverCertChain;
+public class SslServerConfig extends SslConfig {
+  private final File privateKey;
+  private final File serverCertChain;
   // Only needed for mTLS
-  private Boolean mutualAuthn;
-  private File clientCertChain;
+  private final boolean mutualAuthn;
+  private final File clientCertChain;
 
-  private Boolean encryption;
-
-  public GrpcSslServerConfig(File privateKey,
-                             File serverCertChain,
-                             File clientCertChain,
-                             Boolean mutualAuthn,
-                             Boolean encryption) {
+  public SslServerConfig(File privateKey, File serverCertChain, File clientCertChain,
+      boolean mutualAuthn, boolean encryption) {
     super(encryption);
     this.privateKey = privateKey;
     this.serverCertChain = serverCertChain;
@@ -52,7 +47,7 @@ public class GrpcSslServerConfig extends GrpcSslConfig {
     return clientCertChain;
   }
 
-  public Boolean isMutualAuthn() {
+  public boolean isMutualAuthn() {
     return mutualAuthn && clientCertChain!= null;
   }
 
diff --git a/test/src/main/java/org/apache/ratis/thirdparty/demo/GreeterImpl.java b/test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GreeterImpl.java
similarity index 84%
rename from test/src/main/java/org/apache/ratis/thirdparty/demo/GreeterImpl.java
rename to test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GreeterImpl.java
index 2474787..79c2d2b 100644
--- a/test/src/main/java/org/apache/ratis/thirdparty/demo/GreeterImpl.java
+++ b/test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GreeterImpl.java
@@ -15,8 +15,11 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.ratis.thirdparty.demo;
+package org.apache.ratis.thirdparty.demo.grpc;
 
+import org.apache.ratis.thirdparty.demo.proto.GreeterGrpc;
+import org.apache.ratis.thirdparty.demo.proto.HelloReply;
+import org.apache.ratis.thirdparty.demo.proto.HelloRequest;
 import org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver;
 
 class GreeterImpl extends GreeterGrpc.GreeterImplBase {
diff --git a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcClient.java b/test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GrpcClient.java
similarity index 86%
rename from test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcClient.java
rename to test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GrpcClient.java
index e576eab..345421d 100644
--- a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcClient.java
+++ b/test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GrpcClient.java
@@ -15,8 +15,11 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.ratis.thirdparty.demo;
+package org.apache.ratis.thirdparty.demo.grpc;
 
+import org.apache.ratis.thirdparty.demo.proto.GreeterGrpc;
+import org.apache.ratis.thirdparty.demo.proto.HelloReply;
+import org.apache.ratis.thirdparty.demo.proto.HelloRequest;
 import org.apache.ratis.thirdparty.io.grpc.ManagedChannel;
 import org.apache.ratis.thirdparty.io.grpc.ManagedChannelBuilder;
 import org.apache.ratis.thirdparty.io.grpc.StatusRuntimeException;
@@ -28,8 +31,7 @@ import org.slf4j.LoggerFactory;
  * gRPC Demo client with shaded ratis thirdparty jar.
  */
 public class GrpcClient {
-  private static final Logger LOG = LoggerFactory.getLogger(
-      GrpcClient.class.getName());
+  private static final Logger LOG = LoggerFactory.getLogger(GrpcClient.class);
 
   private final ManagedChannel channel;
   private final GreeterGrpc.GreeterBlockingStub blockingStub;
@@ -57,7 +59,7 @@ public class GrpcClient {
       LOG.trace("Greeting: " + response.getMessage());
       return response.getMessage();
     } catch (StatusRuntimeException e) {
-      LOG.warn("RPC failed: {0}", e.getStatus());
+      LOG.warn("RPC failed: " + e.getStatus(), e);
       return "";
     }
   }
diff --git a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcServer.java b/test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GrpcServer.java
similarity index 93%
rename from test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcServer.java
rename to test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GrpcServer.java
index 882832e..1433e1d 100644
--- a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcServer.java
+++ b/test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GrpcServer.java
@@ -15,7 +15,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.ratis.thirdparty.demo;
+package org.apache.ratis.thirdparty.demo.grpc;
 
 import org.apache.ratis.thirdparty.io.grpc.Server;
 import org.apache.ratis.thirdparty.io.grpc.ServerBuilder;
@@ -27,8 +27,7 @@ import org.slf4j.LoggerFactory;
  * gRPC Demo server with shaded ratis thirdparty jar.
  */
 public class GrpcServer {
-  private static final Logger LOG =
-      LoggerFactory.getLogger(GrpcServer.class.getName());
+  private static final Logger LOG = LoggerFactory.getLogger(GrpcServer.class);
 
   private Server server;
   private int port;
diff --git a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslClient.java b/test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GrpcSslClient.java
similarity index 82%
rename from test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslClient.java
rename to test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GrpcSslClient.java
index d93029d..139722c 100644
--- a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslClient.java
+++ b/test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GrpcSslClient.java
@@ -15,14 +15,16 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.ratis.thirdparty.demo;
+package org.apache.ratis.thirdparty.demo.grpc;
 
+import org.apache.ratis.thirdparty.demo.proto.GreeterGrpc;
+import org.apache.ratis.thirdparty.demo.proto.HelloReply;
+import org.apache.ratis.thirdparty.demo.proto.HelloRequest;
+import org.apache.ratis.thirdparty.demo.common.SslClientConfig;
 import org.apache.ratis.thirdparty.io.grpc.ManagedChannel;
-import org.apache.ratis.thirdparty.io.grpc.ManagedChannelBuilder;
 import org.apache.ratis.thirdparty.io.grpc.StatusRuntimeException;
 
 import java.io.IOException;
-import java.net.InetAddress;
 import java.util.concurrent.TimeUnit;
 
 import org.apache.ratis.thirdparty.io.grpc.netty.GrpcSslContexts;
@@ -35,20 +37,17 @@ import org.slf4j.LoggerFactory;
  * gRPC Demo SSL client with shaded ratis thirdparty jar.
  */
 public class GrpcSslClient {
-  private static final Logger LOG = LoggerFactory.getLogger(
-      GrpcSslClient.class.getName());
+  private static final Logger LOG = LoggerFactory.getLogger(GrpcSslClient.class);
 
   private final ManagedChannel channel;
   private final GreeterGrpc.GreeterBlockingStub blockingStub;
 
-  public GrpcSslClient(String host, int port,
-                       GrpcSslClientConfig conf) throws IOException {
+  public GrpcSslClient(String host, int port, SslClientConfig conf) throws IOException {
     channel = initChannel(host, port, conf);
     blockingStub = GreeterGrpc.newBlockingStub(channel);
   }
 
-  private ManagedChannel initChannel(String host, int port,
-      GrpcSslClientConfig conf) throws IOException {
+  private ManagedChannel initChannel(String host, int port, SslClientConfig conf) throws IOException {
     NettyChannelBuilder channelBuilder =
         NettyChannelBuilder.forAddress(host, port);
     // Hacky way to work around hostname verify of the certificate
@@ -82,7 +81,7 @@ public class GrpcSslClient {
       LOG.trace("Greeting: " + response.getMessage());
       return response.getMessage();
     } catch (StatusRuntimeException e) {
-      LOG.warn("RPC failed: {0}", e.getStatus(), e);
+      LOG.warn("RPC failed: " + e.getStatus(), e);
       return "";
     }
   }
diff --git a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslServer.java b/test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GrpcSslServer.java
similarity index 86%
rename from test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslServer.java
rename to test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GrpcSslServer.java
index 8d17d6b..21ae8ea 100644
--- a/test/src/main/java/org/apache/ratis/thirdparty/demo/GrpcSslServer.java
+++ b/test/src/main/java/org/apache/ratis/thirdparty/demo/grpc/GrpcSslServer.java
@@ -15,12 +15,10 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.ratis.thirdparty.demo;
+package org.apache.ratis.thirdparty.demo.grpc;
 
+import org.apache.ratis.thirdparty.demo.common.SslServerConfig;
 import org.apache.ratis.thirdparty.io.grpc.Server;
-import org.apache.ratis.thirdparty.io.grpc.ServerBuilder;
-import java.io.IOException;
-
 import org.apache.ratis.thirdparty.io.grpc.netty.GrpcSslContexts;
 import org.apache.ratis.thirdparty.io.grpc.netty.NettyServerBuilder;
 import org.apache.ratis.thirdparty.io.netty.handler.ssl.ClientAuth;
@@ -28,20 +26,21 @@ import org.apache.ratis.thirdparty.io.netty.handler.ssl.SslContextBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
+
 import static org.apache.ratis.thirdparty.io.netty.handler.ssl.SslProvider.OPENSSL;
 
 /**
  * gRPC Demo SSL server with shaded ratis thirdparty jar.
  */
 public class GrpcSslServer {
-  private static final Logger LOG =
-      LoggerFactory.getLogger(GrpcServer.class.getName());
+  private static final Logger LOG = LoggerFactory.getLogger(GrpcServer.class);
 
   private Server server;
   private int port;
-  private GrpcSslServerConfig conf;
+  private final SslServerConfig conf;
 
-  public GrpcSslServer(int port, GrpcSslServerConfig conf) {
+  public GrpcSslServer(int port, SslServerConfig conf) {
     this.port = port;
     this.conf = conf;
   }
@@ -80,10 +79,4 @@ public class GrpcSslServer {
       server.shutdown();
     }
   }
-
-  void blockUntilShutdown() throws InterruptedException {
-    if (server != null) {
-      server.awaitTermination();
-    }
-  }
 }
diff --git a/test/src/main/proto/hello.proto b/test/src/main/proto/hello.proto
index 1fb1c34..edb29d7 100644
--- a/test/src/main/proto/hello.proto
+++ b/test/src/main/proto/hello.proto
@@ -18,7 +18,7 @@
 syntax = "proto3";
 
 option java_multiple_files = true;
-option java_package = "org.apache.ratis.thirdparty.demo";
+option java_package = "org.apache.ratis.thirdparty.demo.proto";
 option java_outer_classname = "HelloProto";
 
 package org.apache.ratis.thirdparty.demo;
diff --git a/test/src/test/java/org/apache/ratis/thirdparty/demo/common/TestUtils.java b/test/src/test/java/org/apache/ratis/thirdparty/demo/common/TestUtils.java
new file mode 100644
index 0000000..cd41109
--- /dev/null
+++ b/test/src/test/java/org/apache/ratis/thirdparty/demo/common/TestUtils.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ratis.thirdparty.demo.common;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.net.URL;
+import java.util.Optional;
+import java.util.concurrent.ThreadLocalRandom;
+
+public interface TestUtils {
+  Logger LOG = LoggerFactory.getLogger(TestUtils.class);
+
+  ClassLoader CLASS_LOADER = TestUtils.class.getClassLoader();
+
+  static File getResource(String name) {
+    final File file = Optional.ofNullable(CLASS_LOADER.getResource(name))
+        .map(URL::getFile)
+        .map(File::new)
+        .orElse(null);
+    LOG.info("Getting Resource {}: {}", name, file);
+    return file;
+  }
+
+  static SslServerConfig newSslServerConfig(boolean mutualAuthn, boolean encryption) {
+    LOG.info("newSslServerConfig: mutualAuthn? {}, encryption? {}", mutualAuthn, encryption);
+    return new SslServerConfig(
+        getResource("ssl/server.pem"),
+        getResource("ssl/server.crt"),
+        getResource("ssl/client.crt"),
+        mutualAuthn,
+        encryption);
+  }
+
+  static SslClientConfig newSslClientConfig(boolean mutualAuthn, boolean encryption) {
+    LOG.info("newSslClientConfig: mutualAuthn? {}, encryption? {}", mutualAuthn, encryption);
+    return new SslClientConfig(
+        getResource("ssl/client.pem"),
+        getResource("ssl/ca.crt"),
+        getResource("ssl/client.crt"),
+        mutualAuthn,
+        encryption);
+  }
+
+  static int randomPort() {
+    final int port = 50000 + ThreadLocalRandom.current().nextInt(10000);
+    LOG.info("randomPort: {}", port);
+    return port;
+  }
+}
diff --git a/test/src/test/java/org/apache/ratis/thirdparty/demo/GrpcSslTest.java b/test/src/test/java/org/apache/ratis/thirdparty/demo/grpc/GrpcSslTest.java
similarity index 63%
rename from test/src/test/java/org/apache/ratis/thirdparty/demo/GrpcSslTest.java
rename to test/src/test/java/org/apache/ratis/thirdparty/demo/grpc/GrpcSslTest.java
index 1baeb6b..1888219 100644
--- a/test/src/test/java/org/apache/ratis/thirdparty/demo/GrpcSslTest.java
+++ b/test/src/test/java/org/apache/ratis/thirdparty/demo/grpc/GrpcSslTest.java
@@ -15,8 +15,11 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.ratis.thirdparty.demo;
+package org.apache.ratis.thirdparty.demo.grpc;
 
+import org.apache.ratis.thirdparty.demo.common.SslClientConfig;
+import org.apache.ratis.thirdparty.demo.common.SslServerConfig;
+import org.apache.ratis.thirdparty.demo.common.TestUtils;
 import org.junit.Assert;
 import org.junit.Test;
 import org.slf4j.Logger;
@@ -24,29 +27,17 @@ import org.slf4j.LoggerFactory;
 
 import java.io.File;
 import java.io.IOException;
+import java.net.URL;
+import java.util.Optional;
 
 public class GrpcSslTest {
   private final static Logger LOG = LoggerFactory.getLogger(GrpcSslTest.class);
 
-  private int port = 50005;
-
-  private File getResource(String name) {
-    ClassLoader classLoader = getClass().getClassLoader();
-
-    File file = new File(classLoader.getResource(name).getFile());
-    LOG.info("Getting Resource: {}\n", file.getAbsolutePath());
-    return file;
-  }
 
   @Test
   public void testSslClientServer() throws InterruptedException, IOException {
-    GrpcSslServerConfig sslServerConf =
-        new GrpcSslServerConfig(
-            getResource("ssl/server.pem"),
-            getResource("ssl/server.crt"),
-            getResource("ssl/client.crt"),
-            true,
-            false);
+    final int port = TestUtils.randomPort();
+    final SslServerConfig sslServerConf = TestUtils.newSslServerConfig(true, false);
     GrpcSslServer server = new GrpcSslServer(port, sslServerConf);
     try {
       server.start();
@@ -55,21 +46,7 @@ public class GrpcSslTest {
       throw t;
     }
 
-    Thread serverThread = new Thread(() -> {
-      try {
-        server.blockUntilShutdown();
-      } catch (InterruptedException e) {
-        Thread.currentThread().interrupt();
-      }
-    });
-    serverThread.start();
-
-    GrpcSslClientConfig sslClientConfig = new GrpcSslClientConfig(
-        getResource("ssl/client.pem"),
-        getResource("ssl/ca.crt"),
-        getResource("ssl/client.crt"),
-        true,
-        false);
+    final SslClientConfig sslClientConfig = TestUtils.newSslClientConfig(true, false);
 
     GrpcSslClient client = new GrpcSslClient(
         "localhost", port, sslClientConfig);
diff --git a/test/src/test/java/org/apache/ratis/thirdparty/demo/GrpcTest.java b/test/src/test/java/org/apache/ratis/thirdparty/demo/grpc/GrpcTest.java
similarity index 81%
rename from test/src/test/java/org/apache/ratis/thirdparty/demo/GrpcTest.java
rename to test/src/test/java/org/apache/ratis/thirdparty/demo/grpc/GrpcTest.java
index 98c7420..1aabd00 100644
--- a/test/src/test/java/org/apache/ratis/thirdparty/demo/GrpcTest.java
+++ b/test/src/test/java/org/apache/ratis/thirdparty/demo/grpc/GrpcTest.java
@@ -15,8 +15,9 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.ratis.thirdparty.demo;
+package org.apache.ratis.thirdparty.demo.grpc;
 
+import org.apache.ratis.thirdparty.demo.common.TestUtils;
 import org.junit.Assert;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -35,19 +36,11 @@ public class GrpcTest {
   private final static Logger LOG = LoggerFactory.getLogger(GrpcTest.class);
   @Test
   public void testClientServer() throws IOException, InterruptedException {
-    GrpcServer server = new GrpcServer(50001);
+    final int port = TestUtils.randomPort();
+    final GrpcServer server = new GrpcServer(port);
     server.start();
 
-    Thread serverThread = new Thread(() -> {
-      try {
-        server.blockUntilShutdown();
-      } catch (InterruptedException e) {
-        Thread.currentThread().interrupt();
-      }
-    });
-    serverThread.start();
-
-    GrpcClient client = new GrpcClient("localhost", 50001);
+    final GrpcClient client = new GrpcClient("localhost", port);
     try {
       /* Access a service running on the local machine on port 50051 */
       String user = "testuser";