1
// ========================================================================
2
// $Id: TestServer.java,v 1.3 2004/05/09 20:30:43 gregwilkins Exp $
3
// Copyright 1996-2004 Mort Bay Consulting Pty. Ltd.
4
// ------------------------------------------------------------------------
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
// ========================================================================
15
// Copyright (c) 1996 Optimus Solutions Pty. Ltd. All rights reserved.
17
package org.mortbay.ftp;
19
import java.io.OutputStreamWriter;
20
import java.io.Writer;
21
import java.net.InetAddress;
22
import java.net.ServerSocket;
23
import java.net.Socket;
25
import org.apache.commons.logging.Log;
26
import org.apache.commons.logging.LogFactory;
27
import org.mortbay.util.LineInput;
28
import org.mortbay.util.TestCase;
31
// ===========================================================================
32
class TestServer extends Thread
34
private static Log log = LogFactory.getLog(TestServer.class);
37
ServerSocket listen=null;
43
TestServer(TestCase t)
50
try{wait(1000);}catch(InterruptedException e){};
58
listen = new ServerSocket(0);
60
port = listen.getLocalPort();
63
log.debug("TestCase server listening");
64
connection = listen.accept( );
65
log.debug("TestCase server connected");
66
in = new LineInput(connection.getInputStream());
67
out = new OutputStreamWriter(connection.getOutputStream(),"ISO8859_1");
68
out.write(CmdReply.codeServiceReady+" OK\n");
71
// Handle authentication
72
String line = in.readLine();
73
if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
74
test.checkEquals(line,"USER TestUser","Received USER");
75
out.write(CmdReply.codeUsernameNeedsPassword+" Need password\n");
79
if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
80
test.checkEquals(line,"PASS TestPass","Received PASS");
81
out.write(CmdReply.codeUserLoggedIn+" OK\n");
86
if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
87
test.check(line.startsWith("PORT"),"Received PORT");
88
out.write(CmdReply.codeCommandOK+" OK\n");
91
int c = line.lastIndexOf(',');
92
int dataPort = Integer.parseInt(line.substring(c+1));
93
line = line.substring(0,c);
95
Integer.parseInt(line.substring(line.lastIndexOf(',')+1));
96
Socket dataConnection = new Socket(InetAddress.getLocalHost(),
98
test.check(true,"DataPort Opened");
100
line = in.readLine();
101
if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
102
test.checkEquals(line,"RETR TestFileName","Received RETR");
103
out.write(CmdReply.codeFileStatusOK+" Data port opened\n");
107
OutputStreamWriter(dataConnection.getOutputStream(),"ISO8859_1");
110
dataOut.write("How Now Brown Cow\n");
113
dataConnection.close();
114
out.write(CmdReply.codeClosingData+" File transfer complete\n");
118
line = in.readLine();
119
if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
120
test.check(line.startsWith("PORT"),"Received PORT");
121
out.write(CmdReply.codeCommandOK+" OK\n");
124
c = line.lastIndexOf(',');
125
dataPort = Integer.parseInt(line.substring(c+1));
126
line = line.substring(0,c);
128
Integer.parseInt(line.substring(line.lastIndexOf(',')+1));
129
dataConnection = new Socket(InetAddress.getLocalHost(),
131
test.check(true,"DataPort Opened");
133
line = in.readLine();
134
if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
135
test.checkEquals(line,"STOR TestFileName","Received STOR");
136
out.write(CmdReply.codeFileStatusOK+" Data port opened\n");
139
LineInput dataIn = new
140
LineInput(dataConnection.getInputStream());
141
String input = dataIn.readLine();
142
test.checkEquals(input,"How Now Brown Cow","received file");
143
input = dataIn.readLine();
144
test.checkEquals(input,null,"received EOF");
146
out.write(CmdReply.codeClosingData+" File transfer complete\n");
150
line = in.readLine();
151
if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
152
test.check(line.startsWith("PORT"),"Received PORT");
153
out.write(CmdReply.codeCommandOK+" OK\n");
156
c = line.lastIndexOf(',');
157
dataPort = Integer.parseInt(line.substring(c+1));
158
line = line.substring(0,c);
160
Integer.parseInt(line.substring(line.lastIndexOf(',')+1));
161
dataConnection = new Socket(InetAddress.getLocalHost(),
163
test.check(true,"DataPort Opened");
165
line = in.readLine();
166
if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
167
test.checkEquals(line,"RETR TestFileName","Received RETR");
168
out.write(CmdReply.codeFileStatusOK+" Data port opened\n");
171
dataOut = new OutputStreamWriter(dataConnection.getOutputStream(),"ISO8859_1");
172
dataOut.write("How Now Brown Cow\n");
174
line = in.readLine();
175
if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
176
test.check(line.startsWith("ABOR"),"Received ABOR");
179
dataConnection.close();
180
out.write(CmdReply.codeClosingData+" File transfer aborted\n");
183
line = in.readLine();
184
out.write(CmdReply.codeCommandOK+" OK\n");
186
if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
187
test.checkEquals("TYPE I",line,"Received TYPE I");
189
line = in.readLine();
190
out.write(CmdReply.codeCommandOK+" OK\n");
192
if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
193
test.checkEquals("TYPE L 8",line,"Received TYPE L 8");
195
line = in.readLine();
196
out.write(CmdReply.codeCommandOK+" OK\n");
198
if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
199
test.checkEquals("TYPE A C",line,"Received TYPE A C");
201
log.debug("Tests completed");
204
test.check(false,"Server failed: "+e);