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.UserAccount 2460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 2560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair/** 2660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Tests for PassCommandHandler 2760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 2860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @version $Revision$ - $Date$ 2960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * 3060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @author Chris Mair 3160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 3260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismairclass PassCommandHandlerTest extends AbstractFakeCommandHandlerTest { 3360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 3460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair def USERNAME = "user123" 3560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair def PASSWORD = "password123" 3660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair def HOME_DIRECTORY = "/" 3760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair UserAccount userAccount 3860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 3960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair boolean testNotLoggedIn = false 4060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 4160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair void testHandleCommand_UserExists_PasswordCorrect() { 4260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair serverConfiguration.userAccounts[USERNAME] = userAccount 4360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair handleCommand([PASSWORD]) 4460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertSessionReply(ReplyCodes.PASS_OK, 'pass') 4560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertUserAccountInSession(true) 4660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertCurrentDirectory(HOME_DIRECTORY) 4760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 4860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 4960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair void testHandleCommand_UserExists_PasswordCorrect_AccountRequired() { 5060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair serverConfiguration.userAccounts[USERNAME] = userAccount 5160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair userAccount.accountRequiredForLogin = true 5260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair handleCommand([PASSWORD]) 5360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertSessionReply(ReplyCodes.PASS_NEED_ACCOUNT, 'pass.needAccount') 5460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertUserAccountInSession(true) 5560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertCurrentDirectory(HOME_DIRECTORY) 5660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 5760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 5860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair void testHandleCommand_UserExists_PasswordIncorrect() { 5960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair serverConfiguration.userAccounts[USERNAME] = userAccount 6060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair handleCommand(["wrong"]) 6160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertSessionReply(ReplyCodes.PASS_LOG_IN_FAILED, 'pass.loginFailed') 6260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertUserAccountInSession(false) 6360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertCurrentDirectory(null) 6460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 6560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 6660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair void testHandleCommand_UserExists_PasswordWrongButIgnored() { 6760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair userAccount.passwordCheckedDuringValidation = false 6860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair serverConfiguration.userAccounts[USERNAME] = userAccount 6960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair handleCommand(["wrong"]) 7060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertSessionReply(ReplyCodes.PASS_OK, 'pass') 7160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertUserAccountInSession(true) 7260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertCurrentDirectory(HOME_DIRECTORY) 7360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 7460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 7560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair void testHandleCommand_UserExists_HomeDirectoryNotDefinedForUserAccount() { 7660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair userAccount.homeDirectory = '' 7760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair serverConfiguration.userAccounts[USERNAME] = userAccount 7860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair handleCommand([PASSWORD]) 7960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertSessionReply(ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.userAccountNotValid") 8060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertUserAccountInSession(false) 8160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertCurrentDirectory(null) 8260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 8360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 8460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair void testHandleCommand_UserExists_HomeDirectoryDoesNotExist() { 8560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair userAccount.homeDirectory = '/abc/def' 8660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair serverConfiguration.userAccounts[USERNAME] = userAccount 8760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair handleCommand([PASSWORD]) 8860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertSessionReply(ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.homeDirectoryNotValid") 8960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertUserAccountInSession(false) 9060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertCurrentDirectory(null) 9160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 9260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 9360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair void testHandleCommand_UserDoesNotExist() { 9460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair handleCommand([PASSWORD]) 9560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertSessionReply(ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.userAccountNotValid") 9660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertUserAccountInSession(false) 9760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertCurrentDirectory(null) 9860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 9960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 10060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair void testHandleCommand_UsernameNotSetInSession() { 10160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair session.removeAttribute(SessionKeys.USERNAME) 10260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair testHandleCommand_MissingRequiredSessionAttribute() 10360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertUserAccountInSession(false) 10460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertCurrentDirectory(null) 10560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 10660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 10760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair void testHandleCommand_MissingPasswordParameter() { 10860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair testHandleCommand_MissingRequiredParameter([]) 10960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertUserAccountInSession(false) 11060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assertCurrentDirectory(null) 11160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 11260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 11360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair //------------------------------------------------------------------------- 11460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // Abstract and Overridden Methods 11560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair //------------------------------------------------------------------------- 11660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 11760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair void setUp() { 11860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair super.setUp() 11960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 12060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair createDirectory(HOME_DIRECTORY) 12160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 12260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair userAccount = new UserAccount(USERNAME, PASSWORD, HOME_DIRECTORY) 12360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 12460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair session.setAttribute(SessionKeys.USERNAME, USERNAME) 12560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair session.removeAttribute(SessionKeys.USER_ACCOUNT) 12660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 12760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 12860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair CommandHandler createCommandHandler() { 12960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair new PassCommandHandler() 13060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 13160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 13260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair Command createValidCommand() { 13360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair return new Command(CommandNames.PASS, [PASSWORD]) 13460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 13560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 13660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair //------------------------------------------------------------------------- 13760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair // Helper Methods 13860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair //------------------------------------------------------------------------- 13960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 14060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair /** 14160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Assert that the UserAccount object is in the session, depending on the value of isUserAccountInSession. 14260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @param isUserAccountInSession - true if the UserAccount is expected in the session; false if it is not expected 14360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 14460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair private void assertUserAccountInSession(boolean isUserAccountInSession) { 14560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair def expectedValue = isUserAccountInSession ? userAccount : null 14660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assert session.getAttribute(SessionKeys.USER_ACCOUNT) == expectedValue 14760b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 14860b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair 14960b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair /** 15060b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * Assert that the current directory is set in the session, but only if currentDirectory is not null. 15160b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair * @param currentDirectory - the curent directory expected in the session; null if it is not expected 15260b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair */ 15360b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair private void assertCurrentDirectory(String currentDirectory) { 15460b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair assert session.getAttribute(SessionKeys.CURRENT_DIRECTORY) == currentDirectory 15560b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair } 15660b81e2faf8511148f0d1e8f296e0b40ce9c7971chrismair}