|
52 | 52 | import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
53 | 53 |
|
54 | 54 | import static org.assertj.core.api.Assertions.assertThat;
|
| 55 | +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; |
55 | 56 | import static org.assertj.core.api.Assertions.assertThatNoException;
|
56 | 57 | import static org.assertj.core.api.Assertions.fail;
|
57 | 58 | import static org.awaitility.Awaitility.await;
|
|
61 | 62 | * @author Artem Bilan
|
62 | 63 | * @author Auke Zaaiman
|
63 | 64 | * @author Darryl Smith
|
| 65 | + * @author Alastair Mailer |
64 | 66 | *
|
65 | 67 | * @since 3.0.2
|
66 | 68 | */
|
67 |
| -public class SftpSessionFactoryTests { |
| 69 | +class SftpSessionFactoryTests { |
68 | 70 |
|
69 | 71 | /*
|
70 | 72 | * Verify the socket is closed if the channel.connect() fails.
|
71 | 73 | * INT-3305
|
72 | 74 | */
|
73 | 75 | @Test
|
74 |
| - public void testConnectFailSocketOpen() throws Exception { |
| 76 | + void testConnectFailSocketOpen() throws Exception { |
75 | 77 | try (SshServer server = SshServer.setUpDefaultServer()) {
|
76 | 78 | server.setPasswordAuthenticator((arg0, arg1, arg2) -> true);
|
77 | 79 | server.setPort(0);
|
@@ -118,7 +120,7 @@ public void testConnectFailSocketOpen() throws Exception {
|
118 | 120 | }
|
119 | 121 |
|
120 | 122 | @Test
|
121 |
| - public void concurrentGetSessionDoesntCauseFailure() throws Exception { |
| 123 | + void concurrentGetSessionDoesntCauseFailure() throws Exception { |
122 | 124 | try (SshServer server = SshServer.setUpDefaultServer()) {
|
123 | 125 | server.setPasswordAuthenticator((arg0, arg1, arg2) -> true);
|
124 | 126 | server.setPort(0);
|
@@ -335,4 +337,33 @@ protected SftpClient createSftpClient(ClientSession clientSession,
|
335 | 337 | }
|
336 | 338 | }
|
337 | 339 |
|
| 340 | + /* |
| 341 | + * Verify the socket is closed if authentication fails. |
| 342 | + */ |
| 343 | + @Test |
| 344 | + void noClientSessionLeakOnAuthError() throws Exception { |
| 345 | + try (SshServer server = SshServer.setUpDefaultServer()) { |
| 346 | + server.setPasswordAuthenticator((arg0, arg1, arg2) -> false); |
| 347 | + server.setPort(0); |
| 348 | + server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); |
| 349 | + server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); |
| 350 | + server.start(); |
| 351 | + |
| 352 | + DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory(); |
| 353 | + sftpSessionFactory.setHost("localhost"); |
| 354 | + sftpSessionFactory.setPort(server.getPort()); |
| 355 | + sftpSessionFactory.setUser("user"); |
| 356 | + sftpSessionFactory.setAllowUnknownKeys(true); |
| 357 | + |
| 358 | + assertThatIllegalStateException() |
| 359 | + .isThrownBy(sftpSessionFactory::getSession) |
| 360 | + .withCauseInstanceOf(SshException.class) |
| 361 | + .withStackTraceContaining("No more authentication methods available"); |
| 362 | + |
| 363 | + await().untilAsserted(() -> assertThat(server.getActiveSessions()).hasSize(0)); |
| 364 | + |
| 365 | + sftpSessionFactory.destroy(); |
| 366 | + } |
| 367 | + } |
| 368 | + |
338 | 369 | }
|
0 commit comments