Skip to content

Commit 9d5a403

Browse files
authored
Do fuzzer improvements needed for integration test generation (#2316)
1 parent c17b0a7 commit 9d5a403

File tree

5 files changed

+28
-16
lines changed

5 files changed

+28
-16
lines changed

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/IdUtil.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ val primitiveByWrapper = mapOf(
267267

268268
val wrapperByPrimitive = primitiveByWrapper.entries.associateBy({ it.value }) { it.key }
269269

270+
fun replaceWithWrapperIfPrimitive(classId: ClassId): ClassId = wrapperByPrimitive[classId] ?: classId
271+
270272
// We consider void primitive here
271273
// It is sometimes useful even if void is not technically a primitive type
272274
val primitives = setOf(

utbot-instrumentation/src/main/kotlin/org/utbot/instrumentation/instrumentation/execution/constructors/MockValueConstructor.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,9 @@ class MockValueConstructor(
330330
//
331331
// So for such executions we throw `IllegalStateException` that indicates that construction of arguments
332332
// has failed and causes execution to terminate with `UtConcreteExecutionProcessedFailure` execution result.
333+
//
334+
// That is also helpful, because it allows to safely use the constructed value where primitive type is expected,
335+
// otherwise some models generated by `FieldValueProvider` can lead to false-positive NPEs.
333336
checkNotNull(result) {
334337
"Tracked instance can't be null for call ${instantiationExecutableCall.statement} in model $assembleModel"
335338
}

utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/Field.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.utbot.framework.plugin.api.UtDirectGetFieldModel
88
import org.utbot.framework.plugin.api.UtNullModel
99
import org.utbot.framework.plugin.api.UtReferenceModel
1010
import org.utbot.framework.plugin.api.util.jClass
11+
import org.utbot.framework.plugin.api.util.replaceWithWrapperIfPrimitive
1112
import org.utbot.fuzzer.FuzzedType
1213
import org.utbot.fuzzer.FuzzedValue
1314
import org.utbot.fuzzer.IdGenerator
@@ -26,7 +27,9 @@ class FieldValueProvider(
2627
private val logger = KotlinLogging.logger {}
2728
}
2829

29-
override fun accept(type: FuzzedType): Boolean = type.classId == fieldId.type
30+
override fun accept(type: FuzzedType): Boolean =
31+
replaceWithWrapperIfPrimitive(type.classId).jClass
32+
.isAssignableFrom(replaceWithWrapperIfPrimitive(fieldId.type).jClass)
3033

3134
override fun generate(description: FuzzedDescription, type: FuzzedType): Sequence<Seed<FuzzedType, FuzzedValue>> = sequenceOf(
3235
Seed.Recursive(

utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/Objects.kt

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,20 +148,24 @@ internal class FieldDescription(
148148
)
149149

150150
internal fun findAccessibleModifiableFields(description: FuzzedDescription?, classId: ClassId, packageName: String?): List<FieldDescription> {
151-
val jClass = classId.jClass
152-
return jClass.declaredFields.map { field ->
153-
val setterAndGetter = jClass.findPublicSetterGetterIfHasPublicGetter(field, packageName)
154-
FieldDescription(
155-
name = field.name,
156-
type = if (description != null) toFuzzerType(field.type, description.typeCache) else FuzzedType(field.type.id),
157-
canBeSetDirectly = isAccessible(
158-
field,
159-
packageName
160-
) && !Modifier.isFinal(field.modifiers) && !Modifier.isStatic(field.modifiers),
161-
setter = setterAndGetter?.setter,
162-
getter = setterAndGetter?.getter,
163-
)
164-
}
151+
return generateSequence(classId.jClass) { it.superclass }.flatMap { jClass ->
152+
jClass.declaredFields.map { field ->
153+
val setterAndGetter = jClass.findPublicSetterGetterIfHasPublicGetter(field, packageName)
154+
FieldDescription(
155+
name = field.name,
156+
type = if (description != null) toFuzzerType(
157+
field.type,
158+
description.typeCache
159+
) else FuzzedType(field.type.id),
160+
canBeSetDirectly = isAccessible(
161+
field,
162+
packageName
163+
) && !Modifier.isFinal(field.modifiers) && !Modifier.isStatic(field.modifiers),
164+
setter = setterAndGetter?.setter,
165+
getter = setterAndGetter?.getter,
166+
)
167+
}
168+
}.toList()
165169
}
166170

167171
internal fun Class<*>.findPublicSetterGetterIfHasPublicGetter(field: Field, packageName: String?): PublicSetterGetter? {

utbot-java-fuzzing/src/main/kotlin/org/utbot/fuzzing/providers/Primitives.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ object StringValueProvider : PrimitiveValueProvider(stringClassId, java.lang.Cha
215215
.filter { it.classId == stringClassId }
216216
val values = constants
217217
.mapNotNull { it.value as? String } +
218-
sequenceOf("", "abc", "\n\t\r")
218+
sequenceOf("", "abc", "XZ", "#$\\\"'", "\n\t\r", "10", "-3")
219219
values.forEach { yieldKnown(StringValue(it), StringValue::value) }
220220
constants
221221
.filter { it.fuzzedContext.isPatterMatchingContext() }

0 commit comments

Comments
 (0)