Skip to content

Commit

Permalink
added data structure for ExternalServiceMapping
Browse files Browse the repository at this point in the history
  • Loading branch information
seran committed Oct 24, 2023
1 parent 6c2aa17 commit e4ecad1
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ActionDto {
* Mapping of external service signature to internal WireMock
* instance addresses.
*/
public Map<String, String> externalServiceMapping = new HashMap<>();
public List<ExternalServiceMappingDto> externalServiceMapping = new ArrayList<>();

/**
* Mapping of external service domain name and local address used
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.evomaster.client.java.controller.api.dto;

import java.util.Objects;

public class ExternalServiceMappingDto {

public String remoteHostname;

public String localIPAddress;

public String signature;

public Boolean isActive;

public ExternalServiceMappingDto(){};

public ExternalServiceMappingDto(String remoteHostname, String localIPAddress, String signature, Boolean isActive) {
this.remoteHostname = remoteHostname;
this.localIPAddress = localIPAddress;
this.signature = signature;
this.isActive = isActive;
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ExternalServiceMappingDto that = (ExternalServiceMappingDto) o;
return Objects.equals(remoteHostname, that.remoteHostname) && Objects.equals(localIPAddress, that.localIPAddress) && Objects.equals(signature, that.signature) && Objects.equals(isActive, that.isActive);
}

@Override
public int hashCode() {
return Objects.hash(remoteHostname, localIPAddress, signature, isActive);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public final void newActionSpecificHandler(ActionDto dto){
dto.index,
dto.name,
dto.inputVariables,
dto.externalServiceMapping,
dto.externalServiceMapping.stream().map(e -> new ExternalServiceMapping(e.remoteHostname, e.localIPAddress, e.signature, e.isActive)).collect(Collectors.toList()),
dto.localAddressMapping,
dto.skippedExternalServices.stream().map(e -> new ExternalService(e.hostname, e.port)).collect(Collectors.toList())
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ public final void newActionSpecificHandler(ActionDto dto) {
dto.index,
dto.name,
dto.inputVariables,
dto.externalServiceMapping,
dto.externalServiceMapping.stream().map(e -> new ExternalServiceMapping(e.remoteHostname, e.localIPAddress, e.signature, e.isActive)).collect(Collectors.toList()),
dto.localAddressMapping,
dto.skippedExternalServices.stream().map(e -> new ExternalService(e.hostname, e.port)).collect(Collectors.toList())
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Action implements Serializable {
/**
* A map of hostname and WireMock IP to mock external service calls.
*/
private final Map<String, String> externalServiceMapping;
private final List<ExternalServiceMapping> externalServiceMapping;

/**
* A map of external service domain name against to the local IP
Expand All @@ -35,11 +35,11 @@ public class Action implements Serializable {
*/
private final List<ExternalService> skippedExternalServices;

public Action(int index, String name, Collection<String> inputVariables, Map<String, String> externalServiceMapping, Map<String, String> localAddressMapping, List<ExternalService> skippedExternalServices) {
public Action(int index, String name, Collection<String> inputVariables, List<ExternalServiceMapping> externalServiceMapping, Map<String, String> localAddressMapping, List<ExternalService> skippedExternalServices) {
this.index = index;
this.name = name;
this.inputVariables = Collections.unmodifiableSet(new HashSet<>(inputVariables));
this.externalServiceMapping = Collections.unmodifiableMap(new HashMap<>(externalServiceMapping));
this.externalServiceMapping = Collections.unmodifiableList(new ArrayList<>(externalServiceMapping));
this.localAddressMapping = Collections.unmodifiableMap(new HashMap<>(localAddressMapping));
this.skippedExternalServices = Collections.unmodifiableList(new ArrayList<>(skippedExternalServices));
}
Expand All @@ -52,7 +52,7 @@ public Set<String> getInputVariables() {
return inputVariables;
}

public Map<String, String> getExternalServiceMapping() {
public List<ExternalServiceMapping> getExternalServiceMapping() {
return externalServiceMapping;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.evomaster.client.java.instrumentation;

public class ExternalServiceMapping {

private String remoteHostName;

private String localIPAddress;

private String signature;

private Boolean isActive;


public ExternalServiceMapping(String remoteHostName, String localIPAddress, String signature, Boolean isActive) {
this.remoteHostName = remoteHostName;
this.localIPAddress = localIPAddress;
this.signature = signature;
this.isActive = isActive;
}

public String getLocalIPAddress() {
return localIPAddress;
}

public String getRemoteHostname() {
return remoteHostName;
}

public String getSignature() {
return signature;
}

public Boolean isActive() {
return isActive;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.evomaster.client.java.instrumentation.coverage.methodreplacement;

import org.evomaster.client.java.instrumentation.ExternalServiceInfo;
import org.evomaster.client.java.instrumentation.ExternalServiceMapping;
import org.evomaster.client.java.instrumentation.HostnameInfo;
import org.evomaster.client.java.instrumentation.shared.ExternalServiceSharedUtils;
import org.evomaster.client.java.instrumentation.staticstate.ExecutionTracer;
Expand All @@ -23,24 +24,26 @@ public static String[] collectExternalServiceInfo(ExternalServiceInfo remoteHost
// data structure of the external service mapping inside ExecutionTracer

// TODO: Experiment
// ExecutionTracer.addHostnameInfo(new HostnameInfo(remoteHostInfo.getHostname(), true));
ExecutionTracer.addHostnameInfo(new HostnameInfo(remoteHostInfo.getHostname(), true));

ExecutionTracer.addExternalServiceHost(remoteHostInfo);
String signature = remoteHostInfo.signature();

if (!ExecutionTracer.hasExternalMapping(remoteHostInfo.signature())) {
String signature = remoteHostInfo.signature();
if (!ExecutionTracer.hasMockServerForHost(remoteHostInfo.getHostname())) {
int connectPort = remotePort;

// if (!ExecutionTracer.hasExternalMapping(remoteHostInfo.signature())) {
// ExecutionTracer.addEmployedDefaultWMHost(remoteHostInfo);
// signature = ExternalServiceSharedUtils.getWMDefaultSignature(remoteHostInfo.getProtocol(), remotePort);
// connectPort = ExternalServiceSharedUtils.getDefaultWMPort(signature);
// }
if (!ExecutionTracer.hasActiveExternalMapping(signature)) {
ExecutionTracer.addEmployedDefaultWMHost(remoteHostInfo);
signature = ExternalServiceSharedUtils.getWMDefaultSignature(remoteHostInfo.getProtocol(), remotePort);
connectPort = ExternalServiceSharedUtils.getDefaultWMPort(signature);
}

// return new String[]{remoteHostInfo.getHostname(), "" + remotePort};
return new String[]{ExecutionTracer.getExternalMapping(signature), "" + connectPort};
ExternalServiceMapping externalServiceMapping = ExecutionTracer.getExternalMappingBySignature(signature);

return new String[]{externalServiceMapping.getLocalIPAddress(), "" + connectPort};
} else {
return new String[]{ExecutionTracer.getExternalMapping(remoteHostInfo.signature()), "" + remotePort};

return new String[]{ExecutionTracer.getExternalMappingByHostname(remoteHostInfo.getHostname()).getLocalIPAddress(), "" + remotePort};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public Class<?> getTargetClass() {
@Replacement(
type = ReplacementType.TRACKER,
category = ReplacementCategory.NET,
id = "InetAddress_getAllByName_Replacement",
id = "InetAddress_getByName_Replacement",
replacingStatic = true,
usageFilter = UsageFilter.ANY
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class ExecutionTracer {
/**
* A map of external service hostname and WireMock IP mapping information
*/
private static Map<String, String> externalServiceMapping = new HashMap<>();
private static List<ExternalServiceMapping> externalServiceMapping = new CopyOnWriteArrayList<>();


private static Map<String, String> localAddressMapping = new HashMap<>();
Expand Down Expand Up @@ -697,12 +697,35 @@ public static void addEmployedDefaultWMHost(ExternalServiceInfo hostInfo) {
* Return the WireMock IP if there is a mapping for the hostname. If there is
* no mapping NULL will be returned
*/
public static String getExternalMapping(String signature) {
return externalServiceMapping.get(signature);
public static ExternalServiceMapping getExternalMappingBySignature(String signature) {
List<ExternalServiceMapping> m = externalServiceMapping
.stream()
.filter(e -> e.getSignature().equals(signature))
.collect(Collectors.toList());

return m.get(0);
}

public static ExternalServiceMapping getExternalMappingByHostname(String hostname) {
return externalServiceMapping
.stream()
.filter(e -> e.getRemoteHostname().equals(hostname))
.collect(Collectors.toList())
.get(0);
}

public static boolean hasExternalMapping(String signature) {
return externalServiceMapping.containsKey(signature);
public static boolean hasActiveExternalMapping(String signature) {
return externalServiceMapping
.stream()
.filter(e -> e.getSignature().equals(signature) && e.isActive())
.count() > 0;
}

public static boolean hasMockServerForHost(String hostname) {
return externalServiceMapping
.stream()
.filter(e -> e.getRemoteHostname() == hostname)
.count() > 0;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@ import org.evomaster.core.search.EnvironmentAction
import org.evomaster.core.search.StructuralElement
import org.evomaster.core.search.gene.Gene

class DnsAction(
private val hostname: String,
val resolved: Boolean,
) : EnvironmentAction(listOf()) {

//TODO
class DnsAction(children: List<StructuralElement>) : EnvironmentAction(children) {
fun getHostname(): String {
return hostname
}
override fun getName(): String {
TODO("Not yet implemented")
return "Hostname_${hostname}_${resolved}"
}

override fun seeTopGenes(): List<out Gene> {
TODO("Not yet implemented")
return listOf()
}

override fun copyContent(): StructuralElement {
TODO("Not yet implemented")
return DnsAction(hostname, resolved)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.evomaster.core.problem.externalservice.httpws.service

import com.google.inject.Inject
import org.evomaster.client.java.controller.api.dto.ExternalServiceMappingDto
import org.evomaster.client.java.controller.api.dto.problem.ExternalServiceDto
import org.evomaster.client.java.instrumentation.shared.ExternalServiceSharedUtils.isDefaultSignature
import org.evomaster.core.EMConfig
Expand Down Expand Up @@ -185,13 +186,19 @@ class HttpWsExternalServiceHandler {
}
}

fun getExternalServiceMappings(): Map<String, String> {
return externalServices.mapValues { it.value.getIP() }
}
fun getExternalServiceMappings(): List<ExternalServiceMappingDto> {
val output: MutableList<ExternalServiceMappingDto> = mutableListOf()

// fun getLocalAddressMapping(): Map<String, String> {
// return localAddressMapping
// }
externalServices.forEach {
val dto = ExternalServiceMappingDto()
dto.remoteHostname = it.value.getRemoteHostName()
dto.localIPAddress = it.value.getIP()
dto.signature = it.value.getSignature()
dto.isActive = it.value.isActive()
output.add(dto)
}
return output
}

fun getLocalDomainNameMapping(): Map<String, String> {
return hostnameMapping.mapValues { it.value.localIP }
Expand Down
Loading

0 comments on commit e4ecad1

Please sign in to comment.