Skip to content

Commit

Permalink
Update existing unit test.
Browse files Browse the repository at this point in the history
  • Loading branch information
fbarthez authored and darkv committed Nov 9, 2015
1 parent 3c9b429 commit e17724e
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion Tests/ERXTest/Sources/er/extensions/crypting/TestBCrypt.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,80 +97,100 @@ public static void main(String[] args) {
* Test method for 'BCrypt.hashpw(String, String)'
*/
public void testHashpw() {
System.out.print("BCrypt.hashpw(): ");
for (int i = 0; i < test_vectors.length; i++) {
String plain = test_vectors[i][0];
String salt = test_vectors[i][1];
String expected = test_vectors[i][2];
String hashed = BCrypt.hashpw(plain, salt);
assertEquals(hashed, expected);
System.out.print(".");
}
System.out.println("");
}

/**
* Test method for 'BCrypt.gensalt(int)'
*/
public void testGensaltInt() {
System.out.print("BCrypt.gensalt(log_rounds):");
for (int i = 4; i <= 12; i++) {
System.out.print(" " + Integer.toString(i) + ":");
for (int j = 0; j < test_vectors.length; j += 4) {
String plain = test_vectors[j][0];
String salt = BCrypt.gensalt(i);
String hashed1 = BCrypt.hashpw(plain, salt);
String hashed2 = BCrypt.hashpw(plain, hashed1);
assertEquals(hashed1, hashed2);
System.out.print(".");
}
}
System.out.println("");
}

/**
* Test method for 'BCrypt.gensalt()'
*/
public void testGensalt() {
System.out.print("BCrypt.gensalt(): ");
for (int i = 0; i < test_vectors.length; i += 4) {
String plain = test_vectors[i][0];
String salt = BCrypt.gensalt();
String hashed1 = BCrypt.hashpw(plain, salt);
String hashed2 = BCrypt.hashpw(plain, hashed1);
assertEquals(hashed1, hashed2);
System.out.print(".");
}
System.out.println("");
}

/**
* Test method for 'BCrypt.checkpw(String, String)'
* expecting success
*/
public void testCheckpw_success() {
System.out.print("BCrypt.checkpw w/ good passwords: ");
for (int i = 0; i < test_vectors.length; i++) {
String plain = test_vectors[i][0];
String expected = test_vectors[i][2];
assertTrue(BCrypt.checkpw(plain, expected));
System.out.print(".");
}
System.out.println("");
}

/**
* Test method for 'BCrypt.checkpw(String, String)'
* expecting failure
*/
public void testCheckpw_failure() {
System.out.print("BCrypt.checkpw w/ bad passwords: ");
for (int i = 0; i < test_vectors.length; i++) {
int broken_index = (i + 4) % test_vectors.length;
String plain = test_vectors[i][0];
String expected = test_vectors[broken_index][2];
assertFalse(BCrypt.checkpw(plain, expected));
System.out.print(".");
}
System.out.println("");
}

/**
* Test for correct hashing of non-US-ASCII passwords
*/
public void testInternationalChars() {
String pw1 = "ππππππππ";
System.out.print("BCrypt.hashpw w/ international chars: ");
String pw1 = "\u2605\u2605\u2605\u2605\u2605\u2605\u2605\u2605";
String pw2 = "????????";

String h1 = BCrypt.hashpw(pw1, BCrypt.gensalt());
assertFalse(BCrypt.checkpw(pw2, h1));
System.out.print(".");

String h2 = BCrypt.hashpw(pw2, BCrypt.gensalt());
assertFalse(BCrypt.checkpw(pw1, h2));
System.out.print(".");
System.out.println("");
}

}

0 comments on commit e17724e

Please sign in to comment.