Skip to content

Commit ec22ae9

Browse files
committed
Moved test key construction into its own method to simplify test parameters.
1 parent 43e135b commit ec22ae9

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

src/test/java/com/eatthepath/otp/TimeBasedOneTimePasswordGeneratorTest.java

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,32 +66,58 @@ public void testGetTimeStep() throws NoSuchAlgorithmException {
6666
*/
6767
@Test
6868
@Parameters({
69-
"59, 94287082, 12345678901234567890, HmacSHA1",
70-
"1111111109, 7081804, 12345678901234567890, HmacSHA1",
71-
"1111111111, 14050471, 12345678901234567890, HmacSHA1",
72-
"1234567890, 89005924, 12345678901234567890, HmacSHA1",
73-
"2000000000, 69279037, 12345678901234567890, HmacSHA1",
74-
"20000000000, 65353130, 12345678901234567890, HmacSHA1",
75-
"59, 46119246, 12345678901234567890123456789012, HmacSHA256",
76-
"1111111109, 68084774, 12345678901234567890123456789012, HmacSHA256",
77-
"1111111111, 67062674, 12345678901234567890123456789012, HmacSHA256",
78-
"1234567890, 91819424, 12345678901234567890123456789012, HmacSHA256",
79-
"2000000000, 90698825, 12345678901234567890123456789012, HmacSHA256",
80-
"20000000000, 77737706, 12345678901234567890123456789012, HmacSHA256",
81-
"59, 90693936, 1234567890123456789012345678901234567890123456789012345678901234, HmacSHA512",
82-
"1111111109, 25091201, 1234567890123456789012345678901234567890123456789012345678901234, HmacSHA512",
83-
"1111111111, 99943326, 1234567890123456789012345678901234567890123456789012345678901234, HmacSHA512",
84-
"1234567890, 93441116, 1234567890123456789012345678901234567890123456789012345678901234, HmacSHA512",
85-
"2000000000, 38618901, 1234567890123456789012345678901234567890123456789012345678901234, HmacSHA512",
86-
"20000000000, 47863826, 1234567890123456789012345678901234567890123456789012345678901234, HmacSHA512" })
87-
public void testGenerateOneTimePassword(final long epochSeconds, final int expectedOneTimePassword, final String keyString, final String algorithm) throws Exception {
69+
"HmacSHA1, 59, 94287082",
70+
"HmacSHA1, 1111111109, 7081804",
71+
"HmacSHA1, 1111111111, 14050471",
72+
"HmacSHA1, 1234567890, 89005924",
73+
"HmacSHA1, 2000000000, 69279037",
74+
"HmacSHA1, 20000000000, 65353130",
75+
"HmacSHA256, 59, 46119246",
76+
"HmacSHA256, 1111111109, 68084774",
77+
"HmacSHA256, 1111111111, 67062674",
78+
"HmacSHA256, 1234567890, 91819424",
79+
"HmacSHA256, 2000000000, 90698825",
80+
"HmacSHA256, 20000000000, 77737706",
81+
"HmacSHA512, 59, 90693936",
82+
"HmacSHA512, 1111111109, 25091201",
83+
"HmacSHA512, 1111111111, 99943326",
84+
"HmacSHA512, 1234567890, 93441116",
85+
"HmacSHA512, 2000000000, 38618901",
86+
"HmacSHA512, 20000000000, 47863826" })
87+
public void testGenerateOneTimePassword(final String algorithm, final long epochSeconds, final int expectedOneTimePassword) throws Exception {
8888

8989
final TimeBasedOneTimePasswordGenerator totp =
9090
new TimeBasedOneTimePasswordGenerator(30, TimeUnit.SECONDS, 8, algorithm);
9191

92-
final Key key = new SecretKeySpec(keyString.getBytes(StandardCharsets.US_ASCII), "RAW");
9392
final Date date = new Date(TimeUnit.SECONDS.toMillis(epochSeconds));
9493

95-
assertEquals(expectedOneTimePassword, totp.generateOneTimePassword(key, date));
94+
assertEquals(expectedOneTimePassword, totp.generateOneTimePassword(getKeyForAlgorithm(algorithm), date));
95+
}
96+
97+
private static Key getKeyForAlgorithm(final String algorithm) {
98+
final String keyString;
99+
100+
switch (algorithm) {
101+
case "HmacSHA1": {
102+
keyString = "12345678901234567890";
103+
break;
104+
}
105+
106+
case "HmacSHA256": {
107+
keyString = "12345678901234567890123456789012";
108+
break;
109+
}
110+
111+
case "HmacSHA512": {
112+
keyString = "1234567890123456789012345678901234567890123456789012345678901234";
113+
break;
114+
}
115+
116+
default: {
117+
throw new IllegalArgumentException("Unexpected algorithm: " + algorithm);
118+
}
119+
}
120+
121+
return new SecretKeySpec(keyString.getBytes(StandardCharsets.US_ASCII), "RAW");
96122
}
97123
}

0 commit comments

Comments
 (0)