Skip to content

Commit c2ce682

Browse files
committed
apacheGH-283: Add a test verifying the number of password prompts
1 parent 3b16da4 commit c2ce682

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

sshd-core/src/test/java/org/apache/sshd/client/ClientTest.java

+62
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,68 @@ public String getUpdatedPassword(ClientSession clientSession, String prompt, Str
13421342
assertNull("Session closure not signalled", clientSessionHolder.get());
13431343
}
13441344

1345+
@Test // See GH-283
1346+
public void testPasswordPrompts() throws Exception {
1347+
CoreModuleProperties.PASSWORD_PROMPTS.set(client, 1);
1348+
1349+
client.setUserAuthFactories(Collections.singletonList(UserAuthKeyboardInteractiveFactory.INSTANCE));
1350+
client.start();
1351+
1352+
try (ClientSession session = client.connect(getCurrentTestName(), TEST_LOCALHOST, port).verify(CONNECT_TIMEOUT)
1353+
.getSession()) {
1354+
assertNotNull("Client session creation not signalled", clientSessionHolder.get());
1355+
AtomicInteger count = new AtomicInteger();
1356+
session.setUserInteraction(new UserInteraction() {
1357+
@Override
1358+
public boolean isInteractionAllowed(ClientSession session) {
1359+
return true;
1360+
}
1361+
1362+
@Override
1363+
public void serverVersionInfo(ClientSession clientSession, List<String> lines) {
1364+
assertSame("Mismatched server version info session", session, clientSession);
1365+
}
1366+
1367+
@Override
1368+
public void welcome(ClientSession clientSession, String banner, String lang) {
1369+
assertSame("Mismatched welcome session", session, clientSession);
1370+
}
1371+
1372+
@Override
1373+
public String[] interactive(ClientSession clientSession, String name, String instruction, String lang,
1374+
String[] prompt, boolean[] echo) {
1375+
assertSame("Mismatched interactive session", session, clientSession);
1376+
int n = count.incrementAndGet();
1377+
if (n == 1) {
1378+
return new String[] { "bogus" };
1379+
}
1380+
return new String[] { getCurrentTestName() };
1381+
}
1382+
1383+
@Override
1384+
public String getUpdatedPassword(ClientSession clientSession, String prompt, String lang) {
1385+
throw new UnsupportedOperationException("Unexpected call");
1386+
}
1387+
});
1388+
1389+
AuthFuture future = session.auth();
1390+
assertTrue("Failed to complete authentication on time", future.await(CLOSE_TIMEOUT));
1391+
assertTrue("Authentication should have failed", future.isFailure());
1392+
assertEquals("Mismatched authentication attempts count", 1, count.get());
1393+
count.set(0);
1394+
CoreModuleProperties.PASSWORD_PROMPTS.set(client, 2);
1395+
future = session.auth();
1396+
assertTrue("Failed to complete authentication on time", future.await(CLOSE_TIMEOUT));
1397+
assertFalse("Authentication should not have failed", future.isFailure());
1398+
assertTrue("Authentication should have succeeded", future.isSuccess());
1399+
assertEquals("Mismatched authentication attempts count", 2, count.get());
1400+
} finally {
1401+
client.stop();
1402+
}
1403+
1404+
assertNull("Session closure not signalled", clientSessionHolder.get());
1405+
}
1406+
13451407
@Test
13461408
public void testKeyboardInteractiveInSessionUserInteractiveFailure() throws Exception {
13471409
final int maxPrompts = 3;

0 commit comments

Comments
 (0)