Skip to content

Commit dc8015f

Browse files
committed
Remove module level Session initialization in Rust unit tests
This was preventing the enterprise license checkout from dropping, also the initialization story is much better than it was when we added the rstest fixtures, we can get away with initialization in parallel more broadly.
1 parent 4608523 commit dc8015f

25 files changed

+198
-322
lines changed

rust/tests/background_task.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
use binaryninja::background_task::*;
22
use binaryninja::headless::Session;
3-
use rstest::*;
43

5-
#[fixture]
6-
#[once]
7-
fn session() -> Session {
8-
Session::new().expect("Failed to initialize session")
9-
}
10-
11-
#[rstest]
12-
fn test_background_task_registered(_session: &Session) {
4+
#[test]
5+
fn test_background_task_registered() {
6+
let _session = Session::new().expect("Failed to initialize session");
137
let task_progress = "test registered";
148
let task = BackgroundTask::new(task_progress, false);
159
BackgroundTask::running_tasks()
@@ -24,8 +18,9 @@ fn test_background_task_registered(_session: &Session) {
2418
assert!(!still_running, "Task still running");
2519
}
2620

27-
#[rstest]
28-
fn test_background_task_cancellable(_session: &Session) {
21+
#[test]
22+
fn test_background_task_cancellable() {
23+
let _session = Session::new().expect("Failed to initialize session");
2924
let task_progress = "test cancellable";
3025
let task = BackgroundTask::new(task_progress, false);
3126
BackgroundTask::running_tasks()
@@ -37,8 +32,9 @@ fn test_background_task_cancellable(_session: &Session) {
3732
task.finish();
3833
}
3934

40-
#[rstest]
41-
fn test_background_task_progress(_session: &Session) {
35+
#[test]
36+
fn test_background_task_progress() {
37+
let _session = Session::new().expect("Failed to initialize session");
4238
let task = BackgroundTask::new("test progress", false);
4339
let first_progress = task.progress_text().to_string();
4440
assert_eq!(first_progress, "test progress");

rust/tests/base_detection.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
use binaryninja::base_detection::{BaseAddressDetectionConfidence, BaseAddressDetectionSettings};
22
use binaryninja::binary_view::BinaryViewExt;
33
use binaryninja::headless::Session;
4-
use rstest::{fixture, rstest};
54
use std::path::PathBuf;
65

7-
#[fixture]
8-
#[once]
9-
fn session() -> Session {
10-
Session::new().expect("Failed to initialize session")
11-
}
12-
13-
#[rstest]
14-
fn test_base_detection(_session: &Session) {
6+
#[test]
7+
fn test_base_detection() {
8+
let _session = Session::new().expect("Failed to initialize session");
159
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
1610
let view = binaryninja::load(out_dir.join("raw_base_detection_aarch64"))
1711
.expect("Failed to create view");

rust/tests/binary_reader.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
use binaryninja::binary_reader::BinaryReader;
22
use binaryninja::binary_view::{BinaryViewBase, BinaryViewExt};
33
use binaryninja::headless::Session;
4-
use rstest::*;
54
use std::io::{Read, Seek, SeekFrom};
65
use std::path::PathBuf;
76

8-
#[fixture]
9-
#[once]
10-
fn session() -> Session {
11-
Session::new().expect("Failed to initialize session")
12-
}
13-
14-
#[rstest]
15-
fn test_binary_reader_seek(_session: &Session) {
7+
#[test]
8+
fn test_binary_reader_seek() {
9+
let _session = Session::new().expect("Failed to initialize session");
1610
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
1711
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
1812
let mut reader = BinaryReader::new(&view);
@@ -49,8 +43,9 @@ fn test_binary_reader_seek(_session: &Session) {
4943
);
5044
}
5145

52-
#[rstest]
53-
fn test_binary_reader_read(_session: &Session) {
46+
#[test]
47+
fn test_binary_reader_read() {
48+
let _session = Session::new().expect("Failed to initialize session");
5449
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
5550
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
5651
let mut reader = BinaryReader::new(&view);

rust/tests/binary_view.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@ use binaryninja::binary_view::{AnalysisState, BinaryViewBase, BinaryViewExt};
22
use binaryninja::headless::Session;
33
use binaryninja::main_thread::execute_on_main_thread_and_wait;
44
use binaryninja::symbol::{SymbolBuilder, SymbolType};
5-
use rstest::*;
65
use std::path::PathBuf;
76

8-
#[fixture]
9-
#[once]
10-
fn session() -> Session {
11-
Session::new().expect("Failed to initialize session")
12-
}
13-
14-
#[rstest]
15-
fn test_binary_loading(_session: &Session) {
7+
#[test]
8+
fn test_binary_loading() {
9+
let _session = Session::new().expect("Failed to initialize session");
1610
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
1711
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
1812
assert!(view.has_initial_analysis(), "No initial analysis");
@@ -21,8 +15,9 @@ fn test_binary_loading(_session: &Session) {
2115
assert_eq!(view.file().is_database_backed(), false);
2216
}
2317

24-
#[rstest]
25-
fn test_binary_saving(_session: &Session) {
18+
#[test]
19+
fn test_binary_saving() {
20+
let _session = Session::new().expect("Failed to initialize session");
2621
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
2722
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
2823
// Verify the contents before we modify.
@@ -48,8 +43,9 @@ fn test_binary_saving(_session: &Session) {
4843
);
4944
}
5045

51-
#[rstest]
52-
fn test_binary_saving_database(_session: &Session) {
46+
#[test]
47+
fn test_binary_saving_database() {
48+
let _session = Session::new().expect("Failed to initialize session");
5349
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
5450
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
5551
// Update a symbol to verify modification

rust/tests/binary_writer.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ use binaryninja::binary_reader::BinaryReader;
22
use binaryninja::binary_view::{BinaryViewBase, BinaryViewExt};
33
use binaryninja::binary_writer::BinaryWriter;
44
use binaryninja::headless::Session;
5-
use rstest::*;
65
use std::io::{Read, Seek, SeekFrom, Write};
76
use std::path::PathBuf;
87

9-
#[fixture]
10-
#[once]
11-
fn session() -> Session {
12-
Session::new().expect("Failed to initialize session")
13-
}
14-
15-
#[rstest]
16-
fn test_binary_writer_seek(_session: &Session) {
8+
#[test]
9+
fn test_binary_writer_seek() {
10+
let _session = Session::new().expect("Failed to initialize session");
1711
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
1812
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
1913
let mut writer = BinaryWriter::new(&view);
@@ -50,8 +44,9 @@ fn test_binary_writer_seek(_session: &Session) {
5044
);
5145
}
5246

53-
#[rstest]
54-
fn test_binary_writer_write(_session: &Session) {
47+
#[test]
48+
fn test_binary_writer_write() {
49+
let _session = Session::new().expect("Failed to initialize session");
5550
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
5651
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
5752
let mut reader = BinaryReader::new(&view);

rust/tests/collaboration.rs

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
use binaryninja::binary_view::BinaryViewExt;
2-
use binaryninja::collaboration::{
3-
has_collaboration_support, NoNameChangeset, Remote, RemoteFileType, RemoteProject,
4-
};
2+
use binaryninja::collaboration::{NoNameChangeset, Remote, RemoteFileType, RemoteProject};
53
use binaryninja::headless::Session;
4+
use binaryninja::rc::Ref;
65
use binaryninja::symbol::{SymbolBuilder, SymbolType};
76
use rstest::*;
87
use serial_test::serial;
8+
use std::env;
99
use std::path::PathBuf;
1010

11-
#[fixture]
12-
#[once]
13-
fn session() -> Session {
14-
Session::new().expect("Failed to initialize session")
15-
}
16-
17-
// TODO: This cannot run in CI, as headless does not have collaboration, we should gate this.
1811
// TODO: Why cant we create_project for the same project name? why does that fail.
19-
2012
// TODO: Remote connection / disconnection is NOT thread safe, the core needs to lock on each.
2113
// TODO: Because of this we run these tests serially, this isnt _really_ an issue for real code, as
2214
// TODO: Real code shouldnt be trying to connect to the same remote on multiple threads.
@@ -28,6 +20,13 @@ fn temp_project_scope<T: Fn(&RemoteProject)>(remote: &Remote, project_name: &str
2820
// TODO: have connected by the time this errors out. Maybe?
2921
let _ = remote.connect();
3022
}
23+
24+
if let Ok(home_dir) = env::var("HOME").or_else(|_| env::var("USERPROFILE")) {
25+
eprintln!("Current user directory: {}", home_dir);
26+
} else {
27+
eprintln!("Unable to determine the current user directory.");
28+
}
29+
3130
let project = remote
3231
.create_project(project_name, "Test project for test purposes")
3332
.expect("Failed to create project");
@@ -56,15 +55,26 @@ fn temp_project_scope<T: Fn(&RemoteProject)>(remote: &Remote, project_name: &str
5655
.expect("Failed to delete project");
5756
}
5857

58+
/// Get the selected remote to test with.
59+
fn selected_remote() -> Option<Ref<Remote>> {
60+
// If the user has initialized with a enterprise server we might already have an active remote.
61+
match binaryninja::collaboration::active_remote() {
62+
Some(remote) => Some(remote),
63+
None => {
64+
let remotes = binaryninja::collaboration::known_remotes();
65+
remotes.iter().next().map(|r| r.clone())
66+
}
67+
}
68+
}
69+
5970
#[rstest]
6071
#[serial]
61-
fn test_connection(_session: &Session) {
62-
if !has_collaboration_support() {
63-
eprintln!("No collaboration support, skipping test...");
72+
fn test_connection() {
73+
let _session = Session::new().expect("Failed to initialize session");
74+
let Some(remote) = selected_remote() else {
75+
eprintln!("No known remotes, skipping test...");
6476
return;
65-
}
66-
let remotes = binaryninja::collaboration::known_remotes();
67-
let remote = remotes.iter().next().expect("No known remotes!");
77+
};
6878
assert!(remote.connect().is_ok(), "Failed to connect to remote");
6979
remote
7080
.disconnect()
@@ -74,13 +84,12 @@ fn test_connection(_session: &Session) {
7484

7585
#[rstest]
7686
#[serial]
77-
fn test_project_creation(_session: &Session) {
78-
if !has_collaboration_support() {
79-
eprintln!("No collaboration support, skipping test...");
87+
fn test_project_creation() {
88+
let _session = Session::new().expect("Failed to initialize session");
89+
let Some(remote) = selected_remote() else {
90+
eprintln!("No known remotes, skipping test...");
8091
return;
81-
}
82-
let remotes = binaryninja::collaboration::known_remotes();
83-
let remote = remotes.iter().next().expect("No known remotes!");
92+
};
8493
temp_project_scope(&remote, "test_creation", |project| {
8594
// Create the file than verify it by opening and checking contents.
8695
let created_file = project
@@ -155,13 +164,12 @@ fn test_project_creation(_session: &Session) {
155164

156165
#[rstest]
157166
#[serial]
158-
fn test_project_sync(_session: &Session) {
159-
if !has_collaboration_support() {
160-
eprintln!("No collaboration support, skipping test...");
167+
fn test_project_sync() {
168+
let _session = Session::new().expect("Failed to initialize session");
169+
let Some(remote) = selected_remote() else {
170+
eprintln!("No known remotes, skipping test...");
161171
return;
162-
}
163-
let remotes = binaryninja::collaboration::known_remotes();
164-
let remote = remotes.iter().next().expect("No known remotes!");
172+
};
165173
temp_project_scope(&remote, "test_sync", |project| {
166174
// Open a view so that we can upload it.
167175
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();

rust/tests/component.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
use binaryninja::binary_view::BinaryViewExt;
22
use binaryninja::component::ComponentBuilder;
33
use binaryninja::headless::Session;
4-
use rstest::*;
54
use std::path::PathBuf;
65

7-
#[fixture]
8-
#[once]
9-
fn session() -> Session {
10-
Session::new().expect("Failed to initialize session")
11-
}
12-
13-
#[rstest]
14-
fn test_component_creation(_session: &Session) {
6+
#[test]
7+
fn test_component_creation() {
8+
let _session = Session::new().expect("Failed to initialize session");
159
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
1610
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
1711
let component = ComponentBuilder::new(view.clone()).name("test").finalize();

rust/tests/demangler.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@ use binaryninja::demangle::{
66
use binaryninja::headless::Session;
77
use binaryninja::rc::Ref;
88
use binaryninja::types::{QualifiedName, Type};
9-
use rstest::*;
109

11-
#[fixture]
12-
#[once]
13-
fn session() -> Session {
14-
Session::new().expect("Failed to initialize session")
15-
}
16-
17-
#[rstest]
18-
fn test_demangler_simple(_session: &Session) {
10+
#[test]
11+
fn test_demangler_simple() {
12+
let _session = Session::new().expect("Failed to initialize session");
1913
let placeholder_arch = CoreArchitecture::by_name("x86").expect("x86 exists");
2014
// Example LLVM-style mangled name
2115
let llvm_mangled = "_Z3fooi"; // "foo(int)" in LLVM mangling
@@ -45,8 +39,9 @@ fn test_demangler_simple(_session: &Session) {
4539
);
4640
}
4741

48-
#[rstest]
49-
fn test_custom_demangler(_session: &Session) {
42+
#[test]
43+
fn test_custom_demangler() {
44+
let _session = Session::new().expect("Failed to initialize session");
5045
struct TestDemangler;
5146

5247
impl CustomDemangler for TestDemangler {

rust/tests/high_level_il.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
use binaryninja::binary_view::BinaryViewExt;
22
use binaryninja::headless::Session;
33
use binaryninja::high_level_il::{HighLevelILInstructionKind, HighLevelInstructionIndex};
4-
use rstest::*;
54
use std::path::PathBuf;
65

7-
#[fixture]
8-
#[once]
9-
fn session() -> Session {
10-
Session::new().expect("Failed to initialize session")
11-
}
12-
13-
#[rstest]
14-
fn test_hlil_info(_session: &Session) {
6+
#[test]
7+
fn test_hlil_info() {
8+
let _session = Session::new().expect("Failed to initialize session");
159
let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
1610
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
1711
let image_base = view.original_image_base();

rust/tests/initialization.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@ use binaryninja::headless::{
55
init, init_with_opts, shutdown, InitializationError, InitializationOptions,
66
};
77
use binaryninja::set_license;
8-
use rstest::rstest;
98

109
// NOTE: Do not add any tests here, behavior will change (i.e. a failure might pass) if we initialize
1110
// NOTE: The core in another test. The only test here should be `test_license_validation`.
1211

13-
#[rstest]
12+
#[test]
1413
fn test_license_validation() {
1514
// Release floating license if we already have one, otherwise the failure will succeed.
1615
release_license();

0 commit comments

Comments
 (0)