Skip to content

Commit

Permalink
fix(tests): Fix docs and doc tests
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Dimitrov <[email protected]>
  • Loading branch information
dimitrovmaksim committed Aug 14, 2023
1 parent 8a0234e commit 4ddd581
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
19 changes: 13 additions & 6 deletions src/injecting/injections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@ use super::injector::FunctionMapper;

/// # Injection enum
///
/// This enum is used to select which injection to perform i.e what instructions to insert in the beginning of the WASM module.
/// This enum is used to select which injection to perform i.e what instructions to insert in the beginning of a specified export function in the WASM module.
/// # Example
///
/// ```
/// let mut module = load_module(); // you can use wasm_injector::util to load a module
/// let injection = Injection::Noops;
/// injection.inject(&mut module);
/// use std::path::Path;
/// use wasm_injector::{ Injection, util::load_module_from_wasm };
///
/// # fn main() -> Result<(), String> {
/// let source = Path::new("samples/example.wasm");
/// let mut module = load_module_from_wasm(source)?;
/// let injection = Injection::InfiniteLoop;
/// injection.inject(&mut module, "validate_block")?;
/// # Ok(())
/// # }
/// ```

#[derive(clap::ValueEnum, PartialEq, Eq, Clone, Debug)]
Expand Down Expand Up @@ -57,7 +64,7 @@ fn get_injection(injection: Injection) -> Box<InjectionFn> {
}

/// # Takes a module and injects an infinite loop in the beginning of the module.
pub fn inject_infinite_loop(module: &mut Module, function_name: &str) -> Result<(), String> {
fn inject_infinite_loop(module: &mut Module, function_name: &str) -> Result<(), String> {
module.map_function(function_name, |func_body: &mut FuncBody| {
let code = func_body.code_mut();

Expand Down Expand Up @@ -146,7 +153,7 @@ mod injections_tests {
use std::path::Path;

const FUNCTION_NAME: &'static str = "validate_block";
const WASM_PATH: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/test-wasm/test.wasm");
const WASM_PATH: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/samples/example.wasm");

fn get_function_body(module: &mut Module) -> &mut FuncBody {
let function_name = "validate_block";
Expand Down
2 changes: 1 addition & 1 deletion src/injecting/injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ mod injector_tests {
const IMPORT_SECTION_LENGTH: usize = 39;
const MALLOC_INDEX: usize = 25;
const WASM_INSTRUCTION_COUNT: usize = 358;
const WASM_PATH: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/test-wasm/test.wasm");
const WASM_PATH: &'static str = concat!(env!("CARGO_MANIFEST_DIR"), "/samples/example.wasm");

fn load_module() -> Module {
let module_path = Path::new(WASM_PATH);
Expand Down
22 changes: 17 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,24 @@
//! predefined injections. The injections are primarily meant to be used for testing polkadot parachains hosts.
//! # Example
//! ```
//! use wasm_injector::{load_module_from_wasm, save_module_to_wasm, Injection};
//! use std::path::Path;
//! use wasm_injector::{Injection, load_module_from_wasm, save_module_to_wasm};
//!
//! let mut module = load_module_from_wasm(source.as_path())?; // supply your own path here
//! let injection = Injection::Noops; // choose your injection
//! injection.inject(&mut module)?; // inject the module
//! save_module_to_wasm(module, destination.as_path(), compressed, hexified)?; // save the module in your destination. You can choose to compress and/or hexify the module.
//! # fn main() -> Result<(), String> {
//! let source = Path::new("samples/example.wasm");
//! let destination = Path::new("samples/injected_example.wasm");
//! let compressed = false;
//! let hexified = true;
//!
//! let mut module = load_module_from_wasm(source)?; // supply your own path here
//! let injection = Injection::StackOverflow; // choose your injection
//! injection.inject(&mut module, "validate_block")?; // inject the instruction into the specified wasm export function
//!
//! save_module_to_wasm(module, destination, compressed, hexified)?; // save the module in your destination. You can choose to compress and/or hexify the module.
//!
//! # std::fs::remove_file(destination);
//! # Ok(())
//! # }
//! ```

pub mod injecting;
Expand Down

0 comments on commit 4ddd581

Please sign in to comment.