-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
223 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package flank.corellium | ||
|
||
import flank.corellium.adapter.ExecuteAndroidTestPlan | ||
import flank.corellium.adapter.InstallAndroidApps | ||
import flank.corellium.adapter.InvokeAndroidDevices | ||
import flank.corellium.adapter.RequestAuthorization | ||
import flank.corellium.api.CorelliumApi | ||
|
||
fun corelliumApi( | ||
projectName: String | ||
) = CorelliumApi( | ||
authorize = RequestAuthorization, | ||
installAndroidApps = InstallAndroidApps( | ||
projectName = projectName | ||
), | ||
invokeAndroidDevices = InvokeAndroidDevices, | ||
executeTest = ExecuteAndroidTestPlan | ||
) |
11 changes: 11 additions & 0 deletions
11
corellium/adapter/src/main/kotlin/flank/corellium/adapter/ClientReference.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package flank.corellium.adapter | ||
|
||
import flank.corellium.client.core.Corellium | ||
|
||
internal val corellium: Corellium | ||
get() = requireNotNull(corelliumRef) { | ||
"Corellium is not initialized, try to call connectCorellium at first." | ||
} | ||
|
||
// It's totally ok to keep corellium as singleton since we don't need handle more than one connection for single run. | ||
internal var corelliumRef: Corellium? = null |
57 changes: 57 additions & 0 deletions
57
corellium/adapter/src/main/kotlin/flank/corellium/adapter/ExecuteAndroidTestPlan.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package flank.corellium.adapter | ||
|
||
import flank.corellium.api.AndroidTestPlan | ||
import flank.corellium.client.console.clear | ||
import flank.corellium.client.console.flowLogs | ||
import flank.corellium.client.console.sendCommand | ||
import flank.corellium.client.console.waitForIdle | ||
import flank.corellium.client.core.connectConsole | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.channelFlow | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.joinAll | ||
import kotlinx.coroutines.launch | ||
|
||
object ExecuteAndroidTestPlan : AndroidTestPlan.Execute { | ||
override suspend fun AndroidTestPlan.Config.invoke(): Flow<String> = | ||
coroutineScope { | ||
channelFlow { | ||
instances.map { (instanceId, shards: List<AndroidTestPlan.Shard>) -> | ||
println("Getting console $instanceId") | ||
launch { | ||
corellium.connectConsole(instanceId).run { | ||
clear() | ||
launch { | ||
shards.forEach { shard -> | ||
val command = shard.prepareRunCommand() | ||
println("Sending command: $command") | ||
sendCommand(command) | ||
} | ||
} | ||
launch { | ||
flowLogs().collect { | ||
channel.send(it) | ||
} | ||
} | ||
waitForIdle(10_000) | ||
} | ||
} | ||
}.joinAll() | ||
} | ||
} | ||
} | ||
|
||
private fun AndroidTestPlan.Shard.prepareRunCommand(): String { | ||
val base = "am instrument -r -w " | ||
|
||
val testCases = testCases | ||
// group test cases by filter type - [class, package] | ||
.map { it.split(" ") }.groupBy({ it.first() }, { it.last() }).toList() | ||
// build test cases string | ||
.joinToString("") { (type, tests) -> "-e $type ${tests.joinToString(",")} " } | ||
|
||
val runner = "$packageName/$testRunner" | ||
|
||
return base + testCases + runner | ||
} |
37 changes: 37 additions & 0 deletions
37
corellium/adapter/src/main/kotlin/flank/corellium/adapter/InstallAndroidApps.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package flank.corellium.adapter | ||
|
||
import flank.corellium.api.AndroidApps | ||
import flank.corellium.client.agent.uploadFile | ||
import flank.corellium.client.console.sendCommand | ||
import flank.corellium.client.core.connectAgent | ||
import flank.corellium.client.core.connectConsole | ||
import flank.corellium.client.core.getAllProjects | ||
import flank.corellium.client.core.getProjectInstancesList | ||
import java.io.File | ||
|
||
private const val PATH_TO_UPLOAD = "/sdcard" | ||
|
||
class InstallAndroidApps( | ||
private val projectName: String | ||
) : AndroidApps.Install { | ||
|
||
override suspend fun List<AndroidApps>.invoke() { | ||
val corellium = corellium | ||
val projectId = corellium.getAllProjects().first { it.name == projectName }.id | ||
val instances = corellium.getProjectInstancesList(projectId).associateBy { it.id } | ||
|
||
forEach { apps -> | ||
val instance = instances.getValue(apps.instanceId) | ||
val agent = corellium.connectAgent(instance.agent!!.info) | ||
val console = corellium.connectConsole(instance.id) | ||
|
||
apps.paths.forEach { localPath -> | ||
val file = File(localPath) | ||
val remotePath = "$PATH_TO_UPLOAD/${file.name}" | ||
|
||
agent.uploadFile(remotePath, file.readBytes()) | ||
console.sendCommand("pm install $remotePath") | ||
} | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
corellium/adapter/src/main/kotlin/flank/corellium/adapter/InvokeAndroidDevices.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package flank.corellium.adapter | ||
|
||
import flank.corellium.api.AndroidInstance | ||
import flank.corellium.api.AndroidInstance.Config | ||
|
||
object InvokeAndroidDevices : AndroidInstance.Invoke { | ||
override suspend fun Config.invoke(): List<String> = TODO() // https://github.com/flank/flank/issues/1837 | ||
} |
14 changes: 14 additions & 0 deletions
14
corellium/adapter/src/main/kotlin/flank/corellium/adapter/RequestAuthorization.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package flank.corellium.adapter | ||
|
||
import flank.corellium.api.Authorization | ||
import flank.corellium.client.core.connectCorellium | ||
|
||
object RequestAuthorization : Authorization.Request { | ||
override suspend fun Authorization.Credentials.invoke() { | ||
corelliumRef = connectCorellium( | ||
api = host, | ||
username = username, | ||
password = password | ||
) | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
corellium/adapter/src/test/kotlin/flank/corellium/adapter/AdaptersExample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package flank.corellium.adapter | ||
|
||
import flank.corellium.api.AndroidTestPlan | ||
import flank.corellium.api.Authorization | ||
import flank.corellium.api.invoke | ||
import flank.corellium.client.core.getAllProjects | ||
import flank.corellium.client.core.getProjectInstancesList | ||
import flank.corellium.corelliumApi | ||
import kotlinx.coroutines.flow.collect | ||
import kotlinx.coroutines.runBlocking | ||
|
||
private const val PROJECT_NAME = "Default Project" | ||
|
||
// The credentials are not provided along with the code. | ||
// If you need to execute example you have to deliver credentials on your own. | ||
private val credentials = Authorization.Credentials( | ||
host = TODO(), | ||
username = TODO(), | ||
password = TODO(), | ||
) | ||
|
||
private val androidTestPlanConfig = AndroidTestPlan.Config( | ||
instances = mapOf( | ||
"d8ae09fe-a60a-480a-968f-1a30d77a0e11" to listOf( | ||
AndroidTestPlan.Shard( | ||
packageName = "com.example.test_app.test", | ||
testRunner = "androidx.test.runner.AndroidJUnitRunner", | ||
testCases = listOf( | ||
"class com.example.test_app.InstrumentedTest#test0", | ||
"class com.example.test_app.InstrumentedTest#test1", | ||
) | ||
), | ||
AndroidTestPlan.Shard( | ||
packageName = "com.example.test_app.test", | ||
testRunner = "androidx.test.runner.AndroidJUnitRunner", | ||
testCases = listOf( | ||
"class com.example.test_app.InstrumentedTest#test2", | ||
) | ||
), | ||
), | ||
"b7a305e5-b199-4ed6-9ba7-4c9b83c6762e" to listOf( | ||
AndroidTestPlan.Shard( | ||
packageName = "com.example.test_app.test", | ||
testRunner = "androidx.test.runner.AndroidJUnitRunner", | ||
testCases = listOf( | ||
"class com.example.test_app.foo.FooInstrumentedTest#testFoo", | ||
) | ||
) | ||
) | ||
) | ||
) | ||
|
||
fun main() { | ||
val api = corelliumApi(PROJECT_NAME) | ||
|
||
runBlocking { | ||
|
||
println("* Authorizing") | ||
api.authorize(credentials) | ||
|
||
corellium.run { | ||
getProjectInstancesList(getAllProjects().first { it.name == PROJECT_NAME }.id) | ||
}.forEach { | ||
println(it) | ||
} | ||
|
||
println("* Executing tests") | ||
api.executeTest(androidTestPlanConfig).collect { line -> | ||
print(line) | ||
} | ||
println() | ||
println("* Finish") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters