Skip to content

Commit 762090b

Browse files
authored
Select only raw-strings for regex fuzzing and filter blank strings (#2301)
1 parent 5d3465f commit 762090b

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

utbot-python/src/main/kotlin/org/utbot/python/fuzzing/provider/StrValueProvider.kt

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,22 @@ object StrValueProvider : ValueProvider<Type, PythonFuzzedValue, PythonMethodDes
2222
return type.pythonTypeName() == pythonStrClassId.canonicalName
2323
}
2424

25-
private fun getStrConstants(concreteValues: Collection<PythonFuzzedConcreteValue>): List<String> {
25+
private fun getConstants(concreteValues: Collection<PythonFuzzedConcreteValue>): List<String> {
2626
return concreteValues
2727
.filter { accept(it.type) }
28-
.map {
29-
val value = it.value as String
30-
value.transformRawString()
31-
}
32-
.map {
33-
it.transformQuotationMarks()
34-
}
28+
.map { it.value as String }
29+
}
30+
31+
private fun getStrConstants(concreteValues: Collection<PythonFuzzedConcreteValue>): List<String> {
32+
return getConstants(concreteValues)
33+
.filterNot { it.isPattern() }
34+
.map { it.transformQuotationMarks() }
35+
}
36+
37+
private fun getRegexConstants(concreteValues: Collection<PythonFuzzedConcreteValue>): List<String> {
38+
return getConstants(concreteValues)
39+
.filter { it.isPattern() }
40+
.map { it.transformRawString().transformQuotationMarks() }
3541
}
3642

3743
override fun generate(description: PythonMethodDescription, type: Type) = sequence {
@@ -42,13 +48,10 @@ object StrValueProvider : ValueProvider<Type, PythonFuzzedValue, PythonMethodDes
4248
)
4349
strConstants.forEach { yieldStrings(StringValue(it)) { value } }
4450

45-
strConstants
46-
.filter {
47-
it.isPattern()
48-
}
49-
.forEach {
50-
yieldStrings(RegexValue(it, description.random), StringValue::value)
51-
}
51+
val regexConstants = getRegexConstants(description.concreteValues)
52+
regexConstants.forEach {
53+
yieldStrings(RegexValue(it, description.random), StringValue::value)
54+
}
5255
}
5356

5457
private suspend fun <T : KnownValue<T>> SequenceScope<Seed<Type, PythonFuzzedValue>>.yieldStrings(value: T, block: T.() -> Any) {

utbot-python/src/main/kotlin/org/utbot/python/fuzzing/provider/utils/StringUtils.kt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,28 @@ fun String.transformQuotationMarks(): String {
2323
}
2424

2525
fun String.transformRawString(): String {
26-
val rawStringWithDoubleQuotationMarks = this.startsWith("r\"") && this.endsWith("\"")
27-
val rawStringWithOneQuotationMarks = this.startsWith("r'") && this.endsWith("'")
28-
return if (rawStringWithOneQuotationMarks || rawStringWithDoubleQuotationMarks) {
26+
return if (this.isRawString()) {
2927
this.substring(2, this.length-1)
3028
} else {
3129
this
3230
}
3331
}
3432

33+
fun String.isRawString(): Boolean {
34+
val rawStringWithDoubleQuotationMarks = this.startsWith("r\"") && this.endsWith("\"")
35+
val rawStringWithOneQuotationMarks = this.startsWith("r'") && this.endsWith("'")
36+
return rawStringWithOneQuotationMarks || rawStringWithDoubleQuotationMarks
37+
}
38+
3539
fun String.isPattern(): Boolean {
36-
return try {
37-
Pattern.compile(this); true
38-
} catch (_: PatternSyntaxException) {
39-
false
40-
}
40+
return if (this.isRawString()) {
41+
val stringContent = this.transformRawString()
42+
if (stringContent.isNotBlank()) {
43+
try {
44+
Pattern.compile(stringContent); true
45+
} catch (_: PatternSyntaxException) {
46+
false
47+
}
48+
} else false
49+
} else false
4150
}

0 commit comments

Comments
 (0)