package org.openslx.imagemaster; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.ConnectException; import java.net.SocketException; import java.nio.ByteBuffer; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.KeyStore; import java.security.KeyStoreException; import java.security.NoSuchAlgorithmException; import java.security.SignatureException; import java.security.UnrecoverableKeyException; import java.security.cert.CertificateException; import java.util.UUID; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.TrustManagerFactory; import junit.framework.TestCase; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPReply; import org.apache.commons.net.ftp.FTPSClient; import org.apache.log4j.Logger; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.openslx.imagemaster.thrift.iface.FtpCredentials; import org.openslx.imagemaster.thrift.iface.ImageData; import org.openslx.imagemaster.thrift.iface.ImageServer.Client; import org.openslx.imagemaster.thrift.iface.ServerSessionData; import org.openslx.imagemaster.thrift.iface.SessionData; import org.openslx.imagemaster.thrift.iface.UserInfo; import org.openslx.imagemaster.util.AsymMessageSign; public class ServerTest extends TestCase { private static Logger log = Logger.getLogger( ServerTest.class ); @Override public void setUp() throws Exception { // start the server // Thread t = new Thread(new Runnable() { // // @Override // public void run() // { // App.main( null ); // } // }, "App"); // t.start(); // Thread.sleep( 2000 ); } /** * Test the authentication * * @throws TException * @throws IOException */ public void testAuthentication() throws TException, IOException { TTransport transport = new TSocket( "localhost", 9090 ); transport.open(); TProtocol protocol = new TBinaryProtocol( transport ); Client client = new Client( protocol ); assertTrue( "Could not ping server", client.ping() ); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter username: "); String username = reader.readLine(); System.out.print("Enter password: "); String password = reader.readLine(); SessionData sessionData = client.authenticate( username, password ); UserInfo userInfo = client.getUserFromToken( sessionData.getAuthToken() ); System.out.println( "User info: " + userInfo ); System.out.println( "Server address from MySQL: " + sessionData.serverAddress ); } /** * Test the server authentication and FTP Upload. * * @throws TException * @throws IOException * @throws SocketException * @throws KeyStoreException * @throws CertificateException * @throws NoSuchAlgorithmException * @throws UnrecoverableKeyException * @throws SignatureException * @throws InvalidKeyException * @throws InvalidAlgorithmParameterException */ public void testServerAuthAndFtpUpload() throws TException, SocketException, IOException, UnrecoverableKeyException, NoSuchAlgorithmException, CertificateException, KeyStoreException, InvalidKeyException, SignatureException, InvalidAlgorithmParameterException { if (true) return; @SuppressWarnings( "unused" ) TTransport transport = new TSocket( "localhost", 9090 ); transport.open(); TProtocol protocol = new TBinaryProtocol( transport ); Client client = new Client( protocol ); assertTrue( "Could not ping server", client.ping() ); String stringToEncrypt = client.startServerAuthentication( "Test Organization" ); System.out.println( "Authentication started. Got string: " + stringToEncrypt ); AsymMessageSign messageSigner = new AsymMessageSign( "ftp", "password", "./config/keystore.jks" ); byte[] response = messageSigner.signMessage( stringToEncrypt ); System.out.println( "Signed string: " + response ); ByteBuffer bBuffer = ByteBuffer.wrap( response ); ServerSessionData data = client.serverAuthenticate( "Test Organization", bBuffer ); System.out.println( "Authenticated and got sid: '" + data.getSessionId() + "'" ); // Create ImageData int version = 1; String imageName = "maschine.vmkd"; UUID uuid = UUID.randomUUID(); long imageCreateTime = System.currentTimeMillis(); long imageUpdateTime = imageCreateTime; String imageOwner = "ns202"; String contentOperatingSystem = "win7"; boolean statusIsValid = true; boolean statusIsDeleted = false; String imageShortDescrption = "EIN SUPER TOLLES IMAGE!"; String imageLongDescription = "Lorem ipsum dolor sit amet."; String fileName = "/home/nils/file_to_upload.bin"; ImageData imageData = new ImageData( uuid.toString(), version, imageName, imageCreateTime, imageUpdateTime, imageOwner, contentOperatingSystem, statusIsValid, statusIsDeleted, imageShortDescrption, imageLongDescription, new File(fileName).getTotalSpace() ); System.out.println( "Created imageData..." ); FtpCredentials ftpCredentials = client.submitImage( data.sessionId, imageData ); System.out.println( "Got FTP credentials. User: " + ftpCredentials.username + ", password: " + ftpCredentials.password ); FTPSClient FtpClient = new FTPSClient( "SSL", true ); System.out.println("Created new ftpsclient..."); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance( KeyManagerFactory.getDefaultAlgorithm() ); KeyStore keystore = KeyStore.getInstance( "JKS" ); keystore.load( new FileInputStream( new File( "./config/keystore.jks" ) ), "password".toCharArray() ); System.out.println("Loaded keystore.."); trustManagerFactory.init( keystore ); TrustManager trustManager = trustManagerFactory.getTrustManagers()[0]; FtpClient.setTrustManager(trustManager); System.out.println("Trying to connect..."); String host = "localhost"; int port = 2221; String user = ftpCredentials.username; String password = ftpCredentials.password; try { FtpClient.connect( host, port ); System.out.println( "Connected to " + host + ":" + port + ". Reply code: " + FtpClient.getReplyCode() ); if ( !FTPReply.isPositiveCompletion( FtpClient.getReplyCode() ) ) { throw new ConnectException( "No positive reply code." ); } if ( !FtpClient.login( user, password ) ) { throw new ConnectException( "Could not login." ); } System.out.println( "Logged in with user: " + user ); FtpClient.setFileType( FTP.BINARY_FILE_TYPE ); FtpClient.enterLocalPassiveMode(); System.out.println( "Entered PASSIVE MODE" ); InputStream input = new FileInputStream( fileName ); System.out.print( "Starting file upload ... " ); FtpClient.storeFile( "xcvb.vmdk", input ); System.out.println( "done." ); FtpClient.noop(); } catch (Exception e) { e.printStackTrace(); } finally { if ( FtpClient.isConnected() ) { try { FtpClient.logout(); FtpClient.disconnect(); boolean result = client.finshedUpload( ftpCredentials.username, imageData ); System.out.println("Telling server that upload finished: " + result); } catch ( IOException e ) { e.printStackTrace(); } } } } }