|
| 1 | +package org.utbot.fuzzer.providers |
| 2 | + |
| 3 | +import java.text.SimpleDateFormat |
| 4 | +import org.utbot.framework.plugin.api.ClassId |
| 5 | +import org.utbot.framework.plugin.api.UtAssembleModel |
| 6 | +import org.utbot.framework.plugin.api.UtExecutableCallModel |
| 7 | +import org.utbot.framework.plugin.api.UtPrimitiveModel |
| 8 | +import org.utbot.framework.plugin.api.UtStatementModel |
| 9 | +import org.utbot.framework.plugin.api.util.dateClassId |
| 10 | +import org.utbot.framework.plugin.api.util.executableId |
| 11 | +import org.utbot.framework.plugin.api.util.id |
| 12 | +import org.utbot.framework.plugin.api.util.intClassId |
| 13 | +import org.utbot.framework.plugin.api.util.jClass |
| 14 | +import org.utbot.framework.plugin.api.util.longClassId |
| 15 | +import org.utbot.framework.plugin.api.util.stringClassId |
| 16 | +import org.utbot.framework.plugin.api.util.voidClassId |
| 17 | +import org.utbot.fuzzer.FuzzedMethodDescription |
| 18 | +import org.utbot.fuzzer.FuzzedParameter |
| 19 | +import org.utbot.fuzzer.FuzzedValue |
| 20 | +import org.utbot.fuzzer.IdentityPreservingIdGenerator |
| 21 | +import org.utbot.fuzzer.ModelProvider |
| 22 | +import org.utbot.fuzzer.ModelProvider.Companion.yieldAllValues |
| 23 | +import org.utbot.fuzzer.defaultModelProviders |
| 24 | +import org.utbot.fuzzer.fuzz |
| 25 | +import org.utbot.fuzzer.hex |
| 26 | +import org.utbot.fuzzer.objects.assembleModel |
| 27 | + |
| 28 | +class DateConstantModelProvider( |
| 29 | + private val idGenerator: IdentityPreservingIdGenerator<Int>, |
| 30 | +) : ModelProvider { |
| 31 | + |
| 32 | + var totalLimit: Int = 20 |
| 33 | + |
| 34 | + companion object { |
| 35 | + private const val defaultDateFormat = "dd-MM-yyyy HH:mm:ss:ms" |
| 36 | + } |
| 37 | + |
| 38 | + private fun String.isDate(format: String): Boolean { |
| 39 | + val formatter = SimpleDateFormat(format).apply { |
| 40 | + isLenient = false |
| 41 | + } |
| 42 | + return runCatching { formatter.parse(trim()) }.isSuccess |
| 43 | + } |
| 44 | + |
| 45 | + private fun String.isDateFormat(): Boolean { |
| 46 | + return none { it.isDigit() } && // fixes concrete date values |
| 47 | + runCatching { SimpleDateFormat(this) }.isSuccess |
| 48 | + } |
| 49 | + |
| 50 | + private fun generateFromNumbers( |
| 51 | + baseMethodDescription: FuzzedMethodDescription, |
| 52 | + ): Sequence<FuzzedValue> { |
| 53 | + val constructorsFromNumbers = dateClassId.allConstructors |
| 54 | + .filter { constructor -> |
| 55 | + constructor.parameters.isNotEmpty() && |
| 56 | + constructor.parameters.all { it == intClassId || it == longClassId } |
| 57 | + }.map { constructorId -> |
| 58 | + with(constructorId) { |
| 59 | + ModelConstructor(parameters) { assembleModel(idGenerator.createId(), constructorId, it) } |
| 60 | + } |
| 61 | + }.sortedBy { it.neededTypes.size } |
| 62 | + |
| 63 | + return sequence { |
| 64 | + constructorsFromNumbers.forEach { constructor -> |
| 65 | + yieldAll( |
| 66 | + fuzzValues( |
| 67 | + constructor.neededTypes, |
| 68 | + baseMethodDescription, |
| 69 | + defaultModelProviders(idGenerator) |
| 70 | + ).map(constructor.createModel) |
| 71 | + ) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private fun generateFromDates( |
| 77 | + baseMethodDescription: FuzzedMethodDescription, |
| 78 | + ): Sequence<FuzzedValue> { |
| 79 | + val strings = baseMethodDescription.concreteValues |
| 80 | + .asSequence() |
| 81 | + .filter { it.classId == stringClassId } |
| 82 | + .map { it.value as String } |
| 83 | + .distinct() |
| 84 | + val formats = strings.filter { it.isDateFormat() } + defaultDateFormat |
| 85 | + val formatToDates = formats.associateWith { format -> strings.filter { it.isDate(format) } } |
| 86 | + |
| 87 | + return sequence { |
| 88 | + formatToDates.forEach { (format, dates) -> |
| 89 | + dates.forEach { date -> |
| 90 | + yield(assembleDateFromString(idGenerator.createId(), format, date)) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private fun generateNowDate(): Sequence<FuzzedValue> { |
| 97 | + val constructor = dateClassId.allConstructors.first { it.parameters.isEmpty() } |
| 98 | + return sequenceOf(assembleModel(idGenerator.createId(), constructor, emptyList())) |
| 99 | + } |
| 100 | + |
| 101 | + override fun generate(description: FuzzedMethodDescription): Sequence<FuzzedParameter> { |
| 102 | + val parameters = description.parametersMap[dateClassId] |
| 103 | + if (parameters.isNullOrEmpty()) { |
| 104 | + return emptySequence() |
| 105 | + } |
| 106 | + |
| 107 | + return sequence { |
| 108 | + yieldAllValues( |
| 109 | + parameters, |
| 110 | + generateNowDate() + generateFromDates(description) + |
| 111 | + generateFromNumbers(description).take(totalLimit) |
| 112 | + ) |
| 113 | + }.take(totalLimit) |
| 114 | + } |
| 115 | + |
| 116 | + private fun fuzzValues( |
| 117 | + types: List<ClassId>, |
| 118 | + baseMethodDescription: FuzzedMethodDescription, |
| 119 | + modelProvider: ModelProvider, |
| 120 | + ): Sequence<List<FuzzedValue>> { |
| 121 | + if (types.isEmpty()) |
| 122 | + return sequenceOf(listOf()) |
| 123 | + val syntheticMethodDescription = FuzzedMethodDescription( |
| 124 | + "<synthetic method for DateModelProvider>", // TODO: maybe add more info here |
| 125 | + voidClassId, |
| 126 | + types, |
| 127 | + baseMethodDescription.concreteValues |
| 128 | + ).apply { |
| 129 | + packageName = baseMethodDescription.packageName |
| 130 | + } |
| 131 | + return fuzz(syntheticMethodDescription, modelProvider) |
| 132 | + } |
| 133 | + |
| 134 | + private fun assembleDateFromString(id: Int, formatString: String, dateString: String): FuzzedValue { |
| 135 | + val simpleDateFormatModel = assembleSimpleDateFormat(idGenerator.createId(), formatString) |
| 136 | + val dateFormatParse = simpleDateFormatModel.classId.jClass |
| 137 | + .getMethod("parse", String::class.java).executableId |
| 138 | + val instantiationChain = mutableListOf<UtStatementModel>() |
| 139 | + return UtAssembleModel( |
| 140 | + id, |
| 141 | + dateClassId, |
| 142 | + "$dateFormatParse#" + id.hex(), |
| 143 | + instantiationChain |
| 144 | + ).apply { |
| 145 | + instantiationChain += simpleDateFormatModel.allStatementsChain |
| 146 | + instantiationChain += UtExecutableCallModel( |
| 147 | + simpleDateFormatModel, dateFormatParse, listOf(UtPrimitiveModel(dateString)), returnValue = this |
| 148 | + ) |
| 149 | + }.fuzzed { |
| 150 | + summary = "%var% = $dateFormatParse($stringClassId)" |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private fun assembleSimpleDateFormat(id: Int, formatString: String): UtAssembleModel { |
| 155 | + val simpleDateFormatId = SimpleDateFormat::class.java.id |
| 156 | + val formatStringConstructor = simpleDateFormatId.allConstructors.first { |
| 157 | + it.parameters.singleOrNull() == stringClassId |
| 158 | + } |
| 159 | + val formatSetLenient = SimpleDateFormat::setLenient.executableId |
| 160 | + val formatModel = UtPrimitiveModel(formatString) |
| 161 | + |
| 162 | + val instantiationChain = mutableListOf<UtStatementModel>() |
| 163 | + val modificationsChain = mutableListOf<UtStatementModel>() |
| 164 | + return UtAssembleModel( |
| 165 | + id, |
| 166 | + simpleDateFormatId, |
| 167 | + "$simpleDateFormatId[$stringClassId]#" + id.hex(), |
| 168 | + instantiationChain, |
| 169 | + modificationsChain |
| 170 | + ).apply { |
| 171 | + instantiationChain += UtExecutableCallModel( |
| 172 | + instance = null, formatStringConstructor, listOf(formatModel), returnValue = this |
| 173 | + ) |
| 174 | + modificationsChain += UtExecutableCallModel( |
| 175 | + instance = this, formatSetLenient, listOf(UtPrimitiveModel(false)) |
| 176 | + ) |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments