Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE-576: Allow complex ADB server commands execution #597

Merged
merged 14 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ package com.kaspersky.adbserver.commandtypes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General feedback.

I guess we can use Runtime::exec(String[] cmdarray) instead of Runtime::exec(String command) in CmdCommandPerformer.perform. Have a look at java.lang.Runtime, where both methods call the same method and command param in Runtime::exec(String command) is parsing into the array of strings.

I don't support introducing separate classes like ComplexAdbCommand. I think we need to add the new constructors like Command(command: String, arguments: List<String>) and the correspondent methods in AdbTerminal and etc.

I don't support syntaxis like (command: List<String>) and (command): String. It's confusing. Also, the most popular syntaxis in these cases is (command: String, arguments: List<String>). Yes, another command is an argument of the main command. Cases like sh -c "adb shell dumpsys deviceidle | grep mForceIdle" should be described in the documentation and examples.

import com.kaspersky.adbserver.common.api.Command
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you also reflect all these cases in tests? I mean testing a single command, a command with arguments and a command with commands in arguments (complex commands with pipes and etc.).


data class AdbCommand(override val body: String) : Command(body)
data class AdbCommand(
override val command: String,
override val arguments: List<String> = emptyList()
) : Command(command, arguments)
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ package com.kaspersky.adbserver.commandtypes

import com.kaspersky.adbserver.common.api.Command

data class CmdCommand(override val body: String) : Command(body)
data class CmdCommand(
override val command: String,
override val arguments: List<String> = emptyList()
) : Command(command, arguments)
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ import java.io.Serializable
/**
* Command to execute by AdbServer
*/
abstract class Command(open val body: String) : Serializable
abstract class Command(open val command: String, open val arguments: List<String> = emptyList()) : Serializable
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.kaspersky.adbserver.device

import com.kaspersky.adbserver.common.api.CommandResult
import com.kaspersky.adbserver.commandtypes.AdbCommand
import com.kaspersky.adbserver.commandtypes.CmdCommand
import com.kaspersky.adbserver.common.api.CommandResult
import com.kaspersky.adbserver.common.log.LoggerFactory
import com.kaspersky.adbserver.common.log.logger.LogLevel
import com.kaspersky.adbserver.common.log.logger.Logger
Expand All @@ -27,10 +27,32 @@ object AdbTerminal {
AdbCommand(command)
) ?: throw IllegalStateException("Please first of all call [connect] method to establish a connection")

/**
* Allows more control over how arguments are parsed. Each element in the [arguments] list
* is used as is without tokenizing.
* Refer to the Runtime::exec(String[] cmdarray)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide a full link to this method.

*
* Please first of all call [connect] method to establish a connection
*/
fun executeAdb(command: String, arguments: List<String>): CommandResult = device?.fulfill(
AdbCommand(command, arguments)
) ?: throw IllegalStateException("Please first of all call [connect] method to establish a connection")

/**
* Please first of all call [connect] method to establish a connection
*/
fun executeCmd(command: String): CommandResult = device?.fulfill(
CmdCommand(command)
) ?: throw IllegalStateException("Please first of all call [connect] method to establish a connection")

/**
* Allows more control over how arguments are parsed. Each element in the [arguments] list
* is used as is without tokenizing.
* Refer to the Runtime::exec(String[] cmdarray)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide a full link to this method.

*
* Please first of all call [connect] method to establish a connection
*/
fun executeCmd(command: String, arguments: List<String>): CommandResult = device?.fulfill(
CmdCommand(command, arguments)
) ?: throw IllegalStateException("Please first of all call [connect] method to establish a connection")
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,19 @@ internal class CmdCommandPerformer(
/**
* Be aware it's a synchronous method
*/
fun perform(command: String): CommandResult {
fun perform(command: String, arguments: List<String> = emptyList()): CommandResult {
val serviceInfo = "The command was executed on desktop=$desktopName"
val process = Runtime.getRuntime().exec(command)
val process = if (arguments.isEmpty()) {
Runtime.getRuntime().exec(command)
} else {
val fullCommand = buildList {
add(command)
addAll(arguments)
}.toTypedArray()

Runtime.getRuntime().exec(fullCommand)
}

try {
if (process.waitFor(EXECUTION_TIMEOUT_SECONDS, TimeUnit.SECONDS)) {
val exitCode = process.exitValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ internal class CommandExecutorImpl(

override fun execute(command: Command): CommandResult {
return when (command) {
is CmdCommand -> cmdCommandPerformer.perform(command.body)
is CmdCommand -> cmdCommandPerformer.perform(command.command, command.arguments)

is AdbCommand -> {
val adbCommand = "$adbPath ${ adbServerPort?.let { "-P $adbServerPort " } ?: "" }-s $deviceName ${command.body}"
logger.d("The created adbCommand=$adbCommand")
cmdCommandPerformer.perform(adbCommand)
val adbCommand = "$adbPath ${adbServerPort?.let { "-P $adbServerPort " } ?: ""}-s $deviceName ${command.command}"
logger.d("The created adbCommand=$adbCommand, arguments=${command.arguments}")
cmdCommandPerformer.perform(adbCommand, command.arguments)
}

else -> throw UnsupportedOperationException("The command=$command is unsupported command")
}
}
Expand Down
Binary file modified artifacts/adbserver-desktop.jar
Binary file not shown.
8 changes: 8 additions & 0 deletions docs/Wiki/Executing_adb_commands.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ Now the logs looks like:
2020-09-10 12:24:27.427 10349-10378/com.kaspersky.kaspressample I/KASPRESSO_ADBSERVER: The result of command=AdbCommand(body=shell su 0 svc data disable) => CommandResult(status=SUCCESS, description=exitCode=0, message=, serviceInfo=The command was executed on desktop=Desktop-30548)
```

## Complex commands
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The description is unclear for people who don't have the task context.
First of all, provide examples to show the difference between the two methods.

Another point is the existence of two methods that are similar to each other but their implementations are different in terms of tokenization and etc, is not great. This case may confuse devs. I think we should deprecate the first method with a very detailed explanation. I doubt that we will remove the first method but we'll give a clear picture for devs about which method they need to use.

There're two kinds of ADB server methods signatures: `preformCmd(vararg commands: String)` and `performsCmd(command: String, arguments: List<String>)`. The first executes multiple
commands one by one. The latter allows you to gain more control over the commands parsing - ADB server would execute commands "as is" without trying to split command into tokens. This allows you to execute
commands with whitespaces in their arguments and use piping. Example:
```kotlin
adbServer.performCmd("bash", arguments = listOf("-c", "grep adb \"~/Documents/test file.txt\" > \"~/Documents/out file.txt\""))
```

## Development
The source code of AdbServer is available in [adb-server](https://github.com/KasperskyLab/Kaspresso/tree/master/adb-server) module. <br>
If you want to build `adbserver-desktop.jar` manually, just execute `./gradlew :adb-server:adbserver-desktop:assemble`.
8 changes: 8 additions & 0 deletions docs/Wiki/Executing_adb_commands.ru.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,14 @@ class DeviceNetworkSampleTest: TestCase(
2020-09-10 12:24:27.427 10349-10378/com.kaspersky.kaspressample I/KASPRESSO_ADBSERVER: The result of command=AdbCommand(body=shell su 0 svc data disable) => CommandResult(status=SUCCESS, description=exitCode=0, message=, serviceInfo=The command was executed on desktop=Desktop-30548)
```

## Сложные команды
Есть 2 типа сигнатур методов ADB server'а: `preformCmd(vararg commands: String)` и `performsCmd(command: String, arguments: List<String>)`. Первый выполняет несколько
команд одна за одной. Последний дает вам контроль над тем, как происходит парсинг комманды - ADB server выполнит их в таком виде, в каком вы их передали, не пытаясь разбить их на токены. Это позволяет
выполнять команды с пробелами в аргументах и использовать пайпинг. Пример:
```kotlin
adbServer.performCmd("bash", arguments = listOf("-c", "grep adb \"~/Documents/test file.txt\" > \"~/Documents/out file.txt\""))
```

## Разработка
Исходный код AdbServer доступен в модуле [adb-server](https://github.com/KasperskyLab/Kaspresso/ru/tree/master/adb-server). <br>
Если вы хотите собрать `adbserver-desktop.jar` вручную, просто выполните `./gradlew :adb-server:adbserver-desktop:assemble`.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ interface AdbServer {
*/
fun performCmd(vararg commands: String): List<String>

/**
* Performs shell commands blocking current thread. Allows more control over how arguments are parsed.
matzuk marked this conversation as resolved.
Show resolved Hide resolved
* Each list element is used as is. Refer to the https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please unify your comments. Somewhere you write class and method name, somewhere you provide a link.

*
* Please be aware! If any command that is in @param commands failed then AdbServerException will be thrown
*
* Required Permissions: INTERNET.
*
* @param commands commands to execute.
* @throws AdbServerException if a result status of any command in @param commands is Failed
* @return list of answers of commands' execution
*/
fun performCmd(command: String, arguments: List<String>): String

/**
* Performs adb commands blocking current thread.
* Please be aware! If any command that is in @param commands failed then AdbServerException will be thrown
Expand All @@ -38,6 +52,20 @@ interface AdbServer {
*/
fun performAdb(vararg commands: String): List<String>

/**
* Performs adb commands blocking current thread. Allows more control over how arguments are parsed.
* Each list element is used as is. Refer to the https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html.
*
* Please be aware! If any command that is in @param commands failed then AdbServerException will be thrown
*
* Required Permissions: INTERNET.
*
* @param commands commands to execute.
* @throws AdbServerException if a result status of any command in @param commands is Failed
* @return list of answers of commands' execution
*/
fun performAdb(command: String, arguments: List<String>): String

/**
* Performs shell commands blocking current thread.
* Please be aware! If any command that is in @param commands failed then AdbServerException will be thrown
Expand All @@ -50,6 +78,20 @@ interface AdbServer {
*/
fun performShell(vararg commands: String): List<String>

/**
* Performs shell commands blocking current thread. Allows more control over how arguments are parsed.
* Each list element is used as is. Refer to the https://docs.oracle.com/javase/8/docs/api/java/lang/ProcessBuilder.html.
*
* Please be aware! If any command that is in @param commands failed then AdbServerException will be thrown
*
* Required Permissions: INTERNET.
*
* @param commands commands to execute.
* @throws AdbServerException if a result status of any command in @param commands is Failed
* @return list of answers of commands' execution
*/
fun performShell(command: String, arguments: List<String>): String

/**
* Disconnect from AdbServer.
* The method is called by Kaspresso after each test.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,58 +30,36 @@ class AdbServerImpl(
return AdbTerminal
}

/**
* Executes shell commands blocking current thread.
* Please be aware! If any command that is in @param commands failed then AdbServerException will be thrown
*
* Required Permissions: INTERNET.
*
* @param commands commands to execute.
* @throws AdbServerException if a result status of any command in @param commands is Failed
* @return list of answers of commands' execution
*/
override fun performCmd(vararg commands: String): List<String> {
return perform(commands) {
adbTerminal.executeCmd(it)
}
}

/**
* Performs adb commands blocking current thread.
* Please be aware! If any command that is in @param commands failed then AdbServerException will be thrown
*
* Required Permissions: INTERNET.
*
* @param commands commands to execute.
* @throws AdbServerException if a result status of any command in @param commands is Failed
* @return list of answers of commands' execution
*/
override fun performAdb(vararg commands: String): List<String> {
return perform(commands) {
adbTerminal.executeAdb(it)
}
}

/**
* Performs shell commands blocking current thread.
* Please be aware! If any command that is in @param commands failed then AdbServerException will be thrown
*
* Required Permissions: INTERNET.
*
* @param commands commands to execute.
* @throws AdbServerException if a result status of any command in @param commands is Failed
* @return list of answers of commands' execution
*/
override fun performShell(vararg commands: String): List<String> {
return perform(commands) {
adbTerminal.executeAdb("shell $it")
}
}

/**
* Disconnect from AdbServer.
* The method is called by Kaspresso after each test.
*/
override fun performCmd(command: String, arguments: List<String>): String {
return performComplex(command, arguments, adbTerminal::executeCmd)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's better to use the single perform method. Don't see why we need performComplex method.

}

override fun performAdb(command: String, arguments: List<String>): String {
return performComplex(command, arguments, adbTerminal::executeAdb)
}

override fun performShell(command: String, arguments: List<String>): String {
return performComplex("shell $command", arguments, adbTerminal::executeAdb)
}

override fun disconnectServer() {
if (connected) {
adbTerminal.disconnect()
Expand All @@ -95,16 +73,27 @@ class AdbServerImpl(
logger.i("AdbServer. The command to execute=$command")
}
.map { command -> command to executor.invoke(command) }
.onEach { (command, result) ->
logger.i("AdbServer. The command=$command was performed with result=$result")
}
.onEach { (command, result) ->
if (result.status == ExecutorResultStatus.FAILURE) {
throw AdbServerException("AdbServer. The command=$command was performed with failed result=$result")
}
if (result.status == ExecutorResultStatus.TIMEOUT) {
throw AdbServerException(
"""
.onEach { (command, result) -> logCommandResult(command, result) }
.map { (_, result) -> result.description }
.toList()
}

private fun performComplex(command: String, arguments: List<String>, executor: (String, List<String>) -> CommandResult): String {
logger.i("AdbServer. The complex command to execute=$command")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

arguments missed

val result = executor.invoke(command, arguments)
logCommandResult(command, result)

return result.description
}

private fun logCommandResult(command: String, result: CommandResult, arguments: List<String> = emptyList()) {
logger.i("AdbServer. The command=$command with arguments=$arguments was performed with result=$result")
if (result.status == ExecutorResultStatus.FAILURE) {
throw AdbServerException("AdbServer. The command=$command was performed with failed result=$result")
}
if (result.status == ExecutorResultStatus.TIMEOUT) {
throw AdbServerException(
"""

AdbServer. The command=$command was performed with timeout exception.
There are two possible reasons:
Expand All @@ -122,10 +111,7 @@ class AdbServerImpl(
c. Start 'adbserver-desktop.jar' with the command in Terminal - 'java -jar /Users/yuri.gagarin/Desktop/adbserver-desktop.jar

""".trimIndent()
)
}
}
.map { (_, result) -> result.description }
.toList()
)
}
}
}
Loading