Skip to content

Commit

Permalink
Add some more test cases for MODALIAS parsing
Browse files Browse the repository at this point in the history
This includes throwing some random data at parse_modalias at each test
run with help of quickcheck.
  • Loading branch information
sirhcel authored and eldruin committed Jul 27, 2024
1 parent 1e5c12b commit e32fca9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ envconfig = "0.10.0"
# 6.6.0) Until then we are tricking the dependency resolver into using a
# compatible version by adding it as a direct dependency here.
os_str_bytes = ">=6.0, <6.6.0"
quickcheck = "1.0.3"
quickcheck_macros = "1.0.0"
rstest = { version = "0.12.0", default-features = false }
rustversion = "1.0.16"

Expand Down
58 changes: 49 additions & 9 deletions src/posix/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,16 +690,56 @@ cfg_if! {
}
}

#[cfg(all(target_os = "linux", not(target_env = "musl"), feature = "libudev"))]
#[test]
fn parser_modalias() {
const MODALIAS: &str = "usb:v303Ap1001d0101dcEFdsc02dp01ic02isc02ip00in0C";
#[cfg(all(
test,
target_os = "linux",
not(target_env = "musl"),
feature = "libudev"
))]
mod test {
use super::*;

use quickcheck_macros::quickcheck;

#[quickcheck]
fn quickcheck_parse_modalias_does_not_panic_from_random_data(modalias: String) -> bool {
let _ = parse_modalias(&modalias);
true
}

#[test]
fn parse_modalias_canonical() {
const MODALIAS: &str = "usb:v303Ap1001d0101dcEFdsc02dp01ic02isc02ip00in0C";

let port_info = parse_modalias(MODALIAS).expect("parse failed");
let port_info = parse_modalias(MODALIAS).expect("parse failed");

assert_eq!(port_info.vid, 0x303A, "vendor parse invalid");
assert_eq!(port_info.pid, 0x1001, "product parse invalid");
assert_eq!(port_info.vid, 0x303A, "vendor parse invalid");
assert_eq!(port_info.pid, 0x1001, "product parse invalid");

#[cfg(feature = "usbportinfo-interface")]
assert_eq!(port_info.interface, Some(0x0C), "interface parse invalid");
#[cfg(feature = "usbportinfo-interface")]
assert_eq!(port_info.interface, Some(0x0C), "interface parse invalid");
}

#[test]
fn parse_modalias_corner_cases() {
assert!(parse_modalias("").is_none());
assert!(parse_modalias("usb").is_none());
assert!(parse_modalias("usb:").is_none());
assert!(parse_modalias("usb:vdcdc").is_none());
assert!(parse_modalias("usb:pdcdc").is_none());

// Just vendor and product IDs.
let info = parse_modalias("usb:vdcdcpabcd").unwrap();
assert_eq!(info.vid, 0xdcdc);
assert_eq!(info.pid, 0xabcd);
#[cfg(feature = "usbportinfo-interface")]
assert!(info.interface.is_none());

// Vendor and product ID plus an interface number.
let info = parse_modalias("usb:v1234p5678indc").unwrap();
assert_eq!(info.vid, 0x1234);
assert_eq!(info.pid, 0x5678);
#[cfg(feature = "usbportinfo-interface")]
assert_eq!(info.interface, Some(0xdc));
}
}

0 comments on commit e32fca9

Please sign in to comment.