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.FileSystemException
2460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairimport org.mockftpserver.fake.filesystem.Permissions
2560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
2660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/**
2760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Tests for NlstCommandHandler
2860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
2960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @version $Revision$ - $Date$
3060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair *
3160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @author Chris Mair
3260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */
3360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairclass NlstCommandHandlerTest extends AbstractFakeCommandHandlerTest {
3460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
3560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    def DIR = "/usr"
3660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
3760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_SingleFile() {
3860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        createFile("/usr/f1.txt")
3960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommandAndVerifySendDataReplies([DIR])
4060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionData("f1.txt")
4160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
4260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
4360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_FilesAndDirectories() {
4460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        createFile("/usr/f1.txt")
4560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        createDirectory("/usr/OtherFiles")
4660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        createFile("/usr/f2.txt")
4760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        createDirectory("/usr/Archive")
4860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommandAndVerifySendDataReplies([DIR])
4960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def EXPECTED = ["f1.txt", "OtherFiles", "f2.txt", "Archive"] as Set
5160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        def actualLines = session.sentData[0].tokenize(endOfLine()) as Set
5260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        LOG.info("actualLines=$actualLines")
5360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assert actualLines == EXPECTED
5460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
5560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
5660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_NoPath_UseCurrentDirectory() {
5760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        createFile("/usr/f1.txt")
5860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        session.setAttribute(SessionKeys.CURRENT_DIRECTORY, DIR)
5960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommandAndVerifySendDataReplies([])
6060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionData("f1.txt")
6160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
6260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_EmptyDirectory() {
6460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommandAndVerifySendDataReplies([DIR])
6560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionData("")
6660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
6760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
6860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_PathSpecifiesAFile() {
6960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        createFile("/usr/f1.txt")
7060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommandAndVerifySendDataReplies(["/usr/f1.txt"])
7160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionData("f1.txt")
7260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
7360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_PathDoesNotExist() {
7560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommandAndVerifySendDataReplies(["/DoesNotExist"])
7660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionData("")
7760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
7860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
7960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_NoReadAccessToDirectory() {
8060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.getEntry(DIR).permissions = new Permissions('-wx-wx-wx')
8160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommand([DIR])
8260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK)
8360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionReply(1, ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotRead', DIR])
8460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
8560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
8660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void testHandleCommand_ListNamesThrowsException() {
8760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        fileSystem.listNamesMethodException = new FileSystemException("bad", ERROR_MESSAGE_KEY)
8860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        handleCommand([DIR])
8960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionReply(0, ReplyCodes.TRANSFER_DATA_INITIAL_OK)
9060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        assertSessionReply(1, ReplyCodes.SYSTEM_ERROR, ERROR_MESSAGE_KEY)
9160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
9260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
9360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
9460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    // Helper Methods
9560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    //-------------------------------------------------------------------------
9660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
9760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    CommandHandler createCommandHandler() {
9860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        new NlstCommandHandler()
9960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
10060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
10160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    Command createValidCommand() {
10260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        return new Command(CommandNames.NLST, [DIR])
10360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
10460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
10560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    void setUp() {
10660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        super.setUp()
10760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair        createDirectory(DIR)
10860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair    }
10960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair
11060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair}