/
MfaAuthenticatorImplTest.java
156 lines (134 loc) · 5.84 KB
/
MfaAuthenticatorImplTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*******************************************************************************
* Copyright (c) 2021, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.service.authentication.shiro.mfa;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaIllegalArgumentException;
import org.eclipse.kapua.KapuaIllegalNullArgumentException;
import org.eclipse.kapua.qa.markers.junit.JUnitTests;
import org.eclipse.kapua.service.authentication.shiro.utils.AuthenticationUtils;
import org.eclipse.kapua.service.authentication.shiro.utils.CryptAlgorithm;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import java.util.List;
@Category(JUnitTests.class)
public class MfaAuthenticatorImplTest {
MfaAuthenticatorImpl mfaAuthenticatorImpl;
String[] encryptedSecrets, hashedScratchCodes, stringVerificationCodes;
int[] verificationCodes;
@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[]{
"0",
" 1",
"100",
"100000",
"2147483647"
};
}
@Test
public void isEnabledTest() {
Assert.assertTrue("True expected.", mfaAuthenticatorImpl.isEnabled());
}
@Test
public void authorizeEncryptedSecretVerificationCodeParametersTest() throws KapuaException {
for (String encryptedSecret : encryptedSecrets) {
for (int verificationCode : verificationCodes) {
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
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
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 authorizeHashedScratchCodeVerificationCodeParametersFalseTest() throws KapuaException {
for (String hashedScratchCode : hashedScratchCodes) {
for (String stringVerificationCode : stringVerificationCodes) {
Assert.assertFalse(mfaAuthenticatorImpl.authorize(hashedScratchCode, stringVerificationCode));
}
}
}
@Test
public void authorizeHashedScratchCodeVerificationCodeParametersTrueTest() throws KapuaException {
Assert.assertTrue(mfaAuthenticatorImpl.authorize("$2a$12$2AZYOAvilJyNvG8b6rBDaOSIcM3mKc6iyNQUYIXOF4ZFEAYdzM7Jm", "plainValue"));
}
@Test
public void generateKeyTest() {
String generatedKey = mfaAuthenticatorImpl.generateKey();
Assert.assertNotNull(generatedKey);
Assert.assertEquals(32, generatedKey.length());
}
@Test
public void generateCodesTest() {
List<String> generatedScratchCodes = mfaAuthenticatorImpl.generateCodes();
Assert.assertNotNull(generatedScratchCodes);
Assert.assertEquals(5, generatedScratchCodes.size());
for (String generatedScratchCode : generatedScratchCodes) {
Assert.assertNotNull(generatedScratchCode);
}
}
}