|
| 1 | +package com.reajason.javaweb.integration.probe.tomcat; |
| 2 | + |
| 3 | +import com.reajason.javaweb.Server; |
| 4 | +import com.reajason.javaweb.probe.ProbeContent; |
| 5 | +import com.reajason.javaweb.probe.ProbeMethod; |
| 6 | +import com.reajason.javaweb.probe.ProbeShellGenerator; |
| 7 | +import com.reajason.javaweb.probe.ProbeShellResult; |
| 8 | +import com.reajason.javaweb.probe.config.ProbeConfig; |
| 9 | +import com.reajason.javaweb.probe.config.SleepConfig; |
| 10 | +import lombok.SneakyThrows; |
| 11 | +import lombok.extern.slf4j.Slf4j; |
| 12 | +import net.bytebuddy.jar.asm.Opcodes; |
| 13 | +import okhttp3.*; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.testcontainers.containers.GenericContainer; |
| 16 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 17 | +import org.testcontainers.junit.jupiter.Container; |
| 18 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 19 | + |
| 20 | +import static com.reajason.javaweb.integration.ContainerTool.getUrl; |
| 21 | +import static com.reajason.javaweb.integration.ContainerTool.warFile; |
| 22 | +import static org.junit.jupiter.api.Assertions.*; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author ReaJason |
| 26 | + * @since 2025/8/12 |
| 27 | + */ |
| 28 | +@Slf4j |
| 29 | +@Testcontainers |
| 30 | +public class Tomcat8SleepServerContainerTest { |
| 31 | + public static final String imageName = "tomcat:8-jre8"; |
| 32 | + |
| 33 | + @Container |
| 34 | + public final static GenericContainer<?> container = new GenericContainer<>(imageName) |
| 35 | + .withCopyToContainer(warFile, "/usr/local/tomcat/webapps/app.war") |
| 36 | + .waitingFor(Wait.forHttp("/app")) |
| 37 | + .withExposedPorts(8080); |
| 38 | + |
| 39 | + @Test |
| 40 | + void testSleepServerDetection() { |
| 41 | + String url = getUrl(container); |
| 42 | + assertFalse(sleepDetectionIsOk(url, Server.Jetty, Opcodes.V1_8) > 5_000_000_000L); |
| 43 | + assertTrue(sleepDetectionIsOk(url, Server.Tomcat, Opcodes.V1_8) > 5_000_000_000L); |
| 44 | + } |
| 45 | + |
| 46 | + @SneakyThrows |
| 47 | + public static long sleepDetectionIsOk(String url, String server, int targetJreVersion) { |
| 48 | + ProbeConfig probeConfig = ProbeConfig.builder() |
| 49 | + .probeMethod(ProbeMethod.Sleep) |
| 50 | + .probeContent(ProbeContent.Server) |
| 51 | + .debug(true) |
| 52 | + .shrink(true) |
| 53 | + .targetJreVersion(targetJreVersion) |
| 54 | + .build(); |
| 55 | + SleepConfig sleepConfig = SleepConfig.builder() |
| 56 | + .server(server) |
| 57 | + .seconds(5) |
| 58 | + .build(); |
| 59 | + ProbeShellResult probeResult = ProbeShellGenerator.generate(probeConfig, sleepConfig); |
| 60 | + String content = probeResult.getShellBytesBase64Str(); |
| 61 | + RequestBody requestBody = new FormBody.Builder() |
| 62 | + .add("data", content) |
| 63 | + .build(); |
| 64 | + Request request = new Request.Builder() |
| 65 | + .header("Content-Type", "application/x-www-form-urlencoded") |
| 66 | + .url(url + "/b64").post(requestBody) |
| 67 | + .build(); |
| 68 | + long startTime = System.nanoTime(); |
| 69 | + try (Response response = new OkHttpClient().newCall(request).execute()) { |
| 70 | + assertEquals(200, response.code()); |
| 71 | + long endTime = System.nanoTime(); |
| 72 | + return endTime - startTime; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments