~ubuntu-branches/ubuntu/quantal/jetty/quantal

« back to all changes in this revision

Viewing changes to extra/ftp/test/src/org/mortbay/ftp/TestServer.java

  • Committer: Bazaar Package Importer
  • Author(s): Ludovic Claude
  • Date: 2009-09-07 00:22:20 UTC
  • mfrom: (1.1.5 upstream) (2.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090907002220-8w7luxa3m4otve4g
Tags: 6.1.20-2
* Move documentation to /usr/share/doc/libjetty-java
* Better postinst and postrm scripts, aligned with tomcat6 scripts:
   - postinst: user jetty is created with its own group
   - postrm: better cleanup of temporary data,
     thourough remove and purge of data
* debian/changelog:
  - fix suggest for package libjetty-java-doc, add libjetty-java-doc
    to the list of Suggests for libjetty-java
  - add libjetty-extra to the list of Suggests for package jetty.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
16
 
 
17
 
package org.mortbay.ftp;
18
 
 
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;
24
 
 
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;
29
 
 
30
 
 
31
 
// ===========================================================================
32
 
class TestServer extends Thread
33
 
{
34
 
        private static Log log = LogFactory.getLog(TestServer.class);
35
 
 
36
 
    TestCase test;
37
 
    ServerSocket listen=null;
38
 
    int port = -1;
39
 
    Socket connection;
40
 
    LineInput in;
41
 
    Writer out;
42
 
 
43
 
    TestServer(TestCase t)
44
 
    {
45
 
        this.test=t;
46
 
        start();
47
 
 
48
 
        synchronized(this){
49
 
            while (port==-1)
50
 
                try{wait(1000);}catch(InterruptedException e){};
51
 
        }
52
 
 
53
 
    }
54
 
 
55
 
    public void run()
56
 
    {
57
 
        try{
58
 
            listen = new ServerSocket(0);
59
 
            synchronized(this){
60
 
                port = listen.getLocalPort();
61
 
                notifyAll();
62
 
            }
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");
69
 
            out.flush();
70
 
 
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");
76
 
            out.flush();
77
 
 
78
 
            line = in.readLine();
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");
82
 
            out.flush();
83
 
 
84
 
            //Handler get file
85
 
            line = in.readLine();
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");
89
 
            out.flush();
90
 
 
91
 
            int c = line.lastIndexOf(',');
92
 
            int dataPort = Integer.parseInt(line.substring(c+1));
93
 
            line = line.substring(0,c);
94
 
            dataPort += 256 *
95
 
                Integer.parseInt(line.substring(line.lastIndexOf(',')+1));
96
 
            Socket dataConnection = new Socket(InetAddress.getLocalHost(),
97
 
                                               dataPort);
98
 
            test.check(true,"DataPort Opened");
99
 
 
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");
104
 
            out.flush();
105
 
 
106
 
            Writer dataOut = new
107
 
                OutputStreamWriter(dataConnection.getOutputStream(),"ISO8859_1");
108
 
 
109
 
            Thread.sleep(1000);
110
 
            dataOut.write("How Now Brown Cow\n");
111
 
            dataOut.flush();
112
 
            dataOut.close();
113
 
            dataConnection.close();
114
 
            out.write(CmdReply.codeClosingData+" File transfer complete\n");
115
 
            out.flush();
116
 
 
117
 
            //Handler put file
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");
122
 
            out.flush();
123
 
 
124
 
            c = line.lastIndexOf(',');
125
 
            dataPort = Integer.parseInt(line.substring(c+1));
126
 
            line = line.substring(0,c);
127
 
            dataPort += 256 *
128
 
                Integer.parseInt(line.substring(line.lastIndexOf(',')+1));
129
 
            dataConnection = new Socket(InetAddress.getLocalHost(),
130
 
                                               dataPort);
131
 
            test.check(true,"DataPort Opened");
132
 
 
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");
137
 
            out.flush();
138
 
 
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");
145
 
 
146
 
            out.write(CmdReply.codeClosingData+" File transfer complete\n");
147
 
            out.flush();
148
 
 
149
 
            //Handler abort file
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");
154
 
            out.flush();
155
 
 
156
 
            c = line.lastIndexOf(',');
157
 
            dataPort = Integer.parseInt(line.substring(c+1));
158
 
            line = line.substring(0,c);
159
 
            dataPort += 256 *
160
 
                Integer.parseInt(line.substring(line.lastIndexOf(',')+1));
161
 
            dataConnection = new Socket(InetAddress.getLocalHost(),
162
 
                                               dataPort);
163
 
            test.check(true,"DataPort Opened");
164
 
 
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");
169
 
            out.flush();
170
 
 
171
 
            dataOut = new OutputStreamWriter(dataConnection.getOutputStream(),"ISO8859_1");
172
 
            dataOut.write("How Now Brown Cow\n");
173
 
            dataOut.flush();
174
 
            line = in.readLine();
175
 
            if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
176
 
            test.check(line.startsWith("ABOR"),"Received ABOR");
177
 
 
178
 
            dataOut.close();
179
 
            dataConnection.close();
180
 
            out.write(CmdReply.codeClosingData+" File transfer aborted\n");
181
 
            out.flush();
182
 
 
183
 
            line = in.readLine();
184
 
            out.write(CmdReply.codeCommandOK+" OK\n");
185
 
            out.flush();
186
 
            if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
187
 
            test.checkEquals("TYPE I",line,"Received TYPE I");
188
 
 
189
 
            line = in.readLine();
190
 
            out.write(CmdReply.codeCommandOK+" OK\n");
191
 
            out.flush();
192
 
            if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
193
 
            test.checkEquals("TYPE L 8",line,"Received TYPE L 8");
194
 
 
195
 
            line = in.readLine();
196
 
            out.write(CmdReply.codeCommandOK+" OK\n");
197
 
            out.flush();
198
 
            if(log.isDebugEnabled())log.debug("TestCase server got: "+line);
199
 
            test.checkEquals("TYPE A C",line,"Received TYPE A C");
200
 
 
201
 
            log.debug("Tests completed");
202
 
        }
203
 
        catch (Exception e){
204
 
            test.check(false,"Server failed: "+e);
205
 
            TestCase.report();
206
 
            System.exit(1);
207
 
        }
208
 
    }
209
 
}
210