Skip to content

Commit f518c2f

Browse files
authored
Merge pull request #107 from mciantyre/test-class-bulk-zlp-high-speed
Device tests: Send bulk packet ZLP based on max packet size
2 parents a94b2ff + d1495ee commit f518c2f

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

tests/test_class_host/device.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@ pub struct DeviceHandles {
1212
pub en_us: Language,
1313
}
1414

15+
impl DeviceHandles {
16+
/// Indicates if this device is (true) or isn't (false) a
17+
/// high-speed device.
18+
pub fn is_high_speed(&self) -> bool {
19+
self.handle.device().speed() == rusb::Speed::High
20+
}
21+
/// Returns the max packet size for the `TestClass` bulk endpoint(s).
22+
pub fn bulk_max_packet_size(&self) -> u16 {
23+
self.config_descriptor
24+
.interfaces()
25+
.flat_map(|intf| intf.descriptors())
26+
.flat_map(|desc| {
27+
desc.endpoint_descriptors()
28+
.find(|ep| {
29+
// Assumes that IN and OUT endpoint MPSes are the same.
30+
ep.transfer_type() == rusb::TransferType::Bulk
31+
})
32+
.map(|ep| ep.max_packet_size())
33+
})
34+
.next()
35+
.expect("TestClass has at least one bulk endpoint")
36+
}
37+
}
38+
1539
impl ::std::ops::Deref for DeviceHandles {
1640
type Target = DeviceHandle<Context>;
1741

tests/test_class_host/tests.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,13 @@ fn interface_descriptor(dev, _out) {
164164
}
165165

166166
fn bulk_loopback(dev, _out) {
167-
for len in &[0, 1, 2, 32, 63, 64, 65, 127, 128, 129] {
167+
let mut lens = vec![0, 1, 2, 32, 63, 64, 65, 127, 128, 129];
168+
if dev.is_high_speed() {
169+
lens.extend([255, 256, 257, 511, 512, 513, 1023, 1024, 1025]);
170+
}
171+
172+
let max_packet_size: usize = dev.bulk_max_packet_size().into();
173+
for len in &lens {
168174
let data = random_data(*len);
169175

170176
assert_eq!(
@@ -173,7 +179,7 @@ fn bulk_loopback(dev, _out) {
173179
data.len(),
174180
"bulk write len {}", len);
175181

176-
if *len > 0 && *len % 64 == 0 {
182+
if *len > 0 && *len % max_packet_size == 0 {
177183
assert_eq!(
178184
dev.write_bulk(0x01, &[], TIMEOUT)
179185
.expect("bulk write zero-length packet"),

0 commit comments

Comments
 (0)