Merge branch 'main' into dependabot/cargo/vhost-0.2

This commit is contained in:
Viresh Kumar 2021-10-11 11:38:19 +05:30 committed by GitHub
commit b7b5d3e6e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 30 deletions

8
Cargo.lock generated
View File

@ -120,9 +120,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.102"
version = "0.2.103"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2a5ac8f984bfcf3a823267e5fde638acc3325f6496633a5da6bb6eb2171e103"
checksum = "dd8f7255a17a627354f321ef0055d63b898c6fb27eff628af4d1b66b7331edf6"
[[package]]
name = "linked-hash-map"
@ -195,9 +195,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.76"
version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6f107db402c2c2055242dbf4d2af0e69197202e9faacbef9571bbe47f5a1b84"
checksum = "d010a1623fbd906d51d650a9916aaefc05ffa0e4053ff7fe601167f3e715d194"
dependencies = [
"proc-macro2",
"quote",

View File

@ -1,5 +1,5 @@
{
"coverage_score": 49.4,
"coverage_score": 49.6,
"exclude_path": "",
"crate_features": ""
}

@ -1 +1 @@
Subproject commit ae7db2d98a071f52de3d60af9c937204b1f087a4
Subproject commit 7f22582590b5816878e7f3f860766979cab297a0

View File

@ -97,7 +97,7 @@ impl TryFrom<&str> for AdapterConfig {
for device_str in list[1..].iter() {
let addr = device_str
.parse::<u16>()
.map_err(|_| "Invalid device addr: {}")?;
.map_err(|_| "Invalid device addr")?;
adapter.push(addr)?;
}
@ -129,6 +129,10 @@ impl TryFrom<ArgMatches> for I2cConfiguration {
.parse::<usize>()
.map_err(|_| "Invalid socket_count")?;
if socket_count == 0 {
return Err("Socket count can't be 0".to_string());
}
let list = cmd_args.value_of("devices").ok_or("Invalid devices list")?;
let devices = AdapterConfig::try_from(list)?;
Ok(I2cConfiguration {
@ -226,41 +230,55 @@ mod tests {
}
}
fn get_cmd_args(name: &str, devices: &str, count: u32) -> ArgMatches {
fn get_cmd_args(name: &str, devices: &str, count: Option<u32>) -> ArgMatches {
let mut args = vec!["prog", "-s", name, "-l", devices];
let yaml = load_yaml!("cli.yaml");
let app = App::from(yaml);
let socket_count_str;
if count != 0 {
app.try_get_matches_from(vec![
"prog",
"-s",
name,
"-l",
devices,
"-c",
&count.to_string(),
])
.unwrap()
} else {
app.try_get_matches_from(vec!["prog", "-s", name, "-l", devices])
.unwrap()
if let Some(count) = count {
socket_count_str = count.to_string();
args.extend_from_slice(&["-c", &socket_count_str]);
}
app.try_get_matches_from(args).unwrap()
}
#[test]
fn test_parse_failure() {
let cmd_args = get_cmd_args("vi2c.sock_failure", "1:4d", 5);
// TODO: Check against the actual error instead of `is_err`.
assert!(I2cConfiguration::try_from(cmd_args).is_err());
let socket_name = "vi2c.sock";
let cmd_args = get_cmd_args("vi2c.sock_duplicate", "1:4,2:32:21,5:4:23", 5);
// TODO: Check against the actual error instead of `is_err`.
assert!(I2cConfiguration::try_from(cmd_args).is_err());
// Invalid device list
let cmd_args = get_cmd_args(socket_name, "1:4d", Some(5));
assert_eq!(
I2cConfiguration::try_from(cmd_args).unwrap_err(),
"Invalid device addr"
);
// Invalid socket count
let cmd_args = get_cmd_args(socket_name, "1:4", Some(0));
assert_eq!(
I2cConfiguration::try_from(cmd_args).unwrap_err(),
"Socket count can't be 0"
);
// Duplicate client address: 4
let cmd_args = get_cmd_args(socket_name, "1:4,2:32:21,5:4:23", Some(5));
assert_eq!(
I2cConfiguration::try_from(cmd_args).unwrap_err(),
"Address already in use: 4"
);
}
#[test]
fn test_parse_successful() {
let cmd_args = get_cmd_args("vi2c.sock_single", "1:4,2:32:21,5:5:23", 5);
let socket_name = "vi2c.sock";
// Missing socket count, default (1) should be used.
let cmd_args = get_cmd_args(socket_name, "1:4,2:32:21,5:5:23", None);
let config = I2cConfiguration::try_from(cmd_args).unwrap();
assert_eq!(config.socket_count, 1);
let cmd_args = get_cmd_args(socket_name, "1:4,2:32:21,5:5:23", Some(5));
let config = I2cConfiguration::try_from(cmd_args).unwrap();
let expected_devices = AdapterConfig::new_with(vec![
@ -271,7 +289,7 @@ mod tests {
let expected_config = I2cConfiguration {
socket_count: 5,
socket_path: String::from("vi2c.sock_single"),
socket_path: String::from(socket_name),
devices: expected_devices,
};