summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/openslx/satellitedaemon
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/org/openslx/satellitedaemon')
-rw-r--r--src/test/java/org/openslx/satellitedaemon/AppTest.java38
-rw-r--r--src/test/java/org/openslx/satellitedaemon/ftp/FtpTestUtil.java111
-rw-r--r--src/test/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtilTest.java73
3 files changed, 222 insertions, 0 deletions
diff --git a/src/test/java/org/openslx/satellitedaemon/AppTest.java b/src/test/java/org/openslx/satellitedaemon/AppTest.java
new file mode 100644
index 0000000..ff5320c
--- /dev/null
+++ b/src/test/java/org/openslx/satellitedaemon/AppTest.java
@@ -0,0 +1,38 @@
+package org.openslx.satellitedaemon;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit test for simple App.
+ */
+public class AppTest
+ extends TestCase
+{
+ /**
+ * Create the test case
+ *
+ * @param testName name of the test case
+ */
+ public AppTest( String testName )
+ {
+ super( testName );
+ }
+
+ /**
+ * @return the suite of tests being tested
+ */
+ public static Test suite()
+ {
+ return new TestSuite( AppTest.class );
+ }
+
+ /**
+ * Rigourous Test :-)
+ */
+ public void testApp()
+ {
+ assertTrue( true );
+ }
+}
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();
+ }
+}
diff --git a/src/test/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtilTest.java b/src/test/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtilTest.java
new file mode 100644
index 0000000..4caa8db
--- /dev/null
+++ b/src/test/java/org/openslx/satellitedaemon/ftp/FtpUpDownUtilTest.java
@@ -0,0 +1,73 @@
+package org.openslx.satellitedaemon.ftp;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+
+import junit.framework.Assert;
+
+import org.apache.ftpserver.FtpServer;
+import org.apache.ftpserver.ftplet.FtpException;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class FtpUpDownUtilTest {
+ private static final int FTP_PORT = 2121;
+ private static final String FTP_HOST = "localhost";
+ private static final String FTP_HOME_DIR = "target/FtpHome";
+ private static final String FTPUSERSPROPS_FILE = "target/FtpUsers.properties";
+ private static final String READ_USER_NAME = "ReadUserName";
+ private static final String READ_USER_PWD = "ReadUserPwd";
+ private static final String WRITE_USER_NAME = "WriteUserName";
+ private static final String WRITE_USER_PWD = "WriteUserPwd";
+ private static FtpServer ftpServer;
+
+ @BeforeClass
+ public static void startFtpServer() throws FtpException, IOException
+ {
+ ftpServer = FtpTestUtil.createFtpServer( FTP_PORT, FTP_HOME_DIR,
+ READ_USER_NAME, READ_USER_PWD, WRITE_USER_NAME, WRITE_USER_PWD, FTPUSERSPROPS_FILE, 0 );
+ ftpServer.start();
+ }
+
+ @AfterClass
+ public static void stoppFtpServer()
+ {
+ // Um den FTP-Server von ausserhalb des Tests eine Zeit lang ueber
+ // ftp://WriteUserName:WriteUserPwd@localhost:2121
+ // anzusprechen, kann folgende Zeile aktiviert werden:
+ // try { Thread.sleep( 55000 ); } catch( InterruptedException e ) {/*ok*/}
+ ftpServer.stop();
+ ftpServer = null;
+ }
+
+ @Test
+ public void testFtp() throws IOException
+ {
+ final String LOCAL_SRC_FILE = "TestSrc.txt";
+ final String LOCAL_DST_FILE = "target/TestDst.txt";
+ final String REMOTE_FILE = "Test.txt";
+
+ File testFile = new File( LOCAL_SRC_FILE );
+ if( !testFile.exists() ) testFile.createNewFile();
+
+ // Upload-Versuch ohne Schreibberechtigung muss fehlschlagen:
+ Assert.assertFalse( "READ_USER", FtpUpDownUtil.upload( LOCAL_SRC_FILE, REMOTE_FILE, FTP_HOST, FTP_PORT,
+ READ_USER_NAME, READ_USER_PWD, false ) );
+
+ // Teste Upload mit Schreibberechtigung:
+ Assert.assertTrue( "WRITE_USER", FtpUpDownUtil.upload( LOCAL_SRC_FILE, REMOTE_FILE, FTP_HOST, FTP_PORT,
+ WRITE_USER_NAME, WRITE_USER_PWD, false ) );
+ Assert.assertTrue( "REMOTE_FILE.exists", new File( FTP_HOME_DIR, REMOTE_FILE ).exists() );
+
+ // Teste Download:
+ Assert.assertTrue( "Download", FtpUpDownUtil.download( LOCAL_DST_FILE, REMOTE_FILE, FTP_HOST, FTP_PORT,
+ READ_USER_NAME, READ_USER_PWD, false ) );
+ Assert.assertTrue( "LOCAL_DST_FILE.exists", new File( LOCAL_DST_FILE ).exists() );
+
+ // Teste Auflistung:
+ String[] remoteFilenameList = FtpUpDownUtil.list( FTP_HOST, FTP_PORT, READ_USER_NAME, READ_USER_PWD );
+ Assert.assertTrue( "remoteFilenameList", Arrays.asList( remoteFilenameList ).contains( REMOTE_FILE ) );
+ }
+}