From b6e18332708487459ee285a8e3c643ea9d9a1ccc Mon Sep 17 00:00:00 2001 From: Kai Morich Date: Sun, 2 Jun 2024 10:04:19 +0200 Subject: [PATCH] test coverage --- codecov.yml | 5 +++ .../hoho/android/usbserial/DeviceTest.java | 44 ++++++++++++++++++- .../hoho/android/usbserial/util/HexDump.java | 3 ++ .../hoho/android/usbserial/util/UsbUtils.java | 3 ++ .../android/usbserial/util/HexDumpText.java | 44 +++++++++++++++++++ 5 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 codecov.yml create mode 100644 usbSerialForAndroid/src/test/java/com/hoho/android/usbserial/util/HexDumpText.java diff --git a/codecov.yml b/codecov.yml new file mode 100644 index 00000000..6572271f --- /dev/null +++ b/codecov.yml @@ -0,0 +1,5 @@ +codecov: + max_report_age: off + require_ci_to_pass: no + notify: + wait_for_ci: no \ No newline at end of file diff --git a/usbSerialForAndroid/src/androidTest/java/com/hoho/android/usbserial/DeviceTest.java b/usbSerialForAndroid/src/androidTest/java/com/hoho/android/usbserial/DeviceTest.java index 8e43c648..49f39dc5 100644 --- a/usbSerialForAndroid/src/androidTest/java/com/hoho/android/usbserial/DeviceTest.java +++ b/usbSerialForAndroid/src/androidTest/java/com/hoho/android/usbserial/DeviceTest.java @@ -235,6 +235,12 @@ private void purgeWriteBuffer(int timeout) throws Exception { @Test public void openClose() throws Exception { + try { + usb.serialPort.open(null); + fail("null connection error expected"); + } catch (IllegalArgumentException ignored) { + } + usb.open(); telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); usb.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); @@ -1528,10 +1534,18 @@ public void writeAsync() throws Exception { // with internal SerialTimeoutException TestBuffer tbuf = new TestBuffer(usb.writeBufferSize + 2*usb.writePacketSize); + byte[] pbuf1 = new byte[tbuf.buf.length - 4]; + byte[] pbuf2 = new byte[1]; + System.arraycopy(tbuf.buf, 0,pbuf1, 0, pbuf1.length); usb.ioManager.setWriteTimeout(20); // tbuf len >= 128, needs 133msec @ 9600 baud usb.setParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE); telnet.setParameters(9600, 8, 1, UsbSerialPort.PARITY_NONE); - usb.ioManager.writeAsync(tbuf.buf); + usb.ioManager.writeAsync(pbuf1); + for(int i = pbuf1.length; i < tbuf.buf.length; i++) { + Thread.sleep(20); + pbuf2[0] = tbuf.buf[i]; + usb.ioManager.writeAsync(pbuf2); + } while(!tbuf.testRead(telnet.read(-1))) ; } @@ -1577,7 +1591,7 @@ public void readTimeout() throws Exception { telnet.setParameters(19200, 8, 1, UsbSerialPort.PARITY_NONE); int longTimeout = 1000; - int shortTimeout = 10; + int shortTimeout = 20; time = System.currentTimeMillis(); len = usb.serialPort.read(readBuf, shortTimeout); assertEquals(0, len); @@ -1728,6 +1742,8 @@ public void wrongDriver() throws Exception { wrongSerialPort.open(wrongDeviceConnection); } catch (IOException ignored) { } + assertEquals(usb.serialDriver.getDevice(), wrongSerialDriver.getDevice()); + assertEquals(wrongSerialDriver, wrongSerialPort.getDriver()); assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setParameters(9200, 8, 1, 0)); assertEquals(EnumSet.noneOf(ControlLine.class), wrongSerialPort.getSupportedControlLines()); try { @@ -1743,6 +1759,8 @@ public void wrongDriver() throws Exception { wrongSerialPort.open(wrongDeviceConnection); } catch (IOException ignored) { } + assertEquals(usb.serialDriver.getDevice(), wrongSerialDriver.getDevice()); + assertEquals(wrongSerialDriver, wrongSerialPort.getDriver()); assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setParameters(9200, 8, 1, 0)); assertEquals(EnumSet.noneOf(ControlLine.class), wrongSerialPort.getSupportedControlLines()); try { @@ -2093,7 +2111,9 @@ public void deviceConnection() throws Exception { public void commonMethods() throws Exception { String s; assertNotNull(usb.serialPort.getDriver()); + assertEquals(usb.serialDriver, usb.serialPort.getDriver()); assertNotNull(usb.serialPort.getDevice()); + assertEquals(usb.serialDriver.getDevice(), usb.serialPort.getDevice()); assertEquals(test_device_port, usb.serialPort.getPortNumber()); s = usb.serialDriver.toString(); assertNotEquals(0, s.length()); @@ -2124,6 +2144,26 @@ public void commonMethods() throws Exception { usb.serialPort.read(buffer, UsbWrapper.USB_READ_WAIT); fail("read buffer to small expected"); } catch(IllegalArgumentException ignored) {} + try { + byte[] buffer = new byte[1]; + usb.serialPort.read(buffer, 0, UsbWrapper.USB_READ_WAIT); + fail("read length to small expected"); + } catch(IllegalArgumentException ignored) {} + + // use driver that does not override base class + UsbSerialDriver wrongSerialDriver = new ChromeCcdSerialDriver(usb.serialDriver.getDevice()); + UsbSerialPort wrongSerialPort = wrongSerialDriver.getPorts().get(0); + assertThrows(UnsupportedOperationException.class, wrongSerialPort::getCD); + assertThrows(UnsupportedOperationException.class, wrongSerialPort::getCTS); + assertThrows(UnsupportedOperationException.class, wrongSerialPort::getDSR); + assertThrows(UnsupportedOperationException.class, wrongSerialPort::getDTR); + assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setDTR(true)); + assertThrows(UnsupportedOperationException.class, wrongSerialPort::getRI); + assertThrows(UnsupportedOperationException.class, wrongSerialPort::getRTS); + assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setRTS(true)); + assertThrows(UnsupportedOperationException.class, wrongSerialPort::getControlLines); + assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.purgeHwBuffers(true, true)); + assertThrows(UnsupportedOperationException.class, () -> wrongSerialPort.setBreak(true)); } @Test diff --git a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/util/HexDump.java b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/util/HexDump.java index ac95c832..ddf8f0f5 100644 --- a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/util/HexDump.java +++ b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/util/HexDump.java @@ -27,6 +27,9 @@ public class HexDump { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + private HexDump() { + } + public static String dumpHexString(byte[] array) { return dumpHexString(array, 0, array.length); } diff --git a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/util/UsbUtils.java b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/util/UsbUtils.java index 5802b9e1..de567980 100644 --- a/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/util/UsbUtils.java +++ b/usbSerialForAndroid/src/main/java/com/hoho/android/usbserial/util/UsbUtils.java @@ -6,6 +6,9 @@ public class UsbUtils { + private UsbUtils() { + } + public static ArrayList getDescriptors(UsbDeviceConnection connection) { ArrayList descriptors = new ArrayList<>(); byte[] rawDescriptors = connection.getRawDescriptors(); diff --git a/usbSerialForAndroid/src/test/java/com/hoho/android/usbserial/util/HexDumpText.java b/usbSerialForAndroid/src/test/java/com/hoho/android/usbserial/util/HexDumpText.java new file mode 100644 index 00000000..19e06674 --- /dev/null +++ b/usbSerialForAndroid/src/test/java/com/hoho/android/usbserial/util/HexDumpText.java @@ -0,0 +1,44 @@ +package com.hoho.android.usbserial.util; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +import org.junit.Test; + +import java.security.InvalidParameterException; + +public class HexDumpText { + + @Test + public void toByteArray() throws Exception { + assertThat(HexDump.toByteArray((byte)0x4a), equalTo(new byte[]{ 0x4A})); + assertThat(HexDump.toByteArray((short)0x4a5b), equalTo(new byte[]{ 0x4A, 0x5B})); + assertThat(HexDump.toByteArray((int)0x4a5b6c7d), equalTo(new byte[]{ 0x4A, 0x5B, 0x6C, 0x7D})); + } + + @Test + public void toHexString() throws Exception { + assertEquals("4A", HexDump.toHexString((byte)0x4a)); + assertEquals("4A 5B", HexDump.toHexString((short)0x4a5b)); + assertEquals("4A 5B 6C 7D", HexDump.toHexString((int)0x4a5b6c7d)); + assertEquals("4A 5B 6C 7D", HexDump.toHexString(new byte[]{ 0x4A, 0x5B, 0x6C, 0x7D})); + assertEquals("5B 6C", HexDump.toHexString(new byte[]{ 0x4A, 0x5B, 0x6C, 0x7D}, 1, 2)); + } + + @Test + public void dumpHexString() throws Exception { + assertEquals("10 31 32 33 34 35 36 37 .1234567\n18 39 .9", HexDump.dumpHexString(new byte[]{ 0x10, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x18, 0x39})); + assertEquals("31 32 12", HexDump.dumpHexString(new byte[]{ 0x30, 0x31, 0x32, 0x33}, 1, 2)); + } + + @Test + public void toByte() throws Exception { + assertThat(HexDump.hexStringToByteArray("4a 5B-6c\n7d"), equalTo(new byte[]{ 0x4A, 0x5B, 0x6C, 0x7D})); + assertThrows(InvalidParameterException.class, () -> HexDump.hexStringToByteArray("3 ")); + assertThrows(InvalidParameterException.class, () -> HexDump.hexStringToByteArray("3z")); + assertThrows(InvalidParameterException.class, () -> HexDump.hexStringToByteArray("3Z")); + } + +}