Skip to content

Commit 2f2ae94

Browse files
committed
Composite USB test device
1 parent c486bc2 commit 2f2ae94

25 files changed

+1142
-50
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919

2020
public class BulkTransferTest extends TestDeviceBase {
2121

22-
private static final int LOOPBACK_EP_OUT = 1;
23-
private static final int LOOPBACK_EP_IN = 2;
24-
private static final int MAX_PACKET_SIZE = 64;
25-
2622
@Test
2723
void smallTransfer_succeeds() {
2824
byte[] sampleData = generateRandomBytes(12, 293872394);
@@ -66,7 +62,7 @@ static byte[] readBytes(int numBytes) {
6662
var buffer = new ByteArrayOutputStream();
6763
int bytesRead = 0;
6864
while (bytesRead < numBytes) {
69-
byte[] data = testDevice.transferIn(LOOPBACK_EP_IN, MAX_PACKET_SIZE);
65+
byte[] data = testDevice.transferIn(LOOPBACK_EP_IN, LOOPBACK_MAX_PACKET_SIZE);
7066
buffer.writeBytes(data);
7167
bytesRead += data.length;
7268
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,17 @@ void deviceInfo_isCorrect() {
3232
@Test
3333
void interfaceDescriptors_isCorrect() {
3434
assertNotNull(testDevice.interfaces());
35-
assertEquals(1, testDevice.interfaces().size());
35+
assertEquals(LOOPBACK_INTF + 1, testDevice.interfaces().size());
3636

37-
var intf = testDevice.interfaces().get(0);
38-
assertEquals(0, intf.number());
37+
var intf = testDevice.interfaces().get(LOOPBACK_INTF);
38+
assertEquals(LOOPBACK_INTF, intf.number());
3939
assertNotNull(intf.alternate());
4040
assertTrue(intf.isClaimed());
4141
}
4242

4343
@Test
4444
void alternateInterfaceDescriptors_isCorrect() {
45-
var intf = testDevice.interfaces().get(0);
45+
var intf = testDevice.interfaces().get(LOOPBACK_INTF);
4646
var altIntf = intf.alternate();
4747
assertNotNull(intf.alternates());
4848
assertEquals(1, intf.alternates().size());
@@ -56,7 +56,7 @@ void alternateInterfaceDescriptors_isCorrect() {
5656

5757
@Test
5858
void endpointDescriptors_isCorrect() {
59-
var altIntf = testDevice.interfaces().get(0).alternate();
59+
var altIntf = testDevice.interfaces().get(LOOPBACK_INTF).alternate();
6060
assertNotNull(altIntf.endpoints());
6161
assertEquals(4, altIntf.endpoints().size());
6262

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() == 0xcafe && device.productId() == 0xceaf);
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(0xcafe, 0xceaf));
30+
var result = USB.getDevices(new USBDeviceFilter(VID, PID));
3131
assertEquals(1, result.size());
32-
assertEquals(0xcafe, result.get(0).vendorId());
33-
assertEquals(0xceaf, 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(0xcafe, 0xceaf),
39+
new USBDeviceFilter(VID, PID),
4040
new USBDeviceFilter(0x0000, 0xffff)
4141
));
4242
assertEquals(1, result.size());
43-
assertEquals(0xcafe, result.get(0).vendorId());
44-
assertEquals(0xceaf, 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(0xcafe, 0xceaf));
50-
assertEquals(0xcafe, device.vendorId());
51-
assertEquals(0xceaf, 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(0xcafe, 0xceaf),
57+
new USBDeviceFilter(VID, PID),
5858
new USBDeviceFilter(0x0000, 0xffff)
5959
));
60-
assertEquals(0xcafe, device.vendorId());
61-
assertEquals(0xceaf, 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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.junit.jupiter.api.AfterEach;
1313
import org.junit.jupiter.api.Test;
1414

15+
import static net.codecrete.usb.TestDeviceBase.*;
1516
import static org.junit.jupiter.api.Assertions.*;
1617

1718
public class DeviceLifecycleTest {
@@ -20,45 +21,45 @@ public class DeviceLifecycleTest {
2021

2122
@Test
2223
void lifecycle_showsValidState() {
23-
device = USB.getDevice(new USBDeviceFilter(0xcafe, 0xceaf));
24+
device = USB.getDevice(new USBDeviceFilter(VID, PID));
2425
if (device == null)
2526
throw new IllegalStateException("USB loopback test device must be connected");
2627

27-
var intf = device.interfaces().get(0);
28-
assertEquals(0, intf.number());
28+
var intf = device.interfaces().get(LOOPBACK_INTF);
29+
assertEquals(LOOPBACK_INTF, intf.number());
2930

3031
assertFalse(device.isOpen());
3132
assertFalse(intf.isClaimed());
32-
assertThrows(USBException.class, () -> device.claimInterface(0));
33-
assertThrows(USBException.class, () -> device.releaseInterface(0));
33+
assertThrows(USBException.class, () -> device.claimInterface(LOOPBACK_INTF));
34+
assertThrows(USBException.class, () -> device.releaseInterface(LOOPBACK_INTF));
3435

3536
device.open();
3637

3738
assertTrue(device.isOpen());
3839
assertFalse(intf.isClaimed());
3940
assertThrows(USBException.class, () -> device.open());
40-
assertThrows(USBException.class, () -> device.releaseInterface(0));
41+
assertThrows(USBException.class, () -> device.releaseInterface(LOOPBACK_INTF));
4142

42-
device.claimInterface(0);
43+
device.claimInterface(LOOPBACK_INTF);
4344

4445
assertTrue(device.isOpen());
4546
assertTrue(intf.isClaimed());
4647
assertThrows(USBException.class, () -> device.open());
47-
assertThrows(USBException.class, () -> device.claimInterface(0));
48+
assertThrows(USBException.class, () -> device.claimInterface(LOOPBACK_INTF));
4849

49-
device.releaseInterface(0);
50+
device.releaseInterface(LOOPBACK_INTF);
5051

5152
assertTrue(device.isOpen());
5253
assertFalse(intf.isClaimed());
5354
assertThrows(USBException.class, () -> device.open());
54-
assertThrows(USBException.class, () -> device.releaseInterface(0));
55+
assertThrows(USBException.class, () -> device.releaseInterface(LOOPBACK_INTF));
5556

5657
device.close();
5758

5859
assertFalse(device.isOpen());
5960
assertFalse(intf.isClaimed());
60-
assertThrows(USBException.class, () -> device.claimInterface(0));
61-
assertThrows(USBException.class, () -> device.releaseInterface(0));
61+
assertThrows(USBException.class, () -> device.claimInterface(LOOPBACK_INTF));
62+
assertThrows(USBException.class, () -> device.releaseInterface(LOOPBACK_INTF));
6263
}
6364

6465
@AfterEach

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,21 @@
1111

1212
import org.junit.jupiter.api.Test;
1313

14-
import java.io.ByteArrayOutputStream;
15-
import java.util.Arrays;
16-
import java.util.concurrent.CompletableFuture;
17-
1814
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1915

2016
public class InterruptTransferTest extends TestDeviceBase {
2117

22-
private static final int ECHO_EP_OUT = 3;
23-
private static final int ECHO_EP_IN = 3;
24-
private static final int MAX_PACKET_SIZE = 16;
25-
2618
@Test
2719
void smallTransfer_succeeds() {
2820
byte[] sampleData = generateRandomBytes(12, 293872394);
2921
testDevice.transferOut(ECHO_EP_OUT, sampleData);
3022

3123
// receive first echo
32-
byte[] echo = testDevice.transferIn(ECHO_EP_IN, MAX_PACKET_SIZE);
24+
byte[] echo = testDevice.transferIn(ECHO_EP_IN, ECHO_MAX_PACKET_SIZE);
3325
assertArrayEquals(sampleData, echo);
3426

3527
// receive second echo
36-
echo = testDevice.transferIn(ECHO_EP_IN, MAX_PACKET_SIZE);
28+
echo = testDevice.transferIn(ECHO_EP_IN, ECHO_MAX_PACKET_SIZE);
3729
assertArrayEquals(sampleData, echo);
3830
}
3931
}

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
@@ -19,7 +19,7 @@ void claimInvalidInterface_throws() {
1919
// throws error because it's already claimed
2020
Assertions.assertThrows(USBException.class, () -> testDevice.claimInterface(0));
2121
// throws error because it's an invalid interface number
22-
Assertions.assertThrows(USBException.class, () -> testDevice.claimInterface(1));
22+
Assertions.assertThrows(USBException.class, () -> testDevice.claimInterface(3));
2323
// throws error because it's an invalid interface number
2424
Assertions.assertThrows(USBException.class, () -> testDevice.claimInterface(888));
2525
}

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,33 @@
1818
* Base class for tests using the test device.
1919
*/
2020
public class TestDeviceBase {
21+
/**
22+
* Test device vendor ID
23+
*/
24+
static final int VID = 0xcafe;
25+
/**
26+
* Test device product ID
27+
*/
28+
static final int PID = 0xceaf;
29+
/**
30+
* Test device loopback interface number
31+
*/
32+
static final int LOOPBACK_INTF = 0;
33+
protected static final int LOOPBACK_EP_OUT = 1;
34+
protected static final int LOOPBACK_EP_IN = 2;
35+
protected static final int LOOPBACK_MAX_PACKET_SIZE = 64;
36+
protected static final int ECHO_EP_OUT = 3;
37+
protected static final int ECHO_EP_IN = 3;
38+
protected static final int ECHO_MAX_PACKET_SIZE = 16;
2139
protected static USBDevice testDevice;
2240

2341
@BeforeAll
2442
static void openDevice() {
25-
testDevice = USB.getDevice(new USBDeviceFilter(0xcafe, 0xceaf));
43+
testDevice = USB.getDevice(new USBDeviceFilter(VID, PID));
2644
if (testDevice == null)
2745
throw new IllegalStateException("USB loopback test device must be connected");
2846
testDevice.open();
29-
testDevice.claimInterface(0);
47+
testDevice.claimInterface(LOOPBACK_INTF);
3048
}
3149

3250
@AfterAll
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
BasedOnStyle: Google
2+
AllowShortIfStatementsOnASingleLine: false
3+
IndentWidth: 4
4+
ColumnLimit: 120
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.pio
2+
.vscode/.browse.c_cpp.db*
3+
.vscode/c_cpp_properties.json
4+
.vscode/launch.json
5+
.vscode/ipch
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
// See http://go.microsoft.com/fwlink/?LinkId=827846
3+
// for the documentation about the extensions.json format
4+
"recommendations": [
5+
"platformio.platformio-ide"
6+
],
7+
"unwantedRecommendations": [
8+
"ms-vscode.cpptools-extension-pack"
9+
]
10+
}

0 commit comments

Comments
 (0)