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/17 16:11:26 UTC
[commons-net] branch master updated: Add bare-bones FTPSClientTest.
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 1eb0350 Add bare-bones FTPSClientTest.
1eb0350 is described below
commit 1eb03503f39c508b2b3ca157cfd35ee2dcbf1c22
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Sep 17 12:11:17 2020 -0400
Add bare-bones FTPSClientTest.
---
pom.xml | 14 ++
.../org/apache/commons/net/ftp/FTPSClientTest.java | 170 +++++++++++++++++++++
.../apache/commons/net/ftpsserver/ftpserver.jks | Bin 0 -> 1961 bytes
.../apache/commons/net/ftpsserver/users.properties | 43 ++++++
4 files changed, 227 insertions(+)
diff --git a/pom.xml b/pom.xml
index 1d1d2b6..6546d8b 100644
--- a/pom.xml
+++ b/pom.xml
@@ -90,6 +90,20 @@ Supported protocols include: Echo, Finger, FTP, NNTP, NTP, POP3(S), SMTP(S), Tel
<version>4.13</version>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.ftpserver</groupId>
+ <artifactId>ftpserver-core</artifactId>
+ <version>1.1.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <!-- Sigh, Net is on Java 7, IO 2.7 and up require Java 8. -->
+ <!-- <version>2.8.0</version> -->
+ <version>2.6</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
diff --git a/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java b/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java
new file mode 100644
index 0000000..004c968
--- /dev/null
+++ b/src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java
@@ -0,0 +1,170 @@
+/*
+ * 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.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;
+import java.io.IOException;
+import java.net.SocketException;
+import java.net.URL;
+
+import org.apache.commons.io.FileUtils;
+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.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;
+
+/**
+ * Tests {@link FTPSClient}.
+ */
+public class FTPSClientTest {
+
+ private static int SocketPort;
+
+ private static String ConnectionUri;
+
+ private static FtpServer Server;
+
+ 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";
+
+ /**
+ * Returns the test directory as a String.
+ * <p>
+ * {@link #getTestDirectoryFile()} should be preferred.
+ *
+ * @return the test directory as a String
+ */
+ private static String getTestDirectory() {
+ return System.getProperty("test.basedir", "target/test-classes/test-data");
+ }
+
+ private static final boolean implicit = false;
+
+ @BeforeClass
+ public static void setUp() throws Exception {
+ setUpClass(implicit);
+ }
+
+ /**
+ * Creates and starts an embedded Apache MINA FTP Server.
+ *
+ * @param implicit FTPS connection mode
+ * @throws FtpException
+ * @throws IOException
+ */
+ static void setUpClass(final boolean implicit) throws FtpException, IOException {
+ if (Server != null) {
+ return;
+ }
+ 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 user prop file
+ // The user prop file requires the "homedirectory" to be set
+ user.setHomeDirectory(getTestDirectory());
+ serverFactory.setUserManager(userManager);
+ final ListenerFactory factory = new ListenerFactory();
+ // set the port of the listener
+ factory.setPort(SocketPort);
+
+ // define SSL configuration
+ final URL serverJksResource = ClassLoader.getSystemClassLoader().getResource(SERVER_JKS_RES);
+ Assert.assertNotNull(SERVER_JKS_RES, serverJksResource);
+ final SslConfigurationFactory ssl = new SslConfigurationFactory();
+ final File keyStoreFile = FileUtils.toFile(serverJksResource);
+ Assert.assertTrue(keyStoreFile.toString(), keyStoreFile.exists());
+ ssl.setKeystoreFile(keyStoreFile);
+ ssl.setKeystorePassword("password");
+
+ // set the SSL configuration for the listener
+ factory.setSslConfiguration(ssl.createSslConfiguration());
+ factory.setImplicitSsl(implicit);
+
+ // replace the default listener
+ serverFactory.addListener("default", factory.createListener());
+
+ // start the server
+ Server = serverFactory.createServer();
+ Server.start();
+ SocketPort = ((org.apache.ftpserver.impl.DefaultFtpServer) Server).getListener("default").getPort();
+ ConnectionUri = "ftps://test:test@localhost:" + SocketPort;
+ }
+
+ private FTPSClient loginClient() throws SocketException, IOException {
+ FTPSClient client = new FTPSClient(implicit);
+ client.connect("localhost", SocketPort);
+ assertEquals(SocketPort, client.getRemotePort());
+ final int replyCode = client.getReplyCode();
+ assertTrue(FTPReply.isPositiveCompletion(replyCode));
+ assertTrue(client.login("test", "test"));
+ return client;
+ }
+
+ @Test
+ public void testOpenClose() throws SocketException, IOException {
+ loginClient().disconnect();
+ }
+
+ private void testListFiles(String pathname) throws SocketException, IOException {
+ FTPSClient client = loginClient();
+ try {
+ // do it twice
+ assertNotNull(client.listFiles(pathname));
+ assertNotNull(client.listFiles(pathname));
+ } finally {
+ client.disconnect();
+ }
+ }
+
+ @Test
+ public void testListFilesPathNameRoot() throws SocketException, IOException {
+ testListFiles("/");
+ }
+
+ @Test
+ public void testListFilesPathNameEmpty() throws SocketException, IOException {
+ testListFiles("");
+ }
+
+ @Test
+ public void testListFilesPathNameNull() throws SocketException, IOException {
+ testListFiles(null);
+ }
+
+ @Test
+ public void testListFilesPathNameJunk() throws SocketException, IOException {
+ testListFiles(" Junk ");
+ }
+}
diff --git a/src/test/resources/org/apache/commons/net/ftpsserver/ftpserver.jks b/src/test/resources/org/apache/commons/net/ftpsserver/ftpserver.jks
new file mode 100644
index 0000000..28f294b
Binary files /dev/null and b/src/test/resources/org/apache/commons/net/ftpsserver/ftpserver.jks differ
diff --git a/src/test/resources/org/apache/commons/net/ftpsserver/users.properties b/src/test/resources/org/apache/commons/net/ftpsserver/users.properties
new file mode 100644
index 0000000..59fe696
--- /dev/null
+++ b/src/test/resources/org/apache/commons/net/ftpsserver/users.properties
@@ -0,0 +1,43 @@
+# 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.
+
+# Password is "admin"
+ftpserver.user.admin.userpassword=21232F297A57A5A743894A0E4A801FC3
+ftpserver.user.admin.homedirectory=target/test-classes/test-data
+ftpserver.user.admin.enableflag=true
+ftpserver.user.admin.writepermission=true
+ftpserver.user.admin.maxloginnumber=0
+ftpserver.user.admin.maxloginperip=0
+ftpserver.user.admin.idletime=0
+ftpserver.user.admin.uploadrate=0
+ftpserver.user.admin.downloadrate=0
+
+ftpserver.user.anonymous.userpassword=
+ftpserver.user.anonymous.homedirectory=target/test-classes/test-data
+ftpserver.user.anonymous.enableflag=true
+ftpserver.user.anonymous.writepermission=false
+ftpserver.user.anonymous.maxloginnumber=20
+ftpserver.user.anonymous.maxloginperip=2
+ftpserver.user.anonymous.idletime=300
+ftpserver.user.anonymous.uploadrate=4800
+ftpserver.user.anonymous.downloadrate=4800
+
+# password is "test"
+ftpserver.user.test.userpassword=098f6bcd4621d373cade4e832627b4f6
+ftpserver.user.test.homedirectory=target/test-classes/test-data
+ftpserver.user.test.enableflag=true
+ftpserver.user.test.writepermission=true