summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchrismair <chrismair@531de8e6-9941-0410-b38b-9a92acbe0330>2008-08-31 17:56:12 +0000
committerchrismair <chrismair@531de8e6-9941-0410-b38b-9a92acbe0330>2008-08-31 17:56:12 +0000
commit0b92a4802c313851d15dad79c9914fd11fb2bdac (patch)
treea91de43aed148b28c9a9c4a82f038626a8fec9ca
parent777c1c842d19df00d2529ccf43e4f4c26cfd39fb (diff)
downloadmockftpserver-0b92a4802c313851d15dad79c9914fd11fb2bdac.tar.gz
When in ASCII mode, convert LF (\n) to CR+LF
git-svn-id: svn://svn.code.sf.net/p/mockftpserver/code@104 531de8e6-9941-0410-b38b-9a92acbe0330
-rw-r--r--MockFtpServer/src/main/groovy/org/mockftpserver/core/session/SessionKeys.groovy1
-rw-r--r--MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/RetrCommandHandler.groovy20
-rw-r--r--MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/TypeCommandHandler.groovy4
-rw-r--r--MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/RetrCommandHandlerTest.groovy22
-rw-r--r--MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/TypeCommandHandlerTest.groovy19
5 files changed, 61 insertions, 5 deletions
diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/core/session/SessionKeys.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/core/session/SessionKeys.groovy
index 9afe8ec..b8aad50 100644
--- a/MockFtpServer/src/main/groovy/org/mockftpserver/core/session/SessionKeys.groovy
+++ b/MockFtpServer/src/main/groovy/org/mockftpserver/core/session/SessionKeys.groovy
@@ -25,5 +25,6 @@ class SessionKeys {
static final CURRENT_DIRECTORY = "currentDirectory"
static final RENAME_FROM = "renameFrom"
static final ACCOUNT_NAME = "accountName"
+ static final ASCII_TYPE = "asciiType"
} \ No newline at end of file
diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/RetrCommandHandler.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/RetrCommandHandler.groovy
index a9c01fb..58c35f5 100644
--- a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/RetrCommandHandler.groovy
+++ b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/RetrCommandHandler.groovy
@@ -18,6 +18,7 @@ package org.mockftpserver.fake.command
import org.mockftpserver.core.command.Command
import org.mockftpserver.core.command.ReplyCodes
import org.mockftpserver.core.session.Session
+import org.mockftpserver.core.session.SessionKeys
import org.mockftpserver.core.util.IoUtil
import org.mockftpserver.fake.command.AbstractFakeCommandHandler
@@ -51,10 +52,29 @@ class RetrCommandHandler extends AbstractFakeCommandHandler {
session.openDataConnection();
input.withStream {
def bytes = IoUtil.readBytes(it)
+ if (isAsciiMode(session)) {
+ bytes = convertLfToCrLf(bytes)
+ }
session.sendData(bytes, bytes.length)
}
session.closeDataConnection();
sendReply(session, ReplyCodes.TRANSFER_DATA_FINAL_OK)
}
+ /**
+ * Within the specified byte array, replace all LF (\n) that are NOT preceded by a CR (\r) into CRLF (\r\n).
+ * @param text - the text to be converted
+ * @return the result of converting LF to CRLF
+ */
+ protected byte[] convertLfToCrLf(byte[] bytes) {
+ def text = new String(bytes)
+ def converted = text.replaceAll(/([^\r]|\A)(\n)/) {global, g1, g2 -> g1 + '\r\n' }
+ return converted.bytes
+ }
+
+ private boolean isAsciiMode(Session session) {
+ // Defaults to true
+ return session.getAttribute(SessionKeys.ASCII_TYPE) != false
+ }
+
} \ No newline at end of file
diff --git a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/TypeCommandHandler.groovy b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/TypeCommandHandler.groovy
index 0765b9e..51aed54 100644
--- a/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/TypeCommandHandler.groovy
+++ b/MockFtpServer/src/main/groovy/org/mockftpserver/fake/command/TypeCommandHandler.groovy
@@ -18,6 +18,7 @@ package org.mockftpserver.fake.command
import org.mockftpserver.core.command.Command
import org.mockftpserver.core.command.ReplyCodes
import org.mockftpserver.core.session.Session
+import org.mockftpserver.core.session.SessionKeys
import org.mockftpserver.fake.command.AbstractFakeCommandHandler
/**
@@ -35,6 +36,9 @@ class TypeCommandHandler extends AbstractFakeCommandHandler {
protected void handle(Command command, Session session) {
verifyLoggedIn(session)
+ def type = command.getRequiredParameter(0)
+ def asciiType = type == 'A'
+ session.setAttribute(SessionKeys.ASCII_TYPE, asciiType)
sendReply(session, ReplyCodes.TYPE_OK, 'type')
}
diff --git a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/RetrCommandHandlerTest.groovy b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/RetrCommandHandlerTest.groovy
index 435edf8..9de3fc0 100644
--- a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/RetrCommandHandlerTest.groovy
+++ b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/RetrCommandHandlerTest.groovy
@@ -19,6 +19,7 @@ import org.mockftpserver.core.command.Command
import org.mockftpserver.core.command.CommandHandler
import org.mockftpserver.core.command.CommandNames
import org.mockftpserver.core.command.ReplyCodes
+import org.mockftpserver.core.session.SessionKeys
import org.mockftpserver.fake.filesystem.FileEntry
/**
@@ -33,7 +34,8 @@ class RetrCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest {
def DIR = "/"
def FILENAME = "file.txt"
def FILE = p(DIR, FILENAME)
- def CONTENTS = "abc"
+ def CONTENTS = "abc\ndef\nghi"
+ def CONTENTS_ASCII = "abc\r\ndef\r\nghi"
void testHandleCommand_MissingPathParameter() {
testHandleCommand_MissingRequiredParameter([])
@@ -41,13 +43,19 @@ class RetrCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest {
void testHandleCommand_AbsolutePath() {
handleCommandAndVerifySendDataReplies([FILE])
+ assertSessionData(CONTENTS_ASCII)
+ }
+
+ void testHandleCommand_AbsolutePath_NonAsciiMode() {
+ session.setAttribute(SessionKeys.ASCII_TYPE, false)
+ handleCommandAndVerifySendDataReplies([FILE])
assertSessionData(CONTENTS)
}
void testHandleCommand_RelativePath() {
setCurrentDirectory(DIR)
handleCommandAndVerifySendDataReplies([FILENAME])
- assertSessionData(CONTENTS)
+ assertSessionData(CONTENTS_ASCII)
}
void testHandleCommand_PathSpecifiesAnExistingDirectory() {
@@ -62,6 +70,16 @@ class RetrCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest {
assertSessionReply(1, ReplyCodes.EXISTING_FILE_ERROR, ERROR_MESSAGE_KEY)
}
+ void testConvertLfToCrLf() {
+ // LF='\n' and CRLF='\r\n'
+ assert commandHandler.convertLfToCrLf('abc'.bytes) == 'abc'.bytes
+ assert commandHandler.convertLfToCrLf('abc\r\ndef'.bytes) == 'abc\r\ndef'.bytes
+ assert commandHandler.convertLfToCrLf('abc\ndef'.bytes) == 'abc\r\ndef'.bytes
+ assert commandHandler.convertLfToCrLf('abc\ndef\nghi'.bytes) == 'abc\r\ndef\r\nghi'.bytes
+ assert commandHandler.convertLfToCrLf('\n'.bytes) == '\r\n'.bytes
+ assert commandHandler.convertLfToCrLf('\r\nabc\n'.bytes) == '\r\nabc\r\n'.bytes
+ }
+
//-------------------------------------------------------------------------
// Helper Methods
//-------------------------------------------------------------------------
diff --git a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/TypeCommandHandlerTest.groovy b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/TypeCommandHandlerTest.groovy
index 2402c81..afffcaa 100644
--- a/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/TypeCommandHandlerTest.groovy
+++ b/MockFtpServer/src/test/groovy/org/mockftpserver/fake/command/TypeCommandHandlerTest.groovy
@@ -19,6 +19,7 @@ import org.mockftpserver.core.command.Command
import org.mockftpserver.core.command.CommandHandler
import org.mockftpserver.core.command.CommandNames
import org.mockftpserver.core.command.ReplyCodes
+import org.mockftpserver.core.session.SessionKeys
/**
* Tests for TestCommandHandler
@@ -29,9 +30,21 @@ import org.mockftpserver.core.command.ReplyCodes
*/
class TypeCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest {
- void testHandleCommand() {
- handleCommand([])
+ void testHandleCommand_Ascii() {
+ handleCommand(['A'])
assertSessionReply(ReplyCodes.TYPE_OK, 'type')
+ assert session.getAttribute(SessionKeys.ASCII_TYPE) == true
+ }
+
+ void testHandleCommand_NonAscii() {
+ handleCommand(['I'])
+ assertSessionReply(ReplyCodes.TYPE_OK, 'type')
+ assert session.getAttribute(SessionKeys.ASCII_TYPE) == false
+ }
+
+ void testHandleCommand_MissingRequiredParameter() {
+ testHandleCommand_MissingRequiredParameter([])
+ assert session.getAttribute(SessionKeys.ASCII_TYPE) == null
}
//-------------------------------------------------------------------------
@@ -43,7 +56,7 @@ class TypeCommandHandlerTest extends AbstractLoginRequiredCommandHandlerTest {
}
Command createValidCommand() {
- return new Command(CommandNames.TYPE, [])
+ return new Command(CommandNames.TYPE, ['A'])
}
void setUp() {