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 a986182d Refactor for reuse a986182d is described below commit a986182d8add6faa12548c1beba4aa1d16a90a89 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Sat Nov 26 13:54:10 2022 -0500 Refactor for reuse --- .../{FTPSClientTest.java => AbstractFtpsTest.java} | 199 +++------------------ .../org/apache/commons/net/ftp/FTPSClientTest.java | 174 +----------------- 2 files changed, 29 insertions(+), 344 deletions(-) diff --git a/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java b/src/test/java/org/apache/commons/net/ftp/AbstractFtpsTest.java similarity index 54% copy from src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java copy to src/test/java/org/apache/commons/net/ftp/AbstractFtpsTest.java index 67302eb5..43443076 100644 --- a/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java +++ b/src/test/java/org/apache/commons/net/ftp/AbstractFtpsTest.java @@ -18,7 +18,6 @@ package org.apache.commons.net.ftp; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.File; @@ -26,8 +25,6 @@ import java.io.IOException; import java.net.SocketException; import java.net.URL; import java.time.Duration; -import java.time.Instant; -import java.util.Calendar; import org.apache.commons.io.FileUtils; import org.apache.commons.io.output.NullOutputStream; @@ -42,16 +39,11 @@ import org.apache.ftpserver.ssl.SslConfigurationFactory; import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; import org.apache.ftpserver.usermanager.impl.BaseUser; import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; -import org.junit.runners.Parameterized.Parameters; /** * Tests {@link FTPSClient}. * <p> - * To get our test cert to work on Java 11, this test must be run with: + * To get our test certificate to work on Java 11, this test must be run with: * </p> * * <pre> @@ -61,46 +53,37 @@ import org.junit.runners.Parameterized.Parameters; * This test does the above programmatically. * </p> */ -@RunWith(Parameterized.class) -public class FTPSClientTest { +public abstract class AbstractFtpsTest { private static int SocketPort; - private static FtpServer EmbeddedFtpServer; - - private static final String USER_PROPS_RES = "org/apache/commons/net/ftpsserver/users.properties"; - - private static final String SERVER_JKS_RES = "org/apache/commons/net/ftpsserver/ftpserver.jks"; - - private static final boolean IMPLICIT = false; - - private static final long TEST_TIMEOUT = 10000; // individual test timeout - + protected static final boolean IMPLICIT = false; + protected static final long TEST_TIMEOUT = 10000; // individual test timeout private static final boolean TRACE_CALLS = Boolean.parseBoolean(System.getenv("TRACE_CALLS")); private static final boolean ADD_LISTENER = Boolean.parseBoolean(System.getenv("ADD_LISTENER")); private static final long startTime = System.nanoTime(); /** * Returns the test directory as a String. + * @param defaultHome A default value. * * @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"); - } - - @BeforeClass - public static void setUpClass() throws Exception { - setUpClass(IMPLICIT); + protected static String getTestHomeDirectory(final String defaultHome) { + return System.getProperty("test.basedir", defaultHome); } /** * Creates and starts an embedded Apache MINA FTP Server. * - * @param implicit FTPS connection mode - * @throws FtpException + * @param implicit FTPS connection mode. + * @param userPropertiesResource resource path to user properties file. + * @param serverJksResourceResource resource path to server JKS file. + * @param defaultHome default home folder + * @throws FtpException Thrown when a the FTP classes cannot fulfill a request. */ - private synchronized static void setUpClass(final boolean implicit) throws FtpException { + protected synchronized static void setupServer(final boolean implicit, final String userPropertiesResource, final String serverJksResourceResource, final String defaultHome) + throws FtpException { if (EmbeddedFtpServer != null) { return; } @@ -108,21 +91,21 @@ public class FTPSClientTest { SocketPort = 0; final FtpServerFactory serverFactory = new FtpServerFactory(); final PropertiesUserManagerFactory propertiesUserManagerFactory = new PropertiesUserManagerFactory(); - final URL userPropsResource = ClassLoader.getSystemClassLoader().getResource(USER_PROPS_RES); - Assert.assertNotNull(USER_PROPS_RES, userPropsResource); + final URL userPropsResource = ClassLoader.getSystemClassLoader().getResource(userPropertiesResource); + Assert.assertNotNull(userPropertiesResource, userPropsResource); 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 userprop file // The user prop file requires the "homedirectory" to be set - user.setHomeDirectory(getTestHomeDirectory()); + user.setHomeDirectory(getTestHomeDirectory(defaultHome)); serverFactory.setUserManager(userManager); final ListenerFactory factory = new ListenerFactory(); factory.setPort(SocketPort); // define SSL configuration - final URL serverJksResource = ClassLoader.getSystemClassLoader().getResource(SERVER_JKS_RES); - Assert.assertNotNull(SERVER_JKS_RES, serverJksResource); + final URL serverJksResource = ClassLoader.getSystemClassLoader().getResource(serverJksResourceResource); + Assert.assertNotNull(serverJksResourceResource, serverJksResource); System.out.println("Loading " + serverJksResource); final SslConfigurationFactory sllConfigFactory = new SslConfigurationFactory(); final File keyStoreFile = FileUtils.toFile(serverJksResource); @@ -147,12 +130,7 @@ public class FTPSClientTest { trace("Server started"); } - @Parameters(name = "endpointCheckingEnabled={0}") - public static Boolean[] testConstructurData() { - return new Boolean[] { Boolean.FALSE, Boolean.TRUE }; - } - - private static void trace(final String msg) { + protected static void trace(final String msg) { if (TRACE_CALLS) { System.err.println(msg + " " + (System.nanoTime() - startTime)); } @@ -160,16 +138,16 @@ public class FTPSClientTest { private final boolean endpointCheckingEnabled; - public FTPSClientTest(final boolean endpointCheckingEnabled) { + public AbstractFtpsTest(final boolean endpointCheckingEnabled, final String userPropertiesResource, final String serverJksResource) { this.endpointCheckingEnabled = endpointCheckingEnabled; } - private void assertClientCode(final FTPSClient client) { + protected void assertClientCode(final FTPSClient client) { final int replyCode = client.getReplyCode(); assertTrue(FTPReply.isPositiveCompletion(replyCode)); } - private FTPSClient loginClient() throws SocketException, IOException { + protected FTPSClient loginClient() throws SocketException, IOException { trace(">>loginClient"); final FTPSClient client = new FTPSClient(IMPLICIT); if (ADD_LISTENER) { @@ -219,7 +197,7 @@ public class FTPSClientTest { return client; } - private void retrieveFile(final String pathname) throws SocketException, IOException { + protected void retrieveFile(final String pathname) throws SocketException, IOException { final FTPSClient client = loginClient(); try { // Do it twice. @@ -230,133 +208,4 @@ public class FTPSClientTest { client.disconnect(); } } - - @Test(timeout = TEST_TIMEOUT) - public void testHasFeature() throws SocketException, IOException { - trace(">>testHasFeature"); - loginClient().disconnect(); - trace("<<testHasFeature"); - } - - private void testListFiles(final String pathname) throws SocketException, IOException { - final FTPSClient client = loginClient(); - try { - // do it twice - assertNotNull(client.listFiles(pathname)); - assertNotNull(client.listFiles(pathname)); - } finally { - client.disconnect(); - } - } - - @Test(timeout = TEST_TIMEOUT) - public void testListFilesPathNameEmpty() throws SocketException, IOException { - trace(">>testListFilesPathNameEmpty"); - testListFiles(""); - trace("<<testListFilesPathNameEmpty"); - } - - @Test(timeout = TEST_TIMEOUT) - public void testListFilesPathNameJunk() throws SocketException, IOException { - trace(">>testListFilesPathNameJunk"); - testListFiles(" Junk "); - trace("<<testListFilesPathNameJunk"); - } - - @Test(timeout = TEST_TIMEOUT) - public void testListFilesPathNameNull() throws SocketException, IOException { - trace(">>testListFilesPathNameNull"); - testListFiles(null); - trace("<<testListFilesPathNameNull"); - } - - @Test(timeout = TEST_TIMEOUT) - public void testListFilesPathNameRoot() throws SocketException, IOException { - trace(">>testListFilesPathNameRoot"); - testListFiles("/"); - trace("<<testListFilesPathNameRoot"); - } - - @Test(timeout = TEST_TIMEOUT) - public void testMdtmCalendar() throws SocketException, IOException { - trace(">>testMdtmCalendar"); - testMdtmCalendar("/file.txt"); - trace("<<testMdtmCalendar"); - } - - private void testMdtmCalendar(final String pathname) throws SocketException, IOException { - final FTPSClient client = loginClient(); - try { - // do it twice - final Calendar mdtmCalendar1 = client.mdtmCalendar(pathname); - final Calendar mdtmCalendar2 = client.mdtmCalendar(pathname); - assertNotNull(mdtmCalendar1); - assertNotNull(mdtmCalendar2); - assertEquals(mdtmCalendar1, mdtmCalendar2); - } finally { - client.disconnect(); - } - } - - @Test(timeout = TEST_TIMEOUT) - public void testMdtmFile() throws SocketException, IOException { - trace(">>testMdtmFile"); - testMdtmFile("/file.txt"); - trace("<<testMdtmFile"); - } - - private void testMdtmFile(final String pathname) throws SocketException, IOException { - final FTPSClient client = loginClient(); - try { - // do it twice - final FTPFile mdtmFile1 = client.mdtmFile(pathname); - final FTPFile mdtmFile2 = client.mdtmFile(pathname); - assertNotNull(mdtmFile1); - assertNotNull(mdtmFile2); - assertEquals(mdtmFile1.toString(), mdtmFile2.toString()); - } finally { - client.disconnect(); - } - } - - @Test(timeout = TEST_TIMEOUT) - public void testMdtmInstant() throws SocketException, IOException { - trace(">>testMdtmInstant"); - testMdtmInstant("/file.txt"); - trace("<<testMdtmInstant"); - } - - private void testMdtmInstant(final String pathname) throws SocketException, IOException { - final FTPSClient client = loginClient(); - try { - // do it twice - final Instant mdtmInstant1 = client.mdtmInstant(pathname); - final Instant mdtmInstant2 = client.mdtmInstant(pathname); - assertNotNull(mdtmInstant1); - assertNotNull(mdtmInstant2); - assertEquals(mdtmInstant1, mdtmInstant2); - } finally { - client.disconnect(); - } - } - - @Test(timeout = TEST_TIMEOUT) - public void testOpenClose() throws SocketException, IOException { - trace(">>testOpenClose"); - final FTPSClient ftpsClient = loginClient(); - try { - assertTrue(ftpsClient.hasFeature("MODE")); - assertTrue(ftpsClient.hasFeature(FTPCmd.MODE)); - } finally { - ftpsClient.disconnect(); - } - trace("<<testOpenClose"); - } - - @Test(timeout = TEST_TIMEOUT) - public void testRetrieveFilePathNameRoot() throws SocketException, IOException { - trace(">>testRetrieveFilePathNameRoot"); - retrieveFile("/file.txt"); - trace("<<testRetrieveFilePathNameRoot"); - } } 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 67302eb5..f1d7d3bc 100644 --- a/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java +++ b/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java @@ -21,27 +21,11 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; -import java.io.File; import java.io.IOException; import java.net.SocketException; -import java.net.URL; -import java.time.Duration; import java.time.Instant; import java.util.Calendar; -import org.apache.commons.io.FileUtils; -import org.apache.commons.io.output.NullOutputStream; -import org.apache.commons.net.PrintCommandListener; -import org.apache.ftpserver.FtpServer; -import org.apache.ftpserver.FtpServerFactory; -import org.apache.ftpserver.ftplet.FtpException; -import org.apache.ftpserver.ftplet.UserManager; -import org.apache.ftpserver.listener.ListenerFactory; -import org.apache.ftpserver.ssl.SslConfiguration; -import org.apache.ftpserver.ssl.SslConfigurationFactory; -import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; -import org.apache.ftpserver.usermanager.impl.BaseUser; -import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; @@ -62,89 +46,15 @@ import org.junit.runners.Parameterized.Parameters; * </p> */ @RunWith(Parameterized.class) -public class FTPSClientTest { - - private static int SocketPort; - - private static FtpServer EmbeddedFtpServer; +public class FTPSClientTest extends AbstractFtpsTest { private static final String USER_PROPS_RES = "org/apache/commons/net/ftpsserver/users.properties"; private static final String SERVER_JKS_RES = "org/apache/commons/net/ftpsserver/ftpserver.jks"; - private static final boolean IMPLICIT = false; - - private static final long TEST_TIMEOUT = 10000; // individual test timeout - - private static final boolean TRACE_CALLS = Boolean.parseBoolean(System.getenv("TRACE_CALLS")); - private static final boolean ADD_LISTENER = Boolean.parseBoolean(System.getenv("ADD_LISTENER")); - private static final long startTime = System.nanoTime(); - - /** - * 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"); - } - @BeforeClass - public static void setUpClass() throws Exception { - setUpClass(IMPLICIT); - } - - /** - * Creates and starts an embedded Apache MINA FTP Server. - * - * @param implicit FTPS connection mode - * @throws FtpException - */ - private synchronized static void setUpClass(final boolean implicit) throws FtpException { - if (EmbeddedFtpServer != null) { - return; - } - // Let the OS find use an ephemeral port by using 0. - SocketPort = 0; - final FtpServerFactory serverFactory = new FtpServerFactory(); - final PropertiesUserManagerFactory propertiesUserManagerFactory = new PropertiesUserManagerFactory(); - final URL userPropsResource = ClassLoader.getSystemClassLoader().getResource(USER_PROPS_RES); - Assert.assertNotNull(USER_PROPS_RES, userPropsResource); - 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 userprop file - // The user prop file requires the "homedirectory" to be set - user.setHomeDirectory(getTestHomeDirectory()); - serverFactory.setUserManager(userManager); - final ListenerFactory factory = new ListenerFactory(); - factory.setPort(SocketPort); - - // define SSL configuration - final URL serverJksResource = ClassLoader.getSystemClassLoader().getResource(SERVER_JKS_RES); - Assert.assertNotNull(SERVER_JKS_RES, serverJksResource); - System.out.println("Loading " + serverJksResource); - final SslConfigurationFactory sllConfigFactory = new SslConfigurationFactory(); - final File keyStoreFile = FileUtils.toFile(serverJksResource); - Assert.assertTrue(keyStoreFile.toString(), keyStoreFile.exists()); - sllConfigFactory.setKeystoreFile(keyStoreFile); - sllConfigFactory.setKeystorePassword("password"); - - // set the SSL configuration for the listener - final SslConfiguration sslConfiguration = sllConfigFactory.createSslConfiguration(); - final NoProtocolSslConfigurationProxy noProtocolSslConfigurationProxy = new NoProtocolSslConfigurationProxy(sslConfiguration); - factory.setSslConfiguration(noProtocolSslConfigurationProxy); - factory.setImplicitSsl(implicit); - - // replace the default listener - serverFactory.addListener("default", factory.createListener()); - - // start the server - EmbeddedFtpServer = serverFactory.createServer(); - EmbeddedFtpServer.start(); - SocketPort = ((org.apache.ftpserver.impl.DefaultFtpServer) EmbeddedFtpServer).getListener("default").getPort(); - // System.out.printf("jdk.tls.disabledAlgorithms = %s%n", System.getProperty("jdk.tls.disabledAlgorithms")); - trace("Server started"); + public static void setupServer() throws Exception { + setupServer(IMPLICIT, USER_PROPS_RES, SERVER_JKS_RES, "target/test-classes/org/apache/commons/net/test-data"); } @Parameters(name = "endpointCheckingEnabled={0}") @@ -152,83 +62,8 @@ public class FTPSClientTest { return new Boolean[] { Boolean.FALSE, Boolean.TRUE }; } - private static void trace(final String msg) { - if (TRACE_CALLS) { - System.err.println(msg + " " + (System.nanoTime() - startTime)); - } - } - - private final boolean endpointCheckingEnabled; - public FTPSClientTest(final boolean endpointCheckingEnabled) { - this.endpointCheckingEnabled = endpointCheckingEnabled; - } - - private void assertClientCode(final FTPSClient client) { - final int replyCode = client.getReplyCode(); - assertTrue(FTPReply.isPositiveCompletion(replyCode)); - } - - private FTPSClient loginClient() throws SocketException, IOException { - trace(">>loginClient"); - final FTPSClient client = new FTPSClient(IMPLICIT); - if (ADD_LISTENER) { - client.addProtocolCommandListener(new PrintCommandListener(System.err)); - } - // - client.setControlKeepAliveReplyTimeout(null); - assertEquals(0, client.getControlKeepAliveReplyTimeoutDuration().getSeconds()); - client.setControlKeepAliveReplyTimeout(Duration.ofSeconds(60)); - assertEquals(60, client.getControlKeepAliveReplyTimeoutDuration().getSeconds()); - // - client.setControlKeepAliveTimeout(null); - assertEquals(0, client.getControlKeepAliveTimeoutDuration().getSeconds()); - client.setControlKeepAliveTimeout(Duration.ofSeconds(61)); - assertEquals(61, client.getControlKeepAliveTimeoutDuration().getSeconds()); - // - client.setDataTimeout(null); - assertEquals(0, client.getDataTimeout().getSeconds()); - client.setDataTimeout(Duration.ofSeconds(62)); - assertEquals(62, client.getDataTimeout().getSeconds()); - // - client.setEndpointCheckingEnabled(endpointCheckingEnabled); - client.connect("localhost", SocketPort); - // - assertClientCode(client); - assertEquals(SocketPort, client.getRemotePort()); - // - try { - // HACK: Without this sleep, the user command sometimes does not reach the ftpserver - // This only seems to affect GitHub builds, and only Java 11+ - Thread.sleep(200); // 100 seems to be not always enough - } catch (final InterruptedException ignore) { - // ignore - } - assertTrue(client.login("test", "test")); - assertClientCode(client); - // - client.setFileType(FTP.BINARY_FILE_TYPE); - assertClientCode(client); - // - client.execPBSZ(0); - assertClientCode(client); - // - client.execPROT("P"); - assertClientCode(client); - trace("<<loginClient"); - return client; - } - - 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(); - } + super(endpointCheckingEnabled, null, null); } @Test(timeout = TEST_TIMEOUT) @@ -359,4 +194,5 @@ public class FTPSClientTest { retrieveFile("/file.txt"); trace("<<testRetrieveFilePathNameRoot"); } + }