-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into taint-array
- Loading branch information
Showing
94 changed files
with
3,810 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...ntroller/src/main/java/org/evomaster/client/java/controller/internal/db/MongoHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package org.evomaster.client.java.controller.internal.db; | ||
|
||
import org.evomaster.client.java.controller.mongo.MongoHeuristicsCalculator; | ||
import org.evomaster.client.java.instrumentation.MongoInfo; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Class used to act upon Mongo commands executed by the SUT | ||
*/ | ||
public class MongoHandler { | ||
|
||
/** | ||
* Info about Find operations executed | ||
*/ | ||
private final List<MongoInfo> operations; | ||
|
||
/** | ||
* Whether to use execution's info or not | ||
*/ | ||
private volatile boolean extractMongoExecution; | ||
|
||
/** | ||
* The heuristics based on the Mongo execution | ||
*/ | ||
private final List<MongoOperationDistance> distances; | ||
|
||
/** | ||
* Whether to calculate heuristics based on execution or not | ||
*/ | ||
private volatile boolean calculateHeuristics; | ||
|
||
public MongoHandler() { | ||
distances = new ArrayList<>(); | ||
operations = new ArrayList<>(); | ||
extractMongoExecution = true; | ||
calculateHeuristics = true; | ||
} | ||
|
||
public void reset() { | ||
operations.clear(); | ||
distances.clear(); | ||
} | ||
|
||
public void handle(MongoInfo info) { | ||
if (!extractMongoExecution) { | ||
return; | ||
} | ||
|
||
operations.add(info); | ||
} | ||
|
||
public List<MongoOperationDistance> getDistances() { | ||
|
||
operations.stream().filter(info -> info.getQuery() != null).forEach(mongoInfo -> { | ||
double dist; | ||
try { | ||
dist = computeDistance(mongoInfo); | ||
} catch (Exception e) { | ||
dist = Double.MAX_VALUE; | ||
} | ||
distances.add(new MongoOperationDistance(mongoInfo.getQuery(), dist)); | ||
}); | ||
|
||
operations.clear(); | ||
|
||
return distances; | ||
} | ||
|
||
private double computeDistance(MongoInfo info) { | ||
Object collection = info.getCollection(); | ||
Iterable<?> documents = getDocuments(collection); | ||
|
||
MongoHeuristicsCalculator calculator = new MongoHeuristicsCalculator(); | ||
|
||
double min = Double.MAX_VALUE; | ||
|
||
for (Object doc : documents) { | ||
double dist = calculator.computeExpression(info.getQuery(), doc); | ||
if (dist == 0) return 0; | ||
if (dist < min) min = dist; | ||
} | ||
return min; | ||
} | ||
|
||
private static Iterable<?> getDocuments(Object collection) { | ||
try { | ||
Class<?> collectionClass = collection.getClass().getClassLoader().loadClass("com.mongodb.client.MongoCollection"); | ||
return (Iterable<?>) collectionClass.getMethod("find").invoke(collection); | ||
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | | ||
ClassNotFoundException e) { | ||
throw new RuntimeException("Failed to retrieve all documents from a mongo collection", e); | ||
} | ||
} | ||
|
||
public boolean isCalculateHeuristics() { | ||
return calculateHeuristics; | ||
} | ||
|
||
public boolean isExtractMongoExecution() { | ||
return extractMongoExecution; | ||
} | ||
|
||
public void setCalculateHeuristics(boolean calculateHeuristics) { | ||
this.calculateHeuristics = calculateHeuristics; | ||
} | ||
|
||
public void setExtractMongoExecution(boolean extractMongoExecution) { | ||
this.extractMongoExecution = extractMongoExecution; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...rc/main/java/org/evomaster/client/java/controller/internal/db/MongoOperationDistance.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package org.evomaster.client.java.controller.internal.db; | ||
|
||
public class MongoOperationDistance { | ||
|
||
public final Object bson; | ||
|
||
public final double distance; | ||
|
||
|
||
public MongoOperationDistance(Object bson, double distance) { | ||
this.bson= bson; | ||
this.distance = distance; | ||
} | ||
} |
Oops, something went wrong.