|
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); |
@@ -343,4 +345,33 @@ protected SftpClient createSftpClient(ClientSession clientSession, |
343 | 345 | } |
344 | 346 | } |
345 | 347 |
|
| 348 | + /* |
| 349 | + * Verify the socket is closed if authentication fails. |
| 350 | + */ |
| 351 | + @Test |
| 352 | + void noClientSessionLeakOnAuthError() throws Exception { |
| 353 | + try (SshServer server = SshServer.setUpDefaultServer()) { |
| 354 | + server.setPasswordAuthenticator((arg0, arg1, arg2) -> false); |
| 355 | + server.setPort(0); |
| 356 | + server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); |
| 357 | + server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); |
| 358 | + server.start(); |
| 359 | + |
| 360 | + DefaultSftpSessionFactory sftpSessionFactory = new DefaultSftpSessionFactory(); |
| 361 | + sftpSessionFactory.setHost("localhost"); |
| 362 | + sftpSessionFactory.setPort(server.getPort()); |
| 363 | + sftpSessionFactory.setUser("user"); |
| 364 | + sftpSessionFactory.setAllowUnknownKeys(true); |
| 365 | + |
| 366 | + assertThatIllegalStateException() |
| 367 | + .isThrownBy(sftpSessionFactory::getSession) |
| 368 | + .withCauseInstanceOf(SshException.class) |
| 369 | + .withStackTraceContaining("No more authentication methods available"); |
| 370 | + |
| 371 | + await().untilAsserted(() -> assertThat(server.getActiveSessions()).hasSize(0)); |
| 372 | + |
| 373 | + sftpSessionFactory.destroy(); |
| 374 | + } |
| 375 | + } |
| 376 | + |
346 | 377 | } |
0 commit comments