Skip to content

Commit

Permalink
Added more real-world argument parsing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1N committed Feb 22, 2023
1 parent 0e76e83 commit 9291305
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
30 changes: 27 additions & 3 deletions memflow/src/plugins/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ impl std::str::FromStr for Args {
}
}

//let split: Vec<_> = split_str_args(s, ',').collect();

let mut map = HashMap::new();
for (i, kv) in split.iter().enumerate() {
let kvsplit = kv.split('=').collect::<Vec<_>>();
if kvsplit.len() == 2 {
Expand Down Expand Up @@ -429,21 +432,22 @@ impl fmt::Debug for ArgDescriptor {
/// let v: Vec<_> = split_str_args("a:\"hel:lo:c").collect();
/// assert_eq!(v, ["a", "\"hel:lo:c"]);
/// ```
pub fn split_str_args(inp: &str) -> impl Iterator<Item = &str> {
pub fn split_str_args(inp: &str, split_char: char) -> impl Iterator<Item = &str> {
let mut prev_char = '\0';
let mut quotation_char = None;

const VALID_QUOTES: &str = "\"'`";
assert!(!VALID_QUOTES.contains(split_char));

inp.split(move |c| {
let ret = if c == ':' {
let ret = if c == split_char {
let ret = quotation_char.is_none() || Some(prev_char) == quotation_char;
if ret {
quotation_char = None;
}
ret
} else {
if prev_char == ':' && quotation_char.is_none() && VALID_QUOTES.contains(c) {
if prev_char == split_char && quotation_char.is_none() && VALID_QUOTES.contains(c) {
quotation_char = Some(c);
}
false
Expand Down Expand Up @@ -582,6 +586,26 @@ mod tests {
assert_eq!(args2.get("opt2").unwrap(), "test2,test3=test4");
}

#[test]
pub fn slashes() {
let argstr = "device=vmware://,remote=rpc://insecure:computername.local";
let args: Args = argstr.parse().unwrap();
let args2: Args = args.to_string().parse().unwrap();
assert_eq!(args2.get("device").unwrap(), "vmware://");
assert_eq!(
args2.get("remote").unwrap(),
"rpc://insecure:computername.local"
);
}

#[test]
pub fn slashes_quotes() {
let argstr = "device=\"RAWUDP://ip=127.0.0.1\"";
let args: Args = argstr.parse().unwrap();
let args2: Args = args.to_string().parse().unwrap();
assert_eq!(args2.get("device").unwrap(), "RAWUDP://ip=127.0.0.1");
}

#[test]
pub fn validator_success() {
let validator = ArgsValidator::new()
Expand Down
14 changes: 13 additions & 1 deletion memflow/src/plugins/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl std::str::FromStr for ConnectorArgs {
type Err = crate::error::Error;

fn from_str(s: &str) -> Result<Self> {
let mut iter = split_str_args(s);
let mut iter = split_str_args(s, ':');

let target = iter
.next()
Expand Down Expand Up @@ -391,4 +391,16 @@ mod tests {
assert_eq!(args.middleware_args.cache_validity_time, 10);
assert_eq!(args.middleware_args.cache_page_size, 0x1000);
}

#[test]
pub fn connector_args_url() {
let args: ConnectorArgs = ":device=\"RAWUDP://ip=127.0.0.1:8080\":"
.parse()
.expect("unable to parse args");
assert_eq!(args.target, None);
assert_eq!(
args.extra_args.get("device").unwrap(),
"RAWUDP://ip=127.0.0.1:8080"
);
}
}
2 changes: 1 addition & 1 deletion memflow/src/plugins/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl std::str::FromStr for OsArgs {
type Err = crate::error::Error;

fn from_str(s: &str) -> Result<Self> {
let mut iter = split_str_args(s);
let mut iter = split_str_args(s, ':');

let target = iter
.next()
Expand Down

0 comments on commit 9291305

Please sign in to comment.