1bda3441225e0607b5ced8b538123fd7c7a417910chrismair/* 2bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Copyright 2007 the original author or authors. 3bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 4bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Licensed under the Apache License, Version 2.0 (the "License"); 5bda3441225e0607b5ced8b538123fd7c7a417910chrismair * you may not use this file except in compliance with the License. 6bda3441225e0607b5ced8b538123fd7c7a417910chrismair * You may obtain a copy of the License at 7bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 8bda3441225e0607b5ced8b538123fd7c7a417910chrismair * http://www.apache.org/licenses/LICENSE-2.0 9bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 10bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Unless required by applicable law or agreed to in writing, software 11bda3441225e0607b5ced8b538123fd7c7a417910chrismair * distributed under the License is distributed on an "AS IS" BASIS, 12bda3441225e0607b5ced8b538123fd7c7a417910chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13bda3441225e0607b5ced8b538123fd7c7a417910chrismair * See the License for the specific language governing permissions and 14bda3441225e0607b5ced8b538123fd7c7a417910chrismair * limitations under the License. 15bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 16bda3441225e0607b5ced8b538123fd7c7a417910chrismairpackage org.mockftpserver.stub; 17bda3441225e0607b5ced8b538123fd7c7a417910chrismair 18bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.apache.commons.net.ftp.FTP; 19bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.apache.commons.net.ftp.FTPClient; 20bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.apache.commons.net.ftp.FTPFile; 21bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.apache.log4j.Logger; 22bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.CommandHandler; 23bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.CommandNames; 24bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.InvocationRecord; 25bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.SimpleCompositeCommandHandler; 26bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.StaticReplyCommandHandler; 27bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.stub.command.*; 28bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.test.*; 29bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.test.AbstractTestCase; 30bda3441225e0607b5ced8b538123fd7c7a417910chrismair 31bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport java.io.ByteArrayInputStream; 32bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport java.io.ByteArrayOutputStream; 33bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport java.io.IOException; 34bda3441225e0607b5ced8b538123fd7c7a417910chrismair 35bda3441225e0607b5ced8b538123fd7c7a417910chrismair/** 36bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Tests for StubFtpServer using the Apache Jakarta Commons Net FTP client. 37bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 38bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @author Chris Mair 39bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @version $Revision$ - $Date$ 40bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 41bda3441225e0607b5ced8b538123fd7c7a417910chrismairpublic final class StubFtpServerIntegrationTest extends AbstractTestCase implements IntegrationTest { 42bda3441225e0607b5ced8b538123fd7c7a417910chrismair 43bda3441225e0607b5ced8b538123fd7c7a417910chrismair private static final Logger LOG = Logger.getLogger(StubFtpServerIntegrationTest.class); 44bda3441225e0607b5ced8b538123fd7c7a417910chrismair private static final String SERVER = "localhost"; 45bda3441225e0607b5ced8b538123fd7c7a417910chrismair private static final String USERNAME = "user123"; 46bda3441225e0607b5ced8b538123fd7c7a417910chrismair private static final String PASSWORD = "password"; 47bda3441225e0607b5ced8b538123fd7c7a417910chrismair private static final String FILENAME = "abc.txt"; 48bda3441225e0607b5ced8b538123fd7c7a417910chrismair private static final String ASCII_CONTENTS = "abcdef\tghijklmnopqr"; 49bda3441225e0607b5ced8b538123fd7c7a417910chrismair private static final byte[] BINARY_CONTENTS = new byte[256]; 50bda3441225e0607b5ced8b538123fd7c7a417910chrismair 51bda3441225e0607b5ced8b538123fd7c7a417910chrismair private StubFtpServer stubFtpServer; 52bda3441225e0607b5ced8b538123fd7c7a417910chrismair private FTPClient ftpClient; 53bda3441225e0607b5ced8b538123fd7c7a417910chrismair private RetrCommandHandler retrCommandHandler; 54bda3441225e0607b5ced8b538123fd7c7a417910chrismair private StorCommandHandler storCommandHandler; 55bda3441225e0607b5ced8b538123fd7c7a417910chrismair 56bda3441225e0607b5ced8b538123fd7c7a417910chrismair //------------------------------------------------------------------------- 57bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Tests 58bda3441225e0607b5ced8b538123fd7c7a417910chrismair //------------------------------------------------------------------------- 59bda3441225e0607b5ced8b538123fd7c7a417910chrismair 60bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testLogin() throws Exception { 61bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Connect 62bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("Conecting to " + SERVER); 63bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 64bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("connect", 220); 65bda3441225e0607b5ced8b538123fd7c7a417910chrismair 66bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Login 67bda3441225e0607b5ced8b538123fd7c7a417910chrismair String userAndPassword = USERNAME + "/" + PASSWORD; 68bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("Logging in as " + userAndPassword); 69bda3441225e0607b5ced8b538123fd7c7a417910chrismair boolean success = ftpClient.login(USERNAME, PASSWORD); 70bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("Unable to login with " + userAndPassword, success); 71bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("login with " + userAndPassword, 230); 72bda3441225e0607b5ced8b538123fd7c7a417910chrismair 73bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("isStarted", stubFtpServer.isStarted()); 74bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertFalse("isShutdown", stubFtpServer.isShutdown()); 75bda3441225e0607b5ced8b538123fd7c7a417910chrismair 76bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Quit 77bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("Quit"); 78bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClient.quit(); 79bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("quit", 221); 80bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 81bda3441225e0607b5ced8b538123fd7c7a417910chrismair 82bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testAcct() throws Exception { 83bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 84bda3441225e0607b5ced8b538123fd7c7a417910chrismair 85bda3441225e0607b5ced8b538123fd7c7a417910chrismair // ACCT 86bda3441225e0607b5ced8b538123fd7c7a417910chrismair int replyCode = ftpClient.acct("123456"); 87bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("acct", 230, replyCode); 88bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 89bda3441225e0607b5ced8b538123fd7c7a417910chrismair 90bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 91bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Test the stop() method when no session has ever been started 92bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 93bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testStop_NoSessionEverStarted() throws Exception { 94bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("Testing a stop() when no session has ever been started"); 95bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 96bda3441225e0607b5ced8b538123fd7c7a417910chrismair 97bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testHelp() throws Exception { 98bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Modify HELP CommandHandler to return a predefined help message 99bda3441225e0607b5ced8b538123fd7c7a417910chrismair final String HELP = "help message"; 100bda3441225e0607b5ced8b538123fd7c7a417910chrismair HelpCommandHandler helpCommandHandler = (HelpCommandHandler) stubFtpServer.getCommandHandler(CommandNames.HELP); 101bda3441225e0607b5ced8b538123fd7c7a417910chrismair helpCommandHandler.setHelpMessage(HELP); 102bda3441225e0607b5ced8b538123fd7c7a417910chrismair 103bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 104bda3441225e0607b5ced8b538123fd7c7a417910chrismair 105bda3441225e0607b5ced8b538123fd7c7a417910chrismair // HELP 106bda3441225e0607b5ced8b538123fd7c7a417910chrismair String help = ftpClient.listHelp(); 107bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("Wrong response", help.indexOf(HELP) != -1); 108bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("listHelp", 214); 109bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 110bda3441225e0607b5ced8b538123fd7c7a417910chrismair 111bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 112bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Test the LIST and SYST commands. 113bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 114bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testList() throws Exception { 115bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 116bda3441225e0607b5ced8b538123fd7c7a417910chrismair 117bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Set directory listing 118bda3441225e0607b5ced8b538123fd7c7a417910chrismair ListCommandHandler listCommandHandler = (ListCommandHandler) stubFtpServer.getCommandHandler(CommandNames.LIST); 119bda3441225e0607b5ced8b538123fd7c7a417910chrismair listCommandHandler.setDirectoryListing("11-09-01 12:30PM 406348 File2350.log\n" 120bda3441225e0607b5ced8b538123fd7c7a417910chrismair + "11-01-01 1:30PM <DIR> archive"); 121bda3441225e0607b5ced8b538123fd7c7a417910chrismair 122bda3441225e0607b5ced8b538123fd7c7a417910chrismair // LIST 123bda3441225e0607b5ced8b538123fd7c7a417910chrismair FTPFile[] files = ftpClient.listFiles(); 124bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("number of files", 2, files.length); 125bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyFTPFile(files[0], FTPFile.FILE_TYPE, "File2350.log", 406348L); 126bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyFTPFile(files[1], FTPFile.DIRECTORY_TYPE, "archive", 0L); 127bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("list", 226); 128bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 129bda3441225e0607b5ced8b538123fd7c7a417910chrismair 130bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 131bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Test the LIST, PASV and SYST commands, transferring a directory listing in passive mode 132bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 133bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testList_PassiveMode() throws Exception { 134bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 135bda3441225e0607b5ced8b538123fd7c7a417910chrismair 136bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClient.enterLocalPassiveMode(); 137bda3441225e0607b5ced8b538123fd7c7a417910chrismair 138bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Set directory listing 139bda3441225e0607b5ced8b538123fd7c7a417910chrismair ListCommandHandler listCommandHandler = (ListCommandHandler) stubFtpServer.getCommandHandler(CommandNames.LIST); 140bda3441225e0607b5ced8b538123fd7c7a417910chrismair listCommandHandler.setDirectoryListing("11-09-01 12:30PM 406348 File2350.log"); 141bda3441225e0607b5ced8b538123fd7c7a417910chrismair 142bda3441225e0607b5ced8b538123fd7c7a417910chrismair // LIST 143bda3441225e0607b5ced8b538123fd7c7a417910chrismair FTPFile[] files = ftpClient.listFiles(); 144bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("number of files", 1, files.length); 145bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("list", 226); 146bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 147bda3441225e0607b5ced8b538123fd7c7a417910chrismair 148bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testNlst() throws Exception { 149bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 150bda3441225e0607b5ced8b538123fd7c7a417910chrismair 151bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Set directory listing 152bda3441225e0607b5ced8b538123fd7c7a417910chrismair NlstCommandHandler nlstCommandHandler = (NlstCommandHandler) stubFtpServer.getCommandHandler(CommandNames.NLST); 153bda3441225e0607b5ced8b538123fd7c7a417910chrismair nlstCommandHandler.setDirectoryListing("File1.txt\nfile2.data"); 154bda3441225e0607b5ced8b538123fd7c7a417910chrismair 155bda3441225e0607b5ced8b538123fd7c7a417910chrismair // NLST 156bda3441225e0607b5ced8b538123fd7c7a417910chrismair String[] filenames = ftpClient.listNames(); 157bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("number of files", 2, filenames.length); 158bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals(filenames[0], "File1.txt"); 159bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals(filenames[1], "file2.data"); 160bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("listNames", 226); 161bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 162bda3441225e0607b5ced8b538123fd7c7a417910chrismair 163bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 164bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Test printing the current working directory (PWD) 165bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 166bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testPwd() throws Exception { 167bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Modify PWD CommandHandler to return a predefined directory 168bda3441225e0607b5ced8b538123fd7c7a417910chrismair final String DIR = "some/dir"; 169bda3441225e0607b5ced8b538123fd7c7a417910chrismair PwdCommandHandler pwdCommandHandler = (PwdCommandHandler) stubFtpServer.getCommandHandler(CommandNames.PWD); 170bda3441225e0607b5ced8b538123fd7c7a417910chrismair pwdCommandHandler.setDirectory(DIR); 171bda3441225e0607b5ced8b538123fd7c7a417910chrismair 172bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 173bda3441225e0607b5ced8b538123fd7c7a417910chrismair 174bda3441225e0607b5ced8b538123fd7c7a417910chrismair // PWD 175bda3441225e0607b5ced8b538123fd7c7a417910chrismair String dir = ftpClient.printWorkingDirectory(); 176bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("Unable to PWD", DIR, dir); 177bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("printWorkingDirectory", 257); 178bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 179bda3441225e0607b5ced8b538123fd7c7a417910chrismair 180bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testStat() throws Exception { 181bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Modify Stat CommandHandler to return predefined text 182bda3441225e0607b5ced8b538123fd7c7a417910chrismair final String STATUS = "some information 123"; 183bda3441225e0607b5ced8b538123fd7c7a417910chrismair StatCommandHandler statCommandHandler = (StatCommandHandler) stubFtpServer.getCommandHandler(CommandNames.STAT); 184bda3441225e0607b5ced8b538123fd7c7a417910chrismair statCommandHandler.setStatus(STATUS); 185bda3441225e0607b5ced8b538123fd7c7a417910chrismair 186bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 187bda3441225e0607b5ced8b538123fd7c7a417910chrismair 188bda3441225e0607b5ced8b538123fd7c7a417910chrismair // STAT 189bda3441225e0607b5ced8b538123fd7c7a417910chrismair String status = ftpClient.getStatus(); 190bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("STAT reply", "211 " + STATUS + ".", status.trim()); 191bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("getStatus", 211); 192bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 193bda3441225e0607b5ced8b538123fd7c7a417910chrismair 194bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 195bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Test getting the status (STAT), when the reply text contains multiple lines 196bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 197bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testStat_MultilineReplyText() throws Exception { 198bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Modify Stat CommandHandler to return predefined text 199bda3441225e0607b5ced8b538123fd7c7a417910chrismair final String STATUS = "System name: abc.def\nVersion 3.5.7\nNumber of failed logins: 2"; 200bda3441225e0607b5ced8b538123fd7c7a417910chrismair final String FORMATTED_REPLY_STATUS = "211-System name: abc.def\r\nVersion 3.5.7\r\n211 Number of failed logins: 2."; 201bda3441225e0607b5ced8b538123fd7c7a417910chrismair StatCommandHandler statCommandHandler = (StatCommandHandler) stubFtpServer.getCommandHandler(CommandNames.STAT); 202bda3441225e0607b5ced8b538123fd7c7a417910chrismair statCommandHandler.setStatus(STATUS); 203bda3441225e0607b5ced8b538123fd7c7a417910chrismair 204bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 205bda3441225e0607b5ced8b538123fd7c7a417910chrismair 206bda3441225e0607b5ced8b538123fd7c7a417910chrismair // STAT 207bda3441225e0607b5ced8b538123fd7c7a417910chrismair String status = ftpClient.getStatus(); 208bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("STAT reply", FORMATTED_REPLY_STATUS, status.trim()); 209bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("getStatus", 211); 210bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 211bda3441225e0607b5ced8b538123fd7c7a417910chrismair 212bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testSyst() throws Exception { 213bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 214bda3441225e0607b5ced8b538123fd7c7a417910chrismair 215bda3441225e0607b5ced8b538123fd7c7a417910chrismair // SYST 216bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("getSystemName()", "\"WINDOWS\" system type.", ftpClient.getSystemName()); 217bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("syst", 215); 218bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 219bda3441225e0607b5ced8b538123fd7c7a417910chrismair 220bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testCwd() throws Exception { 221bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Connect 222bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("Conecting to " + SERVER); 223bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 224bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("connect", 220); 225bda3441225e0607b5ced8b538123fd7c7a417910chrismair 226bda3441225e0607b5ced8b538123fd7c7a417910chrismair // CWD 227bda3441225e0607b5ced8b538123fd7c7a417910chrismair boolean success = ftpClient.changeWorkingDirectory("dir1/dir2"); 228bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("Unable to CWD", success); 229bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("changeWorkingDirectory", 250); 230bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 231bda3441225e0607b5ced8b538123fd7c7a417910chrismair 232bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 233bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Test changing the current working directory (CWD), when it causes a remote error 234bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 235bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testCwd_Error() throws Exception { 236bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Override CWD CommandHandler to return error reply code 237bda3441225e0607b5ced8b538123fd7c7a417910chrismair final int REPLY_CODE = 500; 238bda3441225e0607b5ced8b538123fd7c7a417910chrismair StaticReplyCommandHandler cwdCommandHandler = new StaticReplyCommandHandler(REPLY_CODE); 239bda3441225e0607b5ced8b538123fd7c7a417910chrismair stubFtpServer.setCommandHandler("CWD", cwdCommandHandler); 240bda3441225e0607b5ced8b538123fd7c7a417910chrismair 241bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 242bda3441225e0607b5ced8b538123fd7c7a417910chrismair 243bda3441225e0607b5ced8b538123fd7c7a417910chrismair // CWD 244bda3441225e0607b5ced8b538123fd7c7a417910chrismair boolean success = ftpClient.changeWorkingDirectory("dir1/dir2"); 245bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertFalse("Expected failure", success); 246bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("changeWorkingDirectory", REPLY_CODE); 247bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 248bda3441225e0607b5ced8b538123fd7c7a417910chrismair 249bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testCdup() throws Exception { 250bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 251bda3441225e0607b5ced8b538123fd7c7a417910chrismair 252bda3441225e0607b5ced8b538123fd7c7a417910chrismair // CDUP 253bda3441225e0607b5ced8b538123fd7c7a417910chrismair boolean success = ftpClient.changeToParentDirectory(); 254bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("Unable to CDUP", success); 255bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("changeToParentDirectory", 200); 256bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 257bda3441225e0607b5ced8b538123fd7c7a417910chrismair 258bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testDele() throws Exception { 259bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 260bda3441225e0607b5ced8b538123fd7c7a417910chrismair 261bda3441225e0607b5ced8b538123fd7c7a417910chrismair // DELE 262bda3441225e0607b5ced8b538123fd7c7a417910chrismair boolean success = ftpClient.deleteFile(FILENAME); 263bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("Unable to DELE", success); 264bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("deleteFile", 250); 265bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 266bda3441225e0607b5ced8b538123fd7c7a417910chrismair 267bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testEprt() throws Exception { 268bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 269bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClient.sendCommand("EPRT", "|2|1080::8:800:200C:417A|5282|"); 270bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("EPRT", 200); 271bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 272bda3441225e0607b5ced8b538123fd7c7a417910chrismair 273bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testEpsv() throws Exception { 274bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 275bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClient.sendCommand("EPSV"); 276bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("EPSV", 229); 277bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 278bda3441225e0607b5ced8b538123fd7c7a417910chrismair 279bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testFeat_UseStaticReplyCommandHandler() throws IOException { 280bda3441225e0607b5ced8b538123fd7c7a417910chrismair // The FEAT command is not supported out of the box 281bda3441225e0607b5ced8b538123fd7c7a417910chrismair final String FEAT_TEXT = "Extensions supported:\n" + 282bda3441225e0607b5ced8b538123fd7c7a417910chrismair "MLST size*;create;modify*;perm;media-type\n" + 283bda3441225e0607b5ced8b538123fd7c7a417910chrismair "SIZE\n" + 284bda3441225e0607b5ced8b538123fd7c7a417910chrismair "COMPRESSION\n" + 285bda3441225e0607b5ced8b538123fd7c7a417910chrismair "END"; 286bda3441225e0607b5ced8b538123fd7c7a417910chrismair StaticReplyCommandHandler featCommandHandler = new StaticReplyCommandHandler(211, FEAT_TEXT); 287bda3441225e0607b5ced8b538123fd7c7a417910chrismair stubFtpServer.setCommandHandler("FEAT", featCommandHandler); 288bda3441225e0607b5ced8b538123fd7c7a417910chrismair 289bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 290bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals(ftpClient.sendCommand("FEAT"), 211); 291bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info(ftpClient.getReplyString()); 292bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 293bda3441225e0607b5ced8b538123fd7c7a417910chrismair 294bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testMkd() throws Exception { 295bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 296bda3441225e0607b5ced8b538123fd7c7a417910chrismair 297bda3441225e0607b5ced8b538123fd7c7a417910chrismair // MKD 298bda3441225e0607b5ced8b538123fd7c7a417910chrismair boolean success = ftpClient.makeDirectory("dir1/dir2"); 299bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("Unable to CWD", success); 300bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("makeDirectory", 257); 301bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 302bda3441225e0607b5ced8b538123fd7c7a417910chrismair 303bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testNoop() throws Exception { 304bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 305bda3441225e0607b5ced8b538123fd7c7a417910chrismair 306bda3441225e0607b5ced8b538123fd7c7a417910chrismair // NOOP 307bda3441225e0607b5ced8b538123fd7c7a417910chrismair boolean success = ftpClient.sendNoOp(); 308bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("Unable to NOOP", success); 309bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("NOOP", 200); 310bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 311bda3441225e0607b5ced8b538123fd7c7a417910chrismair 312bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testRest() throws Exception { 313bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 314bda3441225e0607b5ced8b538123fd7c7a417910chrismair 315bda3441225e0607b5ced8b538123fd7c7a417910chrismair // REST 316bda3441225e0607b5ced8b538123fd7c7a417910chrismair int replyCode = ftpClient.rest("marker"); 317bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("Unable to REST", 350, replyCode); 318bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 319bda3441225e0607b5ced8b538123fd7c7a417910chrismair 320bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testRmd() throws Exception { 321bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 322bda3441225e0607b5ced8b538123fd7c7a417910chrismair 323bda3441225e0607b5ced8b538123fd7c7a417910chrismair // RMD 324bda3441225e0607b5ced8b538123fd7c7a417910chrismair boolean success = ftpClient.removeDirectory("dir1/dir2"); 325bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("Unable to RMD", success); 326bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("removeDirectory", 250); 327bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 328bda3441225e0607b5ced8b538123fd7c7a417910chrismair 329bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testRename() throws Exception { 330bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 331bda3441225e0607b5ced8b538123fd7c7a417910chrismair 332bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Rename (RNFR, RNTO) 333bda3441225e0607b5ced8b538123fd7c7a417910chrismair boolean success = ftpClient.rename(FILENAME, "new_" + FILENAME); 334bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("Unable to RENAME", success); 335bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("rename", 250); 336bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 337bda3441225e0607b5ced8b538123fd7c7a417910chrismair 338bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testAllo() throws Exception { 339bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 340bda3441225e0607b5ced8b538123fd7c7a417910chrismair 341bda3441225e0607b5ced8b538123fd7c7a417910chrismair // ALLO 342bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("ALLO", ftpClient.allocate(1024)); 343bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("ALLO with recordSize", ftpClient.allocate(1024, 64)); 344bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 345bda3441225e0607b5ced8b538123fd7c7a417910chrismair 346bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 347bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Test GET and PUT of ASCII files 348bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 349bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testTransferAsciiFile() throws Exception { 350bda3441225e0607b5ced8b538123fd7c7a417910chrismair retrCommandHandler.setFileContents(ASCII_CONTENTS); 351bda3441225e0607b5ced8b538123fd7c7a417910chrismair 352bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 353bda3441225e0607b5ced8b538123fd7c7a417910chrismair 354bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Get File 355bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("Get File for remotePath [" + FILENAME + "]"); 356bda3441225e0607b5ced8b538123fd7c7a417910chrismair ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 357bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue(ftpClient.retrieveFile(FILENAME, outputStream)); 358bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("File contents=[" + outputStream.toString()); 359bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("File contents", ASCII_CONTENTS, outputStream.toString()); 360bda3441225e0607b5ced8b538123fd7c7a417910chrismair 361bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Put File 362bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("Put File for local path [" + FILENAME + "]"); 363bda3441225e0607b5ced8b538123fd7c7a417910chrismair ByteArrayInputStream inputStream = new ByteArrayInputStream(ASCII_CONTENTS.getBytes()); 364bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue(ftpClient.storeFile(FILENAME, inputStream)); 365bda3441225e0607b5ced8b538123fd7c7a417910chrismair InvocationRecord invocationRecord = storCommandHandler.getInvocation(0); 366bda3441225e0607b5ced8b538123fd7c7a417910chrismair byte[] contents = (byte[]) invocationRecord.getObject(StorCommandHandler.FILE_CONTENTS_KEY); 367bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("File contents=[" + contents + "]"); 368bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("File contents", ASCII_CONTENTS.getBytes(), contents); 369bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 370bda3441225e0607b5ced8b538123fd7c7a417910chrismair 371bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 372bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Test GET and PUT of binary files 373bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 374bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testTransferBinaryFiles() throws Exception { 375bda3441225e0607b5ced8b538123fd7c7a417910chrismair retrCommandHandler.setFileContents(BINARY_CONTENTS); 376bda3441225e0607b5ced8b538123fd7c7a417910chrismair 377bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 378bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 379bda3441225e0607b5ced8b538123fd7c7a417910chrismair 380bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Get File 381bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("Get File for remotePath [" + FILENAME + "]"); 382bda3441225e0607b5ced8b538123fd7c7a417910chrismair ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 383bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("GET", ftpClient.retrieveFile(FILENAME, outputStream)); 384bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("GET File length=" + outputStream.size()); 385bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("File contents", BINARY_CONTENTS, outputStream.toByteArray()); 386bda3441225e0607b5ced8b538123fd7c7a417910chrismair 387bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Put File 388bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("Put File for local path [" + FILENAME + "]"); 389bda3441225e0607b5ced8b538123fd7c7a417910chrismair ByteArrayInputStream inputStream = new ByteArrayInputStream(BINARY_CONTENTS); 390bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("PUT", ftpClient.storeFile(FILENAME, inputStream)); 391bda3441225e0607b5ced8b538123fd7c7a417910chrismair InvocationRecord invocationRecord = storCommandHandler.getInvocation(0); 392bda3441225e0607b5ced8b538123fd7c7a417910chrismair byte[] contents = (byte[]) invocationRecord.getObject(StorCommandHandler.FILE_CONTENTS_KEY); 393bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("PUT File length=" + contents.length); 394bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("File contents", BINARY_CONTENTS, contents); 395bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 396bda3441225e0607b5ced8b538123fd7c7a417910chrismair 397bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testStou() throws Exception { 398bda3441225e0607b5ced8b538123fd7c7a417910chrismair StouCommandHandler stouCommandHandler = (StouCommandHandler) stubFtpServer.getCommandHandler(CommandNames.STOU); 399bda3441225e0607b5ced8b538123fd7c7a417910chrismair stouCommandHandler.setFilename(FILENAME); 400bda3441225e0607b5ced8b538123fd7c7a417910chrismair 401bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 402bda3441225e0607b5ced8b538123fd7c7a417910chrismair 403bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Stor a File (STOU) 404bda3441225e0607b5ced8b538123fd7c7a417910chrismair ByteArrayInputStream inputStream = new ByteArrayInputStream(ASCII_CONTENTS.getBytes()); 405bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue(ftpClient.storeUniqueFile(FILENAME, inputStream)); 406bda3441225e0607b5ced8b538123fd7c7a417910chrismair InvocationRecord invocationRecord = stouCommandHandler.getInvocation(0); 407bda3441225e0607b5ced8b538123fd7c7a417910chrismair byte[] contents = (byte[]) invocationRecord.getObject(StorCommandHandler.FILE_CONTENTS_KEY); 408bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("File contents=[" + contents + "]"); 409bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("File contents", ASCII_CONTENTS.getBytes(), contents); 410bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 411bda3441225e0607b5ced8b538123fd7c7a417910chrismair 412bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testAppe() throws Exception { 413bda3441225e0607b5ced8b538123fd7c7a417910chrismair AppeCommandHandler appeCommandHandler = (AppeCommandHandler) stubFtpServer.getCommandHandler(CommandNames.APPE); 414bda3441225e0607b5ced8b538123fd7c7a417910chrismair 415bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 416bda3441225e0607b5ced8b538123fd7c7a417910chrismair 417bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Append a File (APPE) 418bda3441225e0607b5ced8b538123fd7c7a417910chrismair ByteArrayInputStream inputStream = new ByteArrayInputStream(ASCII_CONTENTS.getBytes()); 419bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue(ftpClient.appendFile(FILENAME, inputStream)); 420bda3441225e0607b5ced8b538123fd7c7a417910chrismair InvocationRecord invocationRecord = appeCommandHandler.getInvocation(0); 421bda3441225e0607b5ced8b538123fd7c7a417910chrismair byte[] contents = (byte[]) invocationRecord.getObject(AppeCommandHandler.FILE_CONTENTS_KEY); 422bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("File contents=[" + contents + "]"); 423bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("File contents", ASCII_CONTENTS.getBytes(), contents); 424bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 425bda3441225e0607b5ced8b538123fd7c7a417910chrismair 426bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testAbor() throws Exception { 427bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 428bda3441225e0607b5ced8b538123fd7c7a417910chrismair 429bda3441225e0607b5ced8b538123fd7c7a417910chrismair // ABOR 430bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("ABOR", ftpClient.abort()); 431bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 432bda3441225e0607b5ced8b538123fd7c7a417910chrismair 433bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testPasv() throws Exception { 434bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 435bda3441225e0607b5ced8b538123fd7c7a417910chrismair 436bda3441225e0607b5ced8b538123fd7c7a417910chrismair // PASV 437bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClient.enterLocalPassiveMode(); 438bda3441225e0607b5ced8b538123fd7c7a417910chrismair // no reply code; the PASV command is sent only when the data connection is opened 439bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 440bda3441225e0607b5ced8b538123fd7c7a417910chrismair 441bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testMode() throws Exception { 442bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 443bda3441225e0607b5ced8b538123fd7c7a417910chrismair 444bda3441225e0607b5ced8b538123fd7c7a417910chrismair // MODE 445bda3441225e0607b5ced8b538123fd7c7a417910chrismair boolean success = ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE); 446bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("Unable to MODE", success); 447bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("setFileTransferMode", 200); 448bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 449bda3441225e0607b5ced8b538123fd7c7a417910chrismair 450bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testStru() throws Exception { 451bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 452bda3441225e0607b5ced8b538123fd7c7a417910chrismair 453bda3441225e0607b5ced8b538123fd7c7a417910chrismair // STRU 454bda3441225e0607b5ced8b538123fd7c7a417910chrismair boolean success = ftpClient.setFileStructure(FTP.FILE_STRUCTURE); 455bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("Unable to STRU", success); 456bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("setFileStructure", 200); 457bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 458bda3441225e0607b5ced8b538123fd7c7a417910chrismair 459bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testSimpleCompositeCommandHandler() throws Exception { 460bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Replace CWD CommandHandler with a SimpleCompositeCommandHandler 461bda3441225e0607b5ced8b538123fd7c7a417910chrismair CommandHandler commandHandler1 = new StaticReplyCommandHandler(500); 462bda3441225e0607b5ced8b538123fd7c7a417910chrismair CommandHandler commandHandler2 = new CwdCommandHandler(); 463bda3441225e0607b5ced8b538123fd7c7a417910chrismair SimpleCompositeCommandHandler simpleCompositeCommandHandler = new SimpleCompositeCommandHandler(); 464bda3441225e0607b5ced8b538123fd7c7a417910chrismair simpleCompositeCommandHandler.addCommandHandler(commandHandler1); 465bda3441225e0607b5ced8b538123fd7c7a417910chrismair simpleCompositeCommandHandler.addCommandHandler(commandHandler2); 466bda3441225e0607b5ced8b538123fd7c7a417910chrismair stubFtpServer.setCommandHandler("CWD", simpleCompositeCommandHandler); 467bda3441225e0607b5ced8b538123fd7c7a417910chrismair 468bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Connect 469bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 470bda3441225e0607b5ced8b538123fd7c7a417910chrismair 471bda3441225e0607b5ced8b538123fd7c7a417910chrismair // CWD 472bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertFalse("first", ftpClient.changeWorkingDirectory("dir1/dir2")); 473bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("first", ftpClient.changeWorkingDirectory("dir1/dir2")); 474bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 475bda3441225e0607b5ced8b538123fd7c7a417910chrismair 476bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testSite() throws Exception { 477bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 478bda3441225e0607b5ced8b538123fd7c7a417910chrismair 479bda3441225e0607b5ced8b538123fd7c7a417910chrismair // SITE 480bda3441225e0607b5ced8b538123fd7c7a417910chrismair int replyCode = ftpClient.site("parameters,1,2,3"); 481bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("SITE", 200, replyCode); 482bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 483bda3441225e0607b5ced8b538123fd7c7a417910chrismair 484bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testSmnt() throws Exception { 485bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 486bda3441225e0607b5ced8b538123fd7c7a417910chrismair 487bda3441225e0607b5ced8b538123fd7c7a417910chrismair // SMNT 488bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertTrue("SMNT", ftpClient.structureMount("dir1/dir2")); 489bda3441225e0607b5ced8b538123fd7c7a417910chrismair verifyReplyCode("structureMount", 250); 490bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 491bda3441225e0607b5ced8b538123fd7c7a417910chrismair 492bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testRein() throws Exception { 493bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 494bda3441225e0607b5ced8b538123fd7c7a417910chrismair 495bda3441225e0607b5ced8b538123fd7c7a417910chrismair // REIN 496bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("REIN", 220, ftpClient.rein()); 497bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 498bda3441225e0607b5ced8b538123fd7c7a417910chrismair 499bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 500bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Test that command names in lowercase or mixed upper/lower case are accepted 501bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 502bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testCommandNamesInLowerOrMixedCase() throws Exception { 503bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 504bda3441225e0607b5ced8b538123fd7c7a417910chrismair 505bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("rein", 220, ftpClient.sendCommand("rein")); 506bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("rEIn", 220, ftpClient.sendCommand("rEIn")); 507bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("reiN", 220, ftpClient.sendCommand("reiN")); 508bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("Rein", 220, ftpClient.sendCommand("Rein")); 509bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 510bda3441225e0607b5ced8b538123fd7c7a417910chrismair 511bda3441225e0607b5ced8b538123fd7c7a417910chrismair public void testUnrecognizedCommand() throws Exception { 512bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClientConnect(); 513bda3441225e0607b5ced8b538123fd7c7a417910chrismair 514bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("Unrecognized:XXXX", 502, ftpClient.sendCommand("XXXX")); 515bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 516bda3441225e0607b5ced8b538123fd7c7a417910chrismair 517bda3441225e0607b5ced8b538123fd7c7a417910chrismair // ------------------------------------------------------------------------- 518bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Test setup and tear-down 519bda3441225e0607b5ced8b538123fd7c7a417910chrismair // ------------------------------------------------------------------------- 520bda3441225e0607b5ced8b538123fd7c7a417910chrismair 521bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 522bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Perform initialization before each test 523bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 524bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @see org.mockftpserver.test.AbstractTestCase#setUp() 525bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 526bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected void setUp() throws Exception { 527bda3441225e0607b5ced8b538123fd7c7a417910chrismair super.setUp(); 528bda3441225e0607b5ced8b538123fd7c7a417910chrismair 529bda3441225e0607b5ced8b538123fd7c7a417910chrismair for (int i = 0; i < BINARY_CONTENTS.length; i++) { 530bda3441225e0607b5ced8b538123fd7c7a417910chrismair BINARY_CONTENTS[i] = (byte) i; 531bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 532bda3441225e0607b5ced8b538123fd7c7a417910chrismair 533bda3441225e0607b5ced8b538123fd7c7a417910chrismair stubFtpServer = new StubFtpServer(); 534bda3441225e0607b5ced8b538123fd7c7a417910chrismair stubFtpServer.setServerControlPort(PortTestUtil.getFtpServerControlPort()); 535bda3441225e0607b5ced8b538123fd7c7a417910chrismair stubFtpServer.start(); 536bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClient = new FTPClient(); 537bda3441225e0607b5ced8b538123fd7c7a417910chrismair retrCommandHandler = (RetrCommandHandler) stubFtpServer.getCommandHandler(CommandNames.RETR); 538bda3441225e0607b5ced8b538123fd7c7a417910chrismair storCommandHandler = (StorCommandHandler) stubFtpServer.getCommandHandler(CommandNames.STOR); 539bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 540bda3441225e0607b5ced8b538123fd7c7a417910chrismair 541bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 542bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Perform cleanup after each test 543bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 544bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @see org.mockftpserver.test.AbstractTestCase#tearDown() 545bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 546bda3441225e0607b5ced8b538123fd7c7a417910chrismair protected void tearDown() throws Exception { 547bda3441225e0607b5ced8b538123fd7c7a417910chrismair super.tearDown(); 548bda3441225e0607b5ced8b538123fd7c7a417910chrismair stubFtpServer.stop(); 549bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 550bda3441225e0607b5ced8b538123fd7c7a417910chrismair 551bda3441225e0607b5ced8b538123fd7c7a417910chrismair // ------------------------------------------------------------------------- 552bda3441225e0607b5ced8b538123fd7c7a417910chrismair // Internal Helper Methods 553bda3441225e0607b5ced8b538123fd7c7a417910chrismair // ------------------------------------------------------------------------- 554bda3441225e0607b5ced8b538123fd7c7a417910chrismair 555bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 556bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Connect to the server from the FTPClient 557bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 558bda3441225e0607b5ced8b538123fd7c7a417910chrismair private void ftpClientConnect() throws IOException { 559bda3441225e0607b5ced8b538123fd7c7a417910chrismair ftpClient.connect(SERVER, PortTestUtil.getFtpServerControlPort()); 560bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 561bda3441225e0607b5ced8b538123fd7c7a417910chrismair 562bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 563bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Assert that the FtpClient reply code is equal to the expected value 564bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 565bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param operation - the description of the operation performed; used in the error message 566bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param expectedReplyCode - the expected FtpClient reply code 567bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 568bda3441225e0607b5ced8b538123fd7c7a417910chrismair private void verifyReplyCode(String operation, int expectedReplyCode) { 569bda3441225e0607b5ced8b538123fd7c7a417910chrismair int replyCode = ftpClient.getReplyCode(); 570bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info("Reply: operation=\"" + operation + "\" replyCode=" + replyCode); 571bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("Unexpected replyCode for " + operation, expectedReplyCode, replyCode); 572bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 573bda3441225e0607b5ced8b538123fd7c7a417910chrismair 574bda3441225e0607b5ced8b538123fd7c7a417910chrismair /** 575bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Verify that the FTPFile has the specified properties 576bda3441225e0607b5ced8b538123fd7c7a417910chrismair * 577bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param ftpFile - the FTPFile to verify 578bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param type - the expected file type 579bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param name - the expected file name 580bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @param size - the expected file size (will be zero for a directory) 581bda3441225e0607b5ced8b538123fd7c7a417910chrismair */ 582bda3441225e0607b5ced8b538123fd7c7a417910chrismair private void verifyFTPFile(FTPFile ftpFile, int type, String name, long size) { 583bda3441225e0607b5ced8b538123fd7c7a417910chrismair LOG.info(ftpFile); 584bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("type: " + ftpFile, type, ftpFile.getType()); 585bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("name: " + ftpFile, name, ftpFile.getName()); 586bda3441225e0607b5ced8b538123fd7c7a417910chrismair assertEquals("size: " + ftpFile, size, ftpFile.getSize()); 587bda3441225e0607b5ced8b538123fd7c7a417910chrismair } 588bda3441225e0607b5ced8b538123fd7c7a417910chrismair 589bda3441225e0607b5ced8b538123fd7c7a417910chrismair} 590