Skip to content

Commit

Permalink
Add more transition messages
Browse files Browse the repository at this point in the history
To README.md and help message.
  • Loading branch information
smoelius committed Jun 29, 2023
1 parent c21ee9c commit adba428
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

<h4 align="center">Fuzzing <a href="https://www.rust-lang.org">Rust</a> code with <a href="https://aflplus.plus/">AFLplusplus</a></h4>

**Notice:** A future version of afl.rs will require you to install the `cargo-afl` binary with:

```sh
cargo install cargo-afl
```

You can use the new command now, if you like. If the binary is already installed, you may need to add `--force`.

## What is it?

[Fuzz testing][] is a software testing technique used to find security and stability issues by providing pseudo-random data as input to the software. [AFLplusplus][] is a popular, effective, and modern fuzz testing tool based on [AFL][american-fuzzy-lop]. This library, afl.rs, allows one to run AFLplusplus on code written in [the Rust programming language][rust].
Expand Down
12 changes: 11 additions & 1 deletion afl/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {

let building_on_docs_rs = env::var("DOCS_RS").is_ok();

let out_dir = env::var("OUT_DIR").unwrap();
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

if installing && !building_cargo_afl {
println!("cargo:warning=You appear to be installing the `cargo-afl` binary with:");
Expand All @@ -36,6 +36,16 @@ fn main() {
);
}

std::fs::write(
out_dir.join("help.txt"),
if building_cargo_afl {
String::new()
} else {
String::from("\n\n") + common::HELP_MSG
},
)
.unwrap();

// smoelius: Build AFLplusplus in a temporary directory when installing or when building on docs.rs.
let work_dir = if installing || building_on_docs_rs {
let tempdir = tempfile::tempdir_in(&out_dir).unwrap();
Expand Down
10 changes: 6 additions & 4 deletions afl/src/bin/cargo-afl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ fn main() {
fn clap_app() -> clap::Command {
use clap::{value_parser, Arg, Command};

let help = String::from(
"In addition to the subcommands above, Cargo subcommands are also \
supported (see `cargo help` for a list of all Cargo subcommands).",
) + include_str!(concat!(env!("OUT_DIR"), "/help.txt"));

Command::new("cargo afl")
.display_name("cargo")
.subcommand_required(true)
Expand All @@ -99,10 +104,7 @@ fn clap_app() -> clap::Command {
.allow_external_subcommands(true)
.external_subcommand_value_parser(value_parser!(OsString))
.override_usage("cargo afl [SUBCOMMAND or Cargo SUBCOMMAND]")
.after_help(
"In addition to the subcommands above, Cargo subcommands are also \
supported (see `cargo help` for a list of all Cargo subcommands).",
)
.after_help(help)
.subcommand(
Command::new("analyze")
.about("Invoke afl-analyze")
Expand Down
8 changes: 8 additions & 0 deletions afl/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
use std::env;
use std::path::{Path, PathBuf};

#[allow(dead_code)]
pub static HELP_MSG: &str = "Note: This binary appears to have been installed with:
cargo install afl
A future version of afl.rs will require you to use:
cargo install cargo-afl
You can use the new command now, if you like. You may need to add --force.
";

fn xdg_dir() -> xdg::BaseDirectories {
let prefix = Path::new("afl.rs")
.join(afl_rustc_version())
Expand Down
27 changes: 18 additions & 9 deletions cargo-afl/tests/crates_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ use predicates::prelude::*;
use std::path::Path;
use tempfile::tempdir;

#[path = "../src/common.rs"]
mod common;

struct Test {
subdir: &'static str,
should_contain_msg: bool,
should_contain_msgs: bool,
}

static MSG: &str = "warning: You appear to be installing the `cargo-afl` binary with:
static INSTALL_MSG: &str = "warning: You appear to be installing the `cargo-afl` binary with:
warning: cargo install afl
warning: A future version of afl.rs will require you to use:
warning: cargo install cargo-afl
Expand All @@ -19,19 +22,19 @@ warning: Note: If the binary is already installed, you may need to add --force.
static TESTS: &[Test] = &[
Test {
subdir: "afl",
should_contain_msg: true,
should_contain_msgs: true,
},
Test {
subdir: "cargo-afl",
should_contain_msg: false,
should_contain_msgs: false,
},
];

#[test]
fn install() {
for &Test {
subdir,
should_contain_msg,
should_contain_msgs,
} in TESTS
{
let tempdir = tempdir().unwrap();
Expand All @@ -52,16 +55,22 @@ fn install() {
.assert()
.success();

if should_contain_msg {
assert.stderr(predicates::str::contains(MSG));
if should_contain_msgs {
assert.stderr(predicates::str::contains(INSTALL_MSG));
} else {
assert.stderr(predicates::str::contains(MSG).not());
assert.stderr(predicates::str::contains(INSTALL_MSG).not());
}

Command::new(cargo_afl)
let assert = Command::new(cargo_afl)
.args(["afl", "--help"])
.assert()
.success();

if should_contain_msgs {
assert.stdout(predicates::str::contains(common::HELP_MSG));
} else {
assert.stdout(predicates::str::contains(common::HELP_MSG).not());
}
}
}

Expand Down

0 comments on commit adba428

Please sign in to comment.