summaryrefslogtreecommitdiff
path: root/rust/tests/data_notification.rs
blob: f277692a0f44b978efbd45b992eac10517a72710 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use std::path::PathBuf;

use binaryninja::binary_view::BinaryView;
use binaryninja::data_notification::*;
use binaryninja::function::Function;
use binaryninja::headless::Session;
use binaryninja::tags::TagReference;

#[test]
fn test_data_notification_dyn_closure() {
    let _session = Session::new().expect("Failed to initialize session");
    let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
    let bv = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");

    let mut func_updated_count = 0usize;
    let custom = DataNotificationClosure::default()
        .function_updated(|_bv: &BinaryView, _func: &Function| {
            func_updated_count += 1;
        })
        .register(&bv);

    let crash = bv.create_tag_type("Test", "🚧");
    let funcs = bv.functions();
    for func in &funcs {
        func.add_tag(
            &crash,
            "Dummy tag",
            Some(10.try_into().unwrap()),
            true,
            None,
        );
    }
    custom.unregister();

    // Verify that we no longer have the notification
    let funcs = bv.functions();
    for func in &funcs {
        func.add_tag(
            &crash,
            "Dummy tag",
            Some(10.try_into().unwrap()),
            true,
            None,
        );
    }

    assert_eq!(funcs.len(), func_updated_count);
}

#[test]
fn test_data_notification_impl() {
    let _session = Session::new().expect("Failed to initialize session");
    let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
    let bv = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");

    #[derive(Default)]
    struct Tag {
        tags: usize,
    }

    impl CustomDataNotification for Tag {
        fn tag_added(&mut self, _view: &BinaryView, _tag_ref: &TagReference) {
            self.tags += 1;
        }
    }

    let triggers = DataNotificationTriggers::default().tag_added();
    let tags_lock = Tag::default().register(&bv, triggers);

    let funcs = bv.functions();
    for (i, func) in funcs.iter().enumerate() {
        let crash = bv.create_tag_type("Test", "🚧");
        func.add_tag(&crash, "Dummy tag", Some(i.try_into().unwrap()), true, None);
    }

    let tags = tags_lock.unregister();

    assert_eq!(funcs.len(), tags.tags);
}