1818package org .apache .commons .vfs2 .provider .sftp ;
1919
2020import static org .apache .commons .vfs2 .VfsTestUtils .getTestDirectoryFile ;
21- import static org .junit .jupiter .api .Assertions .*;
21+ import static org .junit .jupiter .api .Assertions .assertEquals ;
22+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
23+ import static org .junit .jupiter .api .Assertions .assertThrows ;
24+ import static org .junit .jupiter .api .Assertions .assertTrue ;
2225
2326import java .io .File ;
24- import java .security . KeyPair ;
25- import java .security . KeyPairGenerator ;
27+ import java .nio . file . Files ;
28+ import java .nio . file . Path ;
2629import java .time .Duration ;
27- import java .util .ArrayList ;
28- import java .util .List ;
30+ import java .util .Collections ;
2931
30- import org .apache .commons .vfs2 .*;
32+ import org .apache .commons .vfs2 .FileObject ;
33+ import org .apache .commons .vfs2 .FileSystemException ;
34+ import org .apache .commons .vfs2 .FileSystemOptions ;
35+ import org .apache .commons .vfs2 .FileType ;
3136import org .apache .commons .vfs2 .auth .StaticUserAuthenticator ;
3237import org .apache .commons .vfs2 .impl .DefaultFileSystemConfigBuilder ;
3338import org .apache .commons .vfs2 .impl .DefaultFileSystemManager ;
3439import org .apache .commons .vfs2 .provider .local .DefaultLocalFileProvider ;
35- import org .apache .sshd .SshServer ;
36- import org .apache .sshd .common .KeyPairProvider ;
37- import org .apache .sshd .common .NamedFactory ;
38- import org .apache .sshd .common .Session ;
39- import org .apache .sshd .server .Command ;
40- import org .apache .sshd .server .FileSystemFactory ;
41- import org .apache .sshd .server .FileSystemView ;
42- import org .apache .sshd .server .SshFile ;
43- import org .apache .sshd .server .filesystem .NativeSshFile ;
44- import org .apache .sshd .server .sftp .SftpSubsystem ;
40+ import org .apache .sshd .common .file .virtualfs .VirtualFileSystemFactory ;
41+ import org .apache .sshd .server .SshServer ;
42+ import org .apache .sshd .server .keyprovider .SimpleGeneratorHostKeyProvider ;
43+ import org .apache .sshd .sftp .server .SftpSubsystemFactory ;
4544import org .junit .jupiter .api .AfterAll ;
4645import org .junit .jupiter .api .BeforeAll ;
4746import org .junit .jupiter .api .Test ;
5453 * Verifies that credentials supplied via {@link DefaultFileSystemConfigBuilder#setUserAuthenticator}
5554 * are correctly propagated to the SFTP server.
5655 * </p>
57- * <p>
58- * Uses SSHD 0.8.0 with an explicit RSA {@link KeyPairProvider} for Java 17 compatibility
59- * (the default DSA key generation is disabled on modern JDKs).
60- * </p>
6156 */
6257public class SftpPasswordAuthTest {
6358
@@ -73,35 +68,22 @@ static void setUp() throws Exception {
7368 sshServer = SshServer .setUpDefaultServer ();
7469 sshServer .setPort (0 );
7570
76- final KeyPairGenerator hostKeyGen = KeyPairGenerator .getInstance ("RSA" );
77- hostKeyGen .initialize (2048 );
78- final KeyPair hostKey = hostKeyGen .generateKeyPair ();
79- sshServer .setKeyPairProvider (new KeyPairProvider () {
80- @ Override
81- public KeyPair loadKey (final String type ) {
82- return KeyPairProvider .SSH_RSA .equals (type ) ? hostKey : null ;
83- }
84- @ Override
85- public String getKeyTypes () {
86- return KeyPairProvider .SSH_RSA ;
87- }
88- });
71+ final Path tmpKeyFile = Files .createTempFile ("sshd-test-key" , ".ser" );
72+ tmpKeyFile .toFile ().deleteOnExit ();
73+ final SimpleGeneratorHostKeyProvider keyProvider = new SimpleGeneratorHostKeyProvider (tmpKeyFile );
74+ keyProvider .setAlgorithm ("RSA" );
75+ sshServer .setKeyPairProvider (keyProvider );
8976
9077 sshServer .setPasswordAuthenticator (
9178 (user , pass , session ) -> TEST_USERNAME .equals (user ) && TEST_PASSWORD .equals (pass ));
9279
93- final List <NamedFactory <Command >> subsystems = new ArrayList <>();
94- subsystems .add (new NamedFactory <Command >() {
95- @ Override
96- public Command create () { return new SftpSubsystem (); }
97- @ Override
98- public String getName () { return "sftp" ; }
99- });
100- sshServer .setSubsystemFactories (subsystems );
80+ sshServer .setSubsystemFactories (Collections .singletonList (new SftpSubsystemFactory ()));
10181
102- sshServer .setFileSystemFactory (new TestFileSystemFactory ());
103- sshServer .start ();
82+ final File homeDir = getTestDirectoryFile ();
83+ sshServer .setFileSystemFactory (
84+ new VirtualFileSystemFactory (homeDir .toPath ().toAbsolutePath ()));
10485
86+ sshServer .start ();
10587 serverPort = sshServer .getPort ();
10688
10789 manager = new DefaultFileSystemManager ();
@@ -127,7 +109,7 @@ static void tearDown() throws Exception {
127109 private static void stopServerWithTimeout (final long timeoutMs ) {
128110 final Thread stopThread = new Thread (() -> {
129111 try {
130- sshServer .stop ( true );
112+ sshServer .close ( );
131113 } catch (final Exception e ) {
132114 // ignore
133115 }
@@ -205,43 +187,4 @@ void testWrongCredentialsThrowsException() throws FileSystemException {
205187 }
206188 }, "Expected FileSystemException when accessing a resource with wrong credentials" );
207189 }
208-
209- private static class TestFileSystemFactory implements FileSystemFactory {
210- @ Override
211- public FileSystemView createFileSystemView (final Session session ) {
212- final String home = getTestDirectoryFile ().getAbsolutePath ();
213- final String user = session .getUsername ();
214- return new FileSystemView () {
215- @ Override
216- public SshFile getFile (final SshFile baseDir , final String file ) {
217- return getFile (baseDir .getAbsolutePath (), file );
218- }
219- @ Override
220- public SshFile getFile (final String file ) {
221- return getFile (home , file );
222- }
223- private SshFile getFile (final String dir , final String file ) {
224- final String normalized = NativeSshFile .normalizeSeparateChar (file );
225- final String homeNorm = NativeSshFile .normalizeSeparateChar (home );
226- final String prefix = removePrefix (homeNorm );
227- String userFile = removePrefix (normalized );
228- final File f = userFile .startsWith (prefix )
229- ? new File (userFile )
230- : new File (prefix , userFile );
231- userFile = removePrefix (NativeSshFile .normalizeSeparateChar (f .getAbsolutePath ()));
232- return new AccessibleNativeSshFile (userFile , f , user );
233- }
234- private String removePrefix (final String s ) {
235- final int idx = s .indexOf ('/' );
236- return idx < 1 ? s : s .substring (idx );
237- }
238- };
239- }
240- }
241-
242- private static class AccessibleNativeSshFile extends NativeSshFile {
243- AccessibleNativeSshFile (final String fileName , final File file , final String userName ) {
244- super (fileName , file , userName );
245- }
246- }
247190}
0 commit comments