-
Notifications
You must be signed in to change notification settings - Fork 46
Generate test by fuzzing for methods with no parameters #511 #515
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,7 @@ import org.utbot.framework.plugin.api.EnvironmentModels | |
import org.utbot.framework.plugin.api.Instruction | ||
import org.utbot.framework.plugin.api.MissingState | ||
import org.utbot.framework.plugin.api.Step | ||
import org.utbot.framework.plugin.api.UtAssembleModel | ||
import org.utbot.framework.plugin.api.UtConcreteExecutionFailure | ||
import org.utbot.framework.plugin.api.UtError | ||
import org.utbot.framework.plugin.api.UtExecution | ||
|
@@ -79,6 +80,7 @@ import org.utbot.framework.plugin.api.util.id | |
import org.utbot.framework.plugin.api.util.utContext | ||
import org.utbot.framework.plugin.api.util.description | ||
import org.utbot.framework.util.jimpleBody | ||
import org.utbot.framework.plugin.api.util.voidClassId | ||
import org.utbot.fuzzer.FallbackModelProvider | ||
import org.utbot.fuzzer.FuzzedMethodDescription | ||
import org.utbot.fuzzer.FuzzedValue | ||
|
@@ -89,10 +91,12 @@ import org.utbot.fuzzer.defaultModelProviders | |
import org.utbot.fuzzer.fuzz | ||
import org.utbot.fuzzer.names.MethodBasedNameSuggester | ||
import org.utbot.fuzzer.names.ModelBasedNameSuggester | ||
import org.utbot.fuzzer.providers.ObjectModelProvider | ||
import org.utbot.instrumentation.ConcreteExecutor | ||
import soot.jimple.Stmt | ||
import soot.tagkit.ParamNamesTag | ||
import java.lang.reflect.Method | ||
import kotlin.random.Random | ||
import kotlin.system.measureTimeMillis | ||
|
||
val logger = KotlinLogging.logger {} | ||
|
@@ -384,6 +388,7 @@ class UtBotSymbolicEngine( | |
} | ||
|
||
val fallbackModelProvider = FallbackModelProvider { nextDefaultModelId++ } | ||
val constantValues = collectConstantsForFuzzer(graph) | ||
|
||
val thisInstance = when { | ||
methodUnderTest.isStatic -> null | ||
|
@@ -396,7 +401,9 @@ class UtBotSymbolicEngine( | |
null | ||
} | ||
else -> { | ||
fallbackModelProvider.toModel(methodUnderTest.clazz).apply { | ||
ObjectModelProvider { nextDefaultModelId++ }.withFallback(fallbackModelProvider).generate( | ||
FuzzedMethodDescription("thisInstance", voidClassId, listOf(methodUnderTest.clazz.id), constantValues) | ||
).take(10).shuffled(Random(0)).map { it.value.model }.first().apply { | ||
if (this is UtNullModel) { // it will definitely fail because of NPE, | ||
return@flow | ||
} | ||
|
@@ -411,17 +418,34 @@ class UtBotSymbolicEngine( | |
val names = graph.body.method.tags.filterIsInstance<ParamNamesTag>().firstOrNull()?.names | ||
parameterNameMap = { index -> names?.getOrNull(index) } | ||
} | ||
val modelProviderWithFallback = modelProvider(defaultModelProviders { nextDefaultModelId++ }).withFallback(fallbackModelProvider::toModel) | ||
val coveredInstructionTracker = Trie(Instruction::id) | ||
val coveredInstructionValues = mutableMapOf<Trie.Node<Instruction>, List<FuzzedValue>>() | ||
var attempts = UtSettings.fuzzingMaxAttempts | ||
fuzz(methodUnderTestDescription, modelProviderWithFallback).forEach { values -> | ||
val hasMethodUnderTestParametersToFuzz = executableId.parameters.isNotEmpty() | ||
val fuzzedValues = if (hasMethodUnderTestParametersToFuzz) { | ||
fuzz(methodUnderTestDescription, modelProvider(defaultModelProviders { nextDefaultModelId++ })) | ||
} else { | ||
// in case a method with no parameters is passed fuzzing tries to fuzz this instance with different constructors, setters and field mutators | ||
val thisMethodDescription = FuzzedMethodDescription("thisInstance", voidClassId, listOf(methodUnderTest.clazz.id), constantValues).apply { | ||
className = executableId.classId.simpleName | ||
packageName = executableId.classId.packageName | ||
} | ||
fuzz(thisMethodDescription, ObjectModelProvider { nextDefaultModelId++ }.apply { | ||
limitValuesCreatedByFieldAccessors = 500 | ||
}) | ||
} | ||
fuzzedValues.forEach { values -> | ||
if (System.currentTimeMillis() >= until) { | ||
logger.info { "Fuzzing overtime: $methodUnderTest" } | ||
return@flow | ||
} | ||
|
||
val initialEnvironmentModels = EnvironmentModels(thisInstance, values.map { it.model }, mapOf()) | ||
val initialEnvironmentModels = if (hasMethodUnderTestParametersToFuzz) { | ||
EnvironmentModels(thisInstance, values.map { it.model }, mapOf()) | ||
} else { | ||
check(values.size == 1 && values.first().model is UtAssembleModel) | ||
EnvironmentModels(values.first().model, emptyList(), mapOf()) | ||
} | ||
|
||
try { | ||
val concreteExecutionResult = | ||
|
@@ -462,7 +486,7 @@ class UtBotSymbolicEngine( | |
fullPath = emptyList(), | ||
coverage = concreteExecutionResult.coverage, | ||
testMethodName = testMethodName?.testName, | ||
displayName = testMethodName?.displayName | ||
displayName = testMethodName?.takeIf { hasMethodUnderTestParametersToFuzz }?.displayName | ||
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. If I understand this code right, we will not have display name for tests without parameters at all to avoid explicit "this" argument in the name? 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. Yes, it's true |
||
) | ||
) | ||
} catch (e: CancellationException) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.utbot.fuzzer | ||
|
||
/** | ||
* Fuzzed parameter of a method. | ||
* | ||
* @param index of the parameter in method signature | ||
* @param value fuzzed values | ||
*/ | ||
class FuzzedParameter( | ||
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. Could this class be a data class? 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. IMO, data classes creates hashCode/equals/toString that aren't necessary in most cases |
||
val index: Int, | ||
val value: FuzzedValue | ||
) { | ||
operator fun component1() = index | ||
operator fun component2() = value | ||
} |
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.
Are you purposely using the fixed seed here?
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.
Yes, fuzzer should randomize but be reproducable for same method