Skip to content

Commit f920167

Browse files
committed
HTTPCORE-785: Improved ALPN tests with the default (Oracle) and Conscrypt security providers
1 parent 324f3b5 commit f920167

4 files changed

Lines changed: 130 additions & 311 deletions

File tree

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* ====================================================================
3+
* Licensed to the Apache Software Foundation (ASF) under one
4+
* or more contributor license agreements. See the NOTICE file
5+
* distributed with this work for additional information
6+
* regarding copyright ownership. The ASF licenses this file
7+
* to you under the Apache License, Version 2.0 (the
8+
* "License"); you may not use this file except in compliance
9+
* with the License. You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing,
14+
* software distributed under the License is distributed on an
15+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16+
* KIND, either express or implied. See the License for the
17+
* specific language governing permissions and limitations
18+
* under the License.
19+
* ====================================================================
20+
*
21+
* This software consists of voluntary contributions made by many
22+
* individuals on behalf of the Apache Software Foundation. For more
23+
* information on the Apache Software Foundation, please see
24+
* <http://www.apache.org/>.
25+
*
26+
*/
27+
package org.apache.hc.core5.testing.extension;
28+
29+
import java.security.Provider;
30+
import java.security.Security;
31+
import java.util.Arrays;
32+
import java.util.HashSet;
33+
import java.util.Set;
34+
35+
import org.conscrypt.Conscrypt;
36+
import org.junit.jupiter.api.Assertions;
37+
import org.junit.jupiter.api.Assumptions;
38+
import org.junit.jupiter.api.extension.AfterEachCallback;
39+
import org.junit.jupiter.api.extension.BeforeEachCallback;
40+
import org.junit.jupiter.api.extension.ExtensionContext;
41+
42+
public class SecurityProviderResource implements BeforeEachCallback, AfterEachCallback {
43+
44+
private final String securityProviderName;
45+
46+
private Provider securityProvider;
47+
48+
public SecurityProviderResource(final String securityProviderName) {
49+
super();
50+
this.securityProviderName = securityProviderName;
51+
}
52+
53+
@Override
54+
public void beforeEach(final ExtensionContext context) throws Exception {
55+
if ("Conscrypt".equalsIgnoreCase(securityProviderName)) {
56+
final Set<String> supportedArchitectures = new HashSet<>(Arrays.asList("x86", "x86_64",
57+
"x86-64", "amd64", "aarch64", "armeabi-v7a", "arm64-v8a"));
58+
Assumptions.assumeTrue(supportedArchitectures.contains(System.getProperty("os.arch")));
59+
try {
60+
securityProvider = Conscrypt.newProviderBuilder().provideTrustManager(true).build();
61+
} catch (final UnsatisfiedLinkError e) {
62+
Assertions.fail("Conscrypt provider failed to be loaded: " + e.getMessage());
63+
}
64+
} else if ("Oracle".equalsIgnoreCase(securityProviderName)) {
65+
securityProvider = null;
66+
} else if ("SUN".equalsIgnoreCase(securityProviderName)) {
67+
securityProvider = null;
68+
} else {
69+
throw new AssertionError("Unsupported security provider: " + securityProviderName);
70+
}
71+
if (securityProvider != null) {
72+
Security.insertProviderAt(securityProvider, 1);
73+
}
74+
}
75+
76+
@Override
77+
public void afterEach(final ExtensionContext context) throws Exception {
78+
if (securityProvider != null) {
79+
Security.removeProvider(securityProvider.getName());
80+
securityProvider = null;
81+
}
82+
}
83+
84+
public Provider securityProvider() {
85+
return securityProvider;
86+
}
87+
88+
}

httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/H2ProtocolNegotiationTest.java renamed to httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/AlpnTest.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131

3232
import java.net.InetSocketAddress;
3333
import java.util.concurrent.Future;
34+
import java.util.function.Function;
35+
36+
import javax.net.ssl.SSLContext;
3437

3538
import org.apache.hc.core5.function.Supplier;
3639
import org.apache.hc.core5.http.ContentType;
@@ -48,35 +51,47 @@
4851
import org.apache.hc.core5.http.nio.AsyncServerExchangeHandler;
4952
import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer;
5053
import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer;
54+
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
5155
import org.apache.hc.core5.http.nio.support.BasicRequestProducer;
5256
import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
5357
import org.apache.hc.core5.http2.HttpVersionPolicy;
54-
import org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy;
5558
import org.apache.hc.core5.http2.ssl.H2ServerTlsStrategy;
5659
import org.apache.hc.core5.reactor.IOReactorConfig;
5760
import org.apache.hc.core5.reactor.ListenerEndpoint;
5861
import org.apache.hc.core5.testing.SSLTestContexts;
62+
import org.apache.hc.core5.testing.extension.SecurityProviderResource;
5963
import org.apache.hc.core5.testing.extension.nio.H2AsyncRequesterResource;
6064
import org.apache.hc.core5.testing.extension.nio.H2AsyncServerResource;
6165
import org.apache.hc.core5.util.Timeout;
6266
import org.hamcrest.CoreMatchers;
67+
import org.junit.jupiter.api.Order;
6368
import org.junit.jupiter.api.Test;
6469
import org.junit.jupiter.api.extension.RegisterExtension;
6570

66-
class H2ProtocolNegotiationTest {
71+
abstract class AlpnTest {
6772

6873
private static final Timeout TIMEOUT = Timeout.ofMinutes(1);
6974

7075
@RegisterExtension
76+
@Order(1)
77+
private final SecurityProviderResource securityProviderResource;
78+
@RegisterExtension
79+
@Order(2)
7180
private final H2AsyncServerResource serverResource;
7281
@RegisterExtension
82+
@Order(3)
7383
private final H2AsyncRequesterResource clientResource;
7484

75-
public H2ProtocolNegotiationTest() {
85+
public AlpnTest(final String securityProviderName,
86+
final Function<SSLContext, TlsStrategy> serverTlsStrategyFactory,
87+
final Function<SSLContext, TlsStrategy> clientTlsStrategyFactory) {
88+
this.securityProviderResource = new SecurityProviderResource(securityProviderName);
7689
this.serverResource = new H2AsyncServerResource();
7790
this.serverResource.configure(bootstrap -> bootstrap
7891
.setVersionPolicy(HttpVersionPolicy.NEGOTIATE)
79-
.setTlsStrategy(new H2ServerTlsStrategy(SSLTestContexts.createServerSSLContext()))
92+
.setTlsStrategy(serverTlsStrategyFactory != null ?
93+
serverTlsStrategyFactory.apply(SSLTestContexts.createServerSSLContext()) :
94+
new H2ServerTlsStrategy(SSLTestContexts.createServerSSLContext()))
8095
.setIOReactorConfig(
8196
IOReactorConfig.custom()
8297
.setSoTimeout(TIMEOUT)
@@ -89,7 +104,9 @@ public H2ProtocolNegotiationTest() {
89104
this.clientResource = new H2AsyncRequesterResource();
90105
this.clientResource.configure(bootstrap -> bootstrap
91106
.setVersionPolicy(HttpVersionPolicy.NEGOTIATE)
92-
.setTlsStrategy(new H2ClientTlsStrategy(SSLTestContexts.createClientSSLContext()))
107+
.setTlsStrategy(clientTlsStrategyFactory != null ?
108+
clientTlsStrategyFactory.apply(SSLTestContexts.createServerSSLContext()) :
109+
new H2ServerTlsStrategy(SSLTestContexts.createServerSSLContext()))
93110
.setIOReactorConfig(IOReactorConfig.custom()
94111
.setSoTimeout(TIMEOUT)
95112
.build())

httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/JSSEProviderIntegrationTests.java renamed to httpcore5-testing/src/test/java/org/apache/hc/core5/testing/nio/AlpnTests.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,41 +27,48 @@
2727

2828
package org.apache.hc.core5.testing.nio;
2929

30+
import org.apache.hc.core5.http2.ssl.ConscryptClientTlsStrategy;
31+
import org.apache.hc.core5.http2.ssl.ConscryptServerTlsStrategy;
32+
import org.apache.hc.core5.http2.ssl.H2ClientTlsStrategy;
33+
import org.apache.hc.core5.http2.ssl.H2ServerTlsStrategy;
3034
import org.junit.jupiter.api.DisplayName;
3135
import org.junit.jupiter.api.Nested;
3236
import org.junit.jupiter.api.condition.DisabledOnOs;
37+
import org.junit.jupiter.api.condition.EnabledForJreRange;
38+
import org.junit.jupiter.api.condition.JRE;
3339
import org.junit.jupiter.api.condition.OS;
3440

35-
class JSSEProviderIntegrationTests {
41+
class AlpnTests {
3642

3743
@Nested
38-
@DisplayName("Oracle (default)")
39-
class Oracle extends JSSEProviderIntegrationTest {
44+
@DisplayName("ALPN Oracle")
45+
class OracleJSSEAlpnTest extends AlpnTest {
4046

41-
public Oracle() {
42-
super("Oracle", null);
47+
public OracleJSSEAlpnTest() throws Exception {
48+
super("Oracle", H2ServerTlsStrategy::new, H2ClientTlsStrategy::new);
4349
}
4450

4551
}
4652

4753
@Nested
48-
@DisplayName("Conscrypt (TLSv1.2)")
54+
@DisplayName("ALPN Conscrypt (Java 9 or newer)")
4955
@DisabledOnOs(OS.MAC)
50-
class ConscryptTlsV1_2 extends JSSEProviderIntegrationTest {
56+
@EnabledForJreRange(min = JRE.JAVA_9)
57+
class ConscryptJSSEAlpnTest extends AlpnTest {
5158

52-
public ConscryptTlsV1_2() {
53-
super("Conscrypt", "TLSv1.2");
59+
public ConscryptJSSEAlpnTest() throws Exception {
60+
super("Conscrypt", H2ServerTlsStrategy::new, H2ClientTlsStrategy::new);
5461
}
5562

5663
}
5764

5865
@Nested
59-
@DisplayName("Conscrypt (TLSv1.3)")
66+
@DisplayName("ALPN Conscrypt (Conscrypt specific TLS strategies)")
6067
@DisabledOnOs(OS.MAC)
61-
class ConscryptTlsV1_3 extends JSSEProviderIntegrationTest {
68+
class ConscryptJSSEAndStrategiesAlpnTest extends AlpnTest {
6269

63-
public ConscryptTlsV1_3() {
64-
super("Conscrypt", "TLSv1.3");
70+
public ConscryptJSSEAndStrategiesAlpnTest() throws Exception {
71+
super("Conscrypt", ConscryptServerTlsStrategy::new, ConscryptClientTlsStrategy::new);
6572
}
6673

6774
}

0 commit comments

Comments
 (0)