Skip to content

Commit 1771c8e

Browse files
committed
Add string tests to Rust API
1 parent f987c09 commit 1771c8e

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

rust/tests/string.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use binaryninja::string::{BnString, IntoCStr};
2+
use std::ffi::{CStr, CString};
3+
4+
#[test]
5+
fn test_bnstring() {
6+
// Test a basic ASCII string
7+
let str_0 = BnString::new("test");
8+
assert_eq!(str_0.to_string_lossy(), "test");
9+
assert_eq!(str_0.to_bytes_with_nul(), b"test\0");
10+
11+
// Test non-UTF8 bytes
12+
let invalid_utf8 = CStr::from_bytes_with_nul(&[0x48, 0x65, 0x6C, 0x6C, 0x6F, 0xFF, 0x00])
13+
.expect("Failed to create string");
14+
let str_2 = BnString::new(invalid_utf8);
15+
assert_eq!(str_2.to_string_lossy(), "Hello�");
16+
assert_eq!(
17+
str_2.to_bytes_with_nul(),
18+
&[0x48, 0x65, 0x6C, 0x6C, 0x6F, 0xFF, 0x00]
19+
);
20+
21+
// Test empty string
22+
let str_3 = BnString::new("");
23+
assert_eq!(str_3.to_string_lossy(), "");
24+
assert_eq!(str_3.to_bytes_with_nul(), b"\0");
25+
26+
// Test string with Unicode
27+
let str_4 = BnString::new("Hello 世界");
28+
assert_eq!(str_4.to_string_lossy(), "Hello 世界");
29+
assert_eq!(
30+
str_4.to_bytes_with_nul(),
31+
b"Hello \xE4\xB8\x96\xE7\x95\x8C\0"
32+
);
33+
}
34+
35+
#[test]
36+
fn test_cstr() {
37+
let str_0 = BnString::new("test");
38+
let cstr_0: BnString = str_0.to_cstr();
39+
assert_eq!(cstr_0.to_str().unwrap(), "test");
40+
assert_eq!(cstr_0.to_bytes_with_nul(), b"test\0");
41+
42+
let str_1 = String::from("test");
43+
let cstr_1: CString = str_1.to_cstr();
44+
assert_eq!(cstr_1.to_str().unwrap(), "test");
45+
assert_eq!(cstr_1.to_bytes_with_nul(), b"test\0");
46+
47+
let str_2 = "test";
48+
let cstr_2: CString = str_2.to_cstr();
49+
assert_eq!(cstr_2.to_str().unwrap(), "test");
50+
assert_eq!(cstr_2.to_bytes_with_nul(), b"test\0");
51+
}

0 commit comments

Comments
 (0)