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 CdupCommandHandler
27 *
28 * @version $Revision$ - $Date$
29 *
30 * @author Chris Mair
31 */
32class CdupCommandHandlerTest extends AbstractFakeCommandHandlerTest {
33
34    def DIR = "/usr"
35    def SUBDIR = "${DIR}/sub"
36
37    void testHandleCommand() {
38        setCurrentDirectory(SUBDIR)
39        handleCommand([])
40        assertSessionReply(ReplyCodes.CDUP_OK, ['cdup', DIR])
41        assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == DIR
42    }
43
44    void testHandleCommand_NoParentDirectory() {
45        setCurrentDirectory('/')
46        handleCommand([])
47        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.parentDirectoryDoesNotExist', '/'])
48        assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == '/'
49    }
50
51    void testHandleCommand_NoExecuteAccessToDirectory() {
52        setCurrentDirectory(SUBDIR)
53        def dir = fileSystem.getEntry(DIR)
54        dir.permissions = new Permissions('rw-rw-rw-')
55        handleCommand([])
56        assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotExecute', DIR])
57        assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == SUBDIR
58    }
59
60    //-------------------------------------------------------------------------
61    // Helper Methods
62    //-------------------------------------------------------------------------
63
64    CommandHandler createCommandHandler() {
65        new CdupCommandHandler()
66    }
67
68    Command createValidCommand() {
69        return new Command(CommandNames.CDUP, [])
70    }
71
72    void setUp() {
73        super.setUp()
74        createDirectory(SUBDIR)
75    }
76
77}