Skip to content

Commit

Permalink
Modernize and add FFM
Browse files Browse the repository at this point in the history
  • Loading branch information
rmaucher committed May 16, 2024
1 parent 822c76b commit 556a7c3
Showing 1 changed file with 48 additions and 17 deletions.
65 changes: 48 additions & 17 deletions test/org/apache/tomcat/util/net/openssl/TestOpenSSLConf.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@
*/
package org.apache.tomcat.util.net.openssl;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hamcrest.CoreMatchers;
Expand All @@ -26,18 +30,38 @@
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;

import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.AprLifecycleListener;
import org.apache.catalina.core.OpenSSLLifecycleListener;
import org.apache.catalina.core.StandardServer;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.jni.SSLContext;
import org.apache.tomcat.util.net.SSLHostConfig;
import org.apache.tomcat.util.net.TesterSupport;

@RunWith(Parameterized.class)
public class TestOpenSSLConf extends TomcatBaseTest {

@Parameterized.Parameters(name = "{0}")
public static Collection<Object[]> parameters() {
List<Object[]> parameterSets = new ArrayList<>();
parameterSets.add(
new Object[] { "org.apache.tomcat.util.net.openssl.OpenSSLImplementation" });
parameterSets.add(
new Object[] { "org.apache.tomcat.util.net.openssl.panama.OpenSSLImplementation" });

return parameterSets;
}

@Parameter(0)
public String sslImplementationName;


private static final String ENABLED_CIPHER = "AES256-SHA256";
private static final String[] EXPECTED_CIPHERS = {ENABLED_CIPHER};
private static final String[] ENABLED_PROTOCOLS = {"TLSv1.1"};
Expand All @@ -61,7 +85,19 @@ private SSLHostConfig initOpenSSLConfCmd(String... commands) throws Exception {

TesterSupport.initSsl(tomcat);

Assert.assertTrue(connector.setProperty("sslImplementationName", OpenSSLImplementation.class.getName()));
Assert.assertTrue(tomcat.getConnector().setProperty("sslImplementationName", sslImplementationName));

if (OpenSSLImplementation.class.getName().equals(sslImplementationName)) {
AprLifecycleListener listener = new AprLifecycleListener();
Assume.assumeTrue(AprLifecycleListener.isAprAvailable());
StandardServer server = (StandardServer) tomcat.getServer();
server.addLifecycleListener(listener);
} else if ("org.apache.tomcat.util.net.openssl.panama.OpenSSLImplementation".equals(sslImplementationName)) {
OpenSSLLifecycleListener listener = new OpenSSLLifecycleListener();
Assume.assumeTrue(OpenSSLLifecycleListener.isAvailable());
StandardServer server = (StandardServer) tomcat.getServer();
server.addLifecycleListener(listener);
}

OpenSSLConf conf = new OpenSSLConf();
for (int i = 0; i < commands.length;) {
Expand Down Expand Up @@ -95,7 +131,17 @@ public void testOpenSSLConfCmdCipher() throws Exception {
String[] ciphers = sslHostConfig.getEnabledCiphers();
MatcherAssert.assertThat("Wrong HostConfig ciphers", ciphers,
CoreMatchers.is(EXPECTED_CIPHERS));
ciphers = SSLContext.getCiphers(sslHostConfig.getOpenSslContext().longValue());
if (OpenSSLImplementation.class.getName().equals(sslImplementationName)) {
ciphers = SSLContext.getCiphers(sslHostConfig.getOpenSslContext().longValue());
} else {
Class<?> memorySegmentClass = Class.forName("java.lang.foreign.MemorySegment");
Object ssxCtxSegment = memorySegmentClass.getMethod("ofAddress", Long.TYPE)
.invoke(null, sslHostConfig.getOpenSslContext());
Method getCiphersMethod = Class.forName("org.apache.tomcat.util.net.openssl.panama.OpenSSLContext")
.getDeclaredMethod("getCiphers", memorySegmentClass);
getCiphersMethod.setAccessible(true);
ciphers = (String[]) getCiphersMethod.invoke(null, ssxCtxSegment);
}
MatcherAssert.assertThat("Wrong native SSL context ciphers", ciphers,
CoreMatchers.is(EXPECTED_CIPHERS));
}
Expand Down Expand Up @@ -130,19 +176,4 @@ public void testOpenSSLConfCmdProtocol() throws Exception {
}
}


@Override
public void setUp() throws Exception {
super.setUp();

// Tests are only intended for OpenSSL
Assume.assumeTrue(TesterSupport.isOpensslAvailable());

Tomcat tomcat = getTomcatInstance();

AprLifecycleListener listener = new AprLifecycleListener();
Assume.assumeTrue(AprLifecycleListener.isAprAvailable());
StandardServer server = (StandardServer) tomcat.getServer();
server.addLifecycleListener(listener);
}
}

0 comments on commit 556a7c3

Please sign in to comment.