summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAidan Skinner <aidan@apache.org>2008-12-09 17:03:30 +0000
committerAidan Skinner <aidan@apache.org>2008-12-09 17:03:30 +0000
commitab8ee239590430caf25bcce32fe848575977b94c (patch)
treec8dbf4b69257a3188891c9be141aaf5c47847856
parentf747d999f55330e4318f0bba5405c33b6274edee (diff)
downloadqpid-python-ab8ee239590430caf25bcce32fe848575977b94c.tar.gz
QPID-1503: Add more tests for Base64MD5PasswordFilePrincipalDatabase, fix buglets.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@724779 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--java/broker/src/main/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabase.java29
-rw-r--r--java/broker/src/test/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabaseTest.java41
2 files changed, 61 insertions, 9 deletions
diff --git a/java/broker/src/main/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabase.java b/java/broker/src/main/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabase.java
index a2a0be926e..cca9deb6da 100644
--- a/java/broker/src/main/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabase.java
+++ b/java/broker/src/main/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabase.java
@@ -109,6 +109,7 @@ public class Base64MD5PasswordFilePrincipalDatabase implements PrincipalDatabase
/**
* SASL Callback Mechanism - sets the Password in the PasswordCallback based on the value in the PasswordFile
+ * If you want to change the password for a user, use updatePassword instead.
*
* @param principal The Principal to set the password for
* @param callback The PasswordCallback to call setPassword on
@@ -152,17 +153,31 @@ public class Base64MD5PasswordFilePrincipalDatabase implements PrincipalDatabase
{
char[] pwd = lookupPassword(principal);
- int index = 0;
- boolean verified = true;
-
- while (verified & index < password.length)
+ return compareCharArray(pwd, password);
+ }
+
+ private boolean compareCharArray(char[] a, char[] b)
+ {
+ boolean equal = false;
+ if (a.length == b.length)
{
- verified = (pwd[index] == password[index]);
- index++;
+ equal = true;
+ int index = 0;
+ while (equal && index < a.length)
+ {
+ equal = a[index] == b[index];
+ index++;
+ }
}
- return verified;
+ return equal;
}
+ /**
+ * Changes the password for the specified user
+ *
+ * @param principal to change the password for
+ * @param password plaintext password to set the password too
+ */
public boolean updatePassword(Principal principal, char[] password) throws AccountNotFoundException
{
HashedUser user = _users.get(principal.getName());
diff --git a/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabaseTest.java b/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabaseTest.java
index ededb1cb26..b5034d9f5d 100644
--- a/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabaseTest.java
+++ b/java/broker/src/test/java/org/apache/qpid/server/security/auth/database/Base64MD5PasswordFilePrincipalDatabaseTest.java
@@ -23,6 +23,9 @@ package org.apache.qpid.server.security.auth.database;
import junit.framework.TestCase;
import javax.security.auth.login.AccountNotFoundException;
+
+import org.apache.qpid.server.security.auth.sasl.UsernamePrincipal;
+
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
@@ -37,13 +40,20 @@ import java.util.regex.Pattern;
public class Base64MD5PasswordFilePrincipalDatabaseTest extends TestCase
{
- Base64MD5PasswordFilePrincipalDatabase _database;
private static final String TEST_COMMENT = "# Test Comment";
private String USERNAME = "testUser";
+ private String _username = this.getClass().getName()+"username";
+ private char[] _password = "password".toCharArray();
+ private Principal _principal = new UsernamePrincipal(_username);
+ private Base64MD5PasswordFilePrincipalDatabase _database;
+ private File _pwdFile;
- public void setUp()
+ public void setUp() throws Exception
{
_database = new Base64MD5PasswordFilePrincipalDatabase();
+ _pwdFile = File.createTempFile(this.getClass().getName(), "pwd");
+ _pwdFile.deleteOnExit();
+ _database.setPasswordFile(_pwdFile.getAbsolutePath());
}
private File createPasswordFile(int commentLines, int users)
@@ -297,4 +307,31 @@ public class Base64MD5PasswordFilePrincipalDatabaseTest extends TestCase
testFile.delete();
}
+
+ public void testCreateUserPrincipal() throws IOException
+ {
+ _database.createPrincipal(_principal, _password);
+ Principal newPrincipal = _database.getUser(_username);
+ assertNotNull(newPrincipal);
+ assertEquals(_principal.getName(), newPrincipal.getName());
+ }
+
+ public void testVerifyPassword() throws IOException, AccountNotFoundException
+ {
+ testCreateUserPrincipal();
+ //assertFalse(_pwdDB.verifyPassword(_username, null));
+ assertFalse(_database.verifyPassword(_username, new char[]{}));
+ assertFalse(_database.verifyPassword(_username, "massword".toCharArray()));
+ assertTrue(_database.verifyPassword(_username, _password));
+ }
+
+ public void testUpdatePassword() throws IOException, AccountNotFoundException
+ {
+ testCreateUserPrincipal();
+ char[] newPwd = "newpassword".toCharArray();
+ _database.updatePassword(_principal, newPwd);
+ assertFalse(_database.verifyPassword(_username, _password));
+ assertTrue(_database.verifyPassword(_username, newPwd));
+ }
+
}