From 423ccb3a89d90a82c97a69a3e6ed69c52705f599 Mon Sep 17 00:00:00 2001 From: Kris Heid Date: Tue, 2 Mar 2021 10:11:00 +0100 Subject: [PATCH 1/4] always quote command since not only whitespaces require quoting --- src/se/vidstige/jadb/managers/Bash.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/se/vidstige/jadb/managers/Bash.java b/src/se/vidstige/jadb/managers/Bash.java index 34fddbb..e8b3368 100644 --- a/src/se/vidstige/jadb/managers/Bash.java +++ b/src/se/vidstige/jadb/managers/Bash.java @@ -6,10 +6,6 @@ public class Bash { } public static String quote(String s) { - // Check that s contains no whitespace - if (s.matches("\\S+")) { - return s; - } return "'" + s.replace("'", "'\\''") + "'"; } } From ecf5a605fb5e949d12bc159dcd3b628f8de453c6 Mon Sep 17 00:00:00 2001 From: Kris Heid Date: Tue, 2 Mar 2021 10:13:09 +0100 Subject: [PATCH 2/4] =?UTF-8?q?calculate=20the=20correct=20length=20of=20t?= =?UTF-8?q?he=20command=20to=20be=20transmitted=20for=20special=20characte?= =?UTF-8?q?rs=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(); + } + } + } + +} From f4f41efbc3930ef0ec9ee1c2d61783416757d8b0 Mon Sep 17 00:00:00 2001 From: Kris Heid Date: Thu, 4 Mar 2021 13:27:04 +0100 Subject: [PATCH 3/4] add unit test to verify special character handling for commands --- test/se/vidstige/jadb/test/unit/MockedTestCases.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/se/vidstige/jadb/test/unit/MockedTestCases.java b/test/se/vidstige/jadb/test/unit/MockedTestCases.java index 87ec980..017acef 100644 --- a/test/se/vidstige/jadb/test/unit/MockedTestCases.java +++ b/test/se/vidstige/jadb/test/unit/MockedTestCases.java @@ -119,11 +119,15 @@ public class MockedTestCases { server.expectShell("serial-123", "echo 'tab\tstring'").returns("tab\tstring"); server.expectShell("serial-123", "echo 'newline1\nstring'").returns("newline1\nstring"); server.expectShell("serial-123", "echo 'newline2\r\nstring'").returns("newline2\r\nstring"); + server.expectShell("serial-123", "echo 'fuö äzpo'").returns("fuö äzpo"); + server.expectShell("serial-123", "echo 'h¡t]&poli'").returns("h¡t]&poli"); JadbDevice device = connection.getDevices().get(0); device.executeShell("ls", "space file"); device.executeShell("echo", "tab\tstring"); device.executeShell("echo", "newline1\nstring"); device.executeShell("echo", "newline2\r\nstring"); + device.executeShell("echo", "fuö äzpo"); + device.executeShell("echo", "h¡t]&poli"); } @Test From 597102ab83aabc421e703a4e8b73e81f731e57b4 Mon Sep 17 00:00:00 2001 From: Kris Heid Date: Thu, 4 Mar 2021 13:28:52 +0100 Subject: [PATCH 4/4] simplify special character handling intregration test with Stream.readAll() --- .../test/integration/ExecuteCmdTests.java | 38 +------------------ 1 file changed, 2 insertions(+), 36 deletions(-) diff --git a/test/se/vidstige/jadb/test/integration/ExecuteCmdTests.java b/test/se/vidstige/jadb/test/integration/ExecuteCmdTests.java index fb9a509..7443e4f 100644 --- a/test/se/vidstige/jadb/test/integration/ExecuteCmdTests.java +++ b/test/se/vidstige/jadb/test/integration/ExecuteCmdTests.java @@ -7,6 +7,7 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import se.vidstige.jadb.JadbConnection; import se.vidstige.jadb.JadbDevice; +import se.vidstige.jadb.Stream; import java.io.BufferedReader; import java.io.IOException; @@ -60,41 +61,6 @@ public class ExecuteCmdTests { @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); + Assert.assertEquals(input, Stream.readAll(response, StandardCharsets.UTF_8).replaceAll("\n$", "")); } - - - 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(); - } - } - } - }