Skip to content

Commit

Permalink
test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kai-morich committed Jun 2, 2024
1 parent b794092 commit b6e1833
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 2 deletions.
5 changes: 5 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
codecov:
max_report_age: off
require_ci_to_pass: no
notify:
wait_for_ci: no
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)))
;
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

public class UsbUtils {

private UsbUtils() {
}

public static ArrayList<byte[]> getDescriptors(UsbDeviceConnection connection) {
ArrayList<byte[]> descriptors = new ArrayList<>();
byte[] rawDescriptors = connection.getRawDescriptors();
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}

}

0 comments on commit b6e1833

Please sign in to comment.