changeset 1401:64670571181d

agent.auth file fails to parse if NO new line at EOF. Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2014-May/009862.html PR1789
author Severin Gehwolf <sgehwolf@redhat.com>
date Fri, 23 May 2014 19:28:27 +0200
parents 9a8d79ed2844
children 6c613257d90c
files agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentStorageCredentials.java agent/core/src/test/java/com/redhat/thermostat/agent/config/AgentStorageCredentialsTest.java
diffstat 2 files changed, 13 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentStorageCredentials.java	Thu May 22 19:08:03 2014 -0600
+++ b/agent/core/src/main/java/com/redhat/thermostat/agent/config/AgentStorageCredentials.java	Fri May 23 19:28:27 2014 +0200
@@ -277,7 +277,10 @@
     }
 
     private int positionOf(char character, char[] data, int start, int end) {
-        int position = -1;
+        // Corner case. No new line at EOF. In that case the base case is
+        // to set the returned position to the full length of the data
+        // otherwise we get a short array copy in getValue().
+        int position = end;
         for (int possible = start; possible < data.length && possible <= end; possible++) {
             if (data[possible] == character) {
                 position = possible;
--- a/agent/core/src/test/java/com/redhat/thermostat/agent/config/AgentStorageCredentialsTest.java	Thu May 22 19:08:03 2014 -0600
+++ b/agent/core/src/test/java/com/redhat/thermostat/agent/config/AgentStorageCredentialsTest.java	Fri May 23 19:28:27 2014 +0200
@@ -43,6 +43,15 @@
 import org.junit.Test;
 
 public class AgentStorageCredentialsTest {
+    
+    @Test
+    public void testAuthConfigNoNewlineAtEOF() {
+        Reader reader = new StringReader("username=user\npassword=pass");
+        AgentStorageCredentials creds = new AgentStorageCredentials(reader);
+        Assert.assertEquals("user", creds.getUsername());
+        Assert.assertEquals(4, creds.getPassword().length);
+        Assert.assertEquals("pass", new String(creds.getPassword()));
+    }
 
     @Test
     public void testAuthConfig() {