harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r570311 - in /harmony/enhanced/classlib/trunk/modules/auth/src: main/java/common/org/apache/harmony/auth/module/LoginModuleUtils.java test/java/common/org/apache/harmony/auth/tests/module/LoginModuleUtilsTest.java
Date Tue, 28 Aug 2007 05:43:33 GMT
Author: leoli
Date: Mon Aug 27 22:43:33 2007
New Revision: 570311

URL: http://svn.apache.org/viewvc?rev=570311&view=rev
Log:
[classlib][auth]Add a util class org.apache.harmony.auth.module.LoginModuleUtils.

Added:
    harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/module/LoginModuleUtils.java
  (with props)
    harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/module/LoginModuleUtilsTest.java
  (with props)

Added: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/module/LoginModuleUtils.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/module/LoginModuleUtils.java?rev=570311&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/module/LoginModuleUtils.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/module/LoginModuleUtils.java
Mon Aug 27 22:43:33 2007
@@ -0,0 +1,80 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.harmony.auth.module;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public class LoginModuleUtils {
+
+    /**
+     * Reads the password stored in an inputstream to a char array.
+     * 
+     * @param in
+     *            an inputstream which stores the password.
+     * @return a char array which contains the password.
+     * @throws IOException
+     */
+    public static char[] getPassword(InputStream in) throws IOException {
+        char[] buffer = new char[512];
+
+        // 1.Just ASCII encoding is supported. bytes read from inputstream is
+        // cast and put into char array.
+        // 2.just read one line.
+        int length = 0;
+        int nextChar = -1;
+        boolean hasCarriage = false;
+
+        do {
+            nextChar = in.read();
+            if (nextChar == -1 || nextChar == '\n') {
+                break;
+            }
+
+            if (hasCarriage) {
+                buffer = appendChars(buffer, '\r', length++);
+                hasCarriage = false;
+            }
+
+            if (nextChar == '\r') {
+                hasCarriage = true;
+            } else {
+                buffer = appendChars(buffer, (char) nextChar, length++);
+            }
+
+        } while (true);
+
+        if (length == 0) {
+            return null;
+        }
+
+        char[] password = new char[length];
+        System.arraycopy(buffer, 0, password, 0, length);
+        return password;
+    }
+
+    private static char[] appendChars(char[] src, char c, int position) {
+        char[] dest = src;
+        if (position == src.length) {
+            dest = new char[src.length * 2];
+            System.arraycopy(src, 0, dest, 0, src.length);
+        }
+        dest[position] = c;
+        return dest;
+    }
+}

Propchange: harmony/enhanced/classlib/trunk/modules/auth/src/main/java/common/org/apache/harmony/auth/module/LoginModuleUtils.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/module/LoginModuleUtilsTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/module/LoginModuleUtilsTest.java?rev=570311&view=auto
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/module/LoginModuleUtilsTest.java
(added)
+++ harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/module/LoginModuleUtilsTest.java
Mon Aug 27 22:43:33 2007
@@ -0,0 +1,62 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.harmony.auth.tests.module;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.Arrays;
+
+import org.apache.harmony.auth.module.LoginModuleUtils;
+
+import junit.framework.TestCase;
+
+public class LoginModuleUtilsTest extends TestCase {
+
+    public void testGetPassword() throws Exception {
+        final String PASSWORD_AS_STRING = "TESTPASSWORD";
+        final char[] PASSWORD_AS_CHARS = PASSWORD_AS_STRING.toCharArray();
+
+        String password_file_content = PASSWORD_AS_STRING;
+        InputStream in = new ByteArrayInputStream(password_file_content
+                .getBytes());
+        char[] password = LoginModuleUtils.getPassword(in);
+        assertTrue(Arrays.equals(PASSWORD_AS_CHARS, password));
+
+        password_file_content = "TESTPASSWORD" + "\nNONsense";
+        in = new ByteArrayInputStream(password_file_content.getBytes());
+        password = LoginModuleUtils.getPassword(in);
+        assertTrue(Arrays.equals(PASSWORD_AS_CHARS, password));
+
+        password_file_content = "TESTPASSWORD" + "\r\nNONsense";
+        in = new ByteArrayInputStream(password_file_content.getBytes());
+        password = LoginModuleUtils.getPassword(in);
+        assertTrue(Arrays.equals(PASSWORD_AS_CHARS, password));
+
+        password_file_content = "TESTPASSWORD" + "\n\rNONsense";
+        in = new ByteArrayInputStream(password_file_content.getBytes());
+        password = LoginModuleUtils.getPassword(in);
+        assertTrue(Arrays.equals(PASSWORD_AS_CHARS, password));
+
+        password_file_content = "TESTPASSWORD" + "\r\r\nNONsense";
+        in = new ByteArrayInputStream(password_file_content.getBytes());
+        password = LoginModuleUtils.getPassword(in);
+        String expectedString = PASSWORD_AS_STRING + "\r";
+        assertTrue(Arrays.equals(expectedString.toCharArray(), password));
+    }
+
+}

Propchange: harmony/enhanced/classlib/trunk/modules/auth/src/test/java/common/org/apache/harmony/auth/tests/module/LoginModuleUtilsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message