Crates.io | mockrs |
lib.rs | mockrs |
version | 1.1.2 |
source | src |
created_at | 2024-06-25 15:54:51.153992 |
updated_at | 2024-10-13 10:08:00.101543 |
description | A crate that provides a X8664Mocker to mock function calls and intercept on x86_64 architecture. |
homepage | |
repository | https://github.com/skyfireitdiy/mockrs |
max_upload_size | |
id | 1283462 |
size | 27,182 |
The mockrs
crate provides a powerful tool for function mocking and interception in Rust, specifically designed for the x86_64 architecture. This utility is particularly useful for testing and debugging purposes, where you need to replace the behavior of a function temporarily.
To use the mockrs
, include it in your Cargo.toml
and then import it in your Rust code.
[dependencies]
mockrs = "1.*" # Replace with the actual version number
Here's a simple example of how to use the mockrs
to mock the add
function:
use mockrs::mock;
fn add(a: i64, b: i64) -> i64 {
a + b
}
fn mock_add(_a: i64, _b: i64) -> i64 {
100
}
fn main() {
assert_eq!(add(1, 2), 3);
let mocker = mock!(add, mock_add);
assert_eq!(add(1, 2), 100);
drop(mocker);
assert_eq!(add(1, 2), 3);
}
X8664Mocker
The X8664Mocker
struct is the core of the crate, providing the functionality to mock functions.
pub fn mock(old_func: usize, new_func: usize) -> X8664Mocker
: Creates a new instance of X8664Mocker
to mock the specified function.mock!
MacroA convenient macro to create a new X8664Mocker
instance.
mock!($old_func:expr, $new_func:expr)
: Replaces the original function $old_func
with the new function $new_func
.The mockrs
operates at a low level, manipulating memory and handling signals. Use it with caution and ensure that the original and new functions have compatible signatures.
This crate is designed for use on the x86_64 architecture only.
Contributions to the mockrs
crate are welcome! Feel free to submit pull requests or create issues for bugs and feature requests.
The mockrs
crate is licensed under the MIT License. See LICENSE for more information.
This README is automatically generated based on the provided source code. For more detailed documentation, please refer to the inline comments within the code.