// Path: src/main/java/com/github/mlk/junit/rules/Helper.java
// public static int findRandomOpenPortOnAllLocalInterfaces() throws IOException {
//   try (ServerSocket socket = new ServerSocket(0)) {
//     return socket.getLocalPort();
//   }
// }


import static com.github.mlk.junit.rules.Helper.findRandomOpenPortOnAllLocalInterfaces;
import java.io.File;
import java.io.IOException;
import java.security.PublicKey;
import java.util.Collections;
import java.util.function.Supplier;
import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.common.keyprovider.ClassLoadableResourceKeyPairProvider;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.auth.hostbased.StaticHostBasedAuthenticator;
import org.apache.sshd.server.auth.pubkey.AcceptAllPublickeyAuthenticator;
import org.apache.sshd.server.auth.pubkey.UserAuthPublicKeyFactory;
import org.apache.sshd.server.scp.ScpCommandFactory;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
import org.junit.rules.ExternalResource;
package com.github.mlk.junit.rules;



/**
 * Create an SFTP server running on a random port. This server runs in a promiscuous mode, accepting
 * any connection with a private key.
 * TODO:
 * - Allow both public key or username/password auth.
 * - Allow for custom server key
 */
public class SftpRule extends ExternalResource {

  private final Supplier<File> currentFolder;

  private SshServer sshd;

  /**
   * SFTP Server
   *
   * @param currentFolder The user home folder for the SFTP server.
   */
  public SftpRule(Supplier<File> currentFolder) {
    this.currentFolder = currentFolder;
  }

  @Override
  protected void before() throws Throwable {
    sshd = SshServer.setUpDefaultServer();
// Path: src/main/java/com/github/mlk/junit/rules/Helper.java
// public static int findRandomOpenPortOnAllLocalInterfaces() throws IOException {
//   try (ServerSocket socket = new ServerSocket(0)) {
//     return socket.getLocalPort();
//   }
// }


// Path: src/main/java/com/github/mlk/junit/rules/SftpRule.java
import static com.github.mlk.junit.rules.Helper.findRandomOpenPortOnAllLocalInterfaces;
import java.io.File;
import java.io.IOException;
import java.security.PublicKey;
import java.util.Collections;
import java.util.function.Supplier;
import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
import org.apache.sshd.common.keyprovider.ClassLoadableResourceKeyPairProvider;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.server.auth.hostbased.StaticHostBasedAuthenticator;
import org.apache.sshd.server.auth.pubkey.AcceptAllPublickeyAuthenticator;
import org.apache.sshd.server.auth.pubkey.UserAuthPublicKeyFactory;
import org.apache.sshd.server.scp.ScpCommandFactory;
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
import org.junit.rules.ExternalResource;

package com.github.mlk.junit.rules;



/**
 * Create an SFTP server running on a random port. This server runs in a promiscuous mode, accepting
 * any connection with a private key.
 * TODO:
 * - Allow both public key or username/password auth.
 * - Allow for custom server key
 */
public class SftpRule extends ExternalResource {

  private final Supplier<File> currentFolder;

  private SshServer sshd;

  /**
   * SFTP Server
   *
   * @param currentFolder The user home folder for the SFTP server.
   */
  public SftpRule(Supplier<File> currentFolder) {
    this.currentFolder = currentFolder;
  }

  @Override
  protected void before() throws Throwable {
    sshd = SshServer.setUpDefaultServer();
    sshd.setPort(findRandomOpenPortOnAllLocalInterfaces());