From ecf5a605fb5e949d12bc159dcd3b628f8de453c6 Mon Sep 17 00:00:00 2001 From: Kris Heid Date: Tue, 2 Mar 2021 10:13:09 +0100 Subject: [PATCH] =?UTF-8?q?calculate=20the=20correct=20length=20of=20the?= =?UTF-8?q?=20command=20to=20be=20transmitted=20for=20special=20characters?= =?UTF-8?q?=20like=20=C3=A4,=C3=B6...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/se/vidstige/jadb/Transport.java | 2 +- .../test/integration/ExecuteCmdTests.java | 100 ++++++++++++++++++ 2 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 test/se/vidstige/jadb/test/integration/ExecuteCmdTests.java diff --git a/src/se/vidstige/jadb/Transport.java b/src/se/vidstige/jadb/Transport.java index 6ce363d..72a828c 100644 --- a/src/se/vidstige/jadb/Transport.java +++ b/src/se/vidstige/jadb/Transport.java @@ -51,7 +51,7 @@ class Transport implements Closeable { } private String getCommandLength(String command) { - return String.format("%04x", command.length()); + return String.format("%04x", command.getBytes().length); } public void send(String command) throws IOException { diff --git a/test/se/vidstige/jadb/test/integration/ExecuteCmdTests.java b/test/se/vidstige/jadb/test/integration/ExecuteCmdTests.java new file mode 100644 index 0000000..fb9a509 --- /dev/null +++ b/test/se/vidstige/jadb/test/integration/ExecuteCmdTests.java @@ -0,0 +1,100 @@ +package se.vidstige.jadb.test.integration; + +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import se.vidstige.jadb.JadbConnection; +import se.vidstige.jadb.JadbDevice; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collection; + +@RunWith(Parameterized.class) +public class ExecuteCmdTests { + private static JadbConnection jadb; + private static JadbDevice jadbDevice; + + @Parameterized.Parameter + public String input; + + + @Parameterized.Parameters(name="Test {index} input={0}") + public static Collection input() { + return Arrays.asList(new Object[]{ + "öäasd", + "asf dsa", + "sdf&g", + "sd& fg", + "da~f", + "asd'as", + "a¡f", + "asüd", + "adös tz", + "⾀", + "å", + "æ", + "{}"}); + } + + @BeforeClass + public static void connect() { + try { + jadb = new JadbConnection(); + jadb.getHostVersion(); + jadbDevice = jadb.getAnyDevice(); + } catch (Exception e) { + org.junit.Assume.assumeNoException(e); + } + } + + + @Test + public void testExecuteWithSpecialChars() throws Exception { + InputStream response = jadbDevice.execute("echo", input); + ResponseReader responseReader = new ResponseReader(response); + responseReader.start(); + responseReader.join(1000L); + String ret = responseReader.output; + //remove newline + ret = ret.replaceAll("\n$", ""); + Assert.assertEquals(input, ret); + } + + + private class ResponseReader extends Thread { + public String output; + private InputStream response; + + ResponseReader(InputStream response) { + super(); + this.response = response; + } + + @Override + public void run() { + try { + StringBuilder textBuilder = new StringBuilder(); + try (Reader reader = new BufferedReader(new InputStreamReader + (response, Charset.forName(StandardCharsets.UTF_8.name())))) { + int c = 0; + while ((c = reader.read()) != -1) { + textBuilder.append((char) c); + } + } + output = textBuilder.toString(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + +}