Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
vanhauser-thc authored Nov 9, 2023
2 parents 031009f + 8130881 commit 56e91d6
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 58 deletions.
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cargo-afl/AFLplusplus
116 changes: 77 additions & 39 deletions cargo-afl/src/bin/cargo-afl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ mod tests {
use super::*;
use assert_cmd::Command;
use std::os::unix::ffi::OsStringExt;
use std::process::Output;

#[test]
fn test_app() {
Expand All @@ -394,26 +395,34 @@ mod tests {

#[test]
fn display_name() {
assert!(
String::from_utf8(cargo_afl(&["-V"]).output().unwrap().stdout)
.unwrap()
.starts_with("cargo-afl")
);
let output = cargo_afl(&["-V"]).output().unwrap();
assert_success(&output, None);
assert!(String::from_utf8(output.stdout)
.unwrap()
.starts_with("cargo-afl"));
}

#[test]
fn afl_required_else_help() {
let lhs = command().arg("--help").output().unwrap();
let rhs = command().output().unwrap();
assert_success(&lhs, None);
assert_failure(&rhs, None);
assert_eq!(
String::from_utf8(command().arg("--help").output().unwrap().stdout).unwrap(),
String::from_utf8(command().output().unwrap().stderr).unwrap()
String::from_utf8(lhs.stdout).unwrap(),
String::from_utf8(rhs.stderr).unwrap()
);
}

#[test]
fn subcommand_required_else_help() {
let lhs = cargo_afl(&["--help"]).output().unwrap();
let rhs = cargo_afl::<&OsStr>(&[]).output().unwrap();
assert_success(&lhs, None);
assert_failure(&rhs, None);
assert_eq!(
String::from_utf8(cargo_afl(&["--help"]).output().unwrap().stdout).unwrap(),
String::from_utf8(cargo_afl::<&OsStr>(&[]).output().unwrap().stderr).unwrap()
String::from_utf8(lhs.stdout).unwrap(),
String::from_utf8(rhs.stderr).unwrap()
);
}

Expand Down Expand Up @@ -467,52 +476,57 @@ mod tests {

#[test]
fn subcommands_help_subcommand_disabled() {
assert!(
String::from_utf8(cargo_afl(&["help"]).output().unwrap().stdout)
.unwrap()
.starts_with("Usage:")
);
let output = cargo_afl(&["help"]).output().unwrap();
assert_success(&output, None);
assert!(String::from_utf8(output.stdout)
.unwrap()
.starts_with("Usage:"));

for &subcommand in SUBCOMMANDS {
assert!(
!String::from_utf8(cargo_afl(&[subcommand, "help"]).output().unwrap().stdout)
.unwrap()
.starts_with("Usage:")
);
let output = cargo_afl(&[subcommand, "help"]).output().unwrap();
assert_failure(&output, Some(subcommand));
assert!(!String::from_utf8(output.stdout)
.unwrap()
.starts_with("Usage:"));
}
}

#[test]
fn subcommands_help_flag_disabled() {
assert!(
String::from_utf8(cargo_afl(&["--help"]).output().unwrap().stdout)
.unwrap()
.starts_with("Usage:")
);

for &subcommand in SUBCOMMANDS {
assert!(!String::from_utf8(
cargo_afl(&[subcommand, "--help"]).output().unwrap().stdout
)
let output = cargo_afl(&["--help"]).output().unwrap();
assert_success(&output, None);
assert!(String::from_utf8(output.stdout)
.unwrap()
.starts_with("Usage:"));

for &subcommand in SUBCOMMANDS {
let output = cargo_afl(&[subcommand, "--help"]).output().unwrap();
// smoelius: `afl-system-config` has a `--help` flag.
if subcommand == "system-config" {
assert_success(&output, Some(subcommand));
} else {
assert_failure(&output, Some(subcommand));
}
assert!(!String::from_utf8(output.stdout)
.unwrap()
.starts_with("Usage:"));
}
}

#[test]
fn subcommands_version_flag_disabled() {
assert!(
String::from_utf8(cargo_afl(&["-V"]).output().unwrap().stdout)
.unwrap()
.starts_with("cargo-afl")
);
let output = cargo_afl(&["-V"]).output().unwrap();
assert_success(&output, None);
assert!(String::from_utf8(output.stdout)
.unwrap()
.starts_with("cargo-afl"));

for &subcommand in SUBCOMMANDS {
assert!(
!String::from_utf8(cargo_afl(&[subcommand, "-V"]).output().unwrap().stdout)
.unwrap()
.starts_with("cargo-afl")
);
let output = cargo_afl(&[subcommand, "-V"]).output().unwrap();
assert_failure(&output, Some(subcommand));
assert!(!String::from_utf8(output.stdout)
.unwrap()
.starts_with("cargo-afl"));
}
}

Expand All @@ -526,6 +540,30 @@ mod tests {
Command::cargo_bin("cargo-afl").unwrap()
}

fn assert_success(output: &Output, subcommand: Option<&str>) {
assert!(
output.status.success(),
"{}",
if let Some(subcommand) = subcommand {
format!("{subcommand} failed")
} else {
String::new()
}
);
}

fn assert_failure(output: &Output, subcommand: Option<&str>) {
assert!(
!output.status.success(),
"{}",
if let Some(subcommand) = subcommand {
format!("{subcommand} succeeded")
} else {
String::new()
}
);
}

fn invalid_utf8() -> OsString {
OsString::from_vec(vec![0xfe])
}
Expand Down

0 comments on commit 56e91d6

Please sign in to comment.