From 4f72b84c59ae5f12413b3e0892bad05d84ce78a3 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Mon, 17 Jun 2024 11:58:35 -0500 Subject: [PATCH] Cleanup ee9 DefaultServletTest + Fix various warnings + Disable ODD_JAR / extraClassPath (temporarily) + Reenable disabled test Test + Reenable disabled test testListingContextBreakout --- .../jetty/ee9/servlet/DefaultServletTest.java | 1275 +++++++++++------ 1 file changed, 843 insertions(+), 432 deletions(-) diff --git a/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/DefaultServletTest.java b/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/DefaultServletTest.java index 3824d5f5f6ff..49846109bf95 100644 --- a/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/DefaultServletTest.java +++ b/jetty-ee9/jetty-ee9-servlet/src/test/java/org/eclipse/jetty/ee9/servlet/DefaultServletTest.java @@ -15,7 +15,6 @@ import java.io.File; import java.io.IOException; -import java.io.OutputStream; import java.net.URL; import java.net.URLClassLoader; import java.nio.ByteBuffer; @@ -33,7 +32,6 @@ import jakarta.servlet.DispatcherType; import jakarta.servlet.Filter; import jakarta.servlet.FilterChain; -import jakarta.servlet.FilterConfig; import jakarta.servlet.ServletException; import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletResponse; @@ -57,6 +55,7 @@ import org.eclipse.jetty.server.Server; import org.eclipse.jetty.server.SymlinkAllowedResourceAliasChecker; import org.eclipse.jetty.toolchain.test.FS; +import org.eclipse.jetty.toolchain.test.MavenPaths; import org.eclipse.jetty.toolchain.test.MavenTestingUtils; import org.eclipse.jetty.toolchain.test.jupiter.WorkDir; import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension; @@ -67,7 +66,6 @@ import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.api.extension.ExtendWith; @@ -91,43 +89,41 @@ import static org.hamcrest.Matchers.notNullValue; import static org.hamcrest.Matchers.startsWith; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue; @ExtendWith(WorkDirExtension.class) public class DefaultServletTest { - public Path docRoot; + public WorkDir workDir; // The name of the odd-jar used for testing "jar:file://" based resource access. private static final String ODD_JAR = "jar-resource-odd.jar"; private Server server; private LocalConnector connector; - private ServletContextHandler context; @BeforeEach - public void init(WorkDir workDir) throws Exception + public void ensureFileSystemPoolIsSane() { assertThat(FileSystemPool.INSTANCE.mounts(), empty()); - docRoot = workDir.getEmptyPathDir().resolve("docroot"); - FS.ensureDirExists(docRoot); + } + public void startServer(Consumer contextInit) throws Exception + { server = new Server(); connector = new LocalConnector(server); connector.getConnectionFactory(HttpConfiguration.ConnectionFactory.class).getHttpConfiguration().setSendServerVersion(false); - File extraJarResources = MavenTestingUtils.getTargetFile(ODD_JAR); - URL[] urls = new URL[]{extraJarResources.toURI().toURL()}; - - ClassLoader parentClassLoader = Thread.currentThread().getContextClassLoader(); - URLClassLoader extraClassLoader = new URLClassLoader(urls, parentClassLoader); - - context = new ServletContextHandler(); - context.setBaseResource(ResourceFactory.of(context).newResource(docRoot)); + ServletContextHandler context = new ServletContextHandler(); + // DO NOT SET Base Resource here (some tests rely on alternate/none base resources) + // context.setBaseResource(); context.setContextPath("/context"); context.setWelcomeFiles(new String[]{"index.html", "index.jsp", "index.htm"}); - context.setClassLoader(extraClassLoader); + + if (contextInit != null) + contextInit.accept(context); server.setHandler(context); server.addConnector(connector); @@ -138,18 +134,31 @@ public void init(WorkDir workDir) throws Exception @AfterEach public void destroy() throws Exception { - server.stop(); - server.join(); + if (server != null) + { + server.stop(); + server.join(); + } assertThat(FileSystemPool.INSTANCE.mounts(), empty()); } @Test public void testListingWithSession() throws Exception { - ServletHolder defholder = context.addServlet(DefaultServlet.class, "/*"); - defholder.setInitParameter("dirAllowed", "true"); - defholder.setInitParameter("redirectWelcome", "false"); - defholder.setInitParameter("gzip", "false"); + Path docRoot = workDir.getEmptyPathDir().resolve("docroot"); + FS.ensureDirExists(docRoot); + + startServer((context) -> + { + context.setBaseResource(ResourceFactory.of(context).newResource(docRoot)); + + // this url-pattern is intentionally at '/*' to test behaviors when there + // are two (or more) DefaultServlets on overlapping url-patterns. + ServletHolder defholder = context.addServlet(DefaultServlet.class, "/*"); + defholder.setInitParameter("dirAllowed", "true"); + defholder.setInitParameter("redirectWelcome", "false"); + defholder.setInitParameter("gzip", "false"); + }); /* create some content in the docroot */ FS.ensureDirExists(docRoot.resolve("one")); @@ -171,10 +180,20 @@ public void testListingWithSession() throws Exception @Test public void testListingXSS() throws Exception { - ServletHolder defholder = context.addServlet(DefaultServlet.class, "/*"); - defholder.setInitParameter("dirAllowed", "true"); - defholder.setInitParameter("redirectWelcome", "false"); - defholder.setInitParameter("gzip", "false"); + Path docRoot = workDir.getEmptyPathDir().resolve("docroot"); + FS.ensureDirExists(docRoot); + + startServer((context) -> + { + context.setBaseResource(ResourceFactory.of(context).newResource(docRoot)); + + // this url-pattern is intentionally at '/*' to test behaviors when there + // are two (or more) DefaultServlets on overlapping url-patterns. + ServletHolder defholder = context.addServlet(DefaultServlet.class, "/*"); + defholder.setInitParameter("dirAllowed", "true"); + defholder.setInitParameter("redirectWelcome", "false"); + defholder.setInitParameter("gzip", "false"); + }); /* create some content in the docroot */ Path one = docRoot.resolve("one"); @@ -189,16 +208,20 @@ public void testListingXSS() throws Exception * Intentionally bad request URI. Sending a non-encoded URI with typically * encoded characters '<', '>', and '"'. */ - String req1 = "GET /context/; HTTP/1.0\r\n" + - "\r\n"; + String req1 = """ + GET /context/; HTTP/1.0\r + \r + """; String rawResponse = connector.getResponse(req1); HttpTester.Response response = HttpTester.parseResponse(rawResponse); String body = response.getContent(); assertThat(body, not(containsString("