-
Notifications
You must be signed in to change notification settings - Fork 236
/
Copy pathplugin_command.rs
51 lines (46 loc) · 1.57 KB
/
plugin_command.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use binaryninja::binary_view::BinaryView;
use binaryninja::headless::Session;
use binaryninja::plugin_command::{CustomDefaultPluginCommand, PluginCommand, PluginCommandAll};
#[test]
fn test_custom_plugin_command() {
let _session = Session::new().expect("Failed to initialize session");
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
let counter = Arc::new(Mutex::new(0));
struct MyCommand {
counter: Arc<Mutex<usize>>,
}
impl CustomDefaultPluginCommand for MyCommand {
fn is_valid(&mut self, _view: &BinaryView) -> bool {
true
}
fn run(&mut self, _view: &BinaryView) {
let mut counter = self.counter.lock().unwrap();
*counter += 1;
}
}
const PLUGIN_NAME: &str = "MyTestCommand1";
MyCommand {
counter: Arc::clone(&counter),
}
.register(
PLUGIN_NAME,
"Test for the plugin command custom implementation",
);
let all_plugins = PluginCommand::<PluginCommandAll>::valid_plugin_commands();
let my_core_plugin = all_plugins
.iter()
.find(|plugin| plugin.name() == PLUGIN_NAME)
.unwrap();
match my_core_plugin.execution() {
PluginCommandAll::DefaultPluginCommand(mut exe) => {
assert!(exe.is_valid(&view));
exe.run(&view);
}
_ => unreachable!(),
}
let counter = *counter.lock().unwrap();
assert_eq!(counter, 1);
}