11package fr .xephi .authme .security .crypts ;
22
3- import static org .junit .Assert .assertFalse ;
4- import static org .junit .Assert .assertTrue ;
3+ import fr .xephi .authme .security .PasswordSecurity ;
4+ import org .junit .Ignore ;
5+ import org .junit .Test ;
56
67import java .security .NoSuchAlgorithmException ;
78import java .util .HashMap ;
89import java .util .Map ;
910
10- import org .junit .Test ;
11-
12- import fr .xephi .authme .security .PasswordSecurity ;
11+ import static org .junit .Assert .assertFalse ;
12+ import static org .junit .Assert .assertTrue ;
1313
1414/**
1515 * Test for implementations of {@link EncryptionMethod}.
1818public abstract class AbstractEncryptionMethodTest {
1919
2020 public static final String USERNAME = "Test_Player00" ;
21- public static final String [] GIVEN_PASSWORDS = { "password" , "PassWord1" , "&^%te$t?Pw@_" , "âË_3(íù*" };
22- private static final String [] INTERNAL_PASSWORDS = { "test1234" , "Ab_C73" , "(!#&$~`_-Aa0" , "Ûïé1&?+A" };
21+ public static final String [] GIVEN_PASSWORDS = {"password" , "PassWord1" , "&^%te$t?Pw@_" , "âË_3(íù*" };
22+ private static final String [] INTERNAL_PASSWORDS = {"test1234" , "Ab_C73" , "(!#&$~`_-Aa0" , "Ûïé1&?+A" };
2323
2424 private EncryptionMethod method ;
2525 private Map <String , String > hashes ;
2626
27- public AbstractEncryptionMethodTest (EncryptionMethod method , String hash0 ,
28- String hash1 , String hash2 , String hash3 ) {
27+ public AbstractEncryptionMethodTest (EncryptionMethod method , String hash0 , String hash1 ,
28+ String hash2 , String hash3 ) {
2929 this .method = method ;
3030 hashes = new HashMap <>();
3131 hashes .put (GIVEN_PASSWORDS [0 ], hash0 );
@@ -35,36 +35,41 @@ public AbstractEncryptionMethodTest(EncryptionMethod method, String hash0,
3535 }
3636
3737 @ Test
38+ @ Ignore
39+ // TODO #375 Fix and unignore tests
3840 public void testGivenPasswords () {
39- /*
40- * for (String password : GIVEN_PASSWORDS) { try { assertTrue(
41- * "Hash for password '" + password + "' should match",
42- * method.comparePassword(hashes.get(password), password, USERNAME)); }
43- * catch (NoSuchAlgorithmException e) { throw new IllegalStateException(
44- * "EncryptionMethod '" + method + "' threw exception", e); } }
45- */
41+ for (String password : GIVEN_PASSWORDS ) {
42+ try {
43+ assertTrue ("Hash for password '" + password + "' should match" ,
44+ method .comparePassword (hashes .get (password ), password , USERNAME ));
45+ } catch (NoSuchAlgorithmException e ) {
46+ throw new IllegalStateException ("EncryptionMethod '" + method + "' threw exception" , e );
47+ }
48+ }
4649 }
4750
4851 @ Test
4952 public void testPasswordEquality () {
5053 for (String password : INTERNAL_PASSWORDS ) {
5154 try {
5255 String hash = method .computeHash (password , getSalt (method ), USERNAME );
53- assertTrue ("Generated hash for '" + password + "' should match password (hash = '" + hash + "')" , method .comparePassword (hash , password , USERNAME ));
56+ assertTrue ("Generated hash for '" + password + "' should match password (hash = '" + hash + "')" ,
57+ method .comparePassword (hash , password , USERNAME ));
5458 if (!password .equals (password .toLowerCase ())) {
55- assertFalse ("Lower-case of '" + password + "' should not match generated hash '" + hash + "'" , method .comparePassword (hash , password .toLowerCase (), USERNAME ));
59+ assertFalse ("Lower-case of '" + password + "' should not match generated hash '" + hash + "'" ,
60+ method .comparePassword (hash , password .toLowerCase (), USERNAME ));
5661 }
5762 if (!password .equals (password .toUpperCase ())) {
58- assertFalse ("Upper-case of '" + password + "' should not match generated hash '" + hash + "'" , method .comparePassword (hash , password .toUpperCase (), USERNAME ));
63+ assertFalse ("Upper-case of '" + password + "' should not match generated hash '" + hash + "'" ,
64+ method .comparePassword (hash , password .toUpperCase (), USERNAME ));
5965 }
6066 } catch (NoSuchAlgorithmException e ) {
6167 throw new IllegalStateException ("EncryptionMethod '" + method + "' threw exception" , e );
6268 }
6369 }
6470 }
6571
66- // @org.junit.Test public void a() {
67- // AbstractEncryptionMethodTest.generateTest(); }
72+ // @org.junit.Test public void a() { AbstractEncryptionMethodTest.generateTest(); }
6873 // TODO #364: Remove this method
6974 static void generateTest (EncryptionMethod method ) {
7075 String className = method .getClass ().getSimpleName ();
@@ -79,7 +84,8 @@ static void generateTest(EncryptionMethod method) {
7984 delim = "); " ;
8085 }
8186 try {
82- System .out .println ("\t \t \" " + method .computeHash (password , getSalt (method ), USERNAME ) + "\" " + delim + "// " + password );
87+ System .out .println ("\t \t \" " + method .computeHash (password , getSalt (method ), USERNAME )
88+ + "\" " + delim + "// " + password );
8389 } catch (NoSuchAlgorithmException e ) {
8490 throw new RuntimeException ("Could not generate hash" , e );
8591 }
@@ -88,28 +94,31 @@ static void generateTest(EncryptionMethod method) {
8894 System .out .println ("\n }" );
8995 }
9096
91- // TODO #358: Remove this method and use the new salt method on the
92- // interface
97+ // TODO #358: Remove this method and use the new salt method on the interface
9398 private static String getSalt (EncryptionMethod method ) {
9499 try {
95100 if (method instanceof BCRYPT ) {
96101 return BCRYPT .gensalt ();
97- } else if (method instanceof MD5 || method instanceof WORDPRESS || method instanceof SMF || method instanceof SHA512 || method instanceof SHA1 || method instanceof ROYALAUTH || method instanceof DOUBLEMD5 ) {
102+ } else if (method instanceof MD5 || method instanceof WORDPRESS || method instanceof SMF
103+ || method instanceof SHA512 || method instanceof SHA1 || method instanceof ROYALAUTH
104+ || method instanceof DOUBLEMD5 ) {
98105 return "" ;
99106 } else if (method instanceof JOOMLA || method instanceof SALTEDSHA512 ) {
100107 return PasswordSecurity .createSalt (32 );
101- } else if (method instanceof SHA256 || method instanceof PHPBB || method instanceof WHIRLPOOL || method instanceof MD5VB || method instanceof BCRYPT2Y ) {
108+ } else if (method instanceof SHA256 || method instanceof PHPBB || method instanceof WHIRLPOOL
109+ || method instanceof MD5VB || method instanceof BCRYPT2Y ) {
102110 return PasswordSecurity .createSalt (16 );
103111 } else if (method instanceof WBB3 ) {
104112 return PasswordSecurity .createSalt (40 );
105- } else if (method instanceof XAUTH || method instanceof CryptPBKDF2Django || method instanceof CryptPBKDF2 ) {
113+ } else if (method instanceof XAUTH || method instanceof CryptPBKDF2Django
114+ || method instanceof CryptPBKDF2 ) {
106115 return PasswordSecurity .createSalt (12 );
107116 } else if (method instanceof WBB4 ) {
108117 return BCRYPT .gensalt (8 );
109118 }
110119 } catch (NoSuchAlgorithmException e ) {
111- throw new RuntimeException (e );
112- }
120+ throw new RuntimeException (e );
121+ }
113122 throw new RuntimeException ("Unknown EncryptionMethod for salt generation" );
114123 }
115124
0 commit comments