|
| 1 | +package em.external.webgoat; |
| 2 | + |
| 3 | + |
| 4 | +import org.evomaster.client.java.controller.AuthUtils; |
| 5 | +import org.evomaster.client.java.controller.ExternalSutController; |
| 6 | +import org.evomaster.client.java.controller.InstrumentedSutStarter; |
| 7 | +import org.evomaster.client.java.controller.api.dto.auth.AuthenticationDto; |
| 8 | +import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
| 9 | +import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType; |
| 10 | +import org.evomaster.client.java.sql.DbSpecification; |
| 11 | +import org.evomaster.client.java.controller.problem.ProblemInfo; |
| 12 | +import org.evomaster.client.java.controller.problem.RestProblem; |
| 13 | + |
| 14 | +import java.nio.file.Paths; |
| 15 | +import java.sql.Connection; |
| 16 | +import java.sql.DriverManager; |
| 17 | +import java.sql.SQLException; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | +import org.h2.tools.Server; |
| 21 | + |
| 22 | +public class ExternalEvoMasterController extends ExternalSutController { |
| 23 | + |
| 24 | + private static final int DEFAULT_CONTROLLER_PORT = 40100; |
| 25 | + |
| 26 | + private static final int DEFAULT_SUT_PORT = 12345; |
| 27 | + |
| 28 | + |
| 29 | + public static void main(String[] args) { |
| 30 | + |
| 31 | + int controllerPort = DEFAULT_CONTROLLER_PORT; |
| 32 | + if (args.length > 0) { |
| 33 | + controllerPort = Integer.parseInt(args[0]); |
| 34 | + } |
| 35 | + int sutPort = DEFAULT_SUT_PORT; |
| 36 | + if (args.length > 1) { |
| 37 | + sutPort = Integer.parseInt(args[1]); |
| 38 | + } |
| 39 | + String jarLocation = "cs/rest-gui/webgoat/target"; |
| 40 | + if (args.length > 2) { |
| 41 | + jarLocation = args[2]; |
| 42 | + } |
| 43 | + if (!jarLocation.endsWith(".jar")) { |
| 44 | + jarLocation += "/webgoat-sut.jar"; |
| 45 | + } |
| 46 | + |
| 47 | + int timeoutSeconds = 120; |
| 48 | + if (args.length > 3) { |
| 49 | + timeoutSeconds = Integer.parseInt(args[3]); |
| 50 | + } |
| 51 | + |
| 52 | + String command = "java"; |
| 53 | + if (args.length > 4) { |
| 54 | + command = args[4]; |
| 55 | + } |
| 56 | + |
| 57 | + ExternalEvoMasterController controller = |
| 58 | + new ExternalEvoMasterController(controllerPort, jarLocation, sutPort, timeoutSeconds, command); |
| 59 | + controller.setNeedsJdk17Options(true); |
| 60 | + |
| 61 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 62 | + |
| 63 | + starter.start(); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + private final int timeoutSeconds; |
| 68 | + |
| 69 | + private final int sutPort; |
| 70 | + private final int dbPort; |
| 71 | + private final String tmpDir; |
| 72 | + private Server h2; |
| 73 | + |
| 74 | + |
| 75 | + private String jarLocation; |
| 76 | + private Connection sqlConnection; |
| 77 | + private List<DbSpecification> dbSpecification; |
| 78 | + |
| 79 | + public ExternalEvoMasterController() { |
| 80 | + this(DEFAULT_CONTROLLER_PORT, "../target/webgoat-sut.jar", DEFAULT_SUT_PORT, 120, "java"); |
| 81 | + } |
| 82 | + |
| 83 | + public ExternalEvoMasterController(String jarLocation) { |
| 84 | + this(); |
| 85 | + this.jarLocation = jarLocation; |
| 86 | + } |
| 87 | + |
| 88 | + public ExternalEvoMasterController(int controllerPort, String jarLocation, int sutPort, int timeoutSeconds, String command) { |
| 89 | + this.sutPort = sutPort; |
| 90 | + this.dbPort = sutPort + 1; |
| 91 | + this.jarLocation = jarLocation; |
| 92 | + this.timeoutSeconds = timeoutSeconds; |
| 93 | + String base = Paths.get(jarLocation).toAbsolutePath().getParent().normalize().toString(); |
| 94 | + tmpDir = base + "/temp/tmp_webgoat/temp_" + dbPort; |
| 95 | + |
| 96 | + setControllerPort(controllerPort); |
| 97 | + setJavaCommand(command); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public String[] getInputParameters() { |
| 102 | + return new String[]{ |
| 103 | + "--webgoat.port=" + sutPort, |
| 104 | + "--webwolf.port=" + (sutPort + 2), |
| 105 | + "--spring.datasource.url=" + dbUrl() + ";DB_CLOSE_DELAY=-1;", |
| 106 | + "--spring.datasource.driver-class-name=org.h2.Driver", |
| 107 | + "--spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect", |
| 108 | + "--spring.datasource.username=sa", |
| 109 | + "--spring.datasource.password", |
| 110 | + "--spring.jmx.enabled=false", |
| 111 | + "--spring.jpa.properties.jakarta.persistence.schema-generation.scripts.action=none", |
| 112 | + "--spring.sql.init.mode=never", |
| 113 | + "--webgoat.server.directory=" + tmpDir, |
| 114 | + "--webgoat.user.directory=" + tmpDir |
| 115 | + }; |
| 116 | + } |
| 117 | + |
| 118 | + private String dbUrl( ) { |
| 119 | + |
| 120 | + String url = "jdbc"; |
| 121 | + url += ":h2:tcp://localhost:" + dbPort + "/mem:testdb_" + dbPort + ";INIT=CREATE SCHEMA IF NOT EXISTS CONTAINER;"; |
| 122 | + |
| 123 | + return url; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public String[] getJVMParameters() { |
| 128 | + return new String[]{ |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + @Override |
| 134 | + public String getBaseURL() { |
| 135 | + return "http://localhost:" + sutPort; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public String getPathToExecutableJar() { |
| 140 | + return jarLocation; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public String getLogMessageOfInitializedServer() { |
| 145 | + return "Please browse to "; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public long getMaxAwaitForInitializationInSeconds() { |
| 150 | + return timeoutSeconds; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void preStart() { |
| 155 | + try { |
| 156 | + //starting H2 |
| 157 | + h2 = Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "" + dbPort); |
| 158 | + h2.start(); |
| 159 | + |
| 160 | + } catch (SQLException e) { |
| 161 | + throw new RuntimeException(e); |
| 162 | + } |
| 163 | + |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void postStart() { |
| 168 | + closeDatabaseConnection(); |
| 169 | + |
| 170 | + try { |
| 171 | + Class.forName("org.h2.Driver"); |
| 172 | + sqlConnection = DriverManager.getConnection(dbUrl(), "sa", ""); |
| 173 | + dbSpecification = Arrays.asList(new DbSpecification(DatabaseType.H2,sqlConnection) |
| 174 | + .withInitSqlOnResourcePath("/data.sql") |
| 175 | + .withSchemas("CONTAINER") |
| 176 | + ); |
| 177 | + } catch (Exception e) { |
| 178 | + throw new RuntimeException(e); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public void preStop() { |
| 184 | + closeDatabaseConnection(); |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | + private void closeDatabaseConnection() { |
| 189 | + if (sqlConnection != null) { |
| 190 | + try { |
| 191 | + sqlConnection.close(); |
| 192 | + } catch (SQLException e) { |
| 193 | + e.printStackTrace(); |
| 194 | + } |
| 195 | + sqlConnection = null; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public void postStop() { |
| 201 | + if (h2 != null) { |
| 202 | + h2.stop(); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + @Override |
| 207 | + public String getPackagePrefixesToCover() { |
| 208 | + return "org.owasp.webgoat."; |
| 209 | + } |
| 210 | + |
| 211 | + @Override |
| 212 | + public ProblemInfo getProblemInfo() { |
| 213 | + return new RestProblem( |
| 214 | + "http://localhost:" + sutPort + "/WebGoat/v3/api-docs", |
| 215 | + null |
| 216 | + ); |
| 217 | + } |
| 218 | + |
| 219 | + @Override |
| 220 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 221 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_5; |
| 222 | + } |
| 223 | + |
| 224 | + @Override |
| 225 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 226 | + |
| 227 | + return Arrays.asList( |
| 228 | + AuthUtils.getForDefaultSpringFormLogin("user1", "testuser", "testuser", "/WebGoat/login"), |
| 229 | + AuthUtils.getForDefaultSpringFormLogin("user2", "testuser2", "testuser", "/WebGoat/login")); |
| 230 | + } |
| 231 | + |
| 232 | + |
| 233 | + |
| 234 | + @Override |
| 235 | + public void resetStateOfSUT() { |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public List<DbSpecification> getDbSpecifications() { |
| 240 | + return dbSpecification; |
| 241 | + } |
| 242 | + |
| 243 | +} |
0 commit comments