summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/openslx/satellitedaemon/ftp/FtpTestUtil.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org/openslx/satellitedaemon/ftp/FtpTestUtil.java')
-rw-r--r--src/test/java/org/openslx/satellitedaemon/ftp/FtpTestUtil.java111
1 files changed, 111 insertions, 0 deletions
diff --git a/src/test/java/org/openslx/satellitedaemon/ftp/FtpTestUtil.java b/src/test/java/org/openslx/satellitedaemon/ftp/FtpTestUtil.java
new file mode 100644
index 0000000..44d14c5
--- /dev/null
+++ b/src/test/java/org/openslx/satellitedaemon/ftp/FtpTestUtil.java
@@ -0,0 +1,111 @@
+package org.openslx.satellitedaemon.ftp;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+
+import org.apache.ftpserver.ConnectionConfigFactory;
+import org.apache.ftpserver.FtpServer;
+import org.apache.ftpserver.FtpServerFactory;
+import org.apache.ftpserver.ftplet.Authority;
+import org.apache.ftpserver.ftplet.FtpException;
+import org.apache.ftpserver.ftplet.UserManager;
+import org.apache.ftpserver.listener.ListenerFactory;
+import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
+import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
+import org.apache.ftpserver.usermanager.impl.BaseUser;
+import org.apache.ftpserver.usermanager.impl.WritePermission;
+
+/**
+ * FTP-Test-Utility, basierend auf Apache FtpServer: {@link http
+ * ://www.jarvana.com
+ * /jarvana/view/org/apache/ftpserver/ftpserver-core/1.0.6/ftpserver
+ * -core-1.0.6-javadoc.jar!/org/apache/ftpserver/FtpServer.html}
+ */
+public class FtpTestUtil {
+ /**
+ * Erzeuge FTP-Server.
+ *
+ * @param ftpPort
+ * FTP-Port, z.B. 2121
+ * @param ftpHomeDir
+ * FTP-Verzeichnis, z.B. "target/FtpHome"
+ * @param readUserName
+ * leseberechtigter Benutzer: Name
+ * @param readUserPwd
+ * leseberechtigter Benutzer: Passwort
+ * @param writeUserName
+ * schreibberechtigter Benutzer: Name
+ * @param writeUserPwd
+ * schreibberechtigter Benutzer: Passwort
+ * @param ftpUsersPropsFile
+ * kann null sein, oder z.B. "target/FtpUsers.properties"
+ * @param maxLogins
+ * maximale Anzahl von Logins (0 fuer Defaultwert)
+ */
+ public static FtpServer createFtpServer(int ftpPort, String ftpHomeDir,
+ String readUserName, String readUserPwd, String writeUserName,
+ String writeUserPwd) throws FtpException, IOException {
+ return createFtpServer(ftpPort, ftpHomeDir, readUserName, readUserPwd,
+ writeUserName, writeUserPwd, null, 0);
+ }
+
+ public static FtpServer createFtpServer(int ftpPort, String ftpHomeDir,
+ String readUserName, String readUserPwd, String writeUserName,
+ String writeUserPwd, String ftpUsersPropsFile, int maxLogins)
+ throws FtpException, IOException {
+ return createFtpServer(ftpPort, ftpHomeDir, readUserName, readUserPwd,
+ writeUserName, writeUserPwd, ftpUsersPropsFile, maxLogins, 0);
+ }
+
+ public static FtpServer createFtpServer(int ftpPort, String ftpHomeDir,
+ String readUserName, String readUserPwd, String writeUserName,
+ String writeUserPwd, String ftpUsersPropsFile, int maxLogins,
+ int maxIdleTimeSec) throws FtpException, IOException {
+ File fhd = new File(ftpHomeDir);
+ if (!fhd.exists())
+ fhd.mkdirs();
+
+ ListenerFactory listenerFactory = new ListenerFactory();
+ listenerFactory.setPort(ftpPort);
+
+ PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
+ userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());
+ if (ftpUsersPropsFile != null && ftpUsersPropsFile.trim().length() > 0) {
+ File upf = new File(ftpUsersPropsFile);
+ if (!upf.exists())
+ upf.createNewFile();
+ userManagerFactory.setFile(upf);
+ }
+
+ // Einen Nur-Lese-User und einen User mit Schreibberechtigung anlegen:
+ UserManager userManager = userManagerFactory.createUserManager();
+ BaseUser userRd = new BaseUser();
+ BaseUser userWr = new BaseUser();
+ userRd.setName(readUserName);
+ userRd.setPassword(readUserPwd);
+ userRd.setHomeDirectory(ftpHomeDir);
+ userWr.setName(writeUserName);
+ userWr.setPassword(writeUserPwd);
+ userWr.setHomeDirectory(ftpHomeDir);
+ if (maxIdleTimeSec > 0) {
+ userRd.setMaxIdleTime(maxIdleTimeSec);
+ userWr.setMaxIdleTime(maxIdleTimeSec);
+ }
+ ArrayList<Authority> authorities = new ArrayList<Authority>();
+ authorities.add(new WritePermission());
+ userWr.setAuthorities(authorities);
+ userManager.save(userRd);
+ userManager.save(userWr);
+
+ FtpServerFactory serverFactory = new FtpServerFactory();
+ serverFactory.addListener("default", listenerFactory.createListener());
+ serverFactory.setUserManager(userManager);
+ if (maxLogins > 0) {
+ ConnectionConfigFactory ccf = new ConnectionConfigFactory();
+ ccf.setMaxLogins(maxLogins);
+ serverFactory.setConnectionConfig(ccf.createConnectionConfig());
+ }
+ return serverFactory.createServer();
+ }
+}