|
1 | 1 | /* |
2 | | - * Copyright © 2015, 2018 IBM Corp. All rights reserved. |
| 2 | + * Copyright © 2015, 2020 IBM Corp. All rights reserved. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
5 | 5 | * except in compliance with the License. You may obtain a copy of the License at |
|
35 | 35 | import com.cloudant.http.HttpConnectionInterceptorContext; |
36 | 36 | import com.cloudant.http.HttpConnectionResponseInterceptor; |
37 | 37 |
|
| 38 | +import org.junit.jupiter.api.Assumptions; |
| 39 | + |
38 | 40 | import java.io.File; |
39 | 41 | import java.io.FileNotFoundException; |
40 | 42 | import java.net.URI; |
@@ -225,4 +227,34 @@ public static void assertOKResponse(Response r) throws Exception { |
225 | 227 | assertTrue(r.getStatusCode() |
226 | 228 | / 100 == 2, "The response code should be 2XX was " + r.getStatusCode()); |
227 | 229 | } |
| 230 | + |
| 231 | + /** |
| 232 | + * Utility to ignore tests when running with optional OkHttp dependency and Java 8_252 or newer. |
| 233 | + * This should be applied to any test that uses the customSSLSocketFactory or |
| 234 | + * disableSSLAuthentication options. |
| 235 | + * |
| 236 | + * These options cannot be used with OkHttp and Java 8_252 or newer |
| 237 | + * (see https://github.com/square/okhttp/issues/5970. |
| 238 | + * OkHttp treats 8_252 and newer as equivalent to 9+ because of the availability of ALPN |
| 239 | + * support. In 9+ reflection cannot be used to infer the TrustManager from the SslSocketFactory |
| 240 | + * so setting the SslSocketFactory without a TrustManager is prevented. This blocks the route we |
| 241 | + * currently use to supply custom SslSocketFactory via the deprecated OkHttp OkUrlFactory path |
| 242 | + * via javax.net.ssl.HttpsURLConnection#setSSLSocketFactory(javax.net.ssl.SSLSocketFactory). |
| 243 | + * |
| 244 | + */ |
| 245 | + public static void assumeCustomSslAuthUsable(boolean isOkUsable) { |
| 246 | + String version = System.getProperty("java.version"); |
| 247 | + boolean java8_252OrHigher = false; |
| 248 | + // Java 1.8 or lower, 9+ are 9, 10 etc |
| 249 | + if (version.startsWith("1.")) { |
| 250 | + if (version.startsWith("1.8.")) { |
| 251 | + int update = Integer.parseInt(version.split("_")[1]); |
| 252 | + if (update >= 252) java8_252OrHigher = true; |
| 253 | + } |
| 254 | + } else { |
| 255 | + java8_252OrHigher = true; |
| 256 | + } |
| 257 | + Assumptions.assumeFalse(isOkUsable && java8_252OrHigher, "Test uses custom " + |
| 258 | + "SslSocketFactory, incompatible with OkHttp and Java 8_252 or newer."); |
| 259 | + } |
228 | 260 | } |
0 commit comments