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