You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/09/21 00:43:12 UTC

[commons-net] branch master updated: Get this test to pass on Java 11 with a TLS hack.

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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-net.git


The following commit(s) were added to refs/heads/master by this push:
     new e4d503a  Get this test to pass on Java 11 with a TLS hack.
e4d503a is described below

commit e4d503acf68e9cff10e3c8b51d3c16b4c8a11692
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Sep 20 20:43:08 2020 -0400

    Get this test to pass on Java 11 with a TLS hack.
---
 .../org/apache/commons/net/ftp/FTPSClientTest.java | 107 ++++++++++++++-------
 1 file changed, 70 insertions(+), 37 deletions(-)

diff --git a/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java b/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java
index 17337cb..c8ca7eb 100644
--- a/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java
+++ b/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java
@@ -36,15 +36,28 @@ import org.apache.ftpserver.listener.ListenerFactory;
 import org.apache.ftpserver.ssl.SslConfigurationFactory;
 import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
 import org.apache.ftpserver.usermanager.impl.BaseUser;
+import org.junit.AfterClass;
 import org.junit.Assert;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
 /**
  * Tests {@link FTPSClient}.
+ * <p>
+ * To get our test cert to work on Java 11, this test must be run with:
+ * </p>
+ * 
+ * <pre>
+ * -Djdk.tls.client.protocols="TLSv1.1"
+ * </pre>
+ * <p>
+ * This test does the above programmatically.
+ * </p>
  */
 public class FTPSClientTest {
 
+    private static final String JDK_TLS_CLIENT_PROTOCOLS = "jdk.tls.client.protocols";
+
     private static int SocketPort;
 
     private static String ConnectionUri;
@@ -55,21 +68,39 @@ public class FTPSClientTest {
 
     private static final String SERVER_JKS_RES = "org/apache/commons/net/ftpsserver/ftpserver.jks";
 
+    private static final boolean implicit = false;
+
+    private static String TlsProtocols;
+
+    @AfterClass
+    public static void afterClass() {
+        if (TlsProtocols == null) {
+            System.getProperties().remove(JDK_TLS_CLIENT_PROTOCOLS);
+        } else {
+            System.setProperty(JDK_TLS_CLIENT_PROTOCOLS, TlsProtocols);
+        }
+    }
+
     /**
      * Returns the test directory as a String.
+     * 
      * @return the test directory as a String
      */
     private static String getTestHomeDirectory() {
         return System.getProperty("test.basedir", "target/test-classes/org/apache/commons/net/test-data");
     }
 
-    private static final boolean implicit = false;
-
     @BeforeClass
     public static void setUp() throws Exception {
         setUpClass(implicit);
     }
 
+    @BeforeClass
+    public static void setUpClass() {
+        TlsProtocols = System.getProperty(JDK_TLS_CLIENT_PROTOCOLS);
+        System.setProperty(JDK_TLS_CLIENT_PROTOCOLS, "TLSv1");
+    }
+
     /**
      * Creates and starts an embedded Apache MINA FTP Server.
      *
@@ -90,7 +121,8 @@ public class FTPSClientTest {
         propertiesUserManagerFactory.setUrl(userPropsResource);
         final UserManager userManager = propertiesUserManagerFactory.createUserManager();
         final BaseUser user = (BaseUser) userManager.getUserByName("test");
-        // Pickup the home dir value at runtime even though we have it set in the user prop file
+        // Pickup the home dir value at runtime even though we have it set in the user
+        // prop file
         // The user prop file requires the "homedirectory" to be set
         user.setHomeDirectory(getTestHomeDirectory());
         serverFactory.setUserManager(userManager);
@@ -101,14 +133,15 @@ public class FTPSClientTest {
         // define SSL configuration
         final URL serverJksResource = ClassLoader.getSystemClassLoader().getResource(SERVER_JKS_RES);
         Assert.assertNotNull(SERVER_JKS_RES, serverJksResource);
-        final SslConfigurationFactory ssl = new SslConfigurationFactory();
+        final SslConfigurationFactory sllConfigFactory = new SslConfigurationFactory();
         final File keyStoreFile = FileUtils.toFile(serverJksResource);
         Assert.assertTrue(keyStoreFile.toString(), keyStoreFile.exists());
-        ssl.setKeystoreFile(keyStoreFile);
-        ssl.setKeystorePassword("password");
+        sllConfigFactory.setKeystoreFile(keyStoreFile);
+        sllConfigFactory.setKeystorePassword("password");
+        sllConfigFactory.setSslProtocol("TLSv1.1");
 
         // set the SSL configuration for the listener
-        factory.setSslConfiguration(ssl.createSslConfiguration());
+        factory.setSslConfiguration(sllConfigFactory.createSslConfiguration());
         factory.setImplicitSsl(implicit);
 
         // replace the default listener
@@ -119,6 +152,12 @@ public class FTPSClientTest {
         Server.start();
         SocketPort = ((org.apache.ftpserver.impl.DefaultFtpServer) Server).getListener("default").getPort();
         ConnectionUri = "ftps://test:test@localhost:" + SocketPort;
+        System.out.printf("jdk.tls.disabledAlgorithms = %s%n", System.getProperty("jdk.tls.disabledAlgorithms"));
+    }
+
+    private void assertClientCode(final FTPSClient client) {
+        final int replyCode = client.getReplyCode();
+        assertTrue(FTPReply.isPositiveCompletion(replyCode));
     }
 
     private FTPSClient loginClient() throws SocketException, IOException {
@@ -141,14 +180,16 @@ public class FTPSClientTest {
         return client;
     }
 
-    private void assertClientCode(final FTPSClient client) {
-        final int replyCode = client.getReplyCode();
-        assertTrue(FTPReply.isPositiveCompletion(replyCode));
-    }
-
-    @Test
-    public void testOpenClose() throws SocketException, IOException {
-        loginClient().disconnect();
+    private void retrieveFile(final String pathname) throws SocketException, IOException {
+        final FTPSClient client = loginClient();
+        try {
+            // Do it twice.
+            // Just testing that we are not getting an SSL error (the file MUST be present).
+            assertTrue(pathname, client.retrieveFile(pathname, NullOutputStream.NULL_OUTPUT_STREAM));
+            assertTrue(pathname, client.retrieveFile(pathname, NullOutputStream.NULL_OUTPUT_STREAM));
+        } finally {
+            client.disconnect();
+        }
     }
 
     private void testListFiles(final String pathname) throws SocketException, IOException {
@@ -162,41 +203,33 @@ public class FTPSClientTest {
         }
     }
 
-    private void retrieveFile(final String pathname) throws SocketException, IOException {
-        final FTPSClient client = loginClient();
-        try {
-            // Do it twice.
-            // Just testing that we are not getting an SSL error (the file MUST be present).
-            assertTrue(pathname, client.retrieveFile(pathname, NullOutputStream.NULL_OUTPUT_STREAM));
-            assertTrue(pathname, client.retrieveFile(pathname, NullOutputStream.NULL_OUTPUT_STREAM));
-        } finally {
-            client.disconnect();
-        }
+    @Test
+    public void testListFilesPathNameEmpty() throws SocketException, IOException {
+        testListFiles("");
     }
 
     @Test
-    public void testListFilesPathNameRoot() throws SocketException, IOException {
-        testListFiles("/");
+    public void testListFilesPathNameJunk() throws SocketException, IOException {
+        testListFiles("   Junk   ");
     }
 
     @Test
-    public void testRetrieveFilePathNameRoot() throws SocketException, IOException {
-        retrieveFile("/file.txt");
+    public void testListFilesPathNameNull() throws SocketException, IOException {
+        testListFiles(null);
     }
 
     @Test
-    public void testListFilesPathNameEmpty() throws SocketException, IOException {
-        testListFiles("");
+    public void testListFilesPathNameRoot() throws SocketException, IOException {
+        testListFiles("/");
     }
 
     @Test
-    public void testListFilesPathNameNull() throws SocketException, IOException {
-        testListFiles(null);
+    public void testOpenClose() throws SocketException, IOException {
+        loginClient().disconnect();
     }
-    
+
     @Test
-    public void testListFilesPathNameJunk() throws SocketException, IOException {
-        testListFiles("   Junk   ");
+    public void testRetrieveFilePathNameRoot() throws SocketException, IOException {
+        retrieveFile("/file.txt");
     }
 }
-