Merge pull request #116 from dllewellyn/feature/add_ability_to_enable_tcpip

Feature/add ability to enable tcpip
This commit is contained in:
Samuel Carlsson 2019-01-10 07:47:47 +01:00 committed by GitHub
commit 6876adc9df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 5 deletions

View File

@ -24,6 +24,7 @@ public class JadbDevice {
private static final int DEFAULT_MODE = 0664;
private final String serial;
private final ITransportFactory transportFactory;
private static final int DEFAULT_TCPIP_PORT = 5555;
JadbDevice(String serial, ITransportFactory tFactory) {
this.serial = serial;
@ -147,6 +148,28 @@ public class JadbDevice {
return shellLine;
}
/**
* Enable tcpip on the default port (5555)
*
* @return success or failure
*/
public void enableAdbOverTCP() throws IOException, JadbException {
enableAdbOverTCP(DEFAULT_TCPIP_PORT);
}
/**
* Enable tcpip on a specific port
*
* @param port for the device to bind on
*
* @return success or failure
*/
public void enableAdbOverTCP(int port) throws IOException, JadbException {
try (Transport transport = getTransport()) {
send(transport, String.format("tcpip:%d", port));
}
}
public List<RemoteFile> list(String remotePath) throws IOException, JadbException {
try (Transport transport = getTransport()) {
SyncTransport sync = transport.startSync();

View File

@ -3,10 +3,7 @@ package se.vidstige.jadb.server;
import se.vidstige.jadb.JadbException;
import se.vidstige.jadb.RemoteFile;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.*;
import java.util.List;
/**
@ -20,6 +17,7 @@ public interface AdbDeviceResponder {
void filePulled(RemoteFile path, ByteArrayOutputStream buffer) throws JadbException, 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;
}

View File

@ -70,6 +70,8 @@ class AdbProtocolHandler implements Runnable {
hostGetState(output);
} else if (command.startsWith("host-serial:")) {
hostSerial(output, command);
} else if (command.startsWith("tcpip:")) {
handleTcpip(output, command);
} else {
throw new ProtocolException("Unknown command: " + command);
}
@ -81,6 +83,11 @@ class AdbProtocolHandler implements Runnable {
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 {
String[] strs = command.split(":",0);
if (strs.length != 3) {

View File

@ -87,6 +87,10 @@ public class FakeAdbServer implements AdbResponder {
return findBySerial(serial).expectShell(commands);
}
public void expectTcpip(String serial, Integer port) {
findBySerial(serial).expectTcpip(port);
}
public DeviceResponder.ListExpectation expectList(String serial, String remotePath) {
return findBySerial(serial).expectList(remotePath);
}
@ -102,6 +106,7 @@ public class FakeAdbServer implements AdbResponder {
private List<FileExpectation> fileExpectations = new ArrayList<>();
private List<ShellExpectation> shellExpectations = new ArrayList<>();
private List<ListExpectation> listExpectations = new ArrayList<>();
private List<Integer> tcpipExpectations = new ArrayList<>();
private DeviceResponder(String serial, String type) {
this.serial = serial;
@ -156,6 +161,19 @@ public class FakeAdbServer implements AdbResponder {
throw new ProtocolException("Unexpected shell to device " + serial + ": " + command);
}
@Override
public void enableIpCommand(String port, DataOutputStream outputStream) throws IOException {
for (Integer expectation : tcpipExpectations) {
if (expectation == Integer.parseInt(port)) {
tcpipExpectations.remove(expectation);
return;
}
}
throw new ProtocolException("Unexpected tcpip to device " + serial + ": (port) " + port);
}
@Override
public List<RemoteFile> list(String path) throws IOException {
for (ListExpectation le : listExpectations) {
@ -171,6 +189,7 @@ public class FakeAdbServer implements AdbResponder {
org.junit.Assert.assertEquals(0, fileExpectations.size());
org.junit.Assert.assertEquals(0, shellExpectations.size());
org.junit.Assert.assertEquals(0, listExpectations.size());
org.junit.Assert.assertEquals(0, tcpipExpectations.size());
}
private static class FileExpectation implements ExpectationBuilder {
@ -316,5 +335,9 @@ public class FakeAdbServer implements AdbResponder {
listExpectations.add(expectation);
return expectation;
}
public void expectTcpip(int port) {
tcpipExpectations.add(port);
}
}
}

View File

@ -12,11 +12,11 @@ import se.vidstige.jadb.test.fakes.FakeAdbServer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
public class MockedTestCases {
@ -104,6 +104,14 @@ public class MockedTestCases {
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.enableAdbOverTCP();
}
@Test
public void testExecuteShellQuotesWhitespace() throws Exception {
server.add("serial-123");