Tests run now :party:

This commit is contained in:
TheArrayser 2023-06-30 23:09:36 +02:00
parent d64ab8e17f
commit 9f09a4c048

View File

@ -15,6 +15,7 @@ import org.junit.jupiter.api.Test;
import com.cringe_studios.cringe_authenticator_library.OTP; import com.cringe_studios.cringe_authenticator_library.OTP;
import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm; import com.cringe_studios.cringe_authenticator_library.OTPAlgorithm;
import com.cringe_studios.cringe_authenticator_library.OTPException;
import com.cringe_studios.cringe_authenticator_library.OTPType; import com.cringe_studios.cringe_authenticator_library.OTPType;
import com.cringe_studios.cringe_authenticator_library.impl.Base32; import com.cringe_studios.cringe_authenticator_library.impl.Base32;
import com.cringe_studios.cringe_authenticator_library.impl.TOTP; import com.cringe_studios.cringe_authenticator_library.impl.TOTP;
@ -37,19 +38,23 @@ public class OTPTest {
} }
for (int i = 0; i < 10; i++) { for (int i = 0; i < 10; i++) {
OTP testOTP1 = OTPType.HOTP.instance(base32secret, OTPAlgorithm.SHA1, 6, i, 30, false); int tempi = i;
assertDoesNotThrow(() ->{
OTP testOTP1 = OTPType.HOTP.instance(base32secret, OTPAlgorithm.SHA1, 6, tempi, 30, false);
String calculatedPin = testOTP1.getPin(); String calculatedPin = testOTP1.getPin();
if(VERBOSE) { if(VERBOSE) {
System.out.println(calculatedPin + " == " + expectedValues[i]); System.out.println(calculatedPin + " == " + expectedValues[tempi]);
} }
assertEquals(calculatedPin, expectedValues[i]); assertEquals(calculatedPin, expectedValues[tempi]);
});
} }
} }
@Test @Test
public void lengthTest() { public void lengthTest() {
assertDoesNotThrow(() -> {
String secret = "1"; String secret = "1";
String base32secret = Base32.encode(hexStr2Bytes(secret)); String base32secret = Base32.encode(hexStr2Bytes(secret));
OTP testOTP1 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA1, 1, 0, 0, false); OTP testOTP1 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA1, 1, 0, 0, false);
@ -66,10 +71,11 @@ public class OTPTest {
OTP testOTP4 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA512, 10, 0, 0, false); OTP testOTP4 = OTP.createNewOTP(OTPType.HOTP, base32secret, OTPAlgorithm.SHA512, 10, 0, 0, false);
assertDoesNotThrow(() -> testOTP4.getPin()); assertDoesNotThrow(() -> testOTP4.getPin());
assertDoesNotThrow(() -> { assertThrows(OTPException.class, () -> {
OTP testOTP5 = OTP.createNewOTP(OTPType.HOTP, "", OTPAlgorithm.SHA1, 8, 0, 0, false); OTP testOTP5 = OTP.createNewOTP(OTPType.HOTP, "", OTPAlgorithm.SHA1, 8, 0, 0, false);
assertNotNull(testOTP5); assertNotNull(testOTP5);
}); });
});
} }
private static byte[] hexStr2Bytes(String hex) { private static byte[] hexStr2Bytes(String hex) {
@ -120,6 +126,9 @@ public class OTPTest {
long testTime[] = { 59L, 1111111109L, 1111111111L, 1234567890L, 2000000000L, 20000000000L }; long testTime[] = { 59L, 1111111109L, 1111111111L, 1234567890L, 2000000000L, 20000000000L };
assertDoesNotThrow(() -> {
TOTP testOTP1 = (TOTP) OTPType.TOTP.instance(seed, OTPAlgorithm.SHA1, 8, 0, 30, false); TOTP testOTP1 = (TOTP) OTPType.TOTP.instance(seed, OTPAlgorithm.SHA1, 8, 0, 30, false);
TOTP testOTP256 = (TOTP) OTPType.TOTP.instance(seed32, OTPAlgorithm.SHA256, 8, 0, 30, false); TOTP testOTP256 = (TOTP) OTPType.TOTP.instance(seed32, OTPAlgorithm.SHA256, 8, 0, 30, false);
TOTP testOTP512 = (TOTP) OTPType.TOTP.instance(seed64, OTPAlgorithm.SHA512, 8, 0, 30, false); TOTP testOTP512 = (TOTP) OTPType.TOTP.instance(seed64, OTPAlgorithm.SHA512, 8, 0, 30, false);
@ -162,6 +171,7 @@ public class OTPTest {
} catch (final Exception e) { } catch (final Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
});
} }
// @org.junit.jupiter.api.Test // @org.junit.jupiter.api.Test