|
| 1 | +package com.hierynomus.sshj.userauth.method; |
| 2 | + |
| 3 | +import net.schmizz.sshj.userauth.method.PasswordResponseProvider; |
| 4 | +import net.schmizz.sshj.userauth.password.AccountResource; |
| 5 | +import net.schmizz.sshj.userauth.password.PasswordFinder; |
| 6 | +import net.schmizz.sshj.userauth.password.Resource; |
| 7 | +import org.jetbrains.annotations.NotNull; |
| 8 | +import org.junit.Assert; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.regex.Pattern; |
| 13 | + |
| 14 | +public class PasswordResponseProviderTest { |
| 15 | + private static final char[] PASSWORD = "the_password".toCharArray(); |
| 16 | + private static final AccountResource ACCOUNT_RESOURCE = new AccountResource("user", "host"); |
| 17 | + |
| 18 | + @Test |
| 19 | + public void shouldMatchCommonPrompts() { |
| 20 | + PasswordResponseProvider responseProvider = createDefaultResponseProvider(false); |
| 21 | + shouldMatch(responseProvider, "Password: "); |
| 22 | + shouldMatch(responseProvider, "password: "); |
| 23 | + shouldMatch(responseProvider, "Password:"); |
| 24 | + shouldMatch(responseProvider, "password:"); |
| 25 | + shouldMatch(responseProvider, "user@host's Password: "); |
| 26 | + shouldMatch(responseProvider, "user@host's password: "); |
| 27 | + shouldMatch(responseProvider, "user@host's Password:"); |
| 28 | + shouldMatch(responseProvider, "user@host's password:"); |
| 29 | + shouldMatch(responseProvider, "user@host: Password: "); |
| 30 | + shouldMatch(responseProvider, "(user@host) Password: "); |
| 31 | + shouldMatch(responseProvider, "any prefix Password for user@host: "); |
| 32 | + shouldMatch(responseProvider, "any prefix password for user@host: "); |
| 33 | + shouldMatch(responseProvider, "any prefix Password for user@host:"); |
| 34 | + shouldMatch(responseProvider, "any prefix password for user@host:"); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void shouldNotMatchOtherPrompts() { |
| 39 | + PasswordResponseProvider responseProvider = createDefaultResponseProvider(false); |
| 40 | + shouldNotMatch(responseProvider, "Password"); |
| 41 | + shouldNotMatch(responseProvider, "password"); |
| 42 | + shouldNotMatch(responseProvider, "Password: "); |
| 43 | + shouldNotMatch(responseProvider, "password: suffix"); |
| 44 | + shouldNotMatch(responseProvider, "Password of user@host:"); |
| 45 | + shouldNotMatch(responseProvider, ""); |
| 46 | + shouldNotMatch(responseProvider, "password :"); |
| 47 | + shouldNotMatch(responseProvider, "something else"); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void shouldPassRetry() { |
| 52 | + Assert.assertFalse(createDefaultResponseProvider(false).shouldRetry()); |
| 53 | + Assert.assertTrue(createDefaultResponseProvider(true).shouldRetry()); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void shouldHaveNoSubmethods() { |
| 58 | + Assert.assertEquals(createDefaultResponseProvider(true).getSubmethods(), Collections.emptyList()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void shouldWorkWithCustomPattern() { |
| 63 | + PasswordFinder passwordFinder = new TestPasswordFinder(true); |
| 64 | + PasswordResponseProvider responseProvider = new PasswordResponseProvider(passwordFinder, Pattern.compile(".*custom.*")); |
| 65 | + responseProvider.init(ACCOUNT_RESOURCE, "name", "instruction"); |
| 66 | + shouldMatch(responseProvider, "prefix custom suffix: "); |
| 67 | + shouldNotMatch(responseProvider, "something else"); |
| 68 | + } |
| 69 | + |
| 70 | + private static void shouldMatch(PasswordResponseProvider responseProvider, String prompt) { |
| 71 | + checkPrompt(responseProvider, prompt, PASSWORD); |
| 72 | + } |
| 73 | + |
| 74 | + private static void shouldNotMatch(PasswordResponseProvider responseProvider, String prompt) { |
| 75 | + checkPrompt(responseProvider, prompt, new char[0]); |
| 76 | + } |
| 77 | + |
| 78 | + private static void checkPrompt(PasswordResponseProvider responseProvider, String prompt, char[] expected) { |
| 79 | + Assert.assertArrayEquals("Prompt '" + prompt + "'", expected, responseProvider.getResponse(prompt, false)); |
| 80 | + } |
| 81 | + |
| 82 | + @NotNull |
| 83 | + private static PasswordResponseProvider createDefaultResponseProvider(final boolean shouldRetry) { |
| 84 | + PasswordFinder passwordFinder = new TestPasswordFinder(shouldRetry); |
| 85 | + PasswordResponseProvider responseProvider = new PasswordResponseProvider(passwordFinder); |
| 86 | + responseProvider.init(ACCOUNT_RESOURCE, "name", "instruction"); |
| 87 | + return responseProvider; |
| 88 | + } |
| 89 | + |
| 90 | + private static class TestPasswordFinder implements PasswordFinder { |
| 91 | + private final boolean shouldRetry; |
| 92 | + |
| 93 | + public TestPasswordFinder(boolean shouldRetry) { |
| 94 | + this.shouldRetry = shouldRetry; |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public char[] reqPassword(Resource<?> resource) { |
| 99 | + Assert.assertEquals(resource, ACCOUNT_RESOURCE); |
| 100 | + return PASSWORD; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public boolean shouldRetry(Resource<?> resource) { |
| 105 | + Assert.assertEquals(resource, ACCOUNT_RESOURCE); |
| 106 | + return shouldRetry; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments