Skip to content

Commit

Permalink
isolated DNS info from ExternalServiceInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
seran committed Oct 11, 2023
1 parent d6afa91 commit 4dc7bfa
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ public class AdditionalInfoDto {
* a list of external service info which is direct to default WM
*/
public List<ExternalServiceInfoDto> employedDefaultWM = new ArrayList<>();

public List<HostnameInfoDto> hostnameInfoDtos = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,12 @@ public Response getTestResults(
info.lastExecutedStatement = a.getLastExecutedStatement();
info.rawAccessOfHttpBodyPayload = a.isRawAccessOfHttpBodyPayload();
info.parsedDtoNames = new HashSet<>(a.getParsedDtoNamesView());
info.hostnameInfoDtos = a.getHostnameInfos().stream()
.map(hn -> new HostnameInfoDto(
hn.getHostname(),
hn.getResolved()
))
.collect(Collectors.toList());
info.externalServices = a.getExternalServices().stream()
.map(es -> new ExternalServiceInfoDto(
es.getProtocol(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,8 @@ protected BootTimeInfoDto getBootTimeInfoDto(BootTimeObjectiveInfo info){
value = e.getValue();
}}).collect(Collectors.toList());

infoDto.hostnameInfoDtos = info.getHostnameInfos().stream()
.map(h -> new HostnameInfoDto(h.getHostname(), h.getResolved())).collect(Collectors.toList());
infoDto.externalServicesDto = info.getExternalServiceInfo().stream()
.map(e -> new ExternalServiceInfoDto(e.getProtocol(), e.getHostname(), e.getRemotePort()))
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ public void addHostnameInfo(HostnameInfo hostnameInfo) {
hostnameInfos.add(hostnameInfo);
}

public Set<HostnameInfo> getHostnameInfos() {
return Collections.unmodifiableSet(hostnameInfos);
}

public Set<ExternalServiceInfo> getExternalServices() {
return Collections.unmodifiableSet(externalServices);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,18 @@ public class BootTimeObjectiveInfo implements Serializable {
*/
private final List<ExternalServiceInfo> externalServiceInfo = new CopyOnWriteArrayList<>();

private final List<HostnameInfo> hostnameInfos = new CopyOnWriteArrayList<>();

public void reset(){
maxObjectiveCoverage.clear();
externalServiceInfo.clear();
}

public void registerHostnameInfoAtSutBootTime(HostnameInfo hostnameInfo) {
if (hostnameInfos.isEmpty() || hostnameInfos.stream().noneMatch(h -> h.equals(hostnameInfo))) {
hostnameInfos.add(hostnameInfo.copy());
}
}

public void registerExternalServiceInfoAtSutBootTime(ExternalServiceInfo info){
if (externalServiceInfo.isEmpty() || externalServiceInfo.stream().noneMatch(s-> s.equals(info)))
Expand All @@ -56,6 +63,10 @@ public List<ExternalServiceInfo> getExternalServiceInfo(){
return Collections.unmodifiableList(externalServiceInfo);
}

public List<HostnameInfo> getHostnameInfos() {
return Collections.unmodifiableList(hostnameInfos);
}

public void updateMaxObjectiveCoverage(String descriptiveId, double value){
Double h = maxObjectiveCoverage.get(descriptiveId);
if (h == null || value > h)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.evomaster.client.java.instrumentation;

import java.io.Serializable;
import java.util.Objects;

public class HostnameInfo implements Serializable {
private final String remoteHostname;
Expand All @@ -22,4 +23,22 @@ public String getHostname() {
public Boolean getResolved() {
return resolved;
}

public HostnameInfo copy(){
return new HostnameInfo(remoteHostname, resolved);
}

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

@Override
public int hashCode() {
// TODO: Excluded resolved from equals and hashCode, have to verify
return Objects.hash(remoteHostname);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public static InetAddress getByName(String host) throws UnknownHostException {
ExecutionTracer.addHostnameInfo(new HostnameInfo(host, true));
return inetAddress;
} catch (UnknownHostException e) {
ExternalServiceInfo remoteHostInfo = new ExternalServiceInfo(
ExternalServiceSharedUtils.DEFAULT_SOCKET_CONNECT_PROTOCOL,
host,
-1
);
ExecutionTracer.addExternalServiceHost(remoteHostInfo);
// ExternalServiceInfo remoteHostInfo = new ExternalServiceInfo(
// ExternalServiceSharedUtils.DEFAULT_SOCKET_CONNECT_PROTOCOL,
// host,
// -1
// );
// ExecutionTracer.addExternalServiceHost(remoteHostInfo);
ExecutionTracer.addHostnameInfo(new HostnameInfo(host, false));
throw e;
}
Expand All @@ -77,13 +77,13 @@ public static InetAddress[] getAllByName(String host) throws UnknownHostExceptio
ExecutionTracer.addHostnameInfo(new HostnameInfo(host, true));
return inetAddresses;
} catch (UnknownHostException e) {
ExternalServiceInfo remoteHostInfo = new ExternalServiceInfo(
ExternalServiceSharedUtils.DEFAULT_SOCKET_CONNECT_PROTOCOL,
host,
-1
);
// ExternalServiceInfo remoteHostInfo = new ExternalServiceInfo(
// ExternalServiceSharedUtils.DEFAULT_SOCKET_CONNECT_PROTOCOL,
// host,
// -1
// );
// ExecutionTracer.addExternalServiceHost(remoteHostInfo);
ExecutionTracer.addHostnameInfo(new HostnameInfo(host, false));
ExecutionTracer.addExternalServiceHost(remoteHostInfo);
throw e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import org.evomaster.core.search.EnvironmentAction
import org.evomaster.core.search.StructuralElement
import org.evomaster.core.search.gene.Gene

class DomainNameAction : EnvironmentAction(listOf()) {
class HostnameAction : EnvironmentAction(listOf()) {
override fun getName(): String {
return ""
}
Expand All @@ -14,7 +14,7 @@ class DomainNameAction : EnvironmentAction(listOf()) {
}

override fun copyContent(): StructuralElement {
return DomainNameAction()
return HostnameAction()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ package org.evomaster.core.problem.externalservice
open class HostnameInfo (
val remoteHostName: String,
val resolved: Boolean
) {
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.evomaster.core.problem.externalservice

import org.evomaster.core.problem.externalservice.HostnameInfo

open class LocalDomainNameMapping(
val remoteHostnameInfo: HostnameInfo,
val localIP: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.evomaster.core.EMConfig
import org.evomaster.core.Lazy
import org.evomaster.core.problem.externalservice.ExternalService
import org.evomaster.core.problem.externalservice.HostnameInfo
import org.evomaster.core.problem.externalservice.LocalDomainNameMapping
import org.evomaster.core.problem.externalservice.httpws.*
import org.evomaster.core.problem.externalservice.httpws.HttpWsExternalServiceUtils.generateRandomIPAddress
import org.evomaster.core.problem.externalservice.httpws.HttpWsExternalServiceUtils.isAddressAvailable
Expand Down Expand Up @@ -57,16 +58,18 @@ class HttpWsExternalServiceHandler {
* Map from hostname (used in SUT for external services) and local ip addresses, that we resolve
* those hostname (ie like DNS)
*/
private val localAddressMapping: MutableMap<String, String> = mutableMapOf()
// private val localAddressMapping: MutableMap<String, String> = mutableMapOf()

/**
* Skipped external services information provided through the driver to skip from
* handling.
*/
private val skippedExternalServices: MutableList<ExternalService> = mutableListOf()


private val hostnames: MutableList<HostnameInfo> = mutableListOf()
/**
* Map of remote hostname vs local DNS replacement
*/
private val hostnameMapping: MutableMap<String, LocalDomainNameMapping> = mutableMapOf()

/**
* Contains last used loopback address for reference when creating
Expand Down Expand Up @@ -95,6 +98,7 @@ class HttpWsExternalServiceHandler {
private fun initDefaultWM() {
if (config.externalServiceIPSelectionStrategy != EMConfig.ExternalServiceIPSelectionStrategy.NONE) {
if (!isDefaultInitialized) {
addHostname(HostnameInfo("no_host_name", true))
registerHttpExternalServiceInfo(DefaultHttpExternalServiceInfo.createDefaultHttps())
registerHttpExternalServiceInfo(DefaultHttpExternalServiceInfo.createDefaultHttp())
isDefaultInitialized = true
Expand All @@ -116,7 +120,11 @@ class HttpWsExternalServiceHandler {

fun addHostname(hostnameInfo: HostnameInfo) {
if (config.externalServiceIPSelectionStrategy != EMConfig.ExternalServiceIPSelectionStrategy.NONE) {
hostnames.add(hostnameInfo);
if (!hostnameMapping.containsKey(hostnameInfo.remoteHostName)) {
val ip = getNewIP()
lastIPAddress = ip
hostnameMapping[hostnameInfo.remoteHostName] = LocalDomainNameMapping(hostnameInfo, ip)
}
}
}

Expand All @@ -125,13 +133,19 @@ class HttpWsExternalServiceHandler {
return
}

val ip: String = localAddressMapping[externalServiceInfo.remoteHostname]
?: run {
val x = getNewIP()
lastIPAddress = x
localAddressMapping[externalServiceInfo.remoteHostname] = x
x
}
// val ip: String = localAddressMapping[externalServiceInfo.remoteHostname]
// ?: run {
// val x = getNewIP()
// lastIPAddress = x
// localAddressMapping[externalServiceInfo.remoteHostname] = x
// x
// }

if (!hostnameMapping.containsKey(externalServiceInfo.remoteHostname)) {
return
}

val ip: String = hostnameMapping[externalServiceInfo.remoteHostname]!!.localIP

val registered = externalServices.filterValues {
it.getRemoteHostName() == externalServiceInfo.remoteHostname &&
Expand Down Expand Up @@ -175,8 +189,12 @@ class HttpWsExternalServiceHandler {
return externalServices.mapValues { it.value.getIP() }
}

fun getLocalAddressMapping(): Map<String, String> {
return localAddressMapping
// fun getLocalAddressMapping(): Map<String, String> {
// return localAddressMapping
// }

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.evomaster.client.java.controller.api.dto.TestResultsDto
import org.evomaster.client.java.instrumentation.shared.ExternalServiceSharedUtils.getWMDefaultSignature
import org.evomaster.core.Lazy
import org.evomaster.core.logging.LoggingUtil
import org.evomaster.core.problem.externalservice.HostnameInfo
import org.evomaster.core.problem.externalservice.httpws.service.HarvestActualHttpWsResponseHandler
import org.evomaster.core.problem.externalservice.httpws.service.HttpWsExternalServiceHandler
import org.evomaster.core.problem.externalservice.httpws.HttpExternalServiceInfo
Expand Down Expand Up @@ -802,14 +803,21 @@ abstract class AbstractRestFitness<T> : HttpWsFitness<T>() where T : Individual
*/

infoDto.forEachIndexed { index, info ->
info.hostnameInfoDtos.forEach { hn ->
externalServiceHandler.addHostname(HostnameInfo(
hn.remoteHostname,
hn.resolved
))
}

info.externalServices.forEach { es ->

/*
The info here is coming from SUT instrumentation
*/

/*
TODO: check, do we really want to start WireMock isntances right now after a fitness evaluation?
TODO: check, do we really want to start WireMock instances right now after a fitness evaluation?
We need to make sure then, if we do this, that a call in instrumented SUT with (now) and
without (previous fitness evaluation) WM instances would result in same behavior.
Expand Down Expand Up @@ -841,9 +849,9 @@ abstract class AbstractRestFitness<T> : HttpWsFitness<T>() where T : Individual
// TODO: Need to move under ApiWsFitness after the GraphQL and RPC support is completed
if (index == 0) {
actionDto.externalServiceMapping = externalServiceHandler.getExternalServiceMappings()
actionDto.localAddressMapping = externalServiceHandler.getLocalAddressMapping()
actionDto.localAddressMapping = externalServiceHandler.getLocalDomainNameMapping()
actionDto.skippedExternalServices = externalServiceHandler.getSkippedExternalServices()
}
return actionDto
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import org.evomaster.core.EMConfig
import org.evomaster.core.output.service.PartialOracles
import org.evomaster.core.problem.enterprise.SampleType
import org.evomaster.core.problem.externalservice.ExternalService
import org.evomaster.core.problem.externalservice.HostnameInfo
import org.evomaster.core.problem.externalservice.httpws.HttpExternalServiceInfo
import org.evomaster.core.problem.externalservice.httpws.service.HttpWsExternalServiceHandler
import org.evomaster.core.problem.httpws.service.HttpWsSampler
Expand Down Expand Up @@ -104,6 +105,8 @@ abstract class AbstractRestSampler : HttpWsSampler<RestIndividual>() {
setupAuthentication(infoDto)
initSqlInfo(infoDto)

initHostnameInfo(infoDto)

initExternalServiceInfo(infoDto)

// TODO: temp
Expand Down Expand Up @@ -318,6 +321,20 @@ abstract class AbstractRestSampler : HttpWsSampler<RestIndividual>() {
}
}

/**
* To collect external service info through SutInfoDto
*/
private fun initHostnameInfo(info: SutInfoDto) {
if (info.bootTimeInfoDto?.hostnameInfoDtos != null) {
info.bootTimeInfoDto.hostnameInfoDtos.forEach {
externalServiceHandler.addHostname(HostnameInfo(
it.remoteHostname,
it.resolved
))
}
}
}

/**
* To collect external service info through SutInfoDto
*/
Expand Down

0 comments on commit 4dc7bfa

Please sign in to comment.