From e5084ed8db11801d9c11ea0508f237ed392bec84 Mon Sep 17 00:00:00 2001 From: Matt Dailey Date: Fri, 7 Jul 2017 20:27:30 -0400 Subject: [PATCH] Removed Builder, and fixed call to checkFormatString --- .../password/ConsolePasswordFinder.java | 70 ++++--------------- .../password/TestConsolePasswordFinder.java | 25 +++---- 2 files changed, 24 insertions(+), 71 deletions(-) diff --git a/src/main/java/net/schmizz/sshj/userauth/password/ConsolePasswordFinder.java b/src/main/java/net/schmizz/sshj/userauth/password/ConsolePasswordFinder.java index 44b109e9..60e3bd85 100644 --- a/src/main/java/net/schmizz/sshj/userauth/password/ConsolePasswordFinder.java +++ b/src/main/java/net/schmizz/sshj/userauth/password/ConsolePasswordFinder.java @@ -21,17 +21,24 @@ import java.util.IllegalFormatException; /** A PasswordFinder that reads a password from a console */ public class ConsolePasswordFinder implements PasswordFinder { + public static final String DEFAULT_FORMAT = "Enter passphrase for %s:"; + private final Console console; private final String promptFormat; private final int maxTries; private int numTries; - public static ConsolePasswordFinderBuilder builder() { - return new ConsolePasswordFinderBuilder(); + public ConsolePasswordFinder() { + this(System.console()); + } + + public ConsolePasswordFinder(Console console) { + this(console, DEFAULT_FORMAT, 3); } public ConsolePasswordFinder(Console console, String promptFormat, int maxTries) { + checkFormatString(promptFormat); this.console = console; this.promptFormat = promptFormat; this.maxTries = maxTries; @@ -53,59 +60,12 @@ public class ConsolePasswordFinder implements PasswordFinder { return numTries < maxTries; } - public static class ConsolePasswordFinderBuilder { - private Console console; - private String promptFormat; - private int maxTries; - - /** Builder constructor should only be called from parent class */ - private ConsolePasswordFinderBuilder() { - console = System.console(); - promptFormat = "Enter passphrase for %s:"; - maxTries = 3; - } - - public ConsolePasswordFinder build() { - return new ConsolePasswordFinder(console, promptFormat, maxTries); - } - - public ConsolePasswordFinderBuilder setConsole(Console console) { - this.console = console; - return this; - } - - public Console getConsole() { - return console; - } - - /** - * @param promptFormat a StringFormatter string that may contain up to one "%s" - */ - public ConsolePasswordFinderBuilder setPromptFormat(String promptFormat) { - checkFormatString(promptFormat); - this.promptFormat = promptFormat; - return this; - } - - public String getPromptFormat() { - return promptFormat; - } - - public ConsolePasswordFinderBuilder setMaxTries(int maxTries) { - this.maxTries = maxTries; - return this; - } - - public int getMaxTries() { - return maxTries; - } - - private static void checkFormatString(String promptFormat) { - try { - String.format(promptFormat, ""); - } catch (IllegalFormatException e) { - throw new IllegalArgumentException("promptFormat must have no more than one %s and no other markers", e); - } + private static void checkFormatString(String promptFormat) { + try { + String.format(promptFormat, ""); + } catch (IllegalFormatException e) { + throw new IllegalArgumentException("promptFormat must have no more than one %s and no other markers", e); } } + } diff --git a/src/test/java/net/schmizz/sshj/userauth/password/TestConsolePasswordFinder.java b/src/test/java/net/schmizz/sshj/userauth/password/TestConsolePasswordFinder.java index f95bae38..f0bc3d17 100644 --- a/src/test/java/net/schmizz/sshj/userauth/password/TestConsolePasswordFinder.java +++ b/src/test/java/net/schmizz/sshj/userauth/password/TestConsolePasswordFinder.java @@ -23,6 +23,8 @@ import java.io.Console; public class TestConsolePasswordFinder { + private static final String FORMAT = "%s"; + @Test public void testReqPassword() { char[] expectedPassword = "password".toCharArray(); @@ -32,10 +34,7 @@ public class TestConsolePasswordFinder { .thenReturn(expectedPassword); Resource resource = Mockito.mock(Resource.class); - char[] password = ConsolePasswordFinder.builder() - .setConsole(console) - .build() - .reqPassword(resource); + char[] password = new ConsolePasswordFinder(console).reqPassword(resource); Assert.assertArrayEquals("Password should match mocked return value", expectedPassword, password); @@ -45,10 +44,7 @@ public class TestConsolePasswordFinder { @Test public void testReqPasswordNullConsole() { Resource resource = Mockito.mock(Resource.class); - char[] password = ConsolePasswordFinder.builder() - .setConsole(null) - .build() - .reqPassword(resource); + char[] password = new ConsolePasswordFinder(null, FORMAT, 1).reqPassword(resource); Assert.assertNull("Password should be null with null console", password); Mockito.verifyNoMoreInteractions(resource); @@ -57,10 +53,7 @@ public class TestConsolePasswordFinder { @Test public void testShouldRetry() { Resource resource = new PrivateKeyStringResource(""); - ConsolePasswordFinder finder = ConsolePasswordFinder.builder() - .setConsole(null) - .setMaxTries(1) - .build(); + ConsolePasswordFinder finder = new ConsolePasswordFinder(null, FORMAT, 1); Assert.assertTrue("Should allow a retry at first", finder.shouldRetry(resource)); finder.reqPassword(resource); @@ -71,20 +64,20 @@ public class TestConsolePasswordFinder { public void testPromptFormat() { Assert.assertNotNull( "Empty format should create valid ConsolePasswordFinder", - ConsolePasswordFinder.builder().setPromptFormat("").build()); + new ConsolePasswordFinder(null, "", 1)); Assert.assertNotNull( "Single-string format should create valid ConsolePasswordFinder", - ConsolePasswordFinder.builder().setPromptFormat("%s").build()); + new ConsolePasswordFinder(null, FORMAT, 1)); } @Test(expected = IllegalArgumentException.class) public void testPromptFormatTooManyMarkers() { - ConsolePasswordFinder.builder().setPromptFormat("%s%s"); + new ConsolePasswordFinder(null, "%s%s", 1); } @Test(expected = IllegalArgumentException.class) public void testPromptFormatWrongMarkerType() { - ConsolePasswordFinder.builder().setPromptFormat("%d"); + new ConsolePasswordFinder(null, "%d", 1); } }