Skip to content

Commit 401aa67

Browse files
authored
Merge pull request #2442 from exceptionfactory/issue-2440-1
Refactor SSLUtilsTest with shared method and algorithm assertions
2 parents 4ca34f4 + 9eb6200 commit 401aa67

File tree

1 file changed

+62
-31
lines changed

1 file changed

+62
-31
lines changed

util/src/test/java/io/kubernetes/client/util/SSLUtilsTest.java

Lines changed: 62 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,80 @@
1212
*/
1313
package io.kubernetes.client.util;
1414

15+
import static org.junit.Assert.assertEquals;
16+
1517
import io.kubernetes.client.Resources;
16-
import java.io.File;
1718
import java.io.IOException;
18-
import java.nio.file.Files;
19-
import java.nio.file.Paths;
19+
import java.net.URL;
20+
import java.security.GeneralSecurityException;
2021
import java.security.NoSuchAlgorithmException;
2122
import java.security.PrivateKey;
2223
import java.security.spec.InvalidKeySpecException;
23-
import junit.framework.TestCase;
24+
import org.apache.commons.io.IOUtils;
25+
import org.junit.Test;
2426

25-
public class SSLUtilsTest extends TestCase {
27+
public class SSLUtilsTest {
2628

27-
private static final String CLIENT_KEY_PATH =
28-
new File(Resources.getResource("clientauth.key").getPath()).toString();
29-
private static final String CLIENT_KEY_RSA_PATH =
30-
new File(Resources.getResource("clientauth-rsa.key").getPath()).toString();
31-
private static final String CLIENT_KEY_EC_PATH =
32-
new File(Resources.getResource("clientauth-ec.key").getPath()).toString();
29+
private static final String CLIENT_CERT = "clientauth.cert";
3330

34-
public void testPKCS8KeyLoadDump()
35-
throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
36-
byte[] loaded = Files.readAllBytes(Paths.get(CLIENT_KEY_PATH.replace("C:/", "")));
37-
PrivateKey privateKey = SSLUtils.loadKey(loaded);
38-
byte[] dumped = SSLUtils.dumpKey(privateKey);
39-
PrivateKey reloaded = SSLUtils.loadKey(dumped);
40-
assertEquals(privateKey, reloaded);
31+
private static final String CLIENT_KEY_RSA_PKCS8 = "clientauth.key";
32+
33+
private static final String CLIENT_KEY_RSA_PKCS1 = "clientauth-rsa.key";
34+
35+
private static final String CLIENT_KEY_ECDSA_PKCS7 = "clientauth-ec.key";
36+
37+
private static final String CLIENT_KEY_ECDSA_PKCS8 = "clientauth-ec-fixed.key";
38+
39+
private static final String RSA_ALGORITHM = "RSA";
40+
41+
private static final String ECDSA_ALGORITHM = "ECDSA";
42+
43+
@Test
44+
public void testLoadKeyRsaPkcs8() throws IOException, GeneralSecurityException {
45+
final PrivateKey privateKey = assertLoadDumpReloadKeyEquals(CLIENT_KEY_RSA_PKCS8);
46+
assertEquals(RSA_ALGORITHM, privateKey.getAlgorithm());
4147
}
4248

43-
public void testPKCS1RSAKeyLoadDump()
44-
throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
45-
byte[] loaded = Files.readAllBytes(Paths.get(CLIENT_KEY_RSA_PATH));
46-
PrivateKey privateKey = SSLUtils.loadKey(loaded);
47-
byte[] dumped = SSLUtils.dumpKey(privateKey);
48-
PrivateKey reloaded = SSLUtils.loadKey(dumped);
49-
assertEquals(privateKey, reloaded);
49+
@Test
50+
public void testLoadKeyRsaPkcs1() throws IOException, GeneralSecurityException {
51+
final PrivateKey privateKey = assertLoadDumpReloadKeyEquals(CLIENT_KEY_RSA_PKCS1);
52+
assertEquals(RSA_ALGORITHM, privateKey.getAlgorithm());
53+
}
54+
55+
@Test
56+
public void testLoadKeyEcdsaPkcs7() throws IOException, GeneralSecurityException {
57+
final PrivateKey privateKey = assertLoadDumpReloadKeyEquals(CLIENT_KEY_ECDSA_PKCS7);
58+
assertEquals(ECDSA_ALGORITHM, privateKey.getAlgorithm());
59+
}
60+
61+
@Test
62+
public void testLoadKeyEcdsaPkcs8() throws IOException, GeneralSecurityException {
63+
final PrivateKey privateKey = assertLoadDumpReloadKeyEquals(CLIENT_KEY_ECDSA_PKCS8);
64+
assertEquals(ECDSA_ALGORITHM, privateKey.getAlgorithm());
5065
}
5166

52-
public void testPKCS1ECKeyLoadDump()
67+
@Test(expected = InvalidKeySpecException.class)
68+
public void testLoadKeyCertificateNotSupported()
5369
throws IOException, InvalidKeySpecException, NoSuchAlgorithmException {
54-
byte[] loaded = Files.readAllBytes(Paths.get(CLIENT_KEY_EC_PATH));
55-
PrivateKey privateKey = SSLUtils.loadKey(loaded);
56-
byte[] dumped = SSLUtils.dumpKey(privateKey);
57-
PrivateKey reloaded = SSLUtils.loadKey(dumped);
58-
assertEquals(privateKey, reloaded);
70+
final byte[] resourceBytes = getResourceBytes(CLIENT_CERT);
71+
SSLUtils.loadKey(resourceBytes);
72+
}
73+
74+
private PrivateKey assertLoadDumpReloadKeyEquals(final String filePath)
75+
throws IOException, GeneralSecurityException {
76+
final byte[] resourceBytes = getResourceBytes(filePath);
77+
final PrivateKey privateKey = SSLUtils.loadKey(resourceBytes);
78+
79+
byte[] dumpedKey = SSLUtils.dumpKey(privateKey);
80+
final PrivateKey reloadedPrivateKey = SSLUtils.loadKey(dumpedKey);
81+
82+
assertEquals(privateKey, reloadedPrivateKey);
83+
84+
return privateKey;
85+
}
86+
87+
private byte[] getResourceBytes(final String filePath) throws IOException {
88+
final URL resourceUrl = Resources.getResource(filePath);
89+
return IOUtils.toByteArray(resourceUrl);
5990
}
6091
}

0 commit comments

Comments
 (0)