This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push:
new 18e20cda75 Refactor AJP tests to use a common base class
18e20cda75 is described below
commit 18e20cda7531b4bad3b0369045f5bb51a872a5a0
Author: Mark Thomas <[email protected]>
AuthorDate: Tue Oct 20 20:52:05 2026 +0100
Refactor AJP tests to use a common base class
Fixes broken AJP tests
Fix various back-port issues as well
---
.../tribes/membership/TestMcastService.java | 4 +-
.../tribes/tipis/TestAbstractReplicatedMap.java | 4 +-
.../tribes/tipis/TestLazyReplicatedMap.java | 2 +-
test/org/apache/coyote/ajp/AjpBaseTest.java | 58 ++++++++++++++++++++++
.../coyote/ajp/TestAbstractAjpProcessor.java | 30 +----------
test/org/apache/coyote/ajp/TestAjpSecurity.java | 17 +++----
.../tomcat/security/TestSecurity2026Http2.java | 31 ++++++++----
7 files changed, 95 insertions(+), 51 deletions(-)
diff --git a/test/org/apache/catalina/tribes/membership/TestMcastService.java
b/test/org/apache/catalina/tribes/membership/TestMcastService.java
index b54696b3a5..fde5e37dd0 100644
--- a/test/org/apache/catalina/tribes/membership/TestMcastService.java
+++ b/test/org/apache/catalina/tribes/membership/TestMcastService.java
@@ -37,7 +37,7 @@ public class TestMcastService {
*/
@Test(timeout = 60000)
public void testStopWithFailingShutdownSendCompletes() throws Exception {
- int base = 40000 + Math.floorMod(System.nanoTime(), 10000);
+ int base = 40000 + (int) Math.floorMod(System.nanoTime(), 10000);
StringBuilder properties = new StringBuilder();
properties.append("mcastAddress=228.0.0.4\n");
properties.append("mcastPort=").append(base).append("\n");
@@ -166,7 +166,7 @@ public class TestMcastService {
*/
@Test(timeout = 60000)
public void testBroadcastRejectsPacketsTheSocketCannotCarry() throws
Exception {
- int base = 40000 + Math.floorMod(System.nanoTime(), 10000);
+ int base = 40000 + (int) Math.floorMod(System.nanoTime(), 10000);
Properties props = new Properties();
props.setProperty("mcastAddress", "228.0.0.4");
props.setProperty("mcastPort", String.valueOf(base));
diff --git
a/test/org/apache/catalina/tribes/tipis/TestAbstractReplicatedMap.java
b/test/org/apache/catalina/tribes/tipis/TestAbstractReplicatedMap.java
index 7dfd9c28ed..02a57d9c4f 100644
--- a/test/org/apache/catalina/tribes/tipis/TestAbstractReplicatedMap.java
+++ b/test/org/apache/catalina/tribes/tipis/TestAbstractReplicatedMap.java
@@ -123,7 +123,7 @@ public class TestAbstractReplicatedMap {
AtomicInteger disappeared = new AtomicInteger();
AtomicBoolean lockAvailable = new AtomicBoolean();
- LazyReplicatedMap<String,String> map = new LazyReplicatedMap<>((key,
value) -> {
+ LazyReplicatedMap<String,String> map = new
LazyReplicatedMap<String,String>((key, value) -> {
// Not made primary here
}, new TestChannel(), 1000, "TestAbstractReplicatedMap", null) {
@@ -154,7 +154,7 @@ public class TestAbstractReplicatedMap {
AtomicBoolean lockAvailable = new AtomicBoolean();
AtomicInteger published = new AtomicInteger();
- LazyReplicatedMap<String,String> map = new LazyReplicatedMap<>((key,
value) -> {
+ LazyReplicatedMap<String,String> map = new
LazyReplicatedMap<String,String>((key, value) -> {
// Not made primary here
}, new TestChannel(), 1000, "TestAbstractReplicatedMap", null) {
diff --git a/test/org/apache/catalina/tribes/tipis/TestLazyReplicatedMap.java
b/test/org/apache/catalina/tribes/tipis/TestLazyReplicatedMap.java
index 29287c9414..d85c14630a 100644
--- a/test/org/apache/catalina/tribes/tipis/TestLazyReplicatedMap.java
+++ b/test/org/apache/catalina/tribes/tipis/TestLazyReplicatedMap.java
@@ -37,7 +37,7 @@ public class TestLazyReplicatedMap {
// A member added between the two steps of the selection, shown
directly: one member in
// the snapshot, an index computed for a member list that has
meanwhile grown
- LazyReplicatedMap<String,String> map = new LazyReplicatedMap<>((key,
value) -> {
+ LazyReplicatedMap<String,String> map = new
LazyReplicatedMap<String,String>((key, value) -> {
// Not made primary here
}, new GroupChannel(), 1000, "TestLazyReplicatedMap", null) {
diff --git a/test/org/apache/coyote/ajp/AjpBaseTest.java
b/test/org/apache/coyote/ajp/AjpBaseTest.java
new file mode 100644
index 0000000000..5b025a5f00
--- /dev/null
+++ b/test/org/apache/coyote/ajp/AjpBaseTest.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.coyote.ajp;
+
+import org.junit.Before;
+
+import org.apache.catalina.startup.TomcatBaseTest;
+
+/**
+ * Base class for tests that need an AJP connector rather than the default
HTTP connector.
+ */
+public abstract class AjpBaseTest extends TomcatBaseTest {
+
+ @Before
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+
+ getTomcatInstance().getConnector().setProperty("secretRequired",
"false");
+ }
+
+
+ @Override
+ protected String getProtocol() {
+ /*
+ * The tests are all setup for HTTP so need to convert the protocol
values to AJP.
+ */
+ // Has a protocol been specified
+ String protocol = System.getProperty("tomcat.test.protocol");
+
+ // Use NIO by default
+ if (protocol == null) {
+ protocol = "org.apache.coyote.ajp.AjpNioProtocol";
+ } else if (protocol.contains("Nio2")) {
+ protocol = "org.apache.coyote.ajp.AjpNio2Protocol";
+ } else if (protocol.contains("Apr")) {
+ protocol = "org.apache.coyote.ajp.AjpAprProtocol";
+ } else {
+ protocol = "org.apache.coyote.ajp.AjpNioProtocol";
+ }
+
+ return protocol;
+ }
+}
diff --git a/test/org/apache/coyote/ajp/TestAbstractAjpProcessor.java
b/test/org/apache/coyote/ajp/TestAbstractAjpProcessor.java
index e64bf99e10..4106c3e177 100644
--- a/test/org/apache/coyote/ajp/TestAbstractAjpProcessor.java
+++ b/test/org/apache/coyote/ajp/TestAbstractAjpProcessor.java
@@ -38,47 +38,21 @@ import org.junit.Test;
import org.apache.catalina.Context;
import org.apache.catalina.Globals;
-import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
-import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.http.Method;
import org.apache.tomcat.util.res.StringManager;
-public class TestAbstractAjpProcessor extends TomcatBaseTest {
+public class TestAbstractAjpProcessor extends AjpBaseTest {
@Before
@Override
public void setUp() throws Exception {
super.setUp();
- Connector c = getTomcatInstance().getConnector();
- c.setProperty("secretRequired", "false");
- c.setProperty("allowedRequestAttributesPattern", "MYATTRIBUTE.*");
+
getTomcatInstance().getConnector().setProperty("allowedRequestAttributesPattern",
"MYATTRIBUTE.*");
}
- @Override
- protected String getProtocol() {
- /*
- * The tests are all setup for HTTP so need to convert the protocol
values to AJP.
- */
- // Has a protocol been specified
- String protocol = System.getProperty("tomcat.test.protocol");
-
- // Use NIO by default
- if (protocol == null) {
- protocol = "org.apache.coyote.ajp.AjpNioProtocol";
- } else if (protocol.contains("Nio2")) {
- protocol = "org.apache.coyote.ajp.AjpNio2Protocol";
- } else if (protocol.contains("Apr")) {
- protocol = "org.apache.coyote.ajp.AjpAprProtocol";
- } else {
- protocol = "org.apache.coyote.ajp.AjpNioProtocol";
- }
-
- return protocol;
- }
-
private void doSnoopTest(RequestDescriptor desc) throws Exception {
final int ajpPacketSize = 16000;
diff --git a/test/org/apache/coyote/ajp/TestAjpSecurity.java
b/test/org/apache/coyote/ajp/TestAjpSecurity.java
index df4cea10bf..6fc4dd63d6 100644
--- a/test/org/apache/coyote/ajp/TestAjpSecurity.java
+++ b/test/org/apache/coyote/ajp/TestAjpSecurity.java
@@ -19,13 +19,13 @@ package org.apache.coyote.ajp;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
-import jakarta.servlet.AsyncContext;
-import jakarta.servlet.ServletException;
-import jakarta.servlet.ServletOutputStream;
-import jakarta.servlet.WriteListener;
-import jakarta.servlet.http.HttpServlet;
-import jakarta.servlet.http.HttpServletRequest;
-import jakarta.servlet.http.HttpServletResponse;
+import javax.servlet.AsyncContext;
+import javax.servlet.ServletException;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.WriteListener;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
@@ -34,14 +34,13 @@ import org.apache.catalina.Context;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;
-import org.apache.catalina.startup.TomcatBaseTest;
import org.apache.tomcat.util.buf.HexUtils;
/*
* Tests that can't easily be added to the {@code org.apache.tomcat.security}
package because they need access to
* package private classes.
*/
-public class TestAjpSecurity extends TomcatBaseTest {
+public class TestAjpSecurity extends AjpBaseTest {
@Test
diff --git a/test/org/apache/tomcat/security/TestSecurity2026Http2.java
b/test/org/apache/tomcat/security/TestSecurity2026Http2.java
index e8f87d2375..5ade511033 100644
--- a/test/org/apache/tomcat/security/TestSecurity2026Http2.java
+++ b/test/org/apache/tomcat/security/TestSecurity2026Http2.java
@@ -18,6 +18,7 @@ package org.apache.tomcat.security;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.SocketTimeoutException;
@@ -26,14 +27,14 @@ import java.nio.charset.StandardCharsets;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
-import jakarta.servlet.AsyncContext;
-import jakarta.servlet.AsyncEvent;
-import jakarta.servlet.AsyncListener;
-import jakarta.servlet.ServletInputStream;
-import jakarta.servlet.ServletOutputStream;
-import jakarta.servlet.http.HttpServlet;
-import jakarta.servlet.http.HttpServletRequest;
-import jakarta.servlet.http.HttpServletResponse;
+import javax.servlet.AsyncContext;
+import javax.servlet.AsyncEvent;
+import javax.servlet.AsyncListener;
+import javax.servlet.ServletInputStream;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
import org.junit.Assert;
import org.junit.Test;
@@ -189,7 +190,7 @@ public class TestSecurity2026Http2 extends Http2TestBase {
Thread.sleep(VICTIM_HANDLER_MS); // e.g. authentication
back-end
} catch (InterruptedException ignore) {
}
- byte[] body = req.getInputStream().readAllBytes();
+ byte[] body = readFully(req.getInputStream());
resp.setContentType("text/plain");
resp.getOutputStream().write(("echo:" +
body.length).getBytes(StandardCharsets.ISO_8859_1));
}
@@ -308,4 +309,16 @@ public class TestSecurity2026Http2 extends Http2TestBase {
}
return bytes.toByteArray();
}
+
+
+ private static byte[] readFully(InputStream is) throws IOException {
+ byte[] buf = new byte[1024];
+ try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+ int read;
+ while ((read = is.read(buf)) >= 0) {
+ out.write(buf, 0, read);
+ }
+ return out.toByteArray();
+ }
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]