KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > command > plugins > DataConnectionHandlerTest


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.master.command.plugins;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import junit.framework.TestCase;
25 import junit.framework.TestSuite;
26 import net.sf.drftpd.master.ConnectionManager;
27 import net.sf.drftpd.master.FtpReply;
28 import net.sf.drftpd.master.FtpRequest;
29 import net.sf.drftpd.master.config.FtpConfig;
30
31 import org.apache.log4j.BasicConfigurator;
32 import org.drftpd.commands.UnhandledCommandException;
33 import org.drftpd.tests.DummyBaseFtpConnection;
34
35 /**
36  * @author mog
37  * @version $Id: DataConnectionHandlerTest.java,v 1.6.2.1 2004/06/19 23:37:26 mog Exp $
38  */

39 public class DataConnectionHandlerTest extends TestCase {
40
41     private static class FC extends FtpConfig {
42         public FC() {
43             super();
44             try {
45                 loadConfig(new Properties JavaDoc(), null);
46             } catch (IOException JavaDoc e) {
47                 throw new RuntimeException JavaDoc();
48             }
49         }
50     }
51     public static TestSuite suite() {
52         return new TestSuite(DataConnectionHandlerTest.class);
53     }
54     DummyBaseFtpConnection conn;
55     DataConnectionHandler dch;
56     public DataConnectionHandlerTest(String JavaDoc fName) {
57         super(fName);
58     }
59
60     public void assertBetween(int val, int from, int to) {
61         assertTrue(val + " is less than " + from, val >= from);
62         assertTrue(val + " is more than " + from, val <= to);
63     }
64     private String JavaDoc list() {
65         LIST list = (LIST) new LIST().initialize(null, null);
66
67         ByteArrayOutputStream JavaDoc out =
68             conn.getDummySSF().getDummySocket().getByteArrayOutputStream();
69
70         conn.setRequest(new FtpRequest("LIST"));
71         FtpReply reply = list.execute(conn);
72         String JavaDoc replystr = conn.getDummyOut().getBuffer().toString();
73         assertEquals(150, Integer.parseInt(replystr.substring(0, 3)));
74         assertEquals(226, reply.getCode());
75
76         String JavaDoc ret = out.toString();
77         out.reset();
78         return ret;
79     }
80
81     private String JavaDoc pasvList() throws UnhandledCommandException {
82         conn.setRequest(new FtpRequest("PRET LIST"));
83
84         FtpReply reply;
85         reply = dch.execute(conn);
86         assertEquals(reply.toString(), 200, reply.getCode());
87
88         conn.setRequest(new FtpRequest("PASV"));
89         reply = dch.execute(conn);
90         assertEquals(reply.toString(), 227, reply.getCode());
91
92         return list();
93     }
94
95     private String JavaDoc portList() throws UnhandledCommandException {
96         conn.setRequest(new FtpRequest("PORT 127,0,0,1,0,0"));
97         dch.execute(conn);
98
99         return list();
100     }
101
102     protected void setUp() throws Exception JavaDoc {
103         BasicConfigurator.configure();
104         dch =
105             (DataConnectionHandler) new DataConnectionHandler().initialize(
106                 null,
107                 null);
108         conn = new DummyBaseFtpConnection(dch) {
109             public FtpConfig getConfig() {
110                 return new FC();
111             }
112         };
113     }
114
115     protected void tearDown() throws Exception JavaDoc {
116         dch = null;
117     }
118
119     public void testMixedListEqual() throws UnhandledCommandException {
120         String JavaDoc list = portList();
121         assertEquals(list, pasvList());
122         assertEquals(list, portList());
123         assertEquals(list, pasvList());
124         testPASVWithoutPRET();
125         assertEquals(list, portList());
126         testPASVWithoutPRET();
127         assertEquals(list, portList());
128     }
129     public void testPasvList() throws UnhandledCommandException {
130         pasvList();
131     }
132
133     public void testPASVWithoutPRET() throws UnhandledCommandException {
134         conn.setRequest(new FtpRequest("PASV"));
135         FtpReply reply = dch.execute(conn);
136         assertBetween(reply.getCode(), 500, 599);
137     }
138
139     public void testPortList() throws UnhandledCommandException {
140         portList();
141     }
142 }
143
Popular Tags