Skip to content

Commit

Permalink
feat: Install apks asynchronous (#2058)
Browse files Browse the repository at this point in the history
A small improvement for installing apks on Corellium devices in an asynchronous way. 
Should speed up slightly the overall execution time.
  • Loading branch information
jan-goral authored Jul 2, 2021
1 parent 377c7cf commit 2d5fc46
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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 kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.flow.channelFlow
Expand Down Expand Up @@ -38,39 +39,42 @@ private suspend fun SendChannel<AndroidApps.Event>.install(
val instances = corellium.getProjectInstancesList(projectId).associateBy { it.id }

appsList.forEach { apps ->
val instance = instances.getValue(apps.instanceId)
launch(Dispatchers.IO) {

send(Connecting.Agent(apps.instanceId))
val instance = instances.getValue(apps.instanceId)

val agentInfo = requireNotNull(instance.agent?.info) {
"Cannot connect to the agent, no agent info for instance ${instance.name} with id: ${instance.id}"
}
val agent = corellium.connectAgent(agentInfo)
send(Connecting.Agent(instance.id))

send(Connecting.Console(apps.instanceId))
val console = corellium.connectConsole(instance.id)
val agentInfo = requireNotNull(instance.agent?.info) {
"Cannot connect to the agent, no agent info for instance ${instance.name} with id: ${instance.id}"
}
val agent = corellium.connectAgent(agentInfo)

// Disable system logging
flowOf("su", "dmesg -n 1", "exit").collect(console::sendCommand)
send(Connecting.Console(instance.id))
val console = corellium.connectConsole(instance.id)

apps.paths.forEach { localPath ->
val file = File(localPath)
val remotePath = "$PATH_TO_UPLOAD/${file.name}"
// Disable system logging
flowOf("su", "dmesg -n 1", "exit").collect(console::sendCommand)

send(Apk.Uploading(localPath))
agent.uploadFile(remotePath, file.readBytes())
apps.paths.forEach { localPath ->
val file = File(localPath)
val remotePath = "$PATH_TO_UPLOAD/${file.name}"

send(Apk.Installing(localPath))
console.sendCommand(
// Current solution is enough for the MVP.
// Fixme: Find better solution for recognizing test apk.
if (localPath.endsWith("androidTest.apk"))
"pm install -t $remotePath" else
"pm install $remotePath"
)
}
send(Apk.Uploading(instance.id, localPath))
agent.uploadFile(remotePath, file.readBytes())

console.close()
agent.disconnect()
send(Apk.Installing(instance.id, localPath))
console.sendCommand(
// Current solution is enough for the MVP.
// Fixme: Find better solution for recognizing test apk.
if (localPath.endsWith("androidTest.apk"))
"pm install -t $remotePath" else
"pm install $remotePath"
)
}

console.close()
agent.disconnect()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ data class AndroidApps(
}

object Apk {
data class Uploading(val path: String) : Event()
data class Installing(val path: String) : Event()
data class Uploading(val instanceId: String, val path: String) : Event()
data class Installing(val instanceId: String, val path: String) : Event()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ internal val format = buildFormatter<String> {
LoadPreviousDurations.Summary::class { "For $required test cases, found $matching matching and $unknown unknown" }
InstallApks.Status {
when (this) {
is AndroidApps.Event.Connecting.Agent -> "Connecting agent for $instanceId"
is AndroidApps.Event.Connecting.Console -> "Connecting console for $instanceId"
is AndroidApps.Event.Apk.Uploading -> "Uploading apk $path"
is AndroidApps.Event.Apk.Installing -> "Installing apk $path"
is AndroidApps.Event.Connecting.Agent -> "$instanceId: Connecting agent"
is AndroidApps.Event.Connecting.Console -> "$instanceId: Connecting console"
is AndroidApps.Event.Apk.Uploading -> "$instanceId: Uploading apk $path"
is AndroidApps.Event.Apk.Installing -> "$instanceId: Installing apk $path"
}
}
InvokeDevices.Status {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ password: $password
Unit event LoadPreviousDurations.Summary(1, 2, 3),
Unit event InstallApks.Status(AndroidApps.Event.Connecting.Agent("123456")),
Unit event InstallApks.Status(AndroidApps.Event.Connecting.Console("123456")),
Unit event InstallApks.Status(AndroidApps.Event.Apk.Uploading("path/to/apk.apk")),
Unit event InstallApks.Status(AndroidApps.Event.Apk.Installing("path/to/apk.apk")),
Unit event InstallApks.Status(AndroidApps.Event.Apk.Uploading("123456", "path/to/apk.apk")),
Unit event InstallApks.Status(AndroidApps.Event.Apk.Installing("123456", "path/to/apk.apk")),
Unit event InvokeDevices.Status(AndroidInstance.Event.GettingAlreadyCreated),
Unit event InvokeDevices.Status(AndroidInstance.Event.Obtained(5)),
Unit event InvokeDevices.Status(AndroidInstance.Event.Starting(6)),
Expand Down

0 comments on commit 2d5fc46

Please sign in to comment.