@Before public void setUp() throws Exception { fakeFtpServer.setServerControlPort(0); fakeFtpServer.addUserAccount(new UserAccount(username, password, "c:\\data")); FileSystem fileSystem = new WindowsFakeFileSystem(); fileSystem.add(new DirectoryEntry("c:\\data")); fakeFtpServer.setFileSystem(fileSystem); fakeFtpServer.start(); ftpPort = fakeFtpServer.getServerControlPort(); }
/** * Validate the UserAccount for the specified username. If valid, return true. If the UserAccount does * not exist or is invalid, log an error message, send back a reply code of 530 with an appropriate * error message, and return false. A UserAccount is considered invalid if the homeDirectory property * is not set or is set to a non-existent directory. * * @param username - the username * @param session - the session; used to send back an error reply if necessary * @return true only if the UserAccount for the named user is valid */ protected boolean validateUserAccount(String username, Session session) { UserAccount userAccount = serverConfiguration.getUserAccount(username); if (userAccount == null || !userAccount.isValid()) { LOG.error("UserAccount missing or not valid for username [" + username + "]: " + userAccount); sendReply(session, ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.userAccountNotValid", list(username)); return false; } String home = userAccount.getHomeDirectory(); if (!getFileSystem().isDirectory(home)) { LOG.error("Home directory configured for username [" + username + "] is not valid: " + home); sendReply(session, ReplyCodes.USER_ACCOUNT_NOT_VALID, "login.homeDirectoryNotValid", list(username, home)); return false; } return true; }
/** * Return true if this user has execute access to the file/directory represented by the specified FileSystemEntry object. * * @param entry - the FileSystemEntry representing the file or directory * @return true if this use has execute access */ public boolean canExecute(FileSystemEntry entry) { Permissions permissions = entry.getPermissions(); if (permissions == null) { return true; } if (equalOrBothNull(username, entry.getOwner())) { return permissions.canUserExecute(); } if (groups != null && groups.contains(entry.getGroup())) { return permissions.canGroupExecute(); } return permissions.canWorldExecute(); }
/** * Return true if the specified password is the correct, valid password for this user account. * This implementation uses standard (case-sensitive) String comparison. Subclasses can provide * custom comparison behavior, for instance using encrypted password values, by overriding this * method. * * @param password - the password to compare against the configured value * @return true if the password is correct and valid * @throws org.mockftpserver.core.util.AssertFailedException * - if the username property is null */ public boolean isValidPassword(String password) { Assert.notNullOrEmpty(username, "username"); return !passwordCheckedDuringValidation || comparePassword(password); }
/** * Log in the specified user for the current session. Send back a reply of 230 with a message indicated * by the replyMessageKey and set the UserAccount and current directory (homeDirectory) in the session. * * @param userAccount - the userAccount for the user to be logged in * @param session - the session * @param replyCode - the reply code to send * @param replyMessageKey - the message key for the reply text */ protected void login(UserAccount userAccount, Session session, int replyCode, String replyMessageKey) { sendReply(session, replyCode, replyMessageKey); session.setAttribute(SessionKeys.USER_ACCOUNT, userAccount); session.setAttribute(SessionKeys.CURRENT_DIRECTORY, userAccount.getHomeDirectory()); }
/** * Verify that the current user has write permission to the specified path * * @param session - the Session * @param path - the file system path * @throws FileSystemException - if the condition is not true */ protected void verifyWritePermission(Session session, String path) { UserAccount userAccount = getUserAccount(session); FileSystemEntry entry = getFileSystem().getEntry(path); verifyFileSystemCondition(userAccount.canWrite(entry), path, "filesystem.cannotWrite"); }
/** * Verify that the current user has read permission to the specified path * * @param session - the Session * @param path - the file system path * @throws FileSystemException - if the condition is not true */ protected void verifyReadPermission(Session session, String path) { UserAccount userAccount = getUserAccount(session); FileSystemEntry entry = getFileSystem().getEntry(path); verifyFileSystemCondition(userAccount.canRead(entry), path, "filesystem.cannotRead"); }
/** * Verify that the current user has execute permission to the specified path * * @param session - the Session * @param path - the file system path * @throws FileSystemException - if the condition is not true */ protected void verifyExecutePermission(Session session, String path) { UserAccount userAccount = getUserAccount(session); FileSystemEntry entry = getFileSystem().getEntry(path); verifyFileSystemCondition(userAccount.canExecute(entry), path, "filesystem.cannotExecute"); }
getFileSystem().add(file); file.setPermissions(getUserAccount(session).getDefaultPermissionsForNewFile());
protected void handle(Command command, Session session) { verifyLoggedIn(session); String path = getRealPath(session, command.getRequiredParameter(0)); String parent = getFileSystem().getParent(path); this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR; verifyFileSystemCondition(getFileSystem().exists(parent), parent, "filesystem.doesNotExist"); verifyFileSystemCondition(!getFileSystem().exists(path), path, "filesystem.alreadyExists"); // User must have write permission to the parent directory verifyWritePermission(session, parent); // User must have execute permission to the parent directory verifyExecutePermission(session, parent); DirectoryEntry dirEntry = new DirectoryEntry(path); getFileSystem().add(dirEntry); dirEntry.setPermissions(getUserAccount(session).getDefaultPermissionsForNewDirectory()); sendReply(session, ReplyCodes.MKD_OK, "mkd", list(path)); }
/** * Return true if this user has read access to the file/directory represented by the specified FileSystemEntry object. * * @param entry - the FileSystemEntry representing the file or directory * @return true if this use has read access */ public boolean canRead(FileSystemEntry entry) { Permissions permissions = entry.getPermissions(); if (permissions == null) { return true; } if (equalOrBothNull(username, entry.getOwner())) { return permissions.canUserRead(); } if (groups != null && groups.contains(entry.getGroup())) { return permissions.canGroupRead(); } return permissions.canWorldRead(); }
fileSystem.add(directoryEntry); fakeFtpServer.setFileSystem(fileSystem); UserAccount userAccount = new UserAccount(user, password, this.baseDir); fakeFtpServer.addUserAccount(userAccount);
/** * Return true if this user has write access to the file/directory represented by the specified FileSystemEntry object. * * @param entry - the FileSystemEntry representing the file or directory * @return true if this use has write access */ public boolean canWrite(FileSystemEntry entry) { Permissions permissions = entry.getPermissions(); if (permissions == null) { return true; } if (equalOrBothNull(username, entry.getOwner())) { return permissions.canUserWrite(); } if (groups != null && groups.contains(entry.getGroup())) { return permissions.canGroupWrite(); } return permissions.canWorldWrite(); }
@Test public void testRemoteFile() throws IOException { File directory = TEMP_FOLDER.newFolder("ftp"); File ftpFile = new File(directory, "ftp_file"); String ftpFileContents = "Contents of ftp_file"; FileSystem fileSystem = new UnixFakeFileSystem(); fileSystem.add(new FileEntry(ftpFile.getAbsolutePath(), ftpFileContents)); String user = "user"; String password = "password"; FakeFtpServer ftpServer = new FakeFtpServer(); // Use any available port ftpServer.setServerControlPort(0); ftpServer.addUserAccount(new UserAccount(user, password, directory.getAbsolutePath())); ftpServer.setFileSystem(fileSystem); ftpServer.start(); try { URI uri = URI.create(String.format("ftp://%s:%s@localhost:%d/%s", user, password, ftpServer.getServerControlPort(), ftpFile.getName())); File localizationDir = TEMP_FOLDER.newFolder("localRemote"); File localizedResource = LocalizationUtils.localizeResource("file1", new LocalizeResource(uri, false), localizationDir); Assert.assertTrue(localizedResource.exists()); Assert.assertTrue(localizedResource.isFile()); Assert.assertEquals(ftpFileContents, com.google.common.io.Files.toString(localizedResource, Charsets.UTF_8)); } finally { ftpServer.stop(); } }