Browse Source

test_implemented PasswordHasherTest and fixed null password test failure for PasswordHasher

main
Richard Schmidt 11 months ago
parent
commit
0178fc61d0
  1. 4
      src/main/java/PasswordHasher.java
  2. 27
      src/test/java/PasswordHasherTest.java
  3. 28
      user.json

4
src/main/java/PasswordHasher.java

@ -3,6 +3,10 @@ import java.security.NoSuchAlgorithmException;
public class PasswordHasher { public class PasswordHasher {
public static String hashPassword(String password) { public static String hashPassword(String password) {
if (password == null) {
throw new IllegalArgumentException("Password cannot be null");
}
try { try {
MessageDigest digest = MessageDigest.getInstance("SHA-256"); MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(password.getBytes()); byte[] hash = digest.digest(password.getBytes());

27
src/test/java/PasswordHasherTest.java

@ -0,0 +1,27 @@
import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class PasswordHasherTest {
@Test
void testHashPassword() {
String password = "password123";
String hashedPassword = PasswordHasher.hashPassword(password);
// Check that the hashed password is not null
assertEquals(64, hashedPassword.length());
// Test with an empty password
String emptyPassword = "";
String hashedEmptyPassword = PasswordHasher.hashPassword(emptyPassword);
assertEquals(64, hashedEmptyPassword.length());
// Test with a null password
assertThrows(IllegalArgumentException.class, () -> {
PasswordHasher.hashPassword(null);
});
}
}

28
user.json

@ -1,29 +1,3 @@
[ [
{
"id": "c99b1061-13b7-4fa8-b1ac-814e07db3ef4",
"userName": "Test",
"password": "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92",
"birthday": "",
"firstName": "",
"surname": "",
"stayLoggedIn": false
},
{
"id": "89de267d-8456-4393-9fab-a5ddb36c7686",
"userName": "existinguser",
"password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
"birthday": "1990-01-01",
"firstName": "John",
"surname": "Doe",
"stayLoggedIn": false
},
{
"id": "43d45e81-3540-427f-8cb9-8f279d7a18d3",
"userName": "testUser",
"password": "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8",
"birthday": "1990-01-01",
"firstName": "John",
"surname": "Doe",
"stayLoggedIn": false
}
] ]
Loading…
Cancel
Save