summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/openslx/imagemaster/ServerTest.java
blob: 969af13c6e4fa00b785c34756eaad3213ea3fba5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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.AsymMessageVerifier;


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 );
	}
	
	public void test() {
		assertTrue(true);
	}
	
	public void testSomething() throws TException {
		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() );
		
		client.getImage( "8fbaf5cb-ebf6-11e3-996b-f5a55bd7273c", "" );
	}
	
	/**
	 * 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();
//				}
//			}
//		}
//
//	}
}