@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(); }
@After public void tearDown() throws Exception { fakeFtpServer.stop(); }
@Test public void basicFileGet() throws IOException { FileSystem results = fakeFtpServer.getFileSystem(); FileEntry sampleFile = new FileEntry("c:\\data\\randombytes-2"); sampleFile.setContents("Just some random test test test chocolate"); results.add(sampleFile); // Check file exists Assert.assertTrue(results.exists("c:\\data\\randombytes-2")); TestRunner runner = TestRunners.newTestRunner(GetFTP.class); runner.setProperty(FTPTransfer.HOSTNAME, "localhost"); runner.setProperty(FTPTransfer.USERNAME, username); runner.setProperty(FTPTransfer.PASSWORD, password); runner.setProperty(FTPTransfer.PORT, Integer.toString(ftpPort)); runner.setProperty(FTPTransfer.REMOTE_PATH, "/"); runner.run(); final MockFlowFile retrievedFile = runner.getFlowFilesForRelationship(GetFTP.REL_SUCCESS).get(0); retrievedFile.assertContentEquals("Just some random test test test chocolate"); }
protected void handle(Command command, Session session) { String password = command.getRequiredParameter(0); String username = (String) getRequiredSessionAttribute(session, SessionKeys.USERNAME); if (validateUserAccount(username, session)) { UserAccount userAccount = getServerConfiguration().getUserAccount(username); if (userAccount.isValidPassword(password)) { int replyCode = (userAccount.isAccountRequiredForLogin()) ? ReplyCodes.PASS_NEED_ACCOUNT : ReplyCodes.PASS_OK; String replyMessageKey = (userAccount.isAccountRequiredForLogin()) ? "pass.needAccount" : "pass"; login(userAccount, session, replyCode, replyMessageKey); } else { sendReply(session, ReplyCodes.PASS_LOG_IN_FAILED, "pass.loginFailed"); } } }
/** * 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"); }
@Test public void basicFileUpload() throws IOException { TestRunner runner = TestRunners.newTestRunner(PutFTP.class); runner.setProperty(FTPTransfer.HOSTNAME, "localhost"); runner.setProperty(FTPTransfer.USERNAME, username); runner.setProperty(FTPTransfer.PASSWORD, password); runner.setProperty(FTPTransfer.PORT, Integer.toString(ftpPort)); try (FileInputStream fis = new FileInputStream("src/test/resources/randombytes-1");) { Map<String, String> attributes = new HashMap<String, String>(); attributes.put(CoreAttributes.FILENAME.key(), "randombytes-1"); runner.enqueue(fis, attributes); runner.run(); } runner.assertQueueEmpty(); runner.assertAllFlowFilesTransferred(PutFTP.REL_SUCCESS); FileSystem results = fakeFtpServer.getFileSystem(); // Check file was uploaded Assert.assertTrue(results.exists("c:\\data\\randombytes-1")); }
/** * Start the fakeFtpServer. * <p/> * The ssh will normally be started by JUnit using the before() method. This method allows the sshd to * be started manually to support advanced testing scenarios. */ public void start() { fakeFtpServer.start(); }
public FileSystemEntry getFile(String fileName) throws Exception { FileSystem fileSystem = fakeFtpServer.getFileSystem(); return fileSystem.getEntry(baseDir + "/" + fileName); }
protected void handle(Command command, Session session) { String systemStatus = getServerConfiguration().getSystemStatus(); sendReply(session, ReplyCodes.STAT_SYSTEM_OK, "stat", list(systemStatus)); }
protected void handle(Command command, Session session) { sendReply(session, ReplyCodes.SYST_OK, "syst", list(getServerConfiguration().getSystemName())); }
/** * Construct a new initialized instance. * * @param username - the user name * @param password - the password * @param homeDirectory - the home directory */ public UserAccount(String username, String password, String homeDirectory) { setUsername(username); setPassword(password); setHomeDirectory(homeDirectory); }
public int getPort() { return fakeFtpServer.getServerControlPort(); }
/** * Initialize a CommandHandler that has been registered to this server. * * If the CommandHandler implements the <code>ServerConfigurationAware</code> interface, then set its * <code>ServerConfiguration</code> property to <code>this</code>. * * If the CommandHandler implements the <code>ReplyTextBundleAware</code> interface, then set its * <code>replyTextBundle</code> property using the reply text bundle for this server. * * @param commandHandler - the CommandHandler to initialize */ protected void initializeCommandHandler(CommandHandler commandHandler) { if (commandHandler instanceof ServerConfigurationAware) { ServerConfigurationAware sca = (ServerConfigurationAware) commandHandler; sca.setServerConfiguration(this); } ReplyTextBundleUtil.setReplyTextBundleIfAppropriate(commandHandler, getReplyTextBundle()); }
/** * 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()); }
/** * Convenience method to return the FileSystem stored in the ServerConfiguration * * @return the FileSystem */ protected FileSystem getFileSystem() { return serverConfiguration.getFileSystem(); }
/** * Add a single UserAccount. If an account with the same <code>username</code> already exists, * it will be replaced. * * @param userAccount - the UserAccount to add */ public void addUserAccount(UserAccount userAccount) { userAccounts.put(userAccount.getUsername(), userAccount); }
/** * 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); }
@Test public void basicFileFetch() throws IOException { FileSystem results = fakeFtpServer.getFileSystem(); FileEntry sampleFile = new FileEntry("c:\\data\\randombytes-2"); sampleFile.setContents("Just some random test test test chocolate"); results.add(sampleFile); // Check file exists Assert.assertTrue(results.exists("c:\\data\\randombytes-2")); TestRunner runner = TestRunners.newTestRunner(FetchFTP.class); runner.setProperty(FetchFTP.HOSTNAME, "${host}"); runner.setProperty(FetchFTP.USERNAME, "${username}"); runner.setProperty(FTPTransfer.PASSWORD, password); runner.setProperty(FTPTransfer.PORT, "${port}"); runner.setProperty(FetchFTP.REMOTE_FILENAME, "c:\\data\\randombytes-2"); runner.setProperty(FetchFTP.COMPLETION_STRATEGY, FetchFTP.COMPLETION_MOVE); runner.setProperty(FetchFTP.MOVE_DESTINATION_DIR, "data"); Map<String, String> attrs = new HashMap<String, String>(); attrs.put("host", "localhost"); attrs.put("username", username); attrs.put("port", Integer.toString(ftpPort)); runner.enqueue("", attrs); runner.run(); final MockFlowFile retrievedFile = runner.getFlowFilesForRelationship(FetchFTP.REL_SUCCESS).get(0); retrievedFile.assertContentEquals("Just some random test test test chocolate"); }
@Test public void basicFileList() throws IOException, InterruptedException { FileSystem results = fakeFtpServer.getFileSystem(); FileEntry sampleFile = new FileEntry("c:\\data\\randombytes-2"); sampleFile.setContents("Just some random test test test chocolate"); results.add(sampleFile); Assert.assertTrue(results.exists("c:\\data\\randombytes-2"));