-
Notifications
You must be signed in to change notification settings - Fork 114
Http oracle timeout #1608
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
omursahin
wants to merge
10
commits into
master
Choose a base branch
from
http-oracle-timeout
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Http oracle timeout #1608
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
89ec54a
timeout
omursahin e7cabb8
setting timeout in tests
omursahin 6b6353f
http timeout write test case
omursahin 6c23581
add timeout param
omursahin e637561
Merge branch 'master' into http-oracle-timeout
omursahin c5a764a
enable http oracles
omursahin f5c01e2
Merge branch 'master' into http-oracle-timeout
omursahin 811208f
http semantics for bb
omursahin 3bb0ca4
fitness binding in bb
omursahin 8d8b308
variable name constant
omursahin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
...-rest-bb/src/main/kotlin/com/foo/rest/examples/bb/httptimeout/BBHttpTimeoutApplication.kt
This file contains hidden or 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,47 @@ | ||
| package com.foo.rest.examples.bb.httptimeout | ||
|
|
||
| import org.evomaster.e2etests.utils.CoveredTargets | ||
| import org.springframework.boot.SpringApplication | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication | ||
| import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration | ||
| import org.springframework.http.ResponseEntity | ||
| import org.springframework.web.bind.annotation.GetMapping | ||
| import org.springframework.web.bind.annotation.PathVariable | ||
| import org.springframework.web.bind.annotation.RequestMapping | ||
| import org.springframework.web.bind.annotation.RestController | ||
|
|
||
|
|
||
| @SpringBootApplication(exclude = [SecurityAutoConfiguration::class]) | ||
| @RequestMapping(path = ["/api/timeout"]) | ||
| @RestController | ||
| open class BBHttpTimeoutApplication { | ||
|
|
||
| companion object { | ||
| @JvmStatic | ||
| fun main(args: Array<String>) { | ||
| SpringApplication.run(BBHttpTimeoutApplication::class.java, *args) | ||
| } | ||
| } | ||
|
|
||
| // slow endpoint: blocks longer than the client timeout, triggering a HTTP_TIMEOUT fault. | ||
| // the target is covered as soon as the request is handled, before the client gives up. | ||
| @GetMapping(path = ["/slow/{id}"]) | ||
| open fun slow(@PathVariable("id") id: Int): ResponseEntity<String> { | ||
| CoveredTargets.cover("timeout") | ||
| val deadline = System.currentTimeMillis() + 10_000 | ||
| while (System.currentTimeMillis() < deadline) { | ||
| try { | ||
| Thread.sleep(deadline - System.currentTimeMillis()) | ||
| } catch (e: InterruptedException) { | ||
| // ignore and keep blocking | ||
| } | ||
| } | ||
| return ResponseEntity.status(200).body("$id") | ||
| } | ||
|
|
||
| // clean | ||
| @GetMapping(path = ["/fast/{id}"]) | ||
| open fun fast(@PathVariable("id") id: Int): ResponseEntity<String> { | ||
| return ResponseEntity.status(200).body("$id") | ||
| } | ||
| } |
5 changes: 5 additions & 0 deletions
5
...g-rest-bb/src/test/kotlin/com/foo/rest/examples/bb/httptimeout/BBHttpTimeoutController.kt
This file contains hidden or 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,5 @@ | ||
| package com.foo.rest.examples.bb.httptimeout | ||
|
|
||
| import com.foo.rest.examples.bb.SpringController | ||
|
|
||
| class BBHttpTimeoutController : SpringController(BBHttpTimeoutApplication::class.java) |
58 changes: 58 additions & 0 deletions
58
.../src/test/kotlin/org/evomaster/e2etests/spring/rest/bb/httptimeout/BBHttpTimeoutEMTest.kt
This file contains hidden or 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,58 @@ | ||
| package org.evomaster.e2etests.spring.rest.bb.httptimeout | ||
|
|
||
| import com.foo.rest.examples.bb.httptimeout.BBHttpTimeoutController | ||
| import org.evomaster.core.output.OutputFormat | ||
| import org.evomaster.core.problem.enterprise.DetectedFaultUtils | ||
| import org.evomaster.core.problem.enterprise.ExperimentalFaultCategory | ||
| import org.evomaster.e2etests.spring.rest.bb.SpringTestBase | ||
| import org.evomaster.e2etests.utils.EnterpriseTestBase | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.BeforeAll | ||
| import org.junit.jupiter.params.ParameterizedTest | ||
| import org.junit.jupiter.params.provider.EnumSource | ||
|
|
||
| class BBHttpTimeoutEMTest : SpringTestBase() { | ||
|
|
||
| companion object { | ||
|
|
||
| init { | ||
| EnterpriseTestBase.shouldApplyInstrumentation = false | ||
| } | ||
|
|
||
| @BeforeAll | ||
| @JvmStatic | ||
| fun init() { | ||
| initClass(BBHttpTimeoutController()) | ||
| } | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @EnumSource | ||
| fun testBlackBoxOutput(outputFormat: OutputFormat) { | ||
|
|
||
| executeAndEvaluateBBTest( | ||
| outputFormat, | ||
| "bbhttptimeout", | ||
| 5, | ||
| 6, | ||
| "timeout" | ||
| ) { args: MutableList<String> -> | ||
|
|
||
| setOption(args, "useExperimentalOracles", "true") | ||
| setOption(args, "tcpTimeoutMs", "2000") | ||
| setOption(args, "httpOracles", "true") | ||
|
|
||
| val solution = initAndRun(args) | ||
|
|
||
| assertTrue(solution.individuals.size >= 1) | ||
|
|
||
| val timeoutFaults = DetectedFaultUtils.getDetectedFaults(solution) | ||
| .filter { it.category == ExperimentalFaultCategory.HTTP_TIMEOUT } | ||
|
|
||
| // fault on the slow path | ||
| assertTrue(timeoutFaults.any { it.operationId.contains("/api/timeout/slow/") }) | ||
| // no false positive on the fast path | ||
| assertTrue(timeoutFaults.none { it.operationId.contains("/api/timeout/fast/") }) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import org.evomaster.core.output.TestWriterUtils | |
| import org.evomaster.core.output.TestWriterUtils.getWireMockVariableName | ||
| import org.evomaster.core.problem.enterprise.EnterpriseActionResult | ||
| import org.evomaster.core.problem.enterprise.EnterpriseIndividual | ||
| import org.evomaster.core.problem.enterprise.ExperimentalFaultCategory | ||
| import org.evomaster.core.problem.externalservice.HostnameResolutionAction | ||
| import org.evomaster.core.problem.externalservice.httpws.HttpExternalServiceAction | ||
| import org.evomaster.core.problem.externalservice.httpws.param.HttpWsResponseParam | ||
|
|
@@ -44,6 +45,13 @@ abstract class TestCaseWriter { | |
|
|
||
| companion object { | ||
| private val log = LoggerFactory.getLogger(TestCaseWriter::class.java) | ||
|
|
||
|
|
||
| /** | ||
| * message for the assertion that flags a missing expected timeout (Java/Kotlin/C#) | ||
| * JS uses await expect(...).rejects.toThrow() and Python uses with self.assertRaises(...) | ||
| */ | ||
| private const val EXPECTED_TIMEOUT_MSG = "Expected a timeout" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use JavaDoc style |
||
| } | ||
|
|
||
|
|
||
|
|
@@ -320,10 +328,19 @@ abstract class TestCaseWriter { | |
| format.isPython() -> lines.add("try:") | ||
| } | ||
|
|
||
| // a HTTP_TIMEOUT fault means the call is expected to time out (client timeout == fuzzing | ||
| // tcpTimeoutMs). if no timeout exception is thrown, the fault did not reproduce -> fail | ||
| val timeoutFault = res is EnterpriseActionResult | ||
| && res.getFaults().any { it.category == ExperimentalFaultCategory.HTTP_TIMEOUT } | ||
|
|
||
| lines.indented { | ||
| addActionLines(call,index, testCaseName, lines, res, testSuitePath, baseUrlOfSut) | ||
|
|
||
| if (shouldFailIfExceptionNotThrown(res)) { | ||
| if (timeoutFault) { | ||
| // only Java/Kotlin/C# reach here; JS uses expect(...).rejects.toThrow() and | ||
| // Python uses with self.assertRaises(...), neither wrapped in this try/catch | ||
| lines.add("fail(\"$EXPECTED_TIMEOUT_MSG\");") | ||
| } else if (shouldFailIfExceptionNotThrown(res)) { | ||
| if (!format.isJavaScript()) { | ||
| /* | ||
| TODO need a way to do it for JS, see | ||
|
|
@@ -372,17 +389,16 @@ abstract class TestCaseWriter { | |
| format.isPython() -> lines.add("except Exception as e:") | ||
| } | ||
|
|
||
| res.getErrorMessage()?.let { | ||
| lines.indented { | ||
| lines.indented { | ||
| res.getErrorMessage()?.let { | ||
| lines.addSingleCommentLine("${it.replace('\n', ' ').replace('\r', ' ')}") | ||
| } | ||
| } | ||
|
|
||
| if (format.isPython()) { | ||
| lines.indented { | ||
| if (format.isPython()) { | ||
| lines.add("pass") | ||
| } | ||
| } else { | ||
| } | ||
|
|
||
| if (!format.isPython()) { | ||
| lines.add("}") | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -1312,6 +1312,36 @@ abstract class AbstractRestFitness : HttpWsFitness<RestIndividual>() { | |
| analyzeHttpSemantics(individual, actionResults, fv) | ||
| } | ||
|
|
||
|
|
||
| // This oracle should only be considered for Black-Box testing. | ||
| // In White-Box testing, instrumentation may affect execution time, | ||
| // especially for CPU-bound APIs. | ||
| if(config.blackBox && config.isEnabledFaultCategory(ExperimentalFaultCategory.HTTP_TIMEOUT)){ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add comment specifying why we are skipping for white-box testing |
||
| handleTimeout(individual, actionResults, fv) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A timeout is treated as a fault: the SUT should rather answer quickly (eg 202 with a | ||
| * Location header for long computations) instead of hanging until the client gives up. | ||
| */ | ||
| private fun handleTimeout( | ||
| individual: RestIndividual, | ||
| actionResults: List<ActionResult>, | ||
| fv: FitnessValue | ||
| ) { | ||
| val actions = individual.seeMainExecutableActions() | ||
|
|
||
| for (index in actions.indices) { | ||
| val a = actions[index] | ||
| val r = actionResults.find { it.sourceLocalId == a.getLocalId() } as RestCallResult? ?: continue | ||
| if (!r.getTimedout()) continue | ||
|
|
||
| val category = ExperimentalFaultCategory.HTTP_TIMEOUT | ||
| val scenarioId = idMapper.handleLocalTarget(idMapper.getFaultDescriptiveId(category, a.getName())) | ||
| fv.updateTarget(scenarioId, 1.0, index) | ||
| r.addFault(DetectedFault(category, a.getName(), null)) | ||
| } | ||
| } | ||
|
|
||
| private fun analyzeHttpSemantics(individual: RestIndividual, actionResults: List<ActionResult>, fv: FitnessValue) { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this test fail because by default we have
httpOraclesbeingfalse?