1bda3441225e0607b5ced8b538123fd7c7a417910chrismair/*
2bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Copyright 2008 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.fake.command
17bda3441225e0607b5ced8b538123fd7c7a417910chrismair
18bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.Command
19bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.CommandHandler
20bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.CommandNames
21bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.command.ReplyCodes
22bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.core.session.SessionKeys
23bda3441225e0607b5ced8b538123fd7c7a417910chrismairimport org.mockftpserver.fake.filesystem.Permissions
24bda3441225e0607b5ced8b538123fd7c7a417910chrismair
25bda3441225e0607b5ced8b538123fd7c7a417910chrismair/**
26bda3441225e0607b5ced8b538123fd7c7a417910chrismair * Tests for CwdCommandHandler
27bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
28bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @version $Revision$ - $Date$
29bda3441225e0607b5ced8b538123fd7c7a417910chrismair *
30bda3441225e0607b5ced8b538123fd7c7a417910chrismair * @author Chris Mair
31bda3441225e0607b5ced8b538123fd7c7a417910chrismair */
32bda3441225e0607b5ced8b538123fd7c7a417910chrismairclass CwdCommandHandlerTest extends AbstractFakeCommandHandlerTestCase {
33bda3441225e0607b5ced8b538123fd7c7a417910chrismair
34bda3441225e0607b5ced8b538123fd7c7a417910chrismair    def DIR = "/usr"
35bda3441225e0607b5ced8b538123fd7c7a417910chrismair
36bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testHandleCommand() {
37bda3441225e0607b5ced8b538123fd7c7a417910chrismair        createDirectory(DIR)
38bda3441225e0607b5ced8b538123fd7c7a417910chrismair        handleCommand([DIR])
39bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assertSessionReply(ReplyCodes.CWD_OK, ['cwd', DIR])
40bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == DIR
41bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
42bda3441225e0607b5ced8b538123fd7c7a417910chrismair
43bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testHandleCommand_PathIsRelative() {
44bda3441225e0607b5ced8b538123fd7c7a417910chrismair        def SUB = "sub"
45bda3441225e0607b5ced8b538123fd7c7a417910chrismair        createDirectory(p(DIR, SUB))
46bda3441225e0607b5ced8b538123fd7c7a417910chrismair        session.setAttribute(SessionKeys.CURRENT_DIRECTORY, DIR)
47bda3441225e0607b5ced8b538123fd7c7a417910chrismair        handleCommand([SUB])
48bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assertSessionReply(ReplyCodes.CWD_OK, ['cwd', SUB])
49bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == p(DIR, SUB)
50bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
51bda3441225e0607b5ced8b538123fd7c7a417910chrismair
52bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testHandleCommand_PathDoesNotExistInFileSystem() {
53bda3441225e0607b5ced8b538123fd7c7a417910chrismair        handleCommand([DIR])
54bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.doesNotExist', DIR])
55bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == null
56bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
57bda3441225e0607b5ced8b538123fd7c7a417910chrismair
58bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testHandleCommand_PathSpecifiesAFile() {
59bda3441225e0607b5ced8b538123fd7c7a417910chrismair        createFile(DIR)
60bda3441225e0607b5ced8b538123fd7c7a417910chrismair        handleCommand([DIR])
61bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.isNotADirectory', DIR])
62bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == null
63bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
64bda3441225e0607b5ced8b538123fd7c7a417910chrismair
65bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testHandleCommand_NoExecuteAccessToParentDirectory() {
66bda3441225e0607b5ced8b538123fd7c7a417910chrismair        def dir = createDirectory(DIR)
67bda3441225e0607b5ced8b538123fd7c7a417910chrismair        dir.permissions = new Permissions('rw-rw-rw-')
68bda3441225e0607b5ced8b538123fd7c7a417910chrismair        handleCommand([DIR])
69bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotExecute', DIR])
70bda3441225e0607b5ced8b538123fd7c7a417910chrismair        assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == null
71bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
72bda3441225e0607b5ced8b538123fd7c7a417910chrismair
73bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void testHandleCommand_MissingPathParameter() {
74bda3441225e0607b5ced8b538123fd7c7a417910chrismair        testHandleCommand_MissingRequiredParameter([])
75bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
76bda3441225e0607b5ced8b538123fd7c7a417910chrismair
77bda3441225e0607b5ced8b538123fd7c7a417910chrismair    //-------------------------------------------------------------------------
78bda3441225e0607b5ced8b538123fd7c7a417910chrismair    // Helper Methods
79bda3441225e0607b5ced8b538123fd7c7a417910chrismair    //-------------------------------------------------------------------------
80bda3441225e0607b5ced8b538123fd7c7a417910chrismair
81bda3441225e0607b5ced8b538123fd7c7a417910chrismair    CommandHandler createCommandHandler() {
82bda3441225e0607b5ced8b538123fd7c7a417910chrismair        new CwdCommandHandler()
83bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
84bda3441225e0607b5ced8b538123fd7c7a417910chrismair
85bda3441225e0607b5ced8b538123fd7c7a417910chrismair    Command createValidCommand() {
86bda3441225e0607b5ced8b538123fd7c7a417910chrismair        return new Command(CommandNames.CWD, [DIR])
87bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
88bda3441225e0607b5ced8b538123fd7c7a417910chrismair
89bda3441225e0607b5ced8b538123fd7c7a417910chrismair    void setUp() {
90bda3441225e0607b5ced8b538123fd7c7a417910chrismair        super.setUp()
91bda3441225e0607b5ced8b538123fd7c7a417910chrismair        userAccount.username = 'user'
92bda3441225e0607b5ced8b538123fd7c7a417910chrismair    }
93bda3441225e0607b5ced8b538123fd7c7a417910chrismair
94bda3441225e0607b5ced8b538123fd7c7a417910chrismair}