Fix minor bug in strength checker and add unit test.
authorFelix Dörre <felix@dogcraft.de>
Tue, 1 Jul 2014 20:48:59 +0000 (22:48 +0200)
committerFelix Dörre <felix@dogcraft.de>
Tue, 1 Jul 2014 20:48:59 +0000 (22:48 +0200)
src/org/cacert/gigi/util/PasswordStrengthChecker.java
tests/org/cacert/gigi/util/TestPasswordStrengthChecker.java [new file with mode: 0644]

index efc776ef08da4836d2c30701966b87463a38c776..07898f263b681715f141d5405ce236bf4d07efd5 100644 (file)
@@ -9,7 +9,7 @@ public class PasswordStrengthChecker {
        static Pattern lower = Pattern.compile("[a-z]");
        static Pattern upper = Pattern.compile("[A-Z]");
        static Pattern whitespace = Pattern.compile("\\s");
-       static Pattern special = Pattern.compile("\\W");
+       static Pattern special = Pattern.compile("(?!\\s)\\W");
        private PasswordStrengthChecker() {
        }
        private static int checkpwlight(String pw) {
diff --git a/tests/org/cacert/gigi/util/TestPasswordStrengthChecker.java b/tests/org/cacert/gigi/util/TestPasswordStrengthChecker.java
new file mode 100644 (file)
index 0000000..91a918c
--- /dev/null
@@ -0,0 +1,64 @@
+package org.cacert.gigi.util;
+
+import org.cacert.gigi.User;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+public class TestPasswordStrengthChecker {
+       User u;
+       public TestPasswordStrengthChecker() {
+               u = new User();
+               u.setFname("fname");
+               u.setLname("lname");
+               u.setMname("mname");
+               u.setEmail("email");
+               u.setSuffix("suffix");
+       }
+       @Test
+       public void testPasswordLength() {
+               assertEquals(1, PasswordStrengthChecker.checkpw("01234", u));
+               assertEquals(2, PasswordStrengthChecker.checkpw("0123456789012345", u));
+               assertEquals(3,
+                               PasswordStrengthChecker.checkpw("012345678901234567890", u));
+               assertEquals(4, PasswordStrengthChecker.checkpw(
+                               "01234567890123456789012345", u));
+               assertEquals(5, PasswordStrengthChecker.checkpw(
+                               "0123456789012345678901234567890", u));
+       }
+       @Test
+       public void testPasswordCharTypes() {
+               assertEquals(1, PasswordStrengthChecker.checkpw("0", u));
+               assertEquals(2, PasswordStrengthChecker.checkpw("0a", u));
+               assertEquals(2, PasswordStrengthChecker.checkpw("0az", u));
+               assertEquals(3, PasswordStrengthChecker.checkpw("0azZ", u));
+               assertEquals(4, PasswordStrengthChecker.checkpw("0a zZ", u));
+               assertEquals(5, PasswordStrengthChecker.checkpw("0a. zZ", u));
+
+               assertEquals(1, PasswordStrengthChecker.checkpw(".", u));
+               assertEquals(1, PasswordStrengthChecker.checkpw(" ", u));
+               assertEquals(1, PasswordStrengthChecker.checkpw("b", u));
+               assertEquals(1, PasswordStrengthChecker.checkpw("Z", u));
+
+               assertEquals(2, PasswordStrengthChecker.checkpw("0.", u));
+               assertEquals(2, PasswordStrengthChecker.checkpw("0 ", u));
+               assertEquals(2, PasswordStrengthChecker.checkpw("0a", u));
+               assertEquals(2, PasswordStrengthChecker.checkpw("0Z", u));
+
+               assertEquals(2, PasswordStrengthChecker.checkpw(" .", u));
+               assertEquals(2, PasswordStrengthChecker.checkpw(" a", u));
+               assertEquals(2, PasswordStrengthChecker.checkpw(" Z", u));
+
+       }
+       @Test
+       public void testPasswordContains() {
+               assertEquals(-1, PasswordStrengthChecker.checkpw("fnamea", u));
+               assertEquals(-5, PasswordStrengthChecker.checkpw("na", u));
+               assertEquals(0, PasswordStrengthChecker.checkpw("1lname", u));
+               assertEquals(0, PasswordStrengthChecker.checkpw("1email", u));
+               assertEquals(-1, PasswordStrengthChecker.checkpw("mai", u));
+               assertEquals(-1, PasswordStrengthChecker.checkpw("suff", u));
+               assertEquals(0, PasswordStrengthChecker.checkpw("1suffix", u));
+
+       }
+
+}