Skip to content

Commit

Permalink
Refactored and improved MfaAuthenticatorImplTest
Browse files Browse the repository at this point in the history
Signed-off-by: Alberto Codutti <alberto.codutti@eurotech.com>
  • Loading branch information
Coduz committed Nov 3, 2022
1 parent e2a5ba9 commit 2dc3455
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public interface MfaAuthenticator {
* Whether the {@link MfaAuthenticator} service is enabled or not.
*
* @return {@code true} if the {@link MfaAuthenticator} is enabled, {@code false} otherwise.
* @throws KapuaException
* @since 1.3.0
*/
boolean isEnabled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.util.List;

@Category(JUnitTests.class)
public class MfaAuthenticatorImplTest extends Assert {

Expand All @@ -33,12 +35,41 @@ public class MfaAuthenticatorImplTest extends Assert {
@Before
public void initialize() throws KapuaException {
mfaAuthenticatorImpl = new MfaAuthenticatorImpl();
encryptedSecrets = new String[]{AuthenticationUtils.encryptAes("value to encrypt"), AuthenticationUtils.encryptAes("value@#$ en-999crypt"), AuthenticationUtils.encryptAes("!<>v87a-lue to encrypt"),
AuthenticationUtils.encryptAes("value_to$#encr-0y()pt"), AuthenticationUtils.encryptAes("va09l-ue|,,,.to00encrypt")};
verificationCodes = new int[]{-2147483648, -100000, -100, -1, 0, 1, 100, 100000, 2147483647};
hashedScratchCodes = new String[]{AuthenticationUtils.cryptCredential(CryptAlgorithm.BCRYPT, "val-ue99_<11>"), AuthenticationUtils.cryptCredential(CryptAlgorithm.BCRYPT, " !@#$v66a0l-ueee"),
AuthenticationUtils.cryptCredential(CryptAlgorithm.BCRYPT, "val *&^%087,...ueee "), AuthenticationUtils.cryptCredential(CryptAlgorithm.BCRYPT, "_877V.A;;LUE")};
stringVerificationCodes = new String[]{"-2147483648", "-100000", "-100", "-1", "0", " 1", "100", "100000", "2147483647"};

encryptedSecrets = new String[]{
AuthenticationUtils.encryptAes("value to encrypt"),
AuthenticationUtils.encryptAes("value@#$ en-999crypt"),
AuthenticationUtils.encryptAes("!<>v87a-lue to encrypt"),
AuthenticationUtils.encryptAes("value_to$#encr-0y()pt"),
AuthenticationUtils.encryptAes("va09l-ue|,,,.to00encrypt")
};

verificationCodes = new int[]{
-2147483648,
-100000,
-100,
-1,
0,
1,
100,
100000,
2147483647
};

hashedScratchCodes = new String[]{
AuthenticationUtils.cryptCredential(CryptAlgorithm.BCRYPT, "val-ue99_<11>"),
AuthenticationUtils.cryptCredential(CryptAlgorithm.BCRYPT, " !@#$v66a0l-ueee"),
AuthenticationUtils.cryptCredential(CryptAlgorithm.BCRYPT, "val *&^%087,...ueee "),
AuthenticationUtils.cryptCredential(CryptAlgorithm.BCRYPT, "_877V.A;;LUE")
};

stringVerificationCodes = new String[]{
"0",
" 1",
"100",
"100000",
"2147483647"
};
}

@Test
Expand All @@ -50,46 +81,76 @@ public void isEnabledTest() {
public void authorizeEncryptedSecretVerificationCodeParametersTest() throws KapuaException {
for (String encryptedSecret : encryptedSecrets) {
for (int verificationCode : verificationCodes) {
assertFalse("False expected.", mfaAuthenticatorImpl.authorize(encryptedSecret, verificationCode));
if (verificationCode >= 0) {
Assert.assertFalse(mfaAuthenticatorImpl.authorize(encryptedSecret, verificationCode));
} else {
try {
mfaAuthenticatorImpl.authorize(encryptedSecret, verificationCode);

Assert.fail("This should have thrown KapuaIllegalArgumentException");
} catch (KapuaIllegalArgumentException e) {
Assert.assertEquals("verificationCode", e.getArgumentName());
Assert.assertNull(e.getArgumentValue());
}
}
}
}
}

@Test(expected = KapuaIllegalArgumentException.class)
public void authorizeNullEncryptedSecretVerificationCodeParametersTest() throws KapuaException {
for (int verificationCode : verificationCodes) {
assertFalse("False expected.", mfaAuthenticatorImpl.authorize(null, verificationCode));
@Test
public void authorizeNullVerificationCodeParametersTest() throws KapuaException {
try {
mfaAuthenticatorImpl.authorize(encryptedSecrets[0], null);

Assert.fail("This should have thrown KapuaIllegalNullArgumentException");
} catch (KapuaIllegalNullArgumentException e) {
Assert.assertEquals("verificationCode", e.getArgumentName());
Assert.assertNull(e.getArgumentValue());
}
}

@Test(expected = KapuaIllegalNullArgumentException.class)
public void authorizeEncryptedSecretNullVerificationCodeParametersTest() throws KapuaException {
for (String encryptedSecret : encryptedSecrets) {
assertFalse("False expected.", mfaAuthenticatorImpl.authorize(encryptedSecret, null));
@Test
public void authorizeNullEncryptedSecretVerificationCodeParametersTest() throws KapuaException {
try {
mfaAuthenticatorImpl.authorize(null, "123456");

Assert.fail("This should have thrown KapuaIllegalNullArgumentException");
} catch (KapuaIllegalNullArgumentException e) {
Assert.assertEquals("hashedScratchCode", e.getArgumentName());
Assert.assertNull(e.getArgumentValue());
}
}

@Test
public void authorizeHasedScratchCodeVerificationCodeParametersFalseTest() throws KapuaException {
for (String hasedScratchCode : hashedScratchCodes) {
public void authorizeHashedScratchCodeVerificationCodeParametersFalseTest() throws KapuaException {
for (String hashedScratchCode : hashedScratchCodes) {
for (String stringVerificationCode : stringVerificationCodes) {
assertFalse("False expected.", mfaAuthenticatorImpl.authorize(hasedScratchCode, stringVerificationCode));
Assert.assertFalse(mfaAuthenticatorImpl.authorize(hashedScratchCode, stringVerificationCode));
}
}
}

@Test
public void authorizeHasedScratchCodeVerificationCodeParametersTrueTest() throws KapuaException {
Assert.assertTrue("True expected.", mfaAuthenticatorImpl.authorize("$2a$12$2AZYOAvilJyNvG8b6rBDaOSIcM3mKc6iyNQUYIXOF4ZFEAYdzM7Jm", "plainValue"));
public void authorizeHashedScratchCodeVerificationCodeParametersTrueTest() throws KapuaException {
Assert.assertTrue(mfaAuthenticatorImpl.authorize("$2a$12$2AZYOAvilJyNvG8b6rBDaOSIcM3mKc6iyNQUYIXOF4ZFEAYdzM7Jm", "plainValue"));
}

@Test
public void generateKeyTest() {
assertEquals("Expected and actual values should be the same.", 32, mfaAuthenticatorImpl.generateKey().length());
String generatedKey = mfaAuthenticatorImpl.generateKey();

Assert.assertNotNull(generatedKey);
Assert.assertEquals(32, generatedKey.length());
}

@Test
public void generateCodesTest() {
assertEquals("Expected and actual values should be the same.", 5, mfaAuthenticatorImpl.generateCodes().size());
List<String> generatedScratchCodes = mfaAuthenticatorImpl.generateCodes();

Assert.assertNotNull(generatedScratchCodes);
Assert.assertEquals(5, generatedScratchCodes.size());
for (String generatedScratchCode : generatedScratchCodes) {
Assert.assertNotNull(generatedScratchCode);
}
}
}

0 comments on commit 2dc3455

Please sign in to comment.