Skip to content

Commit

Permalink
Merge pull request #1167 from WebFuzzing/example-targets
Browse files Browse the repository at this point in the history
explicit testing targets for examples
  • Loading branch information
arcuri82 authored Feb 1, 2025
2 parents 6edbf61 + cf64239 commit 042ac63
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.evomaster.core.search.action.ActionFilter
import org.evomaster.core.search.gene.*
import org.evomaster.core.search.gene.collection.EnumGene
import org.evomaster.core.search.gene.numeric.NumberGene
import org.evomaster.core.search.gene.optional.ChoiceGene
import org.evomaster.core.search.gene.optional.OptionalGene
import org.evomaster.core.search.gene.string.StringGene
import org.evomaster.core.search.gene.utils.GeneUtils
Expand Down Expand Up @@ -460,6 +461,30 @@ abstract class AbstractRestFitness : HttpWsFitness<RestIndividual>() {

//body payload type in response
fv.coverTarget(idMapper.handleLocalTarget("RESPONSE_BODY_PAYLOAD_${call.id}_${result.getBodyType()}"))

/*
explicit targets for examples
*/
val examples = call.seeTopGenes()
.flatMap { it.flatView() }
.filter { it.staticCheckIfImpactPhenotype() }
.filter { it.name == RestActionBuilderV3.EXAMPLES_NAME }

examples.forEach {
val name = (it.parent as Gene).name
val label = when(it){
is EnumGene<*> -> it.getValueAsRawString()
is ChoiceGene<*> -> ""+it.activeGeneIndex
else ->{
log.warn("Unhandled example gene type: ${it.javaClass}")
assert(false)
"undefined"
}
}

val target = "EXAMPLE_${call.id}_${name}_$label"
fv.coverTarget(idMapper.handleLocalTarget(target))
}
}

private fun handleAdditionalStatusTargetDescription(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,43 @@
"schema": {
"$ref": "#/components/schemas/ExampleObjectDto"
},
"example": {
"id": "Foo",
"b": true,
"x": 42,
"y": 12.3,
"other": {
"name": "Bar",
"x": 88
"examples": {
"X": {
"value": {
"id": "Foo",
"b": true,
"x": 42,
"y": 12.3,
"other": {
"name": "Bar",
"x": 88
}
}
},
"Y0": {
"value": {
"id": "Y0"
}
},
"Y1": {
"value": {
"id": "Y1"
}
},
"Y2": {
"value": {
"id": "Y2"
}
},
"Y3": {
"value": {
"id": "Y3"
}
},
"Y4": {
"value": {
"id": "Y4"
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.foo.rest.examples.bb.exampleobject.BBExampleObjectController
import com.foo.rest.examples.bb.examplevalues.BBExamplesController
import org.evomaster.core.output.OutputFormat
import org.evomaster.core.problem.rest.HttpVerb
import org.evomaster.core.problem.rest.param.BodyParam
import org.evomaster.e2etests.spring.rest.bb.SpringTestBase
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Assertions.assertTrue
Expand Down Expand Up @@ -40,6 +41,22 @@ class BBExampleObjectEMTest : SpringTestBase() {

assertTrue(solution.individuals.size >= 1)
assertHasAtLeastOne(solution, HttpVerb.POST, 200, "/api/bbexampleobject", "OK")

val bodies = solution.individuals
.asSequence()
.map { it.individual }
.flatMap { it.seeMainExecutableActions() }
.flatMap { it.parameters }
.filterIsInstance<BodyParam>()
.map { it.primaryGene().getValueAsRawString() }
.toList()

assertTrue(bodies.any { it.contains("Foo") }, "Missing Foo")
assertTrue(bodies.any { it.contains("Y0") }, "Missing Y0")
assertTrue(bodies.any { it.contains("Y1") }, "Missing Y1")
assertTrue(bodies.any { it.contains("Y2") }, "Missing Y2")
assertTrue(bodies.any { it.contains("Y3") }, "Missing Y3")
assertTrue(bodies.any { it.contains("Y4") }, "Missing Y4")
}
}
}

0 comments on commit 042ac63

Please sign in to comment.