Skip to content

Commit d9bb763

Browse files
committed
General tests for both test devices
1 parent 469d2a7 commit d9bb763

File tree

6 files changed

+91
-52
lines changed

6 files changed

+91
-52
lines changed

java-does-usb/src/test/java/net/codecrete/usb/ControlTransferTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ public class ControlTransferTest extends TestDeviceBase {
2020

2121
@Test
2222
void storeValue_succeeds() {
23-
testDevice.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x01, (short) 10730, (short) LOOPBACK_INTF), null);
23+
testDevice.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x01, (short) 10730, (short) interfaceNumber), null);
2424
}
2525

2626
@Test
2727
void retrieveValue_isSameAsStored() {
28-
testDevice.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x01, (short) 0x9a41, (short) LOOPBACK_INTF), null);
29-
var valueBytes = testDevice.controlTransferIn(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x03, (short) 0, (short) LOOPBACK_INTF), 4);
28+
testDevice.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x01, (short) 0x9a41, (short) interfaceNumber), null);
29+
var valueBytes = testDevice.controlTransferIn(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x03, (short) 0, (short) interfaceNumber), 4);
3030
var expectedBytes = new byte[]{(byte) 0x41, (byte) 0x9a, (byte) 0x00, (byte) 0x00};
3131
assertArrayEquals(expectedBytes, valueBytes);
3232
}
3333

3434
@Test
3535
void storeValueInDataStage_canBeRetrieved() {
3636
var sentValue = new byte[]{(byte) 0x83, (byte) 0x03, (byte) 0xda, (byte) 0x3e};
37-
testDevice.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x02, (short) 0, (short) LOOPBACK_INTF), sentValue);
38-
var retrievedValue = testDevice.controlTransferIn(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x03, (short) 0, (short) LOOPBACK_INTF), 4);
37+
testDevice.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x02, (short) 0, (short) interfaceNumber), sentValue);
38+
var retrievedValue = testDevice.controlTransferIn(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x03, (short) 0, (short) interfaceNumber), 4);
3939
assertArrayEquals(sentValue, retrievedValue);
4040
}
4141
}

java-does-usb/src/test/java/net/codecrete/usb/DescriptionTest.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,33 @@ void deviceInfo_isCorrect() {
2424
assertEquals("Loopback", testDevice.product());
2525
assertEquals(12, testDevice.serialNumber().length());
2626

27-
assertEquals(0xef, testDevice.classCode());
28-
assertEquals(0x02, testDevice.subclassCode());
29-
assertEquals(0x01, testDevice.protocolCode());
27+
if (interfaceNumber == 2) {
28+
// composite device
29+
assertEquals(0xef, testDevice.classCode());
30+
assertEquals(0x02, testDevice.subclassCode());
31+
assertEquals(0x01, testDevice.protocolCode());
32+
} else {
33+
// simple device
34+
assertEquals(0xff, testDevice.classCode());
35+
assertEquals(0x00, testDevice.subclassCode());
36+
assertEquals(0x00, testDevice.protocolCode());
37+
}
3038
}
3139

3240
@Test
3341
void interfaceDescriptors_isCorrect() {
3442
assertNotNull(testDevice.interfaces());
35-
assertEquals(LOOPBACK_INTF + 1, testDevice.interfaces().size());
43+
assertEquals(interfaceNumber + 1, testDevice.interfaces().size());
3644

37-
var intf = testDevice.interfaces().get(LOOPBACK_INTF);
38-
assertEquals(LOOPBACK_INTF, intf.number());
45+
var intf = testDevice.interfaces().get(interfaceNumber);
46+
assertEquals(interfaceNumber, intf.number());
3947
assertNotNull(intf.alternate());
4048
assertTrue(intf.isClaimed());
4149
}
4250

4351
@Test
4452
void alternateInterfaceDescriptors_isCorrect() {
45-
var intf = testDevice.interfaces().get(LOOPBACK_INTF);
53+
var intf = testDevice.interfaces().get(interfaceNumber);
4654
var altIntf = intf.alternate();
4755
assertNotNull(intf.alternates());
4856
assertEquals(1, intf.alternates().size());
@@ -56,7 +64,7 @@ void alternateInterfaceDescriptors_isCorrect() {
5664

5765
@Test
5866
void endpointDescriptors_isCorrect() {
59-
var altIntf = testDevice.interfaces().get(LOOPBACK_INTF).alternate();
67+
var altIntf = testDevice.interfaces().get(interfaceNumber).alternate();
6068
assertNotNull(altIntf.endpoints());
6169
assertEquals(4, altIntf.endpoints().size());
6270

java-does-usb/src/test/java/net/codecrete/usb/DeviceEnumerationTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,43 @@ public class DeviceEnumerationTest extends TestDeviceBase {
2121
@Test
2222
void getAllDevices_includesLoopback() {
2323
var found = USB.getAllDevices().stream()
24-
.anyMatch(device -> device.vendorId() == VID && device.productId() == PID);
24+
.anyMatch(device -> device.vendorId() == vid && device.productId() == pid);
2525
assertTrue(found);
2626
}
2727

2828
@Test
2929
void getDevicesWithFilter_returnsLoopback() {
30-
var result = USB.getDevices(new USBDeviceFilter(VID, PID));
30+
var result = USB.getDevices(new USBDeviceFilter(vid, pid));
3131
assertEquals(1, result.size());
32-
assertEquals(VID, result.get(0).vendorId());
33-
assertEquals(PID, result.get(0).productId());
32+
assertEquals(vid, result.get(0).vendorId());
33+
assertEquals(pid, result.get(0).productId());
3434
}
3535

3636
@Test
3737
void getDevicesWithMultipleFilters_returnsLoopback() {
3838
var result = USB.getDevices(List.of(
39-
new USBDeviceFilter(VID, PID),
39+
new USBDeviceFilter(vid, pid),
4040
new USBDeviceFilter(0x0000, 0xffff)
4141
));
4242
assertEquals(1, result.size());
43-
assertEquals(VID, result.get(0).vendorId());
44-
assertEquals(PID, result.get(0).productId());
43+
assertEquals(vid, result.get(0).vendorId());
44+
assertEquals(pid, result.get(0).productId());
4545
}
4646

4747
@Test
4848
void getDeviceWithFilter_returnsLoopback() {
49-
var device = USB.getDevice(new USBDeviceFilter(VID, PID));
50-
assertEquals(VID, device.vendorId());
51-
assertEquals(PID, device.productId());
49+
var device = USB.getDevice(new USBDeviceFilter(vid, pid));
50+
assertEquals(vid, device.vendorId());
51+
assertEquals(pid, device.productId());
5252
}
5353

5454
@Test
5555
void getDeviceWithMultipleFilters_returnsLoopback() {
5656
var device = USB.getDevice(List.of(
57-
new USBDeviceFilter(VID, PID),
57+
new USBDeviceFilter(vid, pid),
5858
new USBDeviceFilter(0x0000, 0xffff)
5959
));
60-
assertEquals(VID, device.vendorId());
61-
assertEquals(PID, device.productId());
60+
assertEquals(vid, device.vendorId());
61+
assertEquals(pid, device.productId());
6262
}
6363
}

java-does-usb/src/test/java/net/codecrete/usb/DeviceLifecycleTest.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.junit.jupiter.api.AfterEach;
1313
import org.junit.jupiter.api.Test;
1414

15-
import static net.codecrete.usb.TestDeviceBase.*;
1615
import static org.junit.jupiter.api.Assertions.*;
1716

1817
public class DeviceLifecycleTest {
@@ -21,45 +20,44 @@ public class DeviceLifecycleTest {
2120

2221
@Test
2322
void lifecycle_showsValidState() {
24-
device = USB.getDevice(new USBDeviceFilter(VID, PID));
25-
if (device == null)
26-
throw new IllegalStateException("USB loopback test device must be connected");
23+
device = TestDeviceBase.getDevice();
24+
int interfaceNumber = TestDeviceBase.getInterfaceNumber(device);
2725

28-
var intf = device.interfaces().get(LOOPBACK_INTF);
29-
assertEquals(LOOPBACK_INTF, intf.number());
26+
var intf = device.interfaces().get(interfaceNumber);
27+
assertEquals(interfaceNumber, intf.number());
3028

3129
assertFalse(device.isOpen());
3230
assertFalse(intf.isClaimed());
33-
assertThrows(USBException.class, () -> device.claimInterface(LOOPBACK_INTF));
34-
assertThrows(USBException.class, () -> device.releaseInterface(LOOPBACK_INTF));
31+
assertThrows(USBException.class, () -> device.claimInterface(interfaceNumber));
32+
assertThrows(USBException.class, () -> device.releaseInterface(interfaceNumber));
3533

3634
device.open();
3735

3836
assertTrue(device.isOpen());
3937
assertFalse(intf.isClaimed());
4038
assertThrows(USBException.class, () -> device.open());
41-
assertThrows(USBException.class, () -> device.releaseInterface(LOOPBACK_INTF));
39+
assertThrows(USBException.class, () -> device.releaseInterface(interfaceNumber));
4240

43-
device.claimInterface(LOOPBACK_INTF);
41+
device.claimInterface(interfaceNumber);
4442

4543
assertTrue(device.isOpen());
4644
assertTrue(intf.isClaimed());
4745
assertThrows(USBException.class, () -> device.open());
48-
assertThrows(USBException.class, () -> device.claimInterface(LOOPBACK_INTF));
46+
assertThrows(USBException.class, () -> device.claimInterface(interfaceNumber));
4947

50-
device.releaseInterface(LOOPBACK_INTF);
48+
device.releaseInterface(interfaceNumber);
5149

5250
assertTrue(device.isOpen());
5351
assertFalse(intf.isClaimed());
5452
assertThrows(USBException.class, () -> device.open());
55-
assertThrows(USBException.class, () -> device.releaseInterface(LOOPBACK_INTF));
53+
assertThrows(USBException.class, () -> device.releaseInterface(interfaceNumber));
5654

5755
device.close();
5856

5957
assertFalse(device.isOpen());
6058
assertFalse(intf.isClaimed());
61-
assertThrows(USBException.class, () -> device.claimInterface(LOOPBACK_INTF));
62-
assertThrows(USBException.class, () -> device.releaseInterface(LOOPBACK_INTF));
59+
assertThrows(USBException.class, () -> device.claimInterface(interfaceNumber));
60+
assertThrows(USBException.class, () -> device.releaseInterface(interfaceNumber));
6361
}
6462

6563
@AfterEach

java-does-usb/src/test/java/net/codecrete/usb/InvalidOperationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class InvalidOperationTest extends TestDeviceBase {
1717
@Test
1818
void claimInvalidInterface_throws() {
1919
// throws error because it's already claimed
20-
Assertions.assertThrows(USBException.class, () -> testDevice.claimInterface(LOOPBACK_INTF));
20+
Assertions.assertThrows(USBException.class, () -> testDevice.claimInterface(interfaceNumber));
2121
// throws error because it's an invalid interface number
2222
Assertions.assertThrows(USBException.class, () -> testDevice.claimInterface(3));
2323
// throws error because it's an invalid interface number

java-does-usb/src/test/java/net/codecrete/usb/TestDeviceBase.java

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,35 @@
1919
*/
2020
public class TestDeviceBase {
2121
/**
22-
* Test device vendor ID
22+
* Simple test device vendor ID
2323
*/
24-
static final int VID = 0xcafe;
24+
static final int VID_SIMPLE = 0xcafe;
2525
/**
26-
* Test device product ID
26+
* Simple test device product ID
2727
*/
28-
static final int PID = 0xcea0;
28+
static final int PID_SIMPLE = 0xceaf;
2929
/**
30-
* Test device loopback interface number
30+
* Simple test device loopback interface number
3131
*/
32-
static final int LOOPBACK_INTF = 2;
32+
static final int LOOPBACK_INTF_SIMPLE = 0;
33+
/**
34+
* Composite test device vendor ID
35+
*/
36+
static final int VID_COMPOSITE = 0xcafe;
37+
/**
38+
* Composite test device product ID
39+
*/
40+
static final int PID_COMPOSITE = 0xcea0;
41+
/**
42+
* Composite test device loopback interface number
43+
*/
44+
static final int LOOPBACK_INTF_COMPOSITE = 2;
45+
/**
46+
* Interface number of connected test device
47+
*/
48+
protected static int vid = -1;
49+
protected static int pid = -1;
50+
protected static int interfaceNumber = -1;
3351
protected static final int LOOPBACK_EP_OUT = 1;
3452
protected static final int LOOPBACK_EP_IN = 2;
3553
protected static final int LOOPBACK_MAX_PACKET_SIZE = 64;
@@ -38,13 +56,28 @@ public class TestDeviceBase {
3856
protected static final int ECHO_MAX_PACKET_SIZE = 16;
3957
protected static USBDevice testDevice;
4058

59+
static USBDevice getDevice() {
60+
var device = USB.getDevice(new USBDeviceFilter(VID_COMPOSITE, PID_COMPOSITE));
61+
if (device == null)
62+
device = USB.getDevice(new USBDeviceFilter(VID_SIMPLE, PID_SIMPLE));
63+
if (device == null)
64+
throw new IllegalStateException("No test device connected");
65+
return device;
66+
}
67+
68+
static int getInterfaceNumber(USBDevice device) {
69+
return device.productId() == PID_COMPOSITE ? LOOPBACK_INTF_COMPOSITE : LOOPBACK_INTF_SIMPLE;
70+
}
71+
4172
@BeforeAll
4273
static void openDevice() {
43-
testDevice = USB.getDevice(new USBDeviceFilter(VID, PID));
44-
if (testDevice == null)
45-
throw new IllegalStateException("USB loopback test device must be connected");
74+
testDevice = getDevice();
75+
vid = testDevice.vendorId();
76+
pid = testDevice.productId();
77+
interfaceNumber = getInterfaceNumber(testDevice);
78+
4679
testDevice.open();
47-
testDevice.claimInterface(LOOPBACK_INTF);
80+
testDevice.claimInterface(interfaceNumber);
4881
}
4982

5083
@AfterAll

0 commit comments

Comments
 (0)