Skip to content

Commit

Permalink
Streamline configuration for test devices
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Jan 3, 2024
1 parent 51c54c2 commit f5d0f92
Show file tree
Hide file tree
Showing 17 changed files with 194 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,42 @@ static void precondition() {
@Test
void selectAlternateIntf_succeeds() {

testDevice.selectAlternateSetting(LOOPBACK_INTF_LOOPBACK, 1);
testDevice.selectAlternateSetting(config.interfaceNumber(), 1);

var altIntf = testDevice.getInterface(LOOPBACK_INTF_LOOPBACK).getCurrentAlternate();
var altIntf = testDevice.getInterface(config.interfaceNumber()).getCurrentAlternate();
assertNotNull(altIntf);
assertEquals(2, altIntf.getEndpoints().size());
assertEquals(0xff, altIntf.getClassCode());

testDevice.selectAlternateSetting(LOOPBACK_INTF_LOOPBACK, 0);
testDevice.selectAlternateSetting(config.interfaceNumber(), 0);
}

@Test
void selectInvalidAlternateIntf_fails() {
assertThrows(UsbException.class, () -> testDevice.selectAlternateSetting(1, 0));

assertThrows(UsbException.class, () -> testDevice.selectAlternateSetting(LOOPBACK_INTF_LOOPBACK, 2));
var interface_number = config.interfaceNumber();
assertThrows(UsbException.class, () -> testDevice.selectAlternateSetting(interface_number, 2));
}

@Test
void transferOnValidEndpoint_succeeds() {
testDevice.selectAlternateSetting(LOOPBACK_INTF_LOOPBACK, 1);
testDevice.selectAlternateSetting(config.interfaceNumber(), 1);

var sampleData = generateRandomBytes(12, 293872394);
testDevice.transferOut(LOOPBACK_EP_OUT, sampleData);
var received = testDevice.transferIn(LOOPBACK_EP_IN);
testDevice.transferOut(config.endpointLoopbackOut(), sampleData);
var received = testDevice.transferIn(config.endpointLoopbackIn());
assertArrayEquals(sampleData, received);
}

@Test
void transferOnInvalidEndpoint_fails() {
testDevice.selectAlternateSetting(LOOPBACK_INTF_LOOPBACK, 1);
testDevice.selectAlternateSetting(config.interfaceNumber(), 1);

assertThrows(UsbException.class, () -> testDevice.transferOut(ECHO_EP_OUT, new byte[] { 1, 2, 3 }));
var endpointOut = config.endpointEchoOut();
assertThrows(UsbException.class, () -> testDevice.transferOut(endpointOut, new byte[] { 1, 2, 3 }));

assertThrows(UsbException.class, () -> testDevice.transferIn(ECHO_EP_IN));
var endpointIn = config.endpointEchoIn();
assertThrows(UsbException.class, () -> testDevice.transferIn(endpointIn));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ void mediumTransfer_succeeds() {

@Test
void transferWithZLP_succeeds() {
var inEndpoint = testDevice.getEndpoint(UsbDirection.IN, LOOPBACK_EP_IN);
var inEndpoint = testDevice.getEndpoint(UsbDirection.IN, config.endpointLoopbackIn());
var sampleData = generateRandomBytes(inEndpoint.getPacketSize(), 97333894);
testDevice.transferOut(LOOPBACK_EP_OUT, sampleData);
testDevice.transferOut(LOOPBACK_EP_OUT, new byte[0]);
var data = testDevice.transferIn(LOOPBACK_EP_IN);
testDevice.transferOut(config.endpointLoopbackOut(), sampleData);
testDevice.transferOut(config.endpointLoopbackOut(), new byte[0]);
var data = testDevice.transferIn(config.endpointLoopbackIn());
assertArrayEquals(sampleData, data);
data = testDevice.transferIn(LOOPBACK_EP_IN);
data = testDevice.transferIn(config.endpointLoopbackIn());
assertNotNull(data);
assertEquals(0, data.length);
}
Expand All @@ -67,15 +67,15 @@ static void writeBytes(byte[] data) {
var numBytes = 0;
while (numBytes < data.length) {
var size = Math.min(chunkSize, data.length - numBytes);
testDevice.transferOut(LOOPBACK_EP_OUT, Arrays.copyOfRange(data, numBytes, numBytes + size));
testDevice.transferOut(config.endpointLoopbackOut(), Arrays.copyOfRange(data, numBytes, numBytes + size));
numBytes += size;
}
}
static byte[] readBytes(int numBytes) {
var buffer = new ByteArrayOutputStream();
var bytesRead = 0;
while (bytesRead < numBytes) {
var data = testDevice.transferIn(LOOPBACK_EP_IN);
var data = testDevice.transferIn(config.endpointLoopbackIn());
buffer.writeBytes(data);
bytesRead += data.length;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@ class ControlTransferTest extends TestDeviceBase {

@Test
void storeValue_succeeds() {
var setup = new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x01, (short) 10730, (short) interfaceNumber);
var setup = new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x01, (short) 10730, (short) config.interfaceNumber());
assertDoesNotThrow(() -> testDevice.controlTransferOut(setup, null));
}

@Test
void retrieveValue_isSameAsStored() {
testDevice.controlTransferOut(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x01, (short) 0x9a41, (short) interfaceNumber), null);
var valueBytes = testDevice.controlTransferIn(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x03, (short) 0, (short) interfaceNumber), 4);
testDevice.controlTransferOut(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x01, (short) 0x9a41, (short) config.interfaceNumber()), null);
var valueBytes = testDevice.controlTransferIn(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x03, (short) 0, (short) config.interfaceNumber()), 4);
var expectedBytes = new byte[]{(byte) 0x41, (byte) 0x9a, (byte) 0x00, (byte) 0x00};
assertArrayEquals(expectedBytes, valueBytes);
}

@Test
void storeValueInDataStage_canBeRetrieved() {
var sentValue = new byte[]{(byte) 0x83, (byte) 0x03, (byte) 0xda, (byte) 0x3e};
testDevice.controlTransferOut(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x02, (short) 0, (short) interfaceNumber), sentValue);
var retrievedValue = testDevice.controlTransferIn(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x03, (short) 0, (short) interfaceNumber), 4);
testDevice.controlTransferOut(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x02, (short) 0, (short) config.interfaceNumber()), sentValue);
var retrievedValue = testDevice.controlTransferIn(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x03, (short) 0, (short) config.interfaceNumber()), 4);
assertArrayEquals(sentValue, retrievedValue);
}

@Test
void interfaceNumber_canBeRetrieved() {
var response = testDevice.controlTransferIn(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x05, (short) 0, (short) interfaceNumber), 1);
assertEquals(interfaceNumber, response[0] & 0xff);
var response = testDevice.controlTransferIn(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x05, (short) 0, (short) config.interfaceNumber()), 1);
assertEquals(config.interfaceNumber(), response[0] & 0xff);

if (isCompositeDevce()) {
testDevice.claimInterface(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ void deviceInfo_isCorrect() {
@Test
void interfaceDescriptor_isCorrect() {
assertNotNull(testDevice.getInterfaces());
assertEquals(interfaceNumber + 1, testDevice.getInterfaces().size());
assertEquals(config.interfaceNumber() + 1, testDevice.getInterfaces().size());

var intf = testDevice.getInterfaces().get(interfaceNumber);
assertEquals(interfaceNumber, intf.getNumber());
var intf = testDevice.getInterfaces().get(config.interfaceNumber());
assertEquals(config.interfaceNumber(), intf.getNumber());
assertNotNull(intf.getCurrentAlternate());
assertTrue(intf.isClaimed());
}
Expand All @@ -65,7 +65,7 @@ void invalidInterfaceNumber_shouldThrow() {

@Test
void alternateInterfaceDescriptor_isCorrect() {
var intf = testDevice.getInterfaces().get(interfaceNumber);
var intf = testDevice.getInterfaces().get(config.interfaceNumber());
var altIntf = intf.getCurrentAlternate();
assertNotNull(intf.getAlternates());
assertEquals(isLoopbackDevice() ? 2 : 1, intf.getAlternates().size());
Expand All @@ -86,9 +86,10 @@ void alternateInterfaceDescriptor_isCorrect() {
}
}

@SuppressWarnings("java:S5961")
@Test
void endpointDescriptors_areCorrect() {
var altIntf = testDevice.getInterfaces().get(interfaceNumber).getCurrentAlternate();
var altIntf = testDevice.getInterfaces().get(config.interfaceNumber()).getCurrentAlternate();
assertNotNull(altIntf.getEndpoints());
assertEquals(isLoopbackDevice() ? 4 : 2, altIntf.getEndpoints().size());

Expand Down Expand Up @@ -118,7 +119,7 @@ void endpointDescriptors_areCorrect() {
assertEquals(16, endpoint.getPacketSize());

// test alternate interface 1
altIntf = testDevice.getInterfaces().get(interfaceNumber).getAlternates().get(1);
altIntf = testDevice.getInterfaces().get(config.interfaceNumber()).getAlternates().get(1);
assertEquals(2, altIntf.getEndpoints().size());

endpoint = altIntf.getEndpoints().getFirst();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,30 @@ void getAllDevices_includesLoopback() {
var deviceList = Usb.getDevices();
assertThat(deviceList)
.isNotEmpty()
.anyMatch(device -> device.getVendorId() == vid && device.getProductId() == pid);
.anyMatch(device -> device.getVendorId() == config.vid() && device.getProductId() == config.pid());
}

@Test
void getDevices_includesLoopback() {
var deviceList = Usb.findDevices(device -> device.getVendorId() == vid && device.getProductId() == pid);
var deviceList = Usb.findDevices(device -> device.getVendorId() == config.vid() && device.getProductId() == config.pid());
assertThat(deviceList)
.isNotEmpty()
.anyMatch(device -> device.getVendorId() == vid && device.getProductId() == pid);
.anyMatch(device -> device.getVendorId() == config.vid() && device.getProductId() == config.pid());
}

@Test
void getDevicePredicate_returnsLoopback() {
var device = Usb.findDevice(dev -> dev.getVendorId() == vid && dev.getProductId() == pid);
var device = Usb.findDevice(dev -> dev.getVendorId() == config.vid() && dev.getProductId() == config.pid());
assertThat(device).isPresent();
assertThat(device.get().getProductId()).isEqualTo(pid);
assertThat(device.get().getVendorId()).isEqualTo(vid);
assertThat(device.get().getProductId()).isEqualTo(config.pid());
assertThat(device.get().getVendorId()).isEqualTo(config.vid());
}

@Test
void getDeviceVidPid_returnsLoopback() {
var device = Usb.findDevice(vid, pid);
var device = Usb.findDevice(config.vid(), config.pid());
assertThat(device).isPresent();
assertThat(device.get().getProductId()).isEqualTo(pid);
assertThat(device.get().getVendorId()).isEqualTo(vid);
assertThat(device.get().getProductId()).isEqualTo(config.pid());
assertThat(device.get().getVendorId()).isEqualTo(config.vid());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DeviceLifecycleTest {
@Test
void lifecycle_showsValidState() {
device = TestDeviceBase.getDevice();
var interfaceNumber = TestDeviceBase.getInterfaceNumber(device);
var interfaceNumber = TestDeviceBase.getDeviceConfig().interfaceNumber();

var intf = device.getInterfaces().get(interfaceNumber);
assertEquals(interfaceNumber, intf.getNumber());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ void smallTransfer_succeeds() {
"Interrupt transfer only supported by loopback test device");

var sampleData = generateRandomBytes(12, 293872394);
testDevice.transferOut(ECHO_EP_OUT, sampleData);
testDevice.transferOut(config.endpointEchoOut(), sampleData);

// receive first echo
var echo = testDevice.transferIn(ECHO_EP_IN);
var echo = testDevice.transferIn(config.endpointEchoIn());
assertArrayEquals(sampleData, echo);

// receive second echo
echo = testDevice.transferIn(ECHO_EP_IN);
echo = testDevice.transferIn(config.endpointEchoIn());
assertArrayEquals(sampleData, echo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class InvalidOperationTest extends TestDeviceBase {

@Test
void claimInvalidInterface_throws() {
var interfaceNumber = config.interfaceNumber();
// throws error because it's already claimed
Assertions.assertThrows(UsbException.class, () -> testDevice.claimInterface(interfaceNumber));
// throws error because it's an invalid interface number
Expand Down
6 changes: 3 additions & 3 deletions java-does-usb/src/test/java/net/codecrete/usb/SpeedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class SpeedTest extends TestDeviceBase {

@Test
void loopback_isFast() throws Throwable {
final var isHighSpeed = testDevice.getEndpoint(UsbDirection.IN, LOOPBACK_EP_IN).getPacketSize() == 512;
final var isHighSpeed = testDevice.getEndpoint(UsbDirection.IN, config.endpointLoopbackIn()).getPacketSize() == 512;
final var numBytes = isHighSpeed ? 5000000 : 500000;

var sampleData = generateRandomBytes(numBytes, 7219937602343L);
Expand All @@ -43,14 +43,14 @@ void loopback_isFast() throws Throwable {
}

static void writeBytes(byte[] data) {
try (var os = testDevice.openOutputStream(LOOPBACK_EP_OUT)) {
try (var os = testDevice.openOutputStream(config.endpointLoopbackOut())) {
os.write(data);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static byte[] readBytes(int numBytes) {
try (var is = testDevice.openInputStream(LOOPBACK_EP_IN)) {
try (var is = testDevice.openInputStream(config.endpointLoopbackIn())) {
var buffer = new byte[numBytes];
var bytesRead = 0;
while (bytesRead < numBytes) {
Expand Down
26 changes: 15 additions & 11 deletions java-does-usb/src/test/java/net/codecrete/usb/StallTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,40 @@ class StallTest extends TestDeviceBase {

@Test
void stalledBulkTransferOut_recovers() {
haltEndpoint(UsbDirection.OUT, LOOPBACK_EP_OUT);
var endpointIn = config.endpointLoopbackIn();
var endpointOut = config.endpointLoopbackOut();
haltEndpoint(UsbDirection.OUT, endpointOut);

var data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
assertThrows(UsbStallException.class, () -> testDevice.transferOut(LOOPBACK_EP_OUT, data));
assertThrows(UsbStallException.class, () -> testDevice.transferOut(endpointOut, data));

testDevice.clearHalt(UsbDirection.OUT, LOOPBACK_EP_OUT);
testDevice.clearHalt(UsbDirection.OUT, endpointOut);

testDevice.transferOut(LOOPBACK_EP_OUT, data);
var receivedData = testDevice.transferIn(LOOPBACK_EP_IN);
testDevice.transferOut(endpointOut, data);
var receivedData = testDevice.transferIn(endpointIn);
assertArrayEquals(data, receivedData);
}

@Test
void stalledBulkTransferIn_recovers() {
haltEndpoint(UsbDirection.IN, LOOPBACK_EP_IN);
var endpointIn = config.endpointLoopbackIn();
var endpointOut = config.endpointLoopbackOut();
haltEndpoint(UsbDirection.IN, endpointIn);

assertThrows(UsbStallException.class, () -> testDevice.transferIn(LOOPBACK_EP_IN));
assertThrows(UsbStallException.class, () -> testDevice.transferIn(endpointIn));

testDevice.clearHalt(UsbDirection.IN, LOOPBACK_EP_IN);
testDevice.clearHalt(UsbDirection.IN, endpointIn);

var data = new byte[] { 9, 8, 7, 6, 5, 4, 3, 2 };
testDevice.transferOut(LOOPBACK_EP_OUT, data);
var receivedData = testDevice.transferIn(LOOPBACK_EP_IN);
testDevice.transferOut(endpointOut, data);
var receivedData = testDevice.transferIn(endpointIn);
assertArrayEquals(data, receivedData);
}

@Test
void invalidControlTransfer_throws() {
var request = new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x08,
(short) 0, (short) interfaceNumber);
(short) 0, (short) config.interfaceNumber());
assertThrows(UsbStallException.class, () -> testDevice.controlTransferIn(request, 2));
}

Expand Down
11 changes: 6 additions & 5 deletions java-does-usb/src/test/java/net/codecrete/usb/StreamTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ void mediumTransfer_succeeds() {

@Test
void transferWithZLP_succeeds() {
final var sampleData = generateRandomBytes(2 * LOOPBACK_MAX_PACKET_SIZE, 197007894);
var maxPacketSize = testDevice.getEndpoint(UsbDirection.OUT, config.endpointLoopbackOut()).getPacketSize();
final var sampleData = generateRandomBytes(2 * maxPacketSize, 197007894);
var writer = CompletableFuture.runAsync(() -> {
testDevice.transferOut(LOOPBACK_EP_OUT, Arrays.copyOfRange(sampleData, 0, LOOPBACK_MAX_PACKET_SIZE));
testDevice.transferOut(config.endpointLoopbackOut(), Arrays.copyOfRange(sampleData, 0, maxPacketSize));
sleep(200);
testDevice.transferOut(LOOPBACK_EP_OUT, Arrays.copyOfRange(sampleData, LOOPBACK_MAX_PACKET_SIZE, 2 * LOOPBACK_MAX_PACKET_SIZE));
testDevice.transferOut(config.endpointLoopbackOut(), Arrays.copyOfRange(sampleData, maxPacketSize, 2 * maxPacketSize));
});

var reader = CompletableFuture.supplyAsync(() -> readBytes(sampleData.length));
Expand Down Expand Up @@ -76,7 +77,7 @@ void largeTransferBigChunks_succeeds() {
}

static void writeBytes(byte[] data, int chunkSize) {
try (var os = testDevice.openOutputStream(LOOPBACK_EP_OUT)) {
try (var os = testDevice.openOutputStream(config.endpointLoopbackOut())) {
var numBytes = 0;
while (numBytes < data.length) {
var size = Math.min(chunkSize, data.length - numBytes);
Expand All @@ -90,7 +91,7 @@ static void writeBytes(byte[] data, int chunkSize) {
}
static byte[] readBytes(int numBytes) {
var buffer = new byte[numBytes];
try (var is = testDevice.openInputStream(LOOPBACK_EP_IN)) {
try (var is = testDevice.openInputStream(config.endpointLoopbackIn())) {
var bytesRead = 0;
while (bytesRead < numBytes) {
var n = is.read(buffer, bytesRead, numBytes - bytesRead);
Expand Down
Loading

0 comments on commit f5d0f92

Please sign in to comment.