summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/openslx/imagemaster/AppTest.java
blob: 291206b8ba478135ca7dce962ba42da829ca62ec (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
package org.openslx.imagemaster;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.SocketException;
import java.util.Date;
import java.util.UUID;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
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.Sha512Crypt;

/**
 * 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 );
	}

	/**
	 * Test the authentication
	 * 
	 * @throws TException
	 */
	public void testAuthentication() 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() );

		SessionData sessionData = client.authenticate( "ns202", "xxxxxxxxxxxx" );
		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
	 */
	public void testServerAuth() throws TException, SocketException, 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() );

		String stringToEncrypt = client.startServerAuthentication( "Test Organization" );
		System.out.println( "Authentication started. Got string: " + stringToEncrypt );

		String response = stringToEncrypt;

		ServerSessionData data = client.serverAuthenticate( "Test Organization", response );
		System.out.println( "Authenticated and got sid: '" + data.getSessionId() + "'" );

		// Create ImageData
		int version = 1;
		String imageName = "maschine.vmkd";
		UUID uuid = UUID.randomUUID();
		int imageCreateTime = (int)new Date().getTime();
		int 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.";

		ImageData imageData = new ImageData( uuid.toString(), version, imageName,
				imageCreateTime, imageUpdateTime, imageOwner, contentOperatingSystem,
				statusIsValid, statusIsDeleted, imageShortDescrption, imageLongDescription );

		System.out.println( "Created imageData" );

		FtpCredentials ftpCredentials = client.submitImage( data.sessionId, imageData );
		System.out.println( "Got FTP credentials. User: " + ftpCredentials.username + ", password: " + ftpCredentials.password );

		FTPClient FtpClient = new FTPClient();
		String host = "localhost";
		int port = 2221;
		String user = ftpCredentials.username;
		String password = ftpCredentials.password;
		String fileName = "/home/nils/file_to_upload.bin";

		try {
			FtpClient.connect( host, port );
			System.out.println( "Connected to " + host + ":" + port + ". Reply code: " + FtpClient.getReplyCode() );
			if ( !FTPReply.isPositiveCompletion( FtpClient.getReplyCode() ) ) {
				ConnectException ce = new ConnectException( "No positive reply code." );
				throw ce;
			}
			if ( !FtpClient.login( user, password ) ) {
				ConnectException ce = new ConnectException( "Could not login." );
				throw ce;
			}
			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();
			client.finshedUpload( data.sessionId, imageData );
		} finally {
			if ( FtpClient.isConnected() ) {
				try {
					FtpClient.logout();
					FtpClient.disconnect();
				} catch ( IOException e ) {
					e.printStackTrace();
				}
			}
		}

	}

	public void testSha512_Crypt()
	{
		Sha512Crypt.selfTest();
	}
}