summaryrefslogtreecommitdiff
path: root/rust/tests/function.rs
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2025-06-05 18:32:19 -0400
committerMason Reed <mason@vector35.com>2025-06-13 16:43:01 -0400
commit607ca37184716da4641aa178cf0cf2dc3c95f116 (patch)
tree07c0a208c34581d1fac0aae79f8b4aa9ac70371c /rust/tests/function.rs
parent255d616d33004a042b773e2ea3cbedefd76db3ab (diff)
Require a default platform to create functions for a given Binary View
This affects mainly users creating an empty raw view headlessly and adding a function.
Diffstat (limited to 'rust/tests/function.rs')
-rw-r--r--rust/tests/function.rs69
1 files changed, 63 insertions, 6 deletions
diff --git a/rust/tests/function.rs b/rust/tests/function.rs
index 455ba752..ac15fd91 100644
--- a/rust/tests/function.rs
+++ b/rust/tests/function.rs
@@ -1,7 +1,7 @@
-use binaryninja::binary_view::BinaryViewExt;
+use binaryninja::binary_view::{BinaryView, BinaryViewExt};
+use binaryninja::file_metadata::FileMetadata;
use binaryninja::headless::Session;
-use binaryninja::metadata::Metadata;
-use binaryninja::rc::Ref;
+use binaryninja::platform::Platform;
use std::path::PathBuf;
#[test]
@@ -15,7 +15,7 @@ fn store_and_query_function_metadata() {
// Store key/value pairs to user and auto metadata
func.store_metadata("one", "one", false);
- func.store_metadata("two", 2 as u64, true);
+ func.store_metadata("two", 2u64, true);
func.store_metadata("three", "three", true);
func.remove_metadata("three");
@@ -35,8 +35,9 @@ fn store_and_query_function_metadata() {
.unwrap(),
2
);
- assert!(
- func.query_metadata("three") == None,
+ assert_eq!(
+ func.query_metadata("three"),
+ None,
"Query for key \"three\" returned a value"
);
@@ -67,3 +68,59 @@ fn store_and_query_function_metadata() {
);
assert_eq!(auto_metadata.get("one"), Ok(None));
}
+
+#[test]
+fn add_function() {
+ 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 mut func = view
+ .entry_point_function()
+ .expect("Failed to get entry point function");
+
+ // Remove the function then as an auto function then add as auto function.
+ // This tests to make sure that the function is not blacklisted from being added back.
+ view.remove_auto_function(&func, false);
+
+ assert_eq!(
+ view.functions_at(func.start()).len(),
+ 0,
+ "Function was not removed"
+ );
+ func = view
+ .add_auto_function(func.start())
+ .expect("Failed to add function");
+ assert_eq!(
+ view.functions_at(func.start()).len(),
+ 1,
+ "Function was not added back"
+ );
+
+ // Use the user version of remove to blacklist the function, auto function should be prohibited.
+ view.remove_user_function(&func);
+ assert_eq!(
+ view.add_auto_function(func.start()),
+ None,
+ "Function was not blacklisted"
+ );
+
+ // Adding back as a user should override the blacklist.
+ func = view
+ .add_user_function(func.start())
+ .expect("Failed to add function as user");
+ assert_eq!(
+ view.functions_at(func.start()).len(),
+ 1,
+ "Function was not added back"
+ );
+
+ // Make sure you cannot add a function without a default platform.
+ let code = &[0xa1, 0xfa, 0xf8, 0xf0, 0x99, 0x83, 0xc0, 0x37, 0xc3];
+ let view = BinaryView::from_data(&FileMetadata::new(), code).expect("Failed to create view");
+ assert!(view.add_user_function(0).is_none());
+ assert!(view.add_auto_function(0).is_none());
+
+ // Now set it to verify we can add it.
+ let platform = Platform::by_name("x86").expect("Failed to get platform");
+ assert!(view.add_user_function_with_platform(0, &platform).is_some());
+}