blob: e814720ff8a38cca0b30b53103bf8babb68b3d12 [file] [log] [blame]
/*
* Copyright (c) 2015, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.jersey.jdkhttp;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.UriBuilder;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLHandshakeException;
import org.apache.commons.io.IOUtils;
import org.glassfish.jersey.SslConfigurator;
import org.glassfish.jersey.internal.util.JdkVersion;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.After;
import org.junit.Test;
import com.sun.net.httpserver.HttpServer;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsServer;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;
/**
* Jdk Https Server tests.
*
* @author Adam Lindenthal
*/
public class JdkHttpsServerTest extends AbstractJdkHttpServerTester {
private static final String TRUSTSTORE_CLIENT_FILE = "./truststore_client";
private static final String TRUSTSTORE_CLIENT_PWD = "asdfgh";
private static final String KEYSTORE_CLIENT_FILE = "./keystore_client";
private static final String KEYSTORE_CLIENT_PWD = "asdfgh";
private static final String KEYSTORE_SERVER_FILE = "./keystore_server";
private static final String KEYSTORE_SERVER_PWD = "asdfgh";
private static final String TRUSTSTORE_SERVER_FILE = "./truststore_server";
private static final String TRUSTSTORE_SERVER_PWD = "asdfgh";
private HttpServer server;
private final URI httpsUri = UriBuilder.fromUri("https://localhost/").port(getPort()).build();
private final URI httpUri = UriBuilder.fromUri("http://localhost/").port(getPort()).build();
private final ResourceConfig rc = new ResourceConfig(TestResource.class);
@Path("/testHttps")
public static class TestResource {
@GET
public String get() {
return "test";
}
}
/**
* Test, that {@link HttpsServer} instance is returned when providing empty SSLContext (but not starting).
* @throws Exception
*/
@Test
public void testCreateHttpsServerNoSslContext() throws Exception {
server = JdkHttpServerFactory.createHttpServer(httpsUri, rc, null, false);
assertThat(server, instanceOf(HttpsServer.class));
if (JdkVersion.getJdkVersion().getMajor() > 8) {
server.start(); // Address already in bind otherwise
}
}
/**
* Test, that exception is thrown when attempting to start a {@link HttpsServer} with empty SSLContext.
* @throws Exception
*/
@Test(expected = IllegalArgumentException.class)
public void testStartHttpServerNoSslContext() throws Exception {
server = JdkHttpServerFactory.createHttpServer(httpsUri, rc, null, true);
}
/**
* Test, that {@link javax.net.ssl.SSLHandshakeException} is thrown when attepmting to connect to server with client
* not configured correctly.
* @throws Exception
*/
@Test(expected = SSLHandshakeException.class)
public void testCreateHttpsServerDefaultSslContext() throws Throwable {
server = JdkHttpServerFactory.createHttpServer(httpsUri, rc, SSLContext.getDefault(), true);
assertThat(server, instanceOf(HttpsServer.class));
// access the https server with not configured client
final Client client = ClientBuilder.newBuilder().newClient();
try {
client.target(httpsUri).path("testHttps").request().get(String.class);
} catch (final ProcessingException e) {
throw e.getCause();
}
}
/**
* Test, that {@link HttpsServer} can be manually started even with (empty) SSLContext, but will throw an exception
* on request.
* @throws Exception
*/
@Test(expected = IOException.class)
public void testHttpsServerNoSslContextDelayedStart() throws Throwable {
server = JdkHttpServerFactory.createHttpServer(httpsUri, rc, null, false);
assertThat(server, instanceOf(HttpsServer.class));
server.start();
final Client client = ClientBuilder.newBuilder().newClient();
try {
client.target(httpsUri).path("testHttps").request().get(String.class);
} catch (final ProcessingException e) {
throw e.getCause();
}
}
/**
* Test, that {@link HttpsServer} cannot be configured with {@link HttpsConfigurator} after it has started.
* @throws Exception
*/
@Test(expected = IllegalStateException.class)
public void testConfigureSslContextAfterStart() throws Throwable {
server = JdkHttpServerFactory.createHttpServer(httpsUri, rc, null, false);
assertThat(server, instanceOf(HttpsServer.class));
server.start();
((HttpsServer) server).setHttpsConfigurator(new HttpsConfigurator(getServerSslContext()));
}
/**
* Tests a client to server roundtrip with correctly configured SSL on both sides.
* @throws IOException
*/
@Test
public void testCreateHttpsServerRoundTrip() throws IOException {
final SSLContext serverSslContext = getServerSslContext();
server = JdkHttpServerFactory.createHttpServer(httpsUri, rc, serverSslContext, true);
final SSLContext foundContext = ((HttpsServer) server).getHttpsConfigurator().getSSLContext();
assertEquals(serverSslContext, foundContext);
final SSLContext clientSslContext = getClientSslContext();
final Client client = ClientBuilder.newBuilder().sslContext(clientSslContext).build();
final String response = client.target(httpsUri).path("testHttps").request().get(String.class);
assertEquals("test", response);
}
/**
* Test, that if URI uses http scheme instead of https, SSLContext is ignored.
* @throws IOException
*/
@Test
public void testHttpWithSsl() throws IOException {
server = JdkHttpServerFactory.createHttpServer(httpUri, rc, getServerSslContext(), true);
assertThat(server, instanceOf(HttpServer.class));
assertThat(server, not(instanceOf(HttpsServer.class)));
}
private SSLContext getClientSslContext() throws IOException {
final InputStream trustStore = JdkHttpsServerTest.class.getResourceAsStream(TRUSTSTORE_CLIENT_FILE);
final InputStream keyStore = JdkHttpsServerTest.class.getResourceAsStream(KEYSTORE_CLIENT_FILE);
final SslConfigurator sslConfigClient = SslConfigurator.newInstance()
.trustStoreBytes(IOUtils.toByteArray(trustStore))
.trustStorePassword(TRUSTSTORE_CLIENT_PWD)
.keyStoreBytes(IOUtils.toByteArray(keyStore))
.keyPassword(KEYSTORE_CLIENT_PWD);
return sslConfigClient.createSSLContext();
}
private SSLContext getServerSslContext() throws IOException {
final InputStream trustStore = JdkHttpsServerTest.class.getResourceAsStream(TRUSTSTORE_SERVER_FILE);
final InputStream keyStore = JdkHttpsServerTest.class.getResourceAsStream(KEYSTORE_SERVER_FILE);
final SslConfigurator sslConfigServer = SslConfigurator.newInstance()
.keyStoreBytes(IOUtils.toByteArray(keyStore))
.keyPassword(KEYSTORE_SERVER_PWD)
.trustStoreBytes(IOUtils.toByteArray(trustStore))
.trustStorePassword(TRUSTSTORE_SERVER_PWD);
return sslConfigServer.createSSLContext();
}
@After
public void tearDown() {
if (server != null) {
server.stop(0);
server = null;
}
}
}