mirror of
https://github.com/revanced/jadb.git
synced 2025-05-22 19:08:52 +02:00
Code review changes
Adding the following changes: * Adding a unit test * Removing a readToStringFunction * Adding handler into AdbProtocolHandler to enable testing *
This commit is contained in:
parent
8a057bc856
commit
4dd168bb16
@ -1,13 +1,12 @@
|
|||||||
package se.vidstige.jadb;
|
package se.vidstige.jadb;
|
||||||
|
|
||||||
import se.vidstige.jadb.managers.Bash;
|
import se.vidstige.jadb.managers.Bash;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static se.vidstige.jadb.Util.inputStreamToString;
|
|
||||||
|
|
||||||
public class JadbDevice {
|
public class JadbDevice {
|
||||||
@SuppressWarnings("squid:S00115")
|
@SuppressWarnings("squid:S00115")
|
||||||
public enum State {
|
public enum State {
|
||||||
@ -159,11 +158,11 @@ public class JadbDevice {
|
|||||||
* @return success or failure
|
* @return success or failure
|
||||||
*/
|
*/
|
||||||
public boolean enableTcpip(int port) throws IOException, JadbException {
|
public boolean enableTcpip(int port) throws IOException, JadbException {
|
||||||
Transport transport = getTransport();
|
try (Transport transport = getTransport()) {
|
||||||
send(transport, String.format("tcpip:%d", port));
|
send(transport, String.format("tcpip:%d", port));
|
||||||
String expectedResult = String.format("restarting in TCP Mode: %d", port);
|
String expectedResult = String.format("restarting in TCP Mode: %d\n", port);
|
||||||
|
return transport.readString(expectedResult.length()).equals(expectedResult);
|
||||||
return inputStreamToString(transport.getInputStream()).equals(expectedResult);
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
|
public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
package se.vidstige.jadb;
|
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
public class Util {
|
|
||||||
/**
|
|
||||||
* Convert an input stream to string
|
|
||||||
*
|
|
||||||
* @param inputStream input stream
|
|
||||||
*
|
|
||||||
* @return string representation of the input stream
|
|
||||||
*
|
|
||||||
* @throws IOException if an error ocurrs reading the input stream
|
|
||||||
*/
|
|
||||||
public static String inputStreamToString(InputStream inputStream) throws IOException {
|
|
||||||
ByteArrayOutputStream result = new ByteArrayOutputStream();
|
|
||||||
byte[] buffer = new byte[1024];
|
|
||||||
int length;
|
|
||||||
while ((length = inputStream.read(buffer)) != -1) {
|
|
||||||
result.write(buffer, 0, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
return result.toString(StandardCharsets.UTF_8.name());
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,10 +3,7 @@ package se.vidstige.jadb.server;
|
|||||||
import se.vidstige.jadb.JadbException;
|
import se.vidstige.jadb.JadbException;
|
||||||
import se.vidstige.jadb.RemoteFile;
|
import se.vidstige.jadb.RemoteFile;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.*;
|
||||||
import java.io.DataInput;
|
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -20,6 +17,7 @@ public interface AdbDeviceResponder {
|
|||||||
void filePulled(RemoteFile path, ByteArrayOutputStream buffer) throws JadbException, IOException;
|
void filePulled(RemoteFile path, ByteArrayOutputStream buffer) throws JadbException, IOException;
|
||||||
|
|
||||||
void shell(String command, DataOutputStream stdout, DataInput stdin) throws IOException;
|
void shell(String command, DataOutputStream stdout, DataInput stdin) throws IOException;
|
||||||
|
void enableIpCommand(String ip, DataOutputStream outputStream) throws IOException;
|
||||||
|
|
||||||
List<RemoteFile> list(String path) throws IOException;
|
List<RemoteFile> list(String path) throws IOException;
|
||||||
}
|
}
|
||||||
|
@ -70,6 +70,8 @@ class AdbProtocolHandler implements Runnable {
|
|||||||
hostGetState(output);
|
hostGetState(output);
|
||||||
} else if (command.startsWith("host-serial:")) {
|
} else if (command.startsWith("host-serial:")) {
|
||||||
hostSerial(output, command);
|
hostSerial(output, command);
|
||||||
|
} else if (command.startsWith("tcpip:")) {
|
||||||
|
handleTcpip(output, command);
|
||||||
} else {
|
} else {
|
||||||
throw new ProtocolException("Unknown command: " + command);
|
throw new ProtocolException("Unknown command: " + command);
|
||||||
}
|
}
|
||||||
@ -81,6 +83,11 @@ class AdbProtocolHandler implements Runnable {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void handleTcpip(DataOutputStream output, String command) throws IOException {
|
||||||
|
output.writeBytes("OKAY");
|
||||||
|
selected.enableIpCommand(command.substring("tcpip:".length()), output);
|
||||||
|
}
|
||||||
|
|
||||||
private void hostSerial(DataOutput output, String command) throws IOException {
|
private void hostSerial(DataOutput output, String command) throws IOException {
|
||||||
String[] strs = command.split(":",0);
|
String[] strs = command.split(":",0);
|
||||||
if (strs.length != 3) {
|
if (strs.length != 3) {
|
||||||
|
@ -1,15 +1,13 @@
|
|||||||
package se.vidstige.jadb.test.fakes;
|
package se.vidstige.jadb.test.fakes;
|
||||||
|
|
||||||
|
import com.sun.tools.doclets.standard.Standard;
|
||||||
import se.vidstige.jadb.JadbException;
|
import se.vidstige.jadb.JadbException;
|
||||||
import se.vidstige.jadb.RemoteFile;
|
import se.vidstige.jadb.RemoteFile;
|
||||||
import se.vidstige.jadb.server.AdbDeviceResponder;
|
import se.vidstige.jadb.server.AdbDeviceResponder;
|
||||||
import se.vidstige.jadb.server.AdbResponder;
|
import se.vidstige.jadb.server.AdbResponder;
|
||||||
import se.vidstige.jadb.server.AdbServer;
|
import se.vidstige.jadb.server.AdbServer;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.*;
|
||||||
import java.io.DataInput;
|
|
||||||
import java.io.DataOutputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.ProtocolException;
|
import java.net.ProtocolException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -87,6 +85,10 @@ public class FakeAdbServer implements AdbResponder {
|
|||||||
return findBySerial(serial).expectShell(commands);
|
return findBySerial(serial).expectShell(commands);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DeviceResponder.TcpIpException expectTcpip(String serial, String port) {
|
||||||
|
return findBySerial(serial).expectTcpip(port);
|
||||||
|
}
|
||||||
|
|
||||||
public DeviceResponder.ListExpectation expectList(String serial, String remotePath) {
|
public DeviceResponder.ListExpectation expectList(String serial, String remotePath) {
|
||||||
return findBySerial(serial).expectList(remotePath);
|
return findBySerial(serial).expectList(remotePath);
|
||||||
}
|
}
|
||||||
@ -102,6 +104,7 @@ public class FakeAdbServer implements AdbResponder {
|
|||||||
private List<FileExpectation> fileExpectations = new ArrayList<>();
|
private List<FileExpectation> fileExpectations = new ArrayList<>();
|
||||||
private List<ShellExpectation> shellExpectations = new ArrayList<>();
|
private List<ShellExpectation> shellExpectations = new ArrayList<>();
|
||||||
private List<ListExpectation> listExpectations = new ArrayList<>();
|
private List<ListExpectation> listExpectations = new ArrayList<>();
|
||||||
|
private List<TcpIpException> tcpipExpectations = new ArrayList<>();
|
||||||
|
|
||||||
private DeviceResponder(String serial, String type) {
|
private DeviceResponder(String serial, String type) {
|
||||||
this.serial = serial;
|
this.serial = serial;
|
||||||
@ -156,6 +159,21 @@ public class FakeAdbServer implements AdbResponder {
|
|||||||
throw new ProtocolException("Unexpected shell to device " + serial + ": " + command);
|
throw new ProtocolException("Unexpected shell to device " + serial + ": " + command);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enableIpCommand(String port, DataOutputStream outputStream) throws IOException {
|
||||||
|
for (TcpIpException expectation : tcpipExpectations) {
|
||||||
|
if (expectation.matches(port)) {
|
||||||
|
tcpipExpectations.remove(expectation);
|
||||||
|
outputStream.write(String.format("restarting in TCP Mode: %s\n", port).getBytes(StandardCharsets.UTF_8));
|
||||||
|
outputStream.flush();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new ProtocolException("Unexpected tcpip to device " + serial + ": (port) " + port);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<RemoteFile> list(String path) throws IOException {
|
public List<RemoteFile> list(String path) throws IOException {
|
||||||
for (ListExpectation le : listExpectations) {
|
for (ListExpectation le : listExpectations) {
|
||||||
@ -173,6 +191,32 @@ public class FakeAdbServer implements AdbResponder {
|
|||||||
org.junit.Assert.assertEquals(0, listExpectations.size());
|
org.junit.Assert.assertEquals(0, listExpectations.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static class TcpIpException implements ExpectationBuilder {
|
||||||
|
|
||||||
|
private String port;
|
||||||
|
|
||||||
|
public TcpIpException(final String port) {
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean matches(String cmd) {
|
||||||
|
return cmd.equals(port);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void failWith(String message) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void withContent(byte[] content) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void withContent(String content) {
|
||||||
|
}
|
||||||
|
}
|
||||||
private static class FileExpectation implements ExpectationBuilder {
|
private static class FileExpectation implements ExpectationBuilder {
|
||||||
private final RemoteFile path;
|
private final RemoteFile path;
|
||||||
private byte[] content;
|
private byte[] content;
|
||||||
@ -316,5 +360,11 @@ public class FakeAdbServer implements AdbResponder {
|
|||||||
listExpectations.add(expectation);
|
listExpectations.add(expectation);
|
||||||
return expectation;
|
return expectation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TcpIpException expectTcpip(String port) {
|
||||||
|
TcpIpException expectation = new TcpIpException(port);
|
||||||
|
tcpipExpectations.add(expectation);
|
||||||
|
return expectation;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,7 @@ import se.vidstige.jadb.test.fakes.FakeAdbServer;
|
|||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
@ -104,6 +105,14 @@ public class MockedTestCases {
|
|||||||
device.executeShell("ls", "-l");
|
device.executeShell("ls", "-l");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testExecuteEnableTcpip() throws IOException, JadbException {
|
||||||
|
server.add("serial-123");
|
||||||
|
server.expectTcpip("serial-123", "5555");
|
||||||
|
JadbDevice device = connection.getDevices().get(0);
|
||||||
|
device.enableTcpip();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testExecuteShellQuotesWhitespace() throws Exception {
|
public void testExecuteShellQuotesWhitespace() throws Exception {
|
||||||
server.add("serial-123");
|
server.add("serial-123");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user