Skip to content

Commit 5eac028

Browse files
authored
When enum is not accessible, it shouldn't be created by fuzzer (#1660) (#1752)
* When enum is not accessible, it shouldn't be created by fuzzer (#1660) * Add test for enum
1 parent 03cd979 commit 5eac028

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

utbot-fuzzers/src/main/kotlin/org/utbot/fuzzing/providers/Enums.kt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ class EnumValueProvider(
2020
description: FuzzedDescription,
2121
type: FuzzedType
2222
) = sequence<Seed<FuzzedType, FuzzedValue>> {
23-
type.classId.jClass.enumConstants.filterIsInstance<Enum<*>>().forEach { enum ->
24-
val id = idGenerator.getOrCreateIdForValue(enum)
25-
yield(Seed.Simple(UtEnumConstantModel(id, type.classId, enum).fuzzed {
26-
summary = "%var% = $enum"
27-
}))
23+
val jClass = type.classId.jClass
24+
if (isAccessible(jClass, description.description.packageName)) {
25+
jClass.enumConstants.filterIsInstance<Enum<*>>().forEach { enum ->
26+
val id = idGenerator.getOrCreateIdForValue(enum)
27+
yield(Seed.Simple(UtEnumConstantModel(id, type.classId, enum).fuzzed {
28+
summary = "%var% = $enum"
29+
}))
30+
}
2831
}
2932
}
3033
}

utbot-fuzzers/src/test/java/org/utbot/fuzzing/samples/AccessibleObjects.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,18 @@ public Node() {
1616
}
1717
}
1818
}
19+
20+
public int ordinal(InnEn val) {
21+
switch (val) {
22+
case ONE:
23+
return 0;
24+
case TWO:
25+
return 1;
26+
}
27+
return -1;
28+
}
29+
30+
private enum InnEn {
31+
ONE, TWO
32+
}
1933
}

utbot-fuzzers/src/test/kotlin/org/utbot/fuzzing/JavaFuzzingTest.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,23 @@ class JavaFuzzingTest {
216216
assertEquals(0, exec) { "Fuzzer should not create any values of private classes" }
217217
}
218218

219+
@Test
220+
fun `fuzzing should not generate values of private enums`() {
221+
var exec = 0
222+
runBlockingWithContext {
223+
runJavaFuzzing(
224+
TestIdentityPreservingIdGenerator,
225+
methodUnderTest = AccessibleObjects::class.java.declaredMethods.first { it.name == "ordinal" }.executableId,
226+
constants = emptyList(),
227+
names = emptyList(),
228+
) { _, _, _ ->
229+
exec += 1
230+
BaseFeedback(Trie.emptyNode(), Control.STOP)
231+
}
232+
}
233+
assertEquals(0, exec) { "Fuzzer should not create any values of private classes" }
234+
}
235+
219236
@Test
220237
fun `fuzzing generate single test in case of collection with fail-to-generate generic type`() {
221238
val size = 100

0 commit comments

Comments
 (0)