1 18 package net.sf.drftpd.master.command.plugins; 19 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.util.Properties ; 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 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 (), null); 46 } catch (IOException e) { 47 throw new RuntimeException (); 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 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 list() { 65 LIST list = (LIST) new LIST().initialize(null, null); 66 67 ByteArrayOutputStream out = 68 conn.getDummySSF().getDummySocket().getByteArrayOutputStream(); 69 70 conn.setRequest(new FtpRequest("LIST")); 71 FtpReply reply = list.execute(conn); 72 String replystr = conn.getDummyOut().getBuffer().toString(); 73 assertEquals(150, Integer.parseInt(replystr.substring(0, 3))); 74 assertEquals(226, reply.getCode()); 75 76 String ret = out.toString(); 77 out.reset(); 78 return ret; 79 } 80 81 private String 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 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 { 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 { 116 dch = null; 117 } 118 119 public void testMixedListEqual() throws UnhandledCommandException { 120 String 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 |