tests for totp

This commit is contained in:
TheArrayser 2023-06-18 22:25:19 +02:00
parent 1c286dd2f0
commit 39752b36ec

View File

@ -53,7 +53,29 @@ public class OTPTest {
return ret;
}
public static void totpTest() {
@Test
public void totpTest() {
String expectedValues[] = {
"94287082",
"46119246",
"90693936",
"07081804",
"68084774",
"25091201",
"14050471",
"67062674",
"99943326",
"89005924",
"91819424",
"93441116",
"69279037",
"90698825",
"38618901",
"65353130",
"77737706",
"47863826"
};
// Seed for HMAC-SHA1 - 20 bytes
String base16seed = "3132333435363738393031323334353637383930";
// Seed for HMAC-SHA256 - 32 bytes
@ -78,22 +100,34 @@ public class OTPTest {
df.setTimeZone(TimeZone.getTimeZone("UTC"));
try {
if(VERBOSE) {
System.out.println("+---------------+-----------------------+--------+--------+");
System.out.println("| Time(sec) | Time (UTC format) | TOTP | Mode |");
System.out.println("+---------------+-----------------------+--------+--------+");
}
for (int i = 0; i < testTime.length; i++) {
String fmtTime = String.format("%1$-11s", testTime[i]);
String utcTime = df.format(new Date(testTime[i] * 1000));
System.out.println(
"| " + fmtTime + " | " + utcTime + " |" + testOTP1.getPinAt(testTime[i]) + "| SHA1 |");
System.out.println(
"| " + fmtTime + " | " + utcTime + " |" + testOTP256.getPinAt(testTime[i]) + "| SHA256 |");
System.out.println(
"| " + fmtTime + " | " + utcTime + " |" + testOTP512.getPinAt(testTime[i]) + "| SHA512 |");
String sha1Pin = testOTP1.getPinAt(testTime[i]);
String sha256Pin = testOTP256.getPinAt(testTime[i]);
String sha512Pin = testOTP512.getPinAt(testTime[i]);
if(VERBOSE) {
System.out.println("| " + fmtTime + " | " + utcTime + " |" + sha1Pin + "| SHA1 |");
System.out.println("| " + fmtTime + " | " + utcTime + " |" + sha256Pin + "| SHA256 |");
System.out.println("| " + fmtTime + " | " + utcTime + " |" + sha512Pin + "| SHA512 |");
System.out.println("+---------------+-----------------------+--------+--------+");
}
assertEquals(expectedValues[i * 3 + 0], sha1Pin);
assertEquals(expectedValues[i * 3 + 1], sha256Pin);
assertEquals(expectedValues[i * 3 + 2], sha512Pin);
}
} catch (final Exception e) {
e.printStackTrace();
}