2020
2121package com .eatthepath .otp ;
2222
23- import junitparams . JUnitParamsRunner ;
24- import junitparams . Parameters ;
25- import org .junit .Test ;
26- import org .junit .runner . RunWith ;
23+ import org . junit . jupiter . api . Test ;
24+ import org . junit . jupiter . params . ParameterizedTest ;
25+ import org .junit .jupiter . params . provider . Arguments ;
26+ import org .junit .jupiter . params . provider . MethodSource ;
2727
2828import javax .crypto .spec .SecretKeySpec ;
2929import java .nio .charset .StandardCharsets ;
3030import java .security .Key ;
3131import java .security .NoSuchAlgorithmException ;
3232import java .util .Date ;
3333import java .util .concurrent .TimeUnit ;
34+ import java .util .stream .Stream ;
3435
35- import static org .junit .Assert .assertEquals ;
36+ import static org .junit .jupiter .api .Assertions .assertEquals ;
37+ import static org .junit .jupiter .params .provider .Arguments .arguments ;
3638
37- @ RunWith (JUnitParamsRunner .class )
3839public class TimeBasedOneTimePasswordGeneratorTest extends HmacOneTimePasswordGeneratorTest {
3940
41+ private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1" ;
42+ private static final String HMAC_SHA256_ALGORITHM = "HmacSHA256" ;
43+ private static final String HMAC_SHA512_ALGORITHM = "HmacSHA512" ;
44+
45+ private static final Key HMAC_SHA1_KEY =
46+ new SecretKeySpec ("12345678901234567890" .getBytes (StandardCharsets .US_ASCII ), "RAW" );
47+
48+ private static final Key HMAC_SHA256_KEY =
49+ new SecretKeySpec ("12345678901234567890123456789012" .getBytes (StandardCharsets .US_ASCII ), "RAW" );
50+
51+ private static final Key HMAC_SHA512_KEY =
52+ new SecretKeySpec ("1234567890123456789012345678901234567890123456789012345678901234" .getBytes (StandardCharsets .US_ASCII ), "RAW" );
53+
4054 @ Override
4155 protected HmacOneTimePasswordGenerator getDefaultGenerator () throws NoSuchAlgorithmException {
4256 return new TimeBasedOneTimePasswordGenerator ();
4357 }
4458
4559 @ Test
46- public void testGetTimeStep () throws NoSuchAlgorithmException {
60+ void testGetTimeStep () throws NoSuchAlgorithmException {
4761 final long timeStepSeconds = 97 ;
4862
4963 final TimeBasedOneTimePasswordGenerator totp =
@@ -60,60 +74,38 @@ public void testGetTimeStep() throws NoSuchAlgorithmException {
6074 * <a href="https://www.rfc-editor.org/errata_search.php?rfc=6238&eid=2866">errata</a> correctly points out that
6175 * different keys are used for each of the various HMAC algorithms.
6276 */
63- @ Test
64- @ Parameters ({
65- "HmacSHA1, 59, 94287082" ,
66- "HmacSHA1, 1111111109, 7081804" ,
67- "HmacSHA1, 1111111111, 14050471" ,
68- "HmacSHA1, 1234567890, 89005924" ,
69- "HmacSHA1, 2000000000, 69279037" ,
70- "HmacSHA1, 20000000000, 65353130" ,
71- "HmacSHA256, 59, 46119246" ,
72- "HmacSHA256, 1111111109, 68084774" ,
73- "HmacSHA256, 1111111111, 67062674" ,
74- "HmacSHA256, 1234567890, 91819424" ,
75- "HmacSHA256, 2000000000, 90698825" ,
76- "HmacSHA256, 20000000000, 77737706" ,
77- "HmacSHA512, 59, 90693936" ,
78- "HmacSHA512, 1111111109, 25091201" ,
79- "HmacSHA512, 1111111111, 99943326" ,
80- "HmacSHA512, 1234567890, 93441116" ,
81- "HmacSHA512, 2000000000, 38618901" ,
82- "HmacSHA512, 20000000000, 47863826" })
83- public void testGenerateOneTimePassword (final String algorithm , final long epochSeconds , final int expectedOneTimePassword ) throws Exception {
77+ @ ParameterizedTest
78+ @ MethodSource ("totpTestVectorSource" )
79+ void testGenerateOneTimePassword (final String algorithm , final Key key , final long epochSeconds , final int expectedOneTimePassword ) throws Exception {
8480
8581 final TimeBasedOneTimePasswordGenerator totp =
8682 new TimeBasedOneTimePasswordGenerator (30 , TimeUnit .SECONDS , 8 , algorithm );
8783
8884 final Date date = new Date (TimeUnit .SECONDS .toMillis (epochSeconds ));
8985
90- assertEquals (expectedOneTimePassword , totp .generateOneTimePassword (getKeyForAlgorithm ( algorithm ) , date ));
86+ assertEquals (expectedOneTimePassword , totp .generateOneTimePassword (key , date ));
9187 }
9288
93- private static Key getKeyForAlgorithm (final String algorithm ) {
94- final String keyString ;
95-
96- switch (algorithm ) {
97- case "HmacSHA1" : {
98- keyString = "12345678901234567890" ;
99- break ;
100- }
101-
102- case "HmacSHA256" : {
103- keyString = "12345678901234567890123456789012" ;
104- break ;
105- }
106-
107- case "HmacSHA512" : {
108- keyString = "1234567890123456789012345678901234567890123456789012345678901234" ;
109- break ;
110- }
111-
112- default : {
113- throw new IllegalArgumentException ("Unexpected algorithm: " + algorithm );
114- }
115- }
116-
117- return new SecretKeySpec (keyString .getBytes (StandardCharsets .US_ASCII ), "RAW" );
89+ static Stream <Arguments > totpTestVectorSource () {
90+ return Stream .of (
91+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 59L , 94287082 ),
92+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 1111111109L , 7081804 ),
93+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 1111111111L , 14050471 ),
94+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 1234567890L , 89005924 ),
95+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 2000000000L , 69279037 ),
96+ arguments (HMAC_SHA1_ALGORITHM , HMAC_SHA1_KEY , 20000000000L , 65353130 ),
97+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 59L , 46119246 ),
98+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 1111111109L , 68084774 ),
99+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 1111111111L , 67062674 ),
100+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 1234567890L , 91819424 ),
101+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 2000000000L , 90698825 ),
102+ arguments (HMAC_SHA256_ALGORITHM , HMAC_SHA256_KEY , 20000000000L , 77737706 ),
103+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 59L , 90693936 ),
104+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 1111111109L , 25091201 ),
105+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 1111111111L , 99943326 ),
106+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 1234567890L , 93441116 ),
107+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 2000000000L , 38618901 ),
108+ arguments (HMAC_SHA512_ALGORITHM , HMAC_SHA512_KEY , 20000000000L , 47863826 )
109+ );
118110 }
119111}
0 commit comments