Skip to content

Commit

Permalink
Add cli tests (#17)
Browse files Browse the repository at this point in the history
* test(cli): Add cli tests

Signed-off-by: Maksim Dimitrov <[email protected]>

* test: Rename injection and injector test modules

Signed-off-by: Maksim Dimitrov <[email protected]>

* chore: Run fmt

Signed-off-by: Maksim Dimitrov <[email protected]>

---------

Signed-off-by: Maksim Dimitrov <[email protected]>
  • Loading branch information
dimitrovmaksim committed Aug 11, 2023
1 parent e241c3e commit 42defd4
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/injecting/injections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn inject_heap_overflow(module: &mut Module) -> Result<(), String> {
}

#[cfg(test)]
mod tests {
mod injections_tests {
use super::*;
use crate::util::load_module_from_wasm;
use std::path::Path;
Expand Down
2 changes: 1 addition & 1 deletion src/injecting/injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl FunctionMapper for Module {
}

#[cfg(test)]
mod tests {
mod injector_tests {
use super::*;
use crate::util::load_module_from_wasm;
use std::path::Path;
Expand Down
142 changes: 136 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use std::path::PathBuf;
use wasm_injector::injecting::injections::Injection;
use wasm_injector::util::{load_module_from_wasm, modify_file_name, save_module_to_wasm};

#[derive(Parser, Debug)]
#[derive(Parser, Debug, PartialEq, Eq)]
#[command(author, version, about, long_about = None)]
struct Cli {
#[command(subcommand)]
action: Action,
}

#[derive(Debug, Subcommand)]
#[derive(Debug, Subcommand, PartialEq, Eq)]
enum Action {
#[command(about = "Inject invalid instructions into a wasm module")]
Inject {
Expand Down Expand Up @@ -74,10 +74,7 @@ enum Action {
},
}

#[derive(Parser, Debug, Clone)]
struct ConvertOpts {}

#[derive(Parser, Debug, Clone)]
#[derive(Parser, Debug, Clone, PartialEq, Eq)]
struct GlobalOpts {
#[arg(required = true, help = "Wasm source file path. Can be compressed and/or hexified.", value_hint = ValueHint::FilePath)]
source: PathBuf,
Expand Down Expand Up @@ -171,3 +168,136 @@ fn main() -> Result<(), String> {

Ok(())
}

#[cfg(test)]
mod cli_tests {
use super::*;

#[test]
fn test_inject_noops() {
assert_eq!(
Cli::try_parse_from(&["test", "inject", "noops", "test.wasm"]).unwrap(),
Cli {
action: Action::Inject {
injection: Injection::Noops,
global_opts: GlobalOpts {
source: PathBuf::from("test.wasm"),
destination: None
},
compressed: false,
hexified: false
}
}
)
}

#[test]
fn test_inject_heap_overflow() {
assert_eq!(
Cli::try_parse_from(&["test", "inject", "heap-overflow", "test.wasm"]).unwrap(),
Cli {
action: Action::Inject {
injection: Injection::HeapOverflow,
global_opts: GlobalOpts {
source: PathBuf::from("test.wasm"),
destination: None
},
compressed: false,
hexified: false
}
}
)
}

#[test]
fn test_inject_stack_overflow() {
assert_eq!(
Cli::try_parse_from(&["test", "inject", "stack-overflow", "test.wasm"]).unwrap(),
Cli {
action: Action::Inject {
injection: Injection::StackOverflow,
global_opts: GlobalOpts {
source: PathBuf::from("test.wasm"),
destination: None
},
compressed: false,
hexified: false
}
}
)
}

#[test]
fn test_inject_bad_return_value() {
assert_eq!(
Cli::try_parse_from(&["test", "inject", "bad-return-value", "test.wasm"]).unwrap(),
Cli {
action: Action::Inject {
injection: Injection::BadReturnValue,
global_opts: GlobalOpts {
source: PathBuf::from("test.wasm"),
destination: None
},
compressed: false,
hexified: false
}
}
)
}

#[test]
fn test_inject_infinite_loop() {
assert_eq!(
Cli::try_parse_from(&["test", "inject", "infinite-loop", "test.wasm"]).unwrap(),
Cli {
action: Action::Inject {
injection: Injection::InfiniteLoop,
global_opts: GlobalOpts {
source: PathBuf::from("test.wasm"),
destination: None
},
compressed: false,
hexified: false
}
}
)
}

#[test]
fn test_inject_invalid_injection() {
assert!(Cli::try_parse_from(&["test", "inject", "invalid-injection", "test.wasm"]).is_err())
}

#[test]
fn test_convert() {
assert_eq!(
Cli::try_parse_from(&["test", "convert", "test.wasm"]).unwrap(),
Cli {
action: Action::Convert {
global_opts: GlobalOpts {
source: PathBuf::from("test.wasm"),
destination: None
},
raw: true,
compressed: false,
hexified: false
}
}
)
}

#[test]
fn test_convert_raw_exludes_compressed() {
assert!(
Cli::try_parse_from(&["test", "convert", "test.wasm", "--compressed", "--raw"])
.is_err()
)
}

#[test]
fn test_convert_raw_exludes_hexified() {
assert!(
Cli::try_parse_from(&["test", "convert", "test.wasm", "--hexified", "--raw"]).is_err()
)
}
}

0 comments on commit 42defd4

Please sign in to comment.