From 26ed2cdfb13591c63a52e978ead5eae0b6acb0c4 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Sat, 25 Jan 2025 02:19:29 -0500 Subject: Fix collaboration support checks in rust unit tests --- rust/src/collaboration.rs | 10 ++++++++++ rust/tests/collaboration.rs | 20 ++++++++++---------- 2 files changed, 20 insertions(+), 10 deletions(-) (limited to 'rust') diff --git a/rust/src/collaboration.rs b/rust/src/collaboration.rs index 1dce8104..e173afaa 100644 --- a/rust/src/collaboration.rs +++ b/rust/src/collaboration.rs @@ -36,6 +36,16 @@ use crate::string::{BnStrCompatible, BnString}; // TODO: on what functions need to have been called prior? I feel like we should make the user have to pull // TODO: the data because they have a greater understanding of where the function is being called from. +/// Check whether the client has collaboration support. +/// +/// Call this if you intend on providing divergent behavior, as otherwise you will likely +/// crash calling collaboration APIs on unsupported builds of Binary Ninja. +pub fn has_collaboration_support() -> bool { + let mut count = 0; + let value = unsafe { BNCollaborationGetRemotes(&mut count) }; + !value.is_null() +} + /// Get the single actively connected Remote (for ux simplification), if any pub fn active_remote() -> Option> { let value = unsafe { BNCollaborationGetActiveRemote() }; diff --git a/rust/tests/collaboration.rs b/rust/tests/collaboration.rs index 48da859a..7c2a2aae 100644 --- a/rust/tests/collaboration.rs +++ b/rust/tests/collaboration.rs @@ -1,5 +1,5 @@ use binaryninja::binary_view::BinaryViewExt; -use binaryninja::collaboration::{NoNameChangeset, Remote, RemoteFileType, RemoteProject}; +use binaryninja::collaboration::{has_collaboration_support, NoNameChangeset, Remote, RemoteFileType, RemoteProject}; use binaryninja::headless::Session; use binaryninja::symbol::{SymbolBuilder, SymbolType}; use rstest::*; @@ -47,11 +47,11 @@ fn temp_project_scope(remote: &Remote, cb: T) { #[rstest] fn test_connection(_session: &Session) { - let remotes = binaryninja::collaboration::known_remotes(); - if remotes.is_empty() { - eprintln!("No known remotes, skipping test..."); + if !has_collaboration_support() { + eprintln!("No collaboration support, skipping test..."); return; } + let remotes = binaryninja::collaboration::known_remotes(); let remote = remotes.iter().next().expect("No known remotes!"); assert!(remote.connect().is_ok(), "Failed to connect to remote"); remote @@ -62,11 +62,11 @@ fn test_connection(_session: &Session) { #[rstest] fn test_project_creation(_session: &Session) { - let remotes = binaryninja::collaboration::known_remotes(); - if remotes.is_empty() { - eprintln!("No known remotes, skipping test..."); + if !has_collaboration_support() { + eprintln!("No collaboration support, skipping test..."); return; } + let remotes = binaryninja::collaboration::known_remotes(); let remote = remotes.iter().next().expect("No known remotes!"); temp_project_scope(&remote, |project| { // Create the file than verify it by opening and checking contents. @@ -142,11 +142,11 @@ fn test_project_creation(_session: &Session) { #[rstest] fn test_project_sync(_session: &Session) { - let remotes = binaryninja::collaboration::known_remotes(); - if remotes.is_empty() { - eprintln!("No known remotes, skipping test..."); + if !has_collaboration_support() { + eprintln!("No collaboration support, skipping test..."); return; } + let remotes = binaryninja::collaboration::known_remotes(); let remote = remotes.iter().next().expect("No known remotes!"); temp_project_scope(&remote, |project| { // Open a view so that we can upload it. -- cgit v1.3.1