Mercurial > hg > thermostat-ng > agent
changeset 2699:82eec2e341a0
Remove unused code
review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-June/023833.html
reviewed-by: jerboaa
author | Mario Torre <neugens.limasoftware@gmail.com> |
---|---|
date | Thu, 22 Jun 2017 14:18:56 +0200 |
parents | 11a6b082cd1e |
children | 4d45a6637faa |
files | common/core/src/main/java/com/redhat/thermostat/common/tools/StorageAuthInfoGetter.java common/core/src/test/java/com/redhat/thermostat/common/tools/StorageAuthInfoGetterTest.java launcher/src/main/java/com/redhat/thermostat/launcher/InteractiveStorageCredentials.java launcher/src/test/java/com/redhat/thermostat/launcher/InteractiveStorageCredentialsTest.java |
diffstat | 4 files changed, 0 insertions(+), 503 deletions(-) [+] |
line wrap: on
line diff
--- a/common/core/src/main/java/com/redhat/thermostat/common/tools/StorageAuthInfoGetter.java Mon Jun 19 12:15:22 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,145 +0,0 @@ -/* - * Copyright 2012-2017 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.common.tools; - -import java.io.IOException; -import java.util.Arrays; - -import jline.console.ConsoleReader; - -import com.redhat.thermostat.common.cli.Console; -import com.redhat.thermostat.common.internal.LocaleResources; -import com.redhat.thermostat.shared.locale.LocalizedString; -import com.redhat.thermostat.shared.locale.Translate; - -/** - * Utility class to isolate the user-facing strings necessary when prompting user - * for authentication parameters for storage connection. Provides simple convenience - * wrappers for jline.console.ConsoleReader methods, and localization of prompt. - */ -public class StorageAuthInfoGetter { - - private static final int EOF = -1; - private static final int PW_SIZE_INCREMENT = 16; - - private final ConsoleReader reader; - private final Translate<LocaleResources> t; - private final LocalizedString userPrompt; - private final LocalizedString passwordPrompt; - - /** - * Constructor. Allows for setting of prompt(s). - * @param console The console to use. - * @param userPrompt The prompt printed when asking for username. - * @param passwordPrompt The prompt printed when asking for password. - * @throws IOException - */ - public StorageAuthInfoGetter(Console console, LocalizedString userPrompt, - LocalizedString passwordPrompt) throws IOException { - reader = new ConsoleReader(console.getInput(), console.getOutput()); - t = LocaleResources.createLocalizer(); - this.passwordPrompt = passwordPrompt; - this.userPrompt = userPrompt; - } - - public StorageAuthInfoGetter(Console console) throws IOException { - this(console, null, null); - } - - /** - * Prompt the user for username necessary for connecting to a given url. - * @param url - * @return The username entered by the user. This could be the empty string. - * @throws IOException - */ - public String getUserName(String url) throws IOException { - LocalizedString prompt = userPrompt; - if (prompt == null) { - prompt = t.localize(LocaleResources.USERNAME_PROMPT, url); - } - String name = reader.readLine(prompt.getContents()); - return name; - } - - /** - * Prompt the user for password necessary for connecting to a given url. - * The caller is responsible for clearing the char[] to minimize the time during - * which the password is available in cleartext in the heap. - * @param url - * @return The password entered by the user. This could be the empty string. - * @throws IOException - */ - public char[] getPassword(String url) throws IOException { - LocalizedString prompt = passwordPrompt; - if (prompt == null) { - prompt = t.localize(LocaleResources.PASSWORD_PROMPT, url); - } - char[] password = new char[PW_SIZE_INCREMENT]; - reader.setHistoryEnabled(false); - reader.print(prompt.getContents()); - reader.flush(); - Character oldEcho = reader.getEchoCharacter(); - reader.setEchoCharacter('\0'); - int pwChar = reader.readCharacter(); - if (pwChar == EOF) { - return null; - } - int length = 0; - while ((char) pwChar != '\n' && (char) pwChar != '\r' && pwChar != EOF) { - password[length] = (char) pwChar; - length++; - if (length >= password.length) { - password = secureCopyCharArray(password, length + PW_SIZE_INCREMENT); - } - pwChar = reader.readCharacter(); - } - reader.setEchoCharacter(oldEcho); - reader.setHistoryEnabled(true); - reader.println(); - reader.flush(); - password = secureCopyCharArray(password, length); - return password; - } - - // returns new array, fills original with '\0' - private char[] secureCopyCharArray(char[] original, int newLength) { - char[] newArray = Arrays.copyOf(original, newLength); - Arrays.fill(original, '\0'); - return newArray; - } -} -
--- a/common/core/src/test/java/com/redhat/thermostat/common/tools/StorageAuthInfoGetterTest.java Mon Jun 19 12:15:22 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,135 +0,0 @@ -/* - * Copyright 2012-2017 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.common.tools; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.PrintStream; - -import org.junit.Before; -import org.junit.Test; - -import com.redhat.thermostat.common.cli.Console; - -public class StorageAuthInfoGetterTest { - - private Console console; - - @Before - public void setUp() throws IOException { - console = mock(Console.class); - when(console.getOutput()).thenReturn(mock(PrintStream.class)); - } - - /* - * This must not loop infinitely. - */ - @Test - public void testGetPasswordEmpty() throws IOException { - String input = ""; - ByteArrayInputStream bin = new ByteArrayInputStream(input.getBytes()); - when(console.getInput()).thenReturn(bin); - StorageAuthInfoGetter getter = new StorageAuthInfoGetter(console); - char[] password = getter.getPassword("no matter"); - assertNull(password); - } - - @Test - public void testGetUsernameEmpty() throws IOException { - String input = ""; - ByteArrayInputStream bin = new ByteArrayInputStream(input.getBytes()); - when(console.getInput()).thenReturn(bin); - StorageAuthInfoGetter getter = new StorageAuthInfoGetter(console); - String username = getter.getUserName("no matter"); - assertNull(username); - } - - @Test - public void testGetUserNameCarriageReturn() throws IOException { - testGetUsername("user\r\n", "user"); - } - - @Test - public void testGetUserNameNewLine() throws IOException { - testGetUsername("user\n", "user"); - } - - @Test - public void testGetUserNameNoNewLine() throws IOException { - testGetUsername("user", null); - } - - private void testGetUsername(String input, String expectedUsername) throws IOException { - ByteArrayInputStream bin = new ByteArrayInputStream(input.getBytes()); - when(console.getInput()).thenReturn(bin); - StorageAuthInfoGetter getter = new StorageAuthInfoGetter(console); - assertEquals(expectedUsername, getter.getUserName("url_doesn't_matter")); - } - - @Test - public void testGetPasswordCarriageReturn() throws IOException { - testGetPassword("pass\r\n", "pass"); - } - - @Test - public void testGetPasswordNewLine() throws IOException { - testGetPassword("pass\n", "pass"); - } - - @Test - public void testGetPasswordNoNewLine() throws IOException { - testGetPassword("pass?", "pass?"); - } - - @Test - public void testGetPasswordLongerThanIncrement() throws IOException { - testGetPassword("pass|pass|pass|pass\n", "pass|pass|pass|pass"); - } - - private void testGetPassword(String input, String expectedPassword) throws IOException { - ByteArrayInputStream bin = new ByteArrayInputStream(input.getBytes()); - when(console.getInput()).thenReturn(bin); - StorageAuthInfoGetter getter = new StorageAuthInfoGetter(console); - assertEquals(expectedPassword, new String(getter.getPassword("url_doesn't_matter"))); - } -} -
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/InteractiveStorageCredentials.java Mon Jun 19 12:15:22 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,117 +0,0 @@ -/* - * Copyright 2012-2017 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.launcher; - -import java.io.IOException; -import java.util.logging.Logger; - -import com.redhat.thermostat.common.cli.Console; -import com.redhat.thermostat.common.config.ClientPreferences; -import com.redhat.thermostat.common.tools.StorageAuthInfoGetter; -import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.launcher.internal.LocaleResources; -import com.redhat.thermostat.shared.locale.Translate; -import com.redhat.thermostat.storage.core.StorageCredentials; - -public class InteractiveStorageCredentials implements StorageCredentials { - - private static final Translate<LocaleResources> t = LocaleResources.createLocalizer(); - private static final Logger logger = LoggingUtils.getLogger(InteractiveStorageCredentials.class); - - private ClientPreferences prefs; - private String url; - private StorageAuthInfoGetter getter; - - public InteractiveStorageCredentials(ClientPreferences prefs, String url, Console console) { - this.prefs = prefs; - this.url = url; - try { - this.getter = new StorageAuthInfoGetter(console); - } catch (IOException e) { - String message = "IOException while creating interactive authentication credential getter."; - logger.severe(message); - throw new InteractiveException(message, e); - } - } - - @Override - public String getUsername() { - String username = null; - if (url.equals(prefs.getConnectionUrl())) { - username = prefs.getUserName(); - } - if (username == null) { - try { - username = getter.getUserName(url); - } catch (IOException e) { - throw new InteractiveException(t.localize(LocaleResources.LAUNCHER_USER_AUTH_PROMPT_ERROR).getContents(), e); - } - } - // getter.getUsername() might return null on short input - if (username == null) { - return null; - } else { - return username.length() == 0 ? null : username; - } - } - - @Override - public char[] getPassword() { - char[] password = null; - try { - password = getter.getPassword(url); - } catch (IOException e) { - throw new InteractiveException(t.localize(LocaleResources.LAUNCHER_USER_AUTH_PROMPT_ERROR).getContents(), e); - } - // getter.getPassword() might return null on short input - if (password == null) { - return null; - } else { - return password.length == 0 ? null : password; - } - } - - class InteractiveException extends RuntimeException { - - private static final long serialVersionUID = -7921653973987512258L; - - InteractiveException(String message, Exception cause) { - super(message, cause); - } - } -} -
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/InteractiveStorageCredentialsTest.java Mon Jun 19 12:15:22 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,106 +0,0 @@ -/* - * Copyright 2012-2017 Red Hat, Inc. - * - * This file is part of Thermostat. - * - * Thermostat is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published - * by the Free Software Foundation; either version 2, or (at your - * option) any later version. - * - * Thermostat is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Thermostat; see the file COPYING. If not see - * <http://www.gnu.org/licenses/>. - * - * Linking this code with other modules is making a combined work - * based on this code. Thus, the terms and conditions of the GNU - * General Public License cover the whole combination. - * - * As a special exception, the copyright holders of this code give - * you permission to link this code with independent modules to - * produce an executable, regardless of the license terms of these - * independent modules, and to copy and distribute the resulting - * executable under terms of your choice, provided that you also - * meet, for each linked independent module, the terms and conditions - * of the license of that module. An independent module is a module - * which is not derived from or based on this code. If you modify - * this code, you may extend this exception to your version of the - * library, but you are not obligated to do so. If you do not wish - * to do so, delete this exception statement from your version. - */ - -package com.redhat.thermostat.launcher; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - -import org.junit.Before; -import org.junit.Test; - -import com.redhat.thermostat.common.cli.Console; -import com.redhat.thermostat.common.config.ClientPreferences; - -public class InteractiveStorageCredentialsTest { - - private static final String URL = "http://example.com/thermostat/storage"; - private static final String DIFFERENT_URL = "http://example.com/thermostat/storage"; - - private Console console; - private ByteArrayOutputStream baout; - - @Before - public void setup() { - console = mock(Console.class); - baout = new ByteArrayOutputStream(); - when(console.getOutput()).thenReturn(new PrintStream(baout)); - } - - @Test - public void canGetUsernameFromPrefs() { - String input = ""; // should never be used - when(console.getInput()).thenReturn(new ByteArrayInputStream(input.getBytes())); - String username = "foouser"; - ClientPreferences prefs = mock(ClientPreferences.class); - when(prefs.getUserName()).thenReturn(username); - when(prefs.getConnectionUrl()).thenReturn(URL); - InteractiveStorageCredentials creds = new InteractiveStorageCredentials(prefs, URL, console); - String actual = creds.getUsername(); - assertEquals(username, actual); - } - - @Test - public void promptsForUsernameIfNotPresentInPreferences() { - String username = "someuser"; - String input = String.format("%s\n", username); - when(console.getInput()).thenReturn(new ByteArrayInputStream(input.getBytes())); - ClientPreferences prefs = mock(ClientPreferences.class); - when(prefs.getConnectionUrl()).thenReturn(DIFFERENT_URL); // something *not* URL - InteractiveStorageCredentials creds = new InteractiveStorageCredentials(prefs, URL, console); - String actual = creds.getUsername(); - assertEquals(username, actual); - } - - @Test - public void promptsForPasswordIfNotPresentInKeyring() { - String password = "somepassword"; - String input = String.format("%s\n", password); - when(console.getInput()).thenReturn(new ByteArrayInputStream(input.getBytes())); - ClientPreferences prefs = mock(ClientPreferences.class); - when(prefs.getConnectionUrl()).thenReturn(DIFFERENT_URL); // something *not* URL - InteractiveStorageCredentials creds = new InteractiveStorageCredentials(prefs, URL, console); - char[] actual = creds.getPassword(); - assertNotNull("expected password to be read from prompt", actual); - assertEquals(password, new String(actual)); - } -}