Skip to content

Commit 8445e91

Browse files
committed
Add Assumptions.assumeMinimumJdk8
Certain cryptographic algorithms are only supported on JDK8+. This causes failures in JDK 7. This commit adds a JUnit assumption on tests that leverage JDK8 specific cryptographic algorithms. Issue: gh-5323
1 parent 13ccb83 commit 8445e91

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
*
3+
* * Copyright 2002-2018 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * http://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*
18+
*/
19+
20+
package org.springframework.security.crypto.junit;
21+
22+
import org.junit.Assume;
23+
24+
/**
25+
* @author Rob Winch
26+
* @since 4.2.7
27+
*/
28+
public final class Assumptions {
29+
30+
public static void assumeMinimumJdk8() {
31+
int majorVersion = JdkVersion.getMajorJavaVersion();
32+
Assume.assumeTrue(majorVersion >= 8);
33+
}
34+
35+
private Assumptions() {}
36+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
*
3+
* * Copyright 2002-2018 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * http://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*
18+
*/
19+
20+
package org.springframework.security.crypto.junit;
21+
22+
/**
23+
* Internal helper class used to find the Java/JVM version that Spring is
24+
* operating on, to allow for automatically adapting to the present platform's
25+
* capabilities.
26+
*
27+
* <p>Note that Spring requires JVM 1.6 or higher, as of Spring 4.0.
28+
*
29+
* @author Rod Johnson
30+
* @author Juergen Hoeller
31+
* @author Rick Evans
32+
* @author Sam Brannen
33+
* @deprecated as of Spring 4.2.1, in favor of direct checks for the desired
34+
* JDK API variants via reflection
35+
*/
36+
public abstract class JdkVersion {
37+
38+
/**
39+
* Constant identifying the 1.3.x JVM (JDK 1.3).
40+
*/
41+
public static final int JAVA_13 = 0;
42+
43+
/**
44+
* Constant identifying the 1.4.x JVM (J2SE 1.4).
45+
*/
46+
public static final int JAVA_14 = 1;
47+
48+
/**
49+
* Constant identifying the 1.5 JVM (Java 5).
50+
*/
51+
public static final int JAVA_15 = 2;
52+
53+
/**
54+
* Constant identifying the 1.6 JVM (Java 6).
55+
*/
56+
public static final int JAVA_16 = 3;
57+
58+
/**
59+
* Constant identifying the 1.7 JVM (Java 7).
60+
*/
61+
public static final int JAVA_17 = 4;
62+
63+
/**
64+
* Constant identifying the 1.8 JVM (Java 8).
65+
*/
66+
public static final int JAVA_18 = 5;
67+
68+
/**
69+
* Constant identifying the 1.9 JVM (Java 9).
70+
*/
71+
public static final int JAVA_19 = 6;
72+
73+
74+
private static final String javaVersion;
75+
76+
private static final int majorJavaVersion;
77+
78+
static {
79+
javaVersion = System.getProperty("java.version");
80+
// version String should look like "1.4.2_10"
81+
if (javaVersion.contains("1.9.")) {
82+
majorJavaVersion = JAVA_19;
83+
}
84+
else if (javaVersion.contains("1.8.")) {
85+
majorJavaVersion = JAVA_18;
86+
}
87+
else if (javaVersion.contains("1.7.")) {
88+
majorJavaVersion = JAVA_17;
89+
}
90+
else {
91+
// else leave 1.6 as default (it's either 1.6 or unknown)
92+
majorJavaVersion = JAVA_16;
93+
}
94+
}
95+
96+
97+
/**
98+
* Return the full Java version string, as returned by
99+
* {@code System.getProperty("java.version")}.
100+
* @return the full Java version string
101+
* @see System#getProperty(String)
102+
*/
103+
public static String getJavaVersion() {
104+
return javaVersion;
105+
}
106+
107+
/**
108+
* Get the major version code. This means we can do things like
109+
* {@code if (getMajorJavaVersion() >= JAVA_17)}.
110+
* @return a code comparable to the {@code JAVA_XX} codes in this class
111+
* @see #JAVA_16
112+
* @see #JAVA_17
113+
* @see #JAVA_18
114+
* @see #JAVA_19
115+
*/
116+
public static int getMajorJavaVersion() {
117+
return majorJavaVersion;
118+
}
119+
120+
}
121+

crypto/src/test/java/org/springframework/security/crypto/password/Pbkdf2PasswordEncoderTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.security.crypto.codec.Base64;
2323
import org.springframework.security.crypto.codec.Hex;
2424
import org.springframework.security.crypto.codec.Utf8;
25+
import org.springframework.security.crypto.junit.Assumptions;
2526
import org.springframework.security.crypto.keygen.KeyGenerators;
2627

2728
import static org.assertj.core.api.Assertions.assertThat;
@@ -98,6 +99,7 @@ public void matchWhenBase64ThenSuccess() {
9899

99100
@Test
100101
public void encodeAndMatchWhenSha256ThenSuccess() {
102+
Assumptions.assumeMinimumJdk8();
101103
this.encoder.setAlgorithm(Pbkdf2PasswordEncoder.SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA256);
102104

103105
String rawPassword = "password";
@@ -107,6 +109,7 @@ public void encodeAndMatchWhenSha256ThenSuccess() {
107109

108110
@Test
109111
public void matchWhenSha256ThenSuccess() {
112+
Assumptions.assumeMinimumJdk8();
110113
this.encoder.setAlgorithm(Pbkdf2PasswordEncoder.SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA256);
111114

112115
String rawPassword = "password";

0 commit comments

Comments
 (0)