Skip to content

Commit 9f5d094

Browse files
committed
Update tests to JUnit 5.
1 parent 27fdf86 commit 9f5d094

File tree

3 files changed

+94
-96
lines changed

3 files changed

+94
-96
lines changed

pom.xml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,16 @@
4949

5050
<dependencies>
5151
<dependency>
52-
<groupId>junit</groupId>
53-
<artifactId>junit</artifactId>
54-
<version>4.12</version>
52+
<groupId>org.junit.jupiter</groupId>
53+
<artifactId>junit-jupiter-engine</artifactId>
54+
<version>5.4.2</version>
5555
<scope>test</scope>
5656
</dependency>
5757

5858
<dependency>
59-
<groupId>pl.pragmatists</groupId>
60-
<artifactId>JUnitParams</artifactId>
61-
<version>1.1.1</version>
59+
<groupId>org.junit.jupiter</groupId>
60+
<artifactId>junit-jupiter-params</artifactId>
61+
<version>5.4.2</version>
6262
<scope>test</scope>
6363
</dependency>
6464
</dependencies>

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

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,50 @@
2020

2121
package com.eatthepath.otp;
2222

23-
import static org.junit.Assert.*;
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;
2427

28+
import javax.crypto.spec.SecretKeySpec;
2529
import java.nio.charset.StandardCharsets;
26-
import java.security.InvalidKeyException;
2730
import java.security.Key;
2831
import java.security.NoSuchAlgorithmException;
32+
import java.util.stream.Stream;
2933

30-
import javax.crypto.spec.SecretKeySpec;
31-
32-
import junitparams.JUnitParamsRunner;
33-
import junitparams.Parameters;
34-
import org.junit.Test;
35-
import org.junit.runner.RunWith;
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertThrows;
36+
import static org.junit.jupiter.params.provider.Arguments.arguments;
3637

37-
@RunWith(JUnitParamsRunner.class)
3838
public class HmacOneTimePasswordGeneratorTest {
3939

40-
@Test(expected = IllegalArgumentException.class)
41-
public void testHmacOneTimePasswordGeneratorWithShortPasswordLength() throws NoSuchAlgorithmException {
42-
new HmacOneTimePasswordGenerator(5);
40+
private static final Key HOTP_KEY =
41+
new SecretKeySpec("12345678901234567890".getBytes(StandardCharsets.US_ASCII), "RAW");
42+
43+
@Test
44+
void testHmacOneTimePasswordGeneratorWithShortPasswordLength() {
45+
assertThrows(IllegalArgumentException.class, () -> new HmacOneTimePasswordGenerator(5));
4346
}
4447

45-
@Test(expected = IllegalArgumentException.class)
46-
public void testHmacOneTimePasswordGeneratorWithLongPasswordLength() throws NoSuchAlgorithmException {
47-
new HmacOneTimePasswordGenerator(9);
48+
@Test
49+
void testHmacOneTimePasswordGeneratorWithLongPasswordLength() {
50+
assertThrows(IllegalArgumentException.class, () -> new HmacOneTimePasswordGenerator(9));
4851
}
4952

50-
@Test(expected = NoSuchAlgorithmException.class)
51-
public void testHmacOneTimePasswordGeneratorWithBogusAlgorithm() throws NoSuchAlgorithmException {
52-
new HmacOneTimePasswordGenerator(6, "Definitely not a real algorithm");
53+
@Test
54+
void testHmacOneTimePasswordGeneratorWithBogusAlgorithm() {
55+
assertThrows(NoSuchAlgorithmException.class, () ->
56+
new HmacOneTimePasswordGenerator(6, "Definitely not a real algorithm"));
5357
}
5458

5559
@Test
56-
public void testGetPasswordLength() throws NoSuchAlgorithmException {
60+
void testGetPasswordLength() throws NoSuchAlgorithmException {
5761
final int passwordLength = 7;
5862
assertEquals(passwordLength, new HmacOneTimePasswordGenerator(passwordLength).getPasswordLength());
5963
}
6064

6165
@Test
62-
public void testGetAlgorithm() throws NoSuchAlgorithmException {
66+
void testGetAlgorithm() throws NoSuchAlgorithmException {
6367
final String algorithm = "HmacSHA256";
6468
assertEquals(algorithm, new HmacOneTimePasswordGenerator(6, algorithm).getAlgorithm());
6569
}
@@ -68,23 +72,25 @@ public void testGetAlgorithm() throws NoSuchAlgorithmException {
6872
* Tests generation of one-time passwords using the test vectors from
6973
* <a href="https://tools.ietf.org/html/rfc4226#appendix-D">RFC&nbsp;4226, Appendix D</a>.
7074
*/
71-
@Test
72-
@Parameters({
73-
"0, 755224",
74-
"1, 287082",
75-
"2, 359152",
76-
"3, 969429",
77-
"4, 338314",
78-
"5, 254676",
79-
"6, 287922",
80-
"7, 162583",
81-
"8, 399871",
82-
"9, 520489" })
83-
public void testGenerateOneTimePassword(final int counter, final int expectedOneTimePassword) throws Exception {
84-
final HmacOneTimePasswordGenerator hmacOneTimePasswordGenerator = this.getDefaultGenerator();
75+
@ParameterizedTest
76+
@MethodSource("hotpTestVectorSource")
77+
void testGenerateOneTimePassword(final int counter, final int expectedOneTimePassword) throws Exception {
78+
assertEquals(expectedOneTimePassword, this.getDefaultGenerator().generateOneTimePassword(HOTP_KEY, counter));
79+
}
8580

86-
final Key key = new SecretKeySpec("12345678901234567890".getBytes(StandardCharsets.US_ASCII), "RAW");
87-
assertEquals(expectedOneTimePassword, hmacOneTimePasswordGenerator.generateOneTimePassword(key, counter));
81+
static Stream<Arguments> hotpTestVectorSource() {
82+
return Stream.of(
83+
arguments(0, 755224),
84+
arguments(1, 287082),
85+
arguments(2, 359152),
86+
arguments(3, 969429),
87+
arguments(4, 338314),
88+
arguments(5, 254676),
89+
arguments(6, 287922),
90+
arguments(7, 162583),
91+
arguments(8, 399871),
92+
arguments(9, 520489)
93+
);
8894
}
8995

9096
protected HmacOneTimePasswordGenerator getDefaultGenerator() throws NoSuchAlgorithmException {

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

Lines changed: 46 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,44 @@
2020

2121
package 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

2828
import javax.crypto.spec.SecretKeySpec;
2929
import java.nio.charset.StandardCharsets;
3030
import java.security.Key;
3131
import java.security.NoSuchAlgorithmException;
3232
import java.util.Date;
3333
import 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)
3839
public 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

Comments
 (0)