mirror of
https://github.com/revanced/jadb.git
synced 2024-11-19 10:39:23 +01:00
Adding mocked unit test for pushing file.
This commit is contained in:
parent
25745857b7
commit
0e94c45791
@ -6,16 +6,20 @@ import java.nio.charset.Charset;
|
||||
/**
|
||||
* Created by vidstige on 2014-03-19.
|
||||
*/
|
||||
class SyncTransport {
|
||||
public class SyncTransport {
|
||||
|
||||
private final DataOutputStream output;
|
||||
private final DataInputStream input;
|
||||
private final DataOutput output;
|
||||
private final DataInput input;
|
||||
|
||||
public SyncTransport(OutputStream outputStream, InputStream inputStream) {
|
||||
output = new DataOutputStream(outputStream);
|
||||
input = new DataInputStream(inputStream);
|
||||
}
|
||||
|
||||
public SyncTransport(DataOutput outputStream, DataInput inputStream) {
|
||||
output = outputStream;
|
||||
input = inputStream;
|
||||
}
|
||||
public void send(String syncCommand, String name) throws IOException {
|
||||
if (syncCommand.length() != 4) throw new IllegalArgumentException("sync commands must have length 4");
|
||||
output.writeBytes(syncCommand);
|
||||
@ -42,11 +46,11 @@ class SyncTransport {
|
||||
}
|
||||
}
|
||||
|
||||
private int readInt() throws IOException {
|
||||
public int readInt() throws IOException {
|
||||
return Integer.reverseBytes(input.readInt());
|
||||
}
|
||||
|
||||
private String readString(int length) throws IOException {
|
||||
public String readString(int length) throws IOException {
|
||||
byte[] buffer = new byte[length];
|
||||
input.readFully(buffer);
|
||||
return new String(buffer, Charset.forName("utf-8"));
|
||||
@ -78,7 +82,8 @@ class SyncTransport {
|
||||
throw new JadbException(readString(n));
|
||||
}
|
||||
if (!"DATA".equals(id)) return -1;
|
||||
return input.read(buffer, 0, n);
|
||||
input.readFully(buffer, 0, n);
|
||||
return n;
|
||||
}
|
||||
|
||||
public void sendStream(InputStream in) throws IOException {
|
||||
|
@ -1,5 +1,8 @@
|
||||
package se.vidstige.jadb.server;
|
||||
|
||||
import se.vidstige.jadb.JadbException;
|
||||
import se.vidstige.jadb.SyncTransport;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.ProtocolException;
|
||||
import java.net.Socket;
|
||||
@ -34,7 +37,7 @@ public class AdbProtocolHandler implements Runnable {
|
||||
|
||||
private void runServer() throws IOException {
|
||||
DataInput input = new DataInputStream(socket.getInputStream());
|
||||
OutputStreamWriter output = new OutputStreamWriter(socket.getOutputStream());
|
||||
DataOutputStream output = new DataOutputStream(socket.getOutputStream());
|
||||
|
||||
while (true)
|
||||
{
|
||||
@ -52,13 +55,13 @@ public class AdbProtocolHandler implements Runnable {
|
||||
try
|
||||
{
|
||||
if ("host:version".equals(command)) {
|
||||
output.write("OKAY");
|
||||
output.writeBytes("OKAY");
|
||||
send(output, String.format("%04x", responder.getVersion()));
|
||||
}
|
||||
else if ("host:transport-any".equals(command))
|
||||
{
|
||||
// TODO: Check so that exactly one device is selected.
|
||||
output.write("OKAY");
|
||||
output.writeBytes("OKAY");
|
||||
}
|
||||
else if ("host:devices".equals(command)) {
|
||||
ByteArrayOutputStream tmp = new ByteArrayOutputStream();
|
||||
@ -67,32 +70,64 @@ public class AdbProtocolHandler implements Runnable {
|
||||
{
|
||||
writer.writeBytes(d.getSerial() + "\t" + d.getType() + "\n");
|
||||
}
|
||||
output.write("OKAY");
|
||||
output.writeBytes("OKAY");
|
||||
send(output, new String(tmp.toByteArray(), Charset.forName("utf-8")));
|
||||
}
|
||||
else if (command.startsWith("host:transport:"))
|
||||
{
|
||||
String serial = command.substring("host:transport:".length());
|
||||
findDevice(serial);
|
||||
output.writeBytes("OKAY");
|
||||
}
|
||||
else if ("sync:".equals(command)) {
|
||||
output.writeBytes("OKAY");
|
||||
sync(output, input);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new ProtocolException("Unknown command: " + command);
|
||||
}
|
||||
} catch (ProtocolException e) {
|
||||
output.write("FAIL");
|
||||
output.writeBytes("FAIL");
|
||||
send(output, e.getMessage());
|
||||
} catch (JadbException e) {
|
||||
output.writeBytes("FAIL");
|
||||
send(output, e.getMessage());
|
||||
}
|
||||
output.flush();
|
||||
}
|
||||
}
|
||||
|
||||
private String getCommandLength(String command) {
|
||||
|
||||
private int readInt(DataInput input) throws IOException {
|
||||
return Integer.reverseBytes(input.readInt());
|
||||
}
|
||||
|
||||
private String readString(DataInput input, int length) throws IOException {
|
||||
byte[] responseBuffer = new byte[length];
|
||||
input.readFully(responseBuffer);
|
||||
return new String(responseBuffer, Charset.forName("utf-8"));
|
||||
}
|
||||
|
||||
private void sync(DataOutput output, DataInput input) throws IOException, JadbException {
|
||||
String id = readString(input, 4);
|
||||
int length = readInt(input);
|
||||
if ("SEND".equals(id))
|
||||
{
|
||||
String remotePath = readString(input, length);
|
||||
SyncTransport transport = new SyncTransport(output, input);
|
||||
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
||||
transport.readChunksTo(buffer);
|
||||
transport.sendStatus("OKAY", 0); // 0 = ignored
|
||||
}
|
||||
else throw new JadbException("Unknown sync id " + id);
|
||||
}
|
||||
|
||||
private String getCommandLength(String command) {
|
||||
return String.format("%04x", command.length());
|
||||
}
|
||||
|
||||
public void send(OutputStreamWriter writer, String response) throws IOException {
|
||||
writer.write(getCommandLength(response));
|
||||
writer.write(response);
|
||||
public void send(DataOutput writer, String response) throws IOException {
|
||||
writer.writeBytes(getCommandLength(response));
|
||||
writer.writeBytes(response);
|
||||
}
|
||||
}
|
||||
}
|
@ -6,8 +6,13 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import se.vidstige.jadb.JadbConnection;
|
||||
import se.vidstige.jadb.JadbDevice;
|
||||
import se.vidstige.jadb.RemoteFile;
|
||||
import se.vidstige.jadb.test.fakes.FakeAdbServer;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.List;
|
||||
|
||||
public class MockedTestCases {
|
||||
@ -41,4 +46,17 @@ public class MockedTestCases {
|
||||
Assert.assertEquals(0, devices.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPushFile() throws Exception {
|
||||
server.add("serial-123");
|
||||
JadbDevice device = connection.getDevices().get(0);
|
||||
ByteArrayInputStream fileContents = new ByteArrayInputStream("abc".getBytes());
|
||||
device.push(fileContents, parseDate("1981-08-25 13:37"), 0666, new RemoteFile("/remote/path/abc.txt"));
|
||||
}
|
||||
|
||||
private long parseDate(String date) throws ParseException {
|
||||
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
return dateFormat.parse(date).getTime();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user