Skip to content

Commit

Permalink
#823 Handle case when method name is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Adamczyk committed Jun 12, 2020
1 parent 993d61f commit f4f53c6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
20 changes: 13 additions & 7 deletions test_runner/src/main/kotlin/ftl/util/Obfuscation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ fun ObfuscationContext.obfuscateAndroidTestName(input: String): String {
val obfuscatedPackageNameWithClass =
obfuscateAndroidPackageAndClass(input.split(ANDROID_TEST_METHOD_SEPARATOR).first())

val obfuscatedMethodName = obfuscateMethodName(
methodName = input.split(ANDROID_TEST_METHOD_SEPARATOR).last(),
context = getOrPut(obfuscatedPackageNameWithClass) { mutableMapOf() }
)

return "$obfuscatedPackageNameWithClass$ANDROID_TEST_METHOD_SEPARATOR$obfuscatedMethodName"
return obfuscatedPackageNameWithClass + obfuscateAndroidMethodIfPresent(input, obfuscatedPackageNameWithClass)
}

private fun ObfuscationContext.obfuscateAndroidPackageAndClass(packageNameWithClass: String) =
Expand All @@ -28,6 +23,16 @@ private fun ObfuscationContext.obfuscateAndroidPackageAndClass(packageNameWithCl
if (previous.isEmpty()) obfuscatedPart else "$previous$ANDROID_PACKAGE_SEPARATOR$obfuscatedPart"
}

private fun ObfuscationContext.obfuscateAndroidMethodIfPresent(
input: String,
obfuscatedPackageNameWithClass: String
) = if (input.contains(ANDROID_TEST_METHOD_SEPARATOR))
ANDROID_TEST_METHOD_SEPARATOR + obfuscateMethodName(
methodName = input.split(ANDROID_TEST_METHOD_SEPARATOR).last(),
context = getOrPut(obfuscatedPackageNameWithClass) { mutableMapOf() }
)
else ""

fun ObfuscationContext.obfuscateIosTestName(input: String): String {
val className = input.split(IOS_TEST_METHOD_SEPARATOR).first()
val obfuscatedClassName = getOrPut("") { mutableMapOf() }.run {
Expand All @@ -53,4 +58,5 @@ private fun nextSymbol(key: String, context: Map<String, String>): String {
return possibleSymbols[currentContextItemCount % possibleSymbols.length].toString().repeat(repeatSymbol)
}

private fun obfuscateMethodName(methodName: String, context: MutableMap<String, String>) = context.getOrPut(methodName) { nextSymbol(methodName, context) }
private fun obfuscateMethodName(methodName: String, context: MutableMap<String, String>) =
context.getOrPut(methodName) { nextSymbol(methodName, context) }
32 changes: 31 additions & 1 deletion test_runner/src/test/kotlin/ftl/util/ObfuscationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal class ObfuscationTest {
@Test
fun `should obfuscate android like test method string`() {
// given
val testString = "com.flank.Super#Test"
val testString = "com.flank.Super#test"
val obfuscationContext: ObfuscationContext = mutableMapOf()

// when
Expand Down Expand Up @@ -62,6 +62,36 @@ internal class ObfuscationTest {
}
}

@Test
fun `should obfuscate android only package and class if method not present`() {
// given
val testMethods = listOf(
"com.example.Class1",
"com.example.Class1",
"com.example.Class2#method1",
"com.example.foo.Class1",
"flank.support.Test",
"flank.test.Test"
)
val expectedObfuscationResult = listOf(
"a.a.A",
"a.a.A",
"a.a.B#a",
"a.a.a.A",
"b.a.A",
"b.b.A"
)
val obfuscationContext: ObfuscationContext = mutableMapOf()

// when
val testResults = testMethods.map { obfuscationContext.obfuscateAndroidTestName(it) }

// then
testResults.forEachIndexed { index, obfuscatedMethodName ->
assertThat(obfuscatedMethodName).isEqualTo(expectedObfuscationResult[index])
}
}

@Test
fun `should obfuscate ios method name and assign unique symbols to class and method`() {
// given
Expand Down

0 comments on commit f4f53c6

Please sign in to comment.