From 0e94c457917fa57081b178183b7620a3bd734934 Mon Sep 17 00:00:00 2001 From: Samuel Carlsson Date: Tue, 25 Mar 2014 15:46:54 +0100 Subject: [PATCH] Adding mocked unit test for pushing file. --- src/se/vidstige/jadb/SyncTransport.java | 17 ++++-- .../jadb/server/AdbProtocolHandler.java | 57 +++++++++++++++---- .../vidstige/jadb/test/MockedTestCases.java | 18 ++++++ 3 files changed, 75 insertions(+), 17 deletions(-) diff --git a/src/se/vidstige/jadb/SyncTransport.java b/src/se/vidstige/jadb/SyncTransport.java index bb72989..a9680ee 100644 --- a/src/se/vidstige/jadb/SyncTransport.java +++ b/src/se/vidstige/jadb/SyncTransport.java @@ -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 { diff --git a/src/se/vidstige/jadb/server/AdbProtocolHandler.java b/src/se/vidstige/jadb/server/AdbProtocolHandler.java index 18fd81f..29e7091 100644 --- a/src/se/vidstige/jadb/server/AdbProtocolHandler.java +++ b/src/se/vidstige/jadb/server/AdbProtocolHandler.java @@ -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); } -} +} \ No newline at end of file diff --git a/test/se/vidstige/jadb/test/MockedTestCases.java b/test/se/vidstige/jadb/test/MockedTestCases.java index a54aa3c..4046afc 100644 --- a/test/se/vidstige/jadb/test/MockedTestCases.java +++ b/test/se/vidstige/jadb/test/MockedTestCases.java @@ -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(); + } + }