diff --git a/core/src/main/kotlin/org/evomaster/core/output/MongoWriter.kt b/core/src/main/kotlin/org/evomaster/core/output/MongoWriter.kt index 901d4444bf..084e266f9f 100644 --- a/core/src/main/kotlin/org/evomaster/core/output/MongoWriter.kt +++ b/core/src/main/kotlin/org/evomaster/core/output/MongoWriter.kt @@ -58,8 +58,9 @@ object MongoWriter { .forEach { g -> when (g) { is ObjectGene -> { - val printableValue = - StringEscapeUtils.escapeJava(g.getValueAsPrintableString(mode = GeneUtils.EscapeMode.EJSON)) + val ejson = g.getValueAsPrintableString(mode = GeneUtils.EscapeMode.EJSON) + val printableValue = if (format.isJava()) escapeEjsonForJavaLiteral(ejson) + else StringEscapeUtils.escapeJava(ejson) val adaptedPrintableValue = if (format.isKotlin()) printableValue.replace( "$", @@ -95,4 +96,19 @@ object MongoWriter { } + /** + * Escapes EJSON for a Java string literal without leaving a raw backslash-u + * sequence. Java translates Unicode escapes before parsing string literals, + * so an EJSON value containing a literal backslash followed by 'u' can make + * otherwise valid generated tests fail to compile. + */ + internal fun escapeEjsonForJavaLiteral(value: String): String { + return StringEscapeUtils.escapeJava(value).replace(ESCAPED_BACKSLASH_U) { match -> + val slashCount = match.value.length - 1 + "\\134".repeat(slashCount / 2) + "u" + } + } + + private val ESCAPED_BACKSLASH_U = Regex("""(\\\\)+u""") + } diff --git a/core/src/main/kotlin/org/evomaster/core/problem/rest/data/RestPath.kt b/core/src/main/kotlin/org/evomaster/core/problem/rest/data/RestPath.kt index 4e1439b4e6..2836c9eaea 100644 --- a/core/src/main/kotlin/org/evomaster/core/problem/rest/data/RestPath.kt +++ b/core/src/main/kotlin/org/evomaster/core/problem/rest/data/RestPath.kt @@ -354,7 +354,15 @@ class RestPath(path: String) { val data = dynamicResolutionOnlyPathData(params, mapOf()) assert(data.size == 1) assert(!data[0].second) - return data[0].first + return data[0].first.map { c -> + // The URI calls in dynamicResolutionOnlyPathData do not encode non-ASCII characters in path segments. + if (c.code > 127) { + // non-ASCII + encode(c.toString()) + } else { + c.toString() + } + }.joinToString( "") } @@ -413,7 +421,7 @@ class RestPath(path: String) { why not using URI also for Query part??? it seems unclear how to properly build it as a single string... */ - val entry = URI(null, null, path.toString(), null, null).toASCIIString() + val entry = URI(null, null, path.toString(), null, null).rawPath data.add(Pair(entry, false)) path.setLength(0) // clear it data.add(Pair(variable, true)) @@ -446,7 +454,7 @@ class RestPath(path: String) { } if(path.isNotEmpty()){ - val entry = URI(null, null, path.toString(), null, null).toASCIIString() + val entry = URI(null, null, path.toString(), null, null).rawPath data.add(Pair(entry, false)) } diff --git a/core/src/main/kotlin/org/evomaster/core/search/service/monitor/SearchProcessMonitor.kt b/core/src/main/kotlin/org/evomaster/core/search/service/monitor/SearchProcessMonitor.kt index f006895183..224d325945 100644 --- a/core/src/main/kotlin/org/evomaster/core/search/service/monitor/SearchProcessMonitor.kt +++ b/core/src/main/kotlin/org/evomaster/core/search/service/monitor/SearchProcessMonitor.kt @@ -109,8 +109,8 @@ class SearchProcessMonitor: SearchListener { @PostConstruct fun postConstruct(){ - initMonitorProcessOutputs() if(config.enableProcessMonitor){ + initMonitorProcessOutputs() time.addListener(this) if (config.processFormat == EMConfig.ProcessDataFormat.TEST_IND || config.processFormat == EMConfig.ProcessDataFormat.TARGET_TEST_IND){ val dto = try { diff --git a/core/src/test/kotlin/org/evomaster/core/output/MongoWriterTest.kt b/core/src/test/kotlin/org/evomaster/core/output/MongoWriterTest.kt new file mode 100644 index 0000000000..66d5a941a2 --- /dev/null +++ b/core/src/test/kotlin/org/evomaster/core/output/MongoWriterTest.kt @@ -0,0 +1,18 @@ +package org.evomaster.core.output + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Test + +class MongoWriterTest { + + @Test + fun `should escape literal backslash u in EJSON without changing its runtime value`() { + val ejson = """{"field":"El24e\\uJTQGh"}""" + + val escaped = MongoWriter.escapeEjsonForJavaLiteral(ejson) + + assertEquals("""{\"field\":\"El24e\134\134uJTQGh\"}""", escaped) + assertFalse(Regex("""(\\\\)+u""").containsMatchIn(escaped)) + } +} diff --git a/core/src/test/kotlin/org/evomaster/core/search/service/ProcessMonitorTest.kt b/core/src/test/kotlin/org/evomaster/core/search/service/ProcessMonitorTest.kt index ad0d393627..4cfedf6397 100644 --- a/core/src/test/kotlin/org/evomaster/core/search/service/ProcessMonitorTest.kt +++ b/core/src/test/kotlin/org/evomaster/core/search/service/ProcessMonitorTest.kt @@ -72,25 +72,34 @@ class ProcessMonitorTest{ @Test fun testDisableProcessMonitor(){ + val output = Files.createTempDirectory("process-monitor-disabled-") + val marker = output.resolve("marker") + Files.createFile(marker) - config.enableProcessMonitor = false - config.showProgress = true + try { + config.processFiles = output.toString() + config.enableProcessMonitor = false + config.showProgress = true - processMonitor.postConstruct() - assertFalse(Files.exists(Paths.get(config.processFiles))) + processMonitor.postConstruct() + assertTrue(Files.exists(marker)) - val a = OneMaxIndividual(2) - TestUtils.doInitializeIndividualForTesting(a,randomness) - a.setValue(0, 1.0) + val a = OneMaxIndividual(2) + TestUtils.doInitializeIndividualForTesting(a,randomness) + a.setValue(0, 1.0) - val eval = ff.calculateCoverage(a, modifiedSpec = null)!! - processMonitor.eval = eval - processMonitor.newActionsEvaluated(1) + val eval = ff.calculateCoverage(a, modifiedSpec = null)!! + processMonitor.eval = eval + processMonitor.newActionsEvaluated(1) - val added = archive.addIfNeeded(eval) - processMonitor.record(added, true, eval) + val added = archive.addIfNeeded(eval) + processMonitor.record(added, true, eval) - assertFalse(Files.exists(Paths.get(config.processFiles))) + assertTrue(Files.exists(marker)) + } finally { + Files.deleteIfExists(marker) + Files.deleteIfExists(output) + } } @Test