Skip to content

Commit 8f0076c

Browse files
committed
clean up code + add back failing tests
1 parent 4747d71 commit 8f0076c

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

google-api-client-apache-v5/src/test/java/com/google/api/client/googleapis/apache/v5/ITGoogleApache5HttpTransportTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929

3030
public class ITGoogleApache5HttpTransportTest {
3131

32-
/*@Test
33-
public void testHttpRequestFailsWhenMakingRequestToSiteWithoutGoogleCerts()
32+
@Test
33+
public void testHttpRequestFailsWhenMakingRequestToSiteWithoutDefaultJdkCerts()
3434
throws GeneralSecurityException, IOException {
3535
Apache5HttpTransport apache5HttpTransport = GoogleApache5HttpTransport.newTrustedTransport();
36-
HttpGet httpGet = new HttpGet("https://shopify.com/");
36+
// Use a self-signed certificate site that won't be trusted by default trust store
37+
HttpGet httpGet = new HttpGet("https://self-signed.badssl.com/");
3738
Exception exception = null;
3839
try {
3940
apache5HttpTransport
@@ -43,7 +44,7 @@ public void testHttpRequestFailsWhenMakingRequestToSiteWithoutGoogleCerts()
4344
new HttpClientResponseHandler<Void>() {
4445
@Override
4546
public Void handleResponse(ClassicHttpResponse response) {
46-
fail("Should not have been able to complete SSL request on non google site.");
47+
fail("Should not have been able to complete SSL request with untrusted cert.");
4748
return null;
4849
}
4950
});
@@ -54,7 +55,7 @@ public Void handleResponse(ClassicHttpResponse response) {
5455

5556
assertNotNull(exception);
5657
assertEquals(exception.getClass(), SSLHandshakeException.class);
57-
}*/
58+
}
5859

5960
@Test
6061
public void testHttpRequestPassesWhenMakingRequestToGoogleSite() throws Exception {

google-api-client/src/main/java/com/google/api/client/googleapis/GoogleUtils.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,19 @@ public final class GoogleUtils {
7979
/** Bundled keystore password. */
8080
static final String BUNDLED_KEYSTORE_PASSWORD = "notasecret";
8181

82+
/** Default JDK cacerts file path relative to java.home. */
83+
@VisibleForTesting
84+
static String[] possibleJdkPaths = {
85+
"lib/security/cacerts", // Java 9+
86+
"jre/lib/security/cacerts" // Java 8 and earlier
87+
};
88+
89+
/** Java home system property key. */
90+
static final String JAVA_HOME_KEY = "java.home";
91+
92+
/** Default password for JDK cacerts file. */
93+
static final String DEFAULT_CACERTS_PASSWORD = "changeit";
94+
8295
/**
8396
* Loads the bundled google.p12 keystore containing trusted root certificates.
8497
*
@@ -103,16 +116,12 @@ static KeyStore getJdkDefaultKeyStore() throws IOException, GeneralSecurityExcep
103116
KeyStore keyStore = SecurityUtils.getDefaultKeyStore();
104117

105118
// Find the default JDK cacerts location
106-
String javaHome = System.getProperty("java.home");
107-
String[] possiblePaths = {
108-
"lib/security/cacerts", // Java 9+
109-
"jre/lib/security/cacerts" // Java 8 and earlier
110-
};
119+
String javaHome = System.getProperty(JAVA_HOME_KEY);
111120

112-
for (String path : possiblePaths) {
121+
for (String path : possibleJdkPaths) {
113122
File cacertsFile = new File(javaHome, path);
114123
try (FileInputStream fis = new FileInputStream(cacertsFile)) {
115-
keyStore.load(fis, "changeit".toCharArray());
124+
keyStore.load(fis, DEFAULT_CACERTS_PASSWORD.toCharArray());
116125
return keyStore;
117126
} catch (IOException e) {
118127
// File doesn't exist or can't be read, try next path

google-api-client/src/test/java/com/google/api/client/googleapis/GoogleUtilsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public void testGetCertificateTrustStore_LoadsJdkDefaultFirst() throws Exception
4040
trustStore.size());
4141
}
4242

43-
/* public void testGetCertificateTrustStore_LoadsBundledKeystoreIfJdkDefaultLoadFails()
43+
public void testGetCertificateTrustStore_LoadsBundledKeystoreIfJdkDefaultLoadFails()
4444
throws Exception {
4545
GoogleUtils.certTrustStore = null;
46-
GoogleUtils.defaultCacertsPath = "bad/path";
46+
GoogleUtils.possibleJdkPaths = new String[0];
4747

4848
KeyStore trustStore = GoogleUtils.getCertificateTrustStore();
4949

@@ -53,7 +53,7 @@ public void testGetCertificateTrustStore_LoadsJdkDefaultFirst() throws Exception
5353
"Certificate truststore should contain the same amount of certificates as the bundled keystore",
5454
trustStore.size(),
5555
bundled.size());
56-
}*/
56+
}
5757

5858
public void testGetCertificateTrustStore_IsCached() throws Exception {
5959
KeyStore trustStore1 = GoogleUtils.getCertificateTrustStore();

0 commit comments

Comments
 (0)