001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package net.sf.drftpd.master.command.plugins;
019:
020: import java.io.ByteArrayOutputStream;
021:
022: import javax.net.ServerSocketFactory;
023: import javax.net.SocketFactory;
024:
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027: import net.sf.drftpd.master.FtpRequest;
028:
029: import org.apache.log4j.BasicConfigurator;
030: import org.drftpd.commands.Reply;
031: import org.drftpd.commands.ReplyException;
032: import org.drftpd.tests.DummyBaseFtpConnection;
033: import org.drftpd.tests.DummyConnectionManager;
034: import org.drftpd.tests.DummyFtpConfig;
035: import org.drftpd.tests.DummyGlobalContext;
036: import org.drftpd.tests.DummyServerSocketFactory;
037: import org.drftpd.tests.DummySocketFactory;
038:
039: /**
040: * @author mog
041: * @version $Id: DataConnectionHandlerTest.java 849 2004-12-02 05:21:41Z mog $
042: */
043: public class DataConnectionHandlerTest extends TestCase {
044: private DummyGlobalContext gctx;
045: private DummyConnectionManager cm;
046: DummyBaseFtpConnection conn;
047: DummyDataConnectionHandler dch;
048:
049: public DataConnectionHandlerTest(String fName) {
050: super (fName);
051: }
052:
053: public static TestSuite suite() {
054: return new TestSuite(DataConnectionHandlerTest.class);
055: }
056:
057: public void assertBetween(int val, int from, int to) {
058: assertTrue(val + " is less than " + from, val >= from);
059: assertTrue(val + " is more than " + from, val <= to);
060: }
061:
062: private String list() {
063: LIST list = (LIST) new LIST().initialize(null, null);
064:
065: ByteArrayOutputStream out = conn.getDummySSF().getDummySocket()
066: .getByteArrayOutputStream();
067:
068: conn.setRequest(new FtpRequest("LIST"));
069:
070: Reply reply = list.execute(conn);
071: String replystr = conn.getDummyOut().getBuffer().toString();
072: assertEquals(150, Integer.parseInt(replystr.substring(0, 3)));
073: assertEquals(226, reply.getCode());
074:
075: String ret = out.toString();
076: out.reset();
077:
078: return ret;
079: }
080:
081: private String pasvList() throws ReplyException {
082: conn.setRequest(new FtpRequest("PRET LIST"));
083:
084: Reply reply;
085: reply = dch.execute(conn);
086: assertEquals(reply.toString(), 200, reply.getCode());
087:
088: conn.setRequest(new FtpRequest("PASV"));
089: reply = dch.execute(conn);
090: assertEquals(reply.toString(), 227, reply.getCode());
091:
092: return list();
093: }
094:
095: private String portList() throws ReplyException {
096: conn.setRequest(new FtpRequest("PORT 127,0,0,1,0,0"));
097: dch.execute(conn);
098:
099: return list();
100: }
101:
102: protected void setUp() throws Exception {
103: BasicConfigurator.configure();
104: dch = (DummyDataConnectionHandler) new DummyDataConnectionHandler()
105: .initialize(null, null);
106:
107: gctx = new DummyGlobalContext();
108: gctx.setFtpConfig(new DummyFtpConfig());
109:
110: conn = new DummyBaseFtpConnection(dch);
111: cm = new DummyConnectionManager();
112: cm.setGlobalContext(gctx);
113: conn.setGlobalConext(gctx);
114: gctx.setConnectionManager(cm);
115: }
116:
117: protected void tearDown() throws Exception {
118: dch = null;
119: }
120:
121: public void testMixedListEqual() throws ReplyException {
122: String list = portList();
123: assertEquals(list, pasvList());
124: assertEquals(list, portList());
125: assertEquals(list, pasvList());
126: testPASVWithoutPRET();
127: assertEquals(list, portList());
128: testPASVWithoutPRET();
129: assertEquals(list, portList());
130: }
131:
132: public void testPasvList() throws ReplyException {
133: pasvList();
134: }
135:
136: public void testPASVWithoutPRET() throws ReplyException {
137: conn.setRequest(new FtpRequest("PASV"));
138:
139: Reply reply = dch.execute(conn);
140: assertBetween(reply.getCode(), 500, 599);
141: }
142:
143: public void testPortList() throws ReplyException {
144: portList();
145: }
146:
147: public class DummyDataConnectionHandler extends
148: DataConnectionHandler {
149: public DummyDataConnectionHandler() {
150: super ();
151: }
152:
153: public ServerSocketFactory getServerSocketFactory(
154: boolean dataChannel) {
155: return new DummyServerSocketFactory(
156: new DummySocketFactory());
157: }
158:
159: public SocketFactory getSocketFactory(boolean dataChannel) {
160: return new DummySocketFactory();
161: }
162: }
163:
164: }
|