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.UserAccount 24import org.mockftpserver.fake.filesystem.FileSystemException 25import org.mockftpserver.fake.filesystem.Permissions 26 27 28/** 29 * Tests for MkdCommandHandler 30 * 31 * @version $Revision$ - $Date$ 32 * 33 * @author Chris Mair 34 */ 35class MkdCommandHandlerTest extends AbstractFakeCommandHandlerTest { 36 37 static final PARENT = '/' 38 static final DIRNAME = "usr" 39 static final DIR = p(PARENT, DIRNAME) 40 static final PERMISSIONS = new Permissions('rwx------') 41 42 void testHandleCommand() { 43 userAccount.defaultPermissionsForNewDirectory = PERMISSIONS 44 handleCommand([DIR]) 45 assertSessionReply(ReplyCodes.MKD_OK, ['mkd', DIR]) 46 assert fileSystem.exists(DIR) 47 def dirEntry = fileSystem.getEntry(DIR) 48 assert dirEntry.permissions == PERMISSIONS 49 } 50 51 void testHandleCommand_PathIsRelative() { 52 session.setAttribute(SessionKeys.CURRENT_DIRECTORY, '/') 53 handleCommand([DIRNAME]) 54 assertSessionReply(ReplyCodes.MKD_OK, ['mkd', DIRNAME]) 55 assert fileSystem.exists(DIR) 56 def dirEntry = fileSystem.getEntry(DIR) 57 assert dirEntry.permissions == UserAccount.DEFAULT_PERMISSIONS_FOR_NEW_DIRECTORY 58 } 59 60 void testHandleCommand_ParentDirectoryDoesNotExist() { 61 handleCommand(['/abc/def']) 62 assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.doesNotExist', '/abc']) 63 } 64 65 void testHandleCommand_PathSpecifiesAFile() { 66 createFile(DIR) 67 handleCommand([DIR]) 68 assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.alreadyExists', DIR]) 69 assert fileSystem.exists(DIR) 70 } 71 72 void testHandleCommand_MissingPathParameter() { 73 testHandleCommand_MissingRequiredParameter([]) 74 } 75 76 void testHandleCommand_NoWriteAccessToParentDirectory() { 77 fileSystem.getEntry(PARENT).permissions = new Permissions('r-xr-xr-x') 78 handleCommand([DIR]) 79 assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotWrite', PARENT]) 80 } 81 82 void testHandleCommand_NoExecuteAccessToParentDirectory() { 83 fileSystem.getEntry(PARENT).permissions = new Permissions('rw-rw-rw-') 84 handleCommand([DIR]) 85 assertSessionReply(ReplyCodes.READ_FILE_ERROR, ['filesystem.cannotExecute', PARENT]) 86 } 87 88 void testHandleCommand_CreateDirectoryThrowsException() { 89 fileSystem.addMethodException = new FileSystemException("bad", ERROR_MESSAGE_KEY) 90 handleCommand([DIR]) 91 assertSessionReply(ReplyCodes.READ_FILE_ERROR, ERROR_MESSAGE_KEY) 92 } 93 94 void setUp() { 95 super.setUp() 96 createDirectory(PARENT) 97 } 98 99 //------------------------------------------------------------------------- 100 // Helper Methods 101 //------------------------------------------------------------------------- 102 103 CommandHandler createCommandHandler() { 104 new MkdCommandHandler() 105 } 106 107 Command createValidCommand() { 108 return new Command(CommandNames.MKD, [DIR]) 109 } 110 111}