160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/*
260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Copyright 2008 the original author or authors.
360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Licensed under the Apache License, Version 2.0 (the "License");
560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * you may not use this file except in compliance with the License.
660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * You may obtain a copy of the License at
760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *      http://www.apache.org/licenses/LICENSE-2.0
960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
1060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Unless required by applicable law or agreed to in writing, software
1160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * distributed under the License is distributed on an "AS IS" BASIS,
1260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * See the License for the specific language governing permissions and
1460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * limitations under the License.
1560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
1660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairpackage org.mockftpserver.fake.command
1760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
1860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.command.Command
1960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.command.CommandHandler
2060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.command.CommandNames
2160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.command.ReplyCodes
2260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.core.session.SessionKeys
2360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.fake.filesystem.FileEntry
2460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.fake.filesystem.FileSystemException
2560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.fake.filesystem.Permissions
2660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
2760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/**
2860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Tests for RetrCommandHandler
2960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
3060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @version $Revision$ - $Date$
3160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
3260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @author Chris Mair
3360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
3460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairclass RetrCommandHandlerTest extends AbstractFakeCommandHandlerTest {
3560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
3660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    def DIR = "/"
3760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    def FILENAME = "file.txt"
3860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    def FILE = p(DIR, FILENAME)
3960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    def CONTENTS = "abc\ndef\nghi"
4060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    def CONTENTS_ASCII = "abc\r\ndef\r\nghi"
4160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_MissingPathParameter() {
4360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        testHandleCommand_MissingRequiredParameter([])
4460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
4560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_AbsolutePath() {
4760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommandAndVerifySendDataReplies([FILE])
4860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionData(CONTENTS_ASCII)
4960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
5060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_AbsolutePath_NonAsciiMode() {
5260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        session.setAttribute(SessionKeys.ASCII_TYPE, false)
5360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommandAndVerifySendDataReplies([FILE])
5460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionData(CONTENTS)
5560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
5660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_RelativePath() {
5860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        setCurrentDirectory(DIR)
5960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommandAndVerifySendDataReplies([FILENAME])
6060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionData(CONTENTS_ASCII)
6160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
6260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_PathSpecifiesAnExistingDirectory() {
6460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommand([DIR])
6560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.isNotAFile', DIR])
6660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
6760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_PathDoesNotExist() {
6960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def path = FILE + "XXX"
7060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommand([path])
7160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.pathDoesNotExist', path])
7260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
7360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_NoReadAccessToFile() {
7560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.getEntry(FILE).permissions = Permissions.NONE
7660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommand([FILE])
7760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotRead', FILE])
7860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
7960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
8060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_NoExecuteAccessToDirectory() {
8160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.getEntry(DIR).permissions = Permissions.NONE
8260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommand([FILE])
8360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotExecute', DIR])
8460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
8560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
8660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_ThrowsFileSystemException() {
8760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.delete(FILE)
8860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def fileEntry = new BadFileEntry(FILE)
8960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.add(fileEntry)
9060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
9160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommand([FILE])
9260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK)
9360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionReply(1, ReplyCodes.READ_FILE_ERROR, ERROR_MESSAGE_KEY)
9460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
9560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
9660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testConvertLfToCrLf() {
9760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        // LF='\n' and CRLF='\r\n'
9860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert commandHandler.convertLfToCrLf('abc'.bytes) == 'abc'.bytes
9960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert commandHandler.convertLfToCrLf('abc\r\ndef'.bytes) == 'abc\r\ndef'.bytes
10060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert commandHandler.convertLfToCrLf('abc\ndef'.bytes) == 'abc\r\ndef'.bytes
10160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert commandHandler.convertLfToCrLf('abc\ndef\nghi'.bytes) == 'abc\r\ndef\r\nghi'.bytes
10260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert commandHandler.convertLfToCrLf('\n'.bytes) == '\r\n'.bytes
10360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert commandHandler.convertLfToCrLf('\r\nabc\n'.bytes) == '\r\nabc\r\n'.bytes
10460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
10560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
10660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
10760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    // Helper Methods
10860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
10960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
11060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    CommandHandler createCommandHandler() {
11160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        new RetrCommandHandler()
11260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
11360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
11460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    Command createValidCommand() {
11560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        return new Command(CommandNames.RETR, [FILE])
11660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
11760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
11860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void setUp() {
11960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        super.setUp()
12060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        createDirectory(DIR)
12160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        createFile(FILE, CONTENTS)
12260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
12360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
12460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair}
12560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
12660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairclass BadFileEntry extends FileEntry {
12760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
12860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    BadFileEntry(String path) {
12960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        super(path)
13060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
13160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
13260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    InputStream createInputStream() {
13360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        throw new FileSystemException("BAD", AbstractFakeCommandHandlerTest.ERROR_MESSAGE_KEY)
13460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
13560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair}