RetrCommandHandlerTest.groovy revision dc5be681ed2232085080c0af73613491c7a764ab
1/*
2 * Copyright 2008 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.fake.command
17
18import org.mockftpserver.core.command.Command
19import org.mockftpserver.core.command.CommandHandler
20import org.mockftpserver.core.command.CommandNames
21import org.mockftpserver.core.command.ReplyCodes
22import org.mockftpserver.core.session.SessionKeys
23import org.mockftpserver.fake.filesystem.FileEntry
24import org.mockftpserver.fake.filesystem.FileSystemException
25import org.mockftpserver.fake.filesystem.Permissions
26
27/**
28 * Tests for RetrCommandHandler
29 *
30 * @version $Revision$ - $Date$
31 *
32 * @author Chris Mair
33 */
34class RetrCommandHandlerTest extends AbstractFakeCommandHandlerTest {
35
36    def DIR = "/"
37    def FILENAME = "file.txt"
38    def FILE = p(DIR, FILENAME)
39    def CONTENTS = "abc\ndef\nghi"
40    def CONTENTS_ASCII = "abc\r\ndef\r\nghi"
41
42    void testHandleCommand_MissingPathParameter() {
43        testHandleCommand_MissingRequiredParameter([])
44    }
45
46    void testHandleCommand_AbsolutePath() {
47        handleCommandAndVerifySendDataReplies([FILE])
48        assertSessionData(CONTENTS_ASCII)
49    }
50
51    void testHandleCommand_AbsolutePath_NonAsciiMode() {
52        session.setAttribute(SessionKeys.ASCII_TYPE, false)
53        handleCommandAndVerifySendDataReplies([FILE])
54        assertSessionData(CONTENTS)
55    }
56
57    void testHandleCommand_RelativePath() {
58        setCurrentDirectory(DIR)
59        handleCommandAndVerifySendDataReplies([FILENAME])
60        assertSessionData(CONTENTS_ASCII)
61    }
62
63    void testHandleCommand_PathSpecifiesAnExistingDirectory() {
64        commandHandler.handleCommand(createCommand([DIR]), session)
65        assertSessionReply(ReplyCodes.EXISTING_FILE_ERROR, ['filesystem.isNotAFile', DIR])
66    }
67
68    void testHandleCommand_PathDoesNotExist() {
69        def path = FILE + "XXX"
70        commandHandler.handleCommand(createCommand([path]), session)
71        assertSessionReply(ReplyCodes.EXISTING_FILE_ERROR, ['filesystem.pathDoesNotExist', path])
72    }
73
74    void testHandleCommand_NoReadAccessToFile() {
75        fileSystem.getEntry(FILE).permissions = Permissions.NONE
76        commandHandler.handleCommand(createCommand([FILE]), session)
77        assertSessionReply(ReplyCodes.READ_ACCESS_ERROR, ['filesystem.cannotRead', FILE])
78    }
79
80    void testHandleCommand_NoExecuteAccessToDirectory() {
81        fileSystem.getEntry(DIR).permissions = Permissions.NONE
82        commandHandler.handleCommand(createCommand([FILE]), session)
83        assertSessionReply(ReplyCodes.READ_ACCESS_ERROR, ['filesystem.cannotExecute', DIR])
84    }
85
86    void testHandleCommand_ThrowsFileSystemException() {
87        fileSystem.delete(FILE)
88        def fileEntry = new BadFileEntry(FILE)
89        fileSystem.add(fileEntry)
90
91        handleCommand([FILE])
92        assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK)
93        assertSessionReply(1, ReplyCodes.EXISTING_FILE_ERROR, ERROR_MESSAGE_KEY)
94    }
95
96    void testConvertLfToCrLf() {
97        // LF='\n' and CRLF='\r\n'
98        assert commandHandler.convertLfToCrLf('abc'.bytes) == 'abc'.bytes
99        assert commandHandler.convertLfToCrLf('abc\r\ndef'.bytes) == 'abc\r\ndef'.bytes
100        assert commandHandler.convertLfToCrLf('abc\ndef'.bytes) == 'abc\r\ndef'.bytes
101        assert commandHandler.convertLfToCrLf('abc\ndef\nghi'.bytes) == 'abc\r\ndef\r\nghi'.bytes
102        assert commandHandler.convertLfToCrLf('\n'.bytes) == '\r\n'.bytes
103        assert commandHandler.convertLfToCrLf('\r\nabc\n'.bytes) == '\r\nabc\r\n'.bytes
104    }
105
106    //-------------------------------------------------------------------------
107    // Helper Methods
108    //-------------------------------------------------------------------------
109
110    CommandHandler createCommandHandler() {
111        new RetrCommandHandler()
112    }
113
114    Command createValidCommand() {
115        return new Command(CommandNames.RETR, [FILE])
116    }
117
118    void setUp() {
119        super.setUp()
120        createDirectory(DIR)
121        createFile(FILE, CONTENTS)
122    }
123
124}
125
126class BadFileEntry extends FileEntry {
127
128    BadFileEntry(String path) {
129        super(path)
130    }
131
132    InputStream createInputStream() {
133        throw new FileSystemException("BAD", AbstractFakeCommandHandlerTest.ERROR_MESSAGE_KEY)
134    }
135}